1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package ch.elca.el4j.services.gui.swing.cookswing.binding;
18
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import org.jdesktop.beansbinding.AutoBinding;
22 import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
23 import org.jdesktop.beansbinding.Property;
24
25 import org.w3c.dom.Element;
26
27 import com.silvermindsoftware.hitch.binding.PropertyUtil;
28
29 import cookxml.core.DecodeEngine;
30 import cookxml.core.exception.CookXmlException;
31 import cookxml.core.interfaces.Creator;
32
33
34
35
36
37
38
39
40 public abstract class AbstractBindingCreator implements Creator {
41
42 private static final String SOURCE = "src";
43 protected static final String PROPERTY = "property";
44 protected static final String VALIDATION = "validation";
45 protected static final String UPDATE_STRATEGY = "updateStrategy";
46
47
48
49
50 private static final Logger s_logger = LoggerFactory
51 .getLogger(AbstractBindingCreator.class);
52
53
54
55
56
57 protected UpdateStrategy getUpdateStrategy(Element elm) {
58 UpdateStrategy updateStrategy = UpdateStrategy.READ;
59 String updateStrategyString = elm.getAttribute(UPDATE_STRATEGY);
60 if (updateStrategyString.equals("read once")) {
61 updateStrategy = UpdateStrategy.READ_ONCE;
62 } else if (updateStrategyString.equals("read write")) {
63 updateStrategy = UpdateStrategy.READ_WRITE;
64 }
65 return updateStrategy;
66 }
67
68
69
70
71
72
73 @SuppressWarnings("unchecked")
74 protected Object getSource(DecodeEngine decodeEngine, Element elm) {
75 Object obj = null;
76 String src = elm.getAttribute(SOURCE);
77 if (!src.equals("")) {
78 if (src.contains(".")) {
79 Property prop = PropertyUtil.create(
80 src.substring(src.indexOf(".") + 1));
81 obj = prop.getValue(decodeEngine.getVariable(
82 src.substring(0, src.indexOf("."))));
83 } else {
84 obj = decodeEngine.getVariable(elm.getAttribute(SOURCE));
85 }
86 } else {
87 s_logger.error("Error processing XML element " + elm.getNodeName()
88 + ". Mandatory attribute '" + SOURCE + "' not found.");
89 }
90 return obj;
91 }
92
93
94
95
96
97 protected boolean getValidate(Element elm) {
98 boolean validate = Boolean.parseBoolean(elm.getAttribute(VALIDATION));
99 if (!elm.getAttribute(VALIDATION).equals("")) {
100 validate = true;
101 }
102 return validate;
103 }
104
105
106
107
108
109
110 @SuppressWarnings("unchecked")
111 protected void addBinding(DecodeEngine decodeEngine, AutoBinding binding) {
112 Object form = decodeEngine.getVariable("this");
113 if (Bindable.class.isAssignableFrom(form.getClass())) {
114 Bindable bindableForm = (Bindable) form;
115
116 bindableForm.getBinder().addManualBinding(binding);
117 } else {
118 s_logger.warn("No class to bind found. " + form
119 + " does not implement interface Bindable");
120 }
121 }
122
123
124 public Object editFinished(String parentNS, String parentTag, Element elm,
125 Object parentObj, Object obj, DecodeEngine decodeEngine)
126 throws CookXmlException {
127
128 return obj;
129 }
130 }