1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.xulux.guilayer.swing.widgets;
17
18 import java.awt.Container;
19
20 import javax.swing.Icon;
21 import javax.swing.UIManager;
22
23 import org.xulux.api.dataprovider.IDataProvider;
24 import org.xulux.api.dataprovider.IField;
25 import org.xulux.api.dataprovider.IMapping;
26 import org.xulux.api.dataprovider.InvalidValueException;
27 import org.xulux.api.gui.IXuluxListener;
28 import org.xulux.core.XuluxContext;
29 import org.xulux.gui.utils.ColorUtils;
30 import org.xulux.guilayer.swing.SwingWidget;
31 import org.xulux.guilayer.swing.extensions.NyxJCheckBox;
32 import org.xulux.guilayer.swing.listeners.PrePostFieldListener;
33 import org.xulux.utils.BooleanUtils;
34
35
36
37
38
39
40
41
42
43 public class CheckBox extends SwingWidget {
44
45
46
47
48 private NyxJCheckBox checkBox;
49
50
51
52
53 private PrePostFieldListener itemListener;
54
55
56
57
58 public CheckBox(String name) {
59 super(name);
60 }
61
62
63
64
65
66 public void destroy() {
67 processDestroy();
68 removeAllRules();
69 if (checkBox != null) {
70 Container container = checkBox.getParent();
71 checkBox.setVisible(false);
72 if (container != null) {
73 container.remove(checkBox);
74 }
75 if (itemListener != null) {
76 checkBox.removeItemListener(itemListener);
77 checkBox.removeFocusListener(itemListener);
78 }
79 itemListener = null;
80 checkBox = null;
81 }
82 removeAllRules();
83 getPart().removeWidget(this, this);
84 }
85
86
87
88
89
90 public Object getNativeWidget() {
91 initialize();
92 return checkBox;
93 }
94
95
96
97
98
99 public void initialize() {
100 if (this.initialized) {
101 return;
102 }
103 this.initialized = true;
104 this.checkBox = new NyxJCheckBox();
105 String backgroundColor = null;
106 if (isRequired() && isEnabled()) {
107 backgroundColor = getProperty("required-background-color");
108 } else if (!isEnabled()) {
109 backgroundColor = getProperty("disabled-background-color");
110 } else {
111 backgroundColor = getProperty("default-background-color");
112 }
113 if (backgroundColor != null) {
114 checkBox.setRealBackground(ColorUtils.getSwingColor(backgroundColor));
115 }
116
117 this.checkBox.setIcon((Icon) UIManager.get("CheckBox.icon"));
118 this.checkBox.setSelectedIcon((Icon) UIManager.get("CheckBox.icon"));
119 this.itemListener = new PrePostFieldListener(this);
120
121
122 checkBox.addItemListener(this.itemListener);
123 checkBox.addFocusListener(this.itemListener);
124 if ("false".equalsIgnoreCase(getProperty("focuspossible"))) {
125 checkBox.setFocusTraversable(false);
126 }
127 refresh();
128 processInit();
129 }
130
131
132
133
134
135 public void refresh() {
136 isRefreshing = true;
137 initialize();
138 checkBox.setVisible(isVisible());
139 if (getProperty("text") != null) {
140 checkBox.setText(getProperty("text"));
141 }
142 if (getValue() instanceof Boolean) {
143 checkBox.setSelected(BooleanUtils.toBoolean((Boolean) getValue()));
144 } else if (getValue() instanceof String) {
145 checkBox.setSelected(BooleanUtils.toBoolean((String) getValue()));
146 }
147 checkBox.setEnabled(isEnabled());
148
149
150
151
152
153
154
155
156
157
158
159 isRefreshing = false;
160 }
161
162
163
164
165
166 public Object getValue() {
167 if (getProvider() != null) {
168 return this.value;
169 } else if (getPart().getBean() == null || getField() == null) {
170 return super.getValue();
171 }
172 IMapping map = XuluxContext.getDictionary().getDefaultProvider().getMapping(getPart().getBean().getClass());
173 if (map != null) {
174 IField field = map.getField(getField());
175 if (field != null) {
176 return field.getValue(getPart().getBean());
177 }
178 }
179 return super.getValue();
180 }
181
182
183
184
185 public void setValue(Object object) {
186 if (getProvider() != null) {
187 IDataProvider pr = XuluxContext.getDictionary().getProvider(getProvider());
188 IMapping mapping = null;
189 IField field = null;
190 if (!(object instanceof String) && this.value == null) {
191 this.value = object;
192 } else {
193 mapping = pr.getMapping(this.value);
194 if (getField() != null) {
195 field = mapping.getField(getField());
196 } else {
197 field = mapping.getField(this.value);
198 }
199 try {
200 field.setValue(this.value, object);
201 setValidValue(true);
202 } catch(InvalidValueException ive) {
203 setValidValue(false);
204 }
205 }
206 } else {
207 if (this.value == null) {
208 this.value = "false";
209 }
210 this.previousValue = this.value;
211 IMapping map = XuluxContext.getDictionary().getDefaultProvider().getMapping(getPart().getBean());
212 if (map != null) {
213 if (getField() != null) {
214 IField f = map.getField(getField());
215 Class cClass = f.getType();
216 if (cClass == Boolean.class || cClass == Boolean.TYPE) {
217 if (object.getClass() == String.class) {
218 object = BooleanUtils.toBooleanObject((String) object);
219 }
220 } else if (cClass == String.class) {
221 if (object.getClass() == Boolean.class) {
222 object = BooleanUtils.toStringTrueFalse((Boolean) object);
223 }
224 }
225 f.setValue(getPart().getBean(), object);
226 }
227 }
228 this.value = object;
229 }
230 if (initialized) {
231 refresh();
232 }
233 }
234
235
236
237
238
239 public Object getGuiValue() {
240 return BooleanUtils.toBooleanObject(checkBox.isSelected());
241 }
242
243
244
245
246
247 public boolean canContainValue() {
248 return true;
249 }
250
251
252
253
254 public boolean isValueEmpty() {
255 return false;
256 }
257
258
259
260
261 public void addXuluxListener(IXuluxListener listener) {
262 }
263
264 }