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 import java.awt.event.FocusEvent;
20 import java.awt.event.FocusListener;
21
22 import javax.swing.Icon;
23 import javax.swing.ImageIcon;
24 import javax.swing.JToggleButton;
25
26 import org.xulux.api.dataprovider.IField;
27 import org.xulux.api.dataprovider.IMapping;
28 import org.xulux.api.gui.IWidgetInitializer;
29 import org.xulux.core.XuluxContext;
30 import org.xulux.gui.XuluxWidget;
31 import org.xulux.guilayer.swing.listeners.PrePostFieldListener;
32 import org.xulux.guilayer.swing.util.SwingUtils;
33 import org.xulux.utils.BooleanUtils;
34
35
36
37
38
39
40
41 public class ToggleButton extends XuluxWidget {
42
43
44
45
46 protected JToggleButton toggleButton;
47
48
49
50 protected PrePostFieldListener itemListener;
51
52
53
54 protected FocusListener focusListener;
55
56
57
58
59 public ToggleButton(String name) {
60 super(name);
61 }
62
63
64
65
66 public void destroy() {
67 if (!initialized) {
68 return;
69 }
70 processDestroy();
71 removeAllRules();
72 if (toggleButton != null) {
73 if (itemListener != null) {
74 toggleButton.removeItemListener(itemListener);
75 }
76 toggleButton.removeAll();
77 Container container = toggleButton.getParent();
78 if (container != null) {
79 container.remove(toggleButton);
80 }
81 toggleButton = null;
82 }
83 getPart().removeWidget(this, this);
84
85 }
86
87
88
89
90 public Object getNativeWidget() {
91 initialize();
92 return this.toggleButton;
93 }
94
95
96
97
98 public void initialize() {
99 if (initialized) {
100 return;
101 }
102 toggleButton = new JToggleButton();
103 itemListener = new PrePostFieldListener(this);
104 toggleButton.addItemListener(this.itemListener);
105 initialized = true;
106 refresh();
107 processInit();
108
109 }
110
111
112
113
114 public void refresh() {
115 if (isRefreshing()) {
116 return;
117 }
118 isRefreshing = true;
119 initialize();
120 if (getProperty("text") != null) {
121 toggleButton.setText(getProperty("text"));
122 }
123 if (getProperty("selected") != null) {
124 setValue(BooleanUtils.toBooleanObject(getProperty("selected")));
125 setProperty("selected", null);
126 }
127 if (getValue() instanceof Boolean) {
128 toggleButton.setSelected(BooleanUtils.toBoolean((Boolean) getValue()));
129 } else if (getValue() instanceof String) {
130 toggleButton.setSelected(BooleanUtils.toBoolean((String) getValue()));
131 }
132 toggleButton.setEnabled(isEnabled());
133
134
135
136
137
138
139
140
141
142
143
144
145
146 String image = getProperty("image");
147 if (image != null) {
148 ImageIcon normalIcon = SwingUtils.getIcon(image, this);
149 toggleButton.setIcon(normalIcon);
150 toggleButton.setFocusPainted(true);
151 }
152 String disabledImage = getProperty("image-disabled");
153 if (disabledImage != null) {
154 ImageIcon icon = SwingUtils.getIcon(disabledImage, this);
155 toggleButton.setDisabledIcon(icon);
156 toggleButton.setDisabledSelectedIcon(icon);
157 }
158 String rolloverImage = getProperty("image-rollover");
159 if (rolloverImage != null) {
160 ImageIcon icon = SwingUtils.getIcon(rolloverImage, this);
161 toggleButton.setRolloverEnabled(true);
162 toggleButton.setRolloverIcon(icon);
163 }
164
165 String selectedImage = getProperty("image-selected");
166 if (selectedImage != null) {
167 ImageIcon icon = SwingUtils.getIcon(selectedImage, this);
168 toggleButton.setSelectedIcon(icon);
169 toggleButton.setPressedIcon(icon);
170 toggleButton.setRolloverSelectedIcon(icon);
171 toggleButton.setRolloverEnabled(true);
172
173
174 if (this.focusListener == null) {
175 this.focusListener = new FocusListener() {
176 Icon normalIcon;
177
178
179
180 public void focusGained(FocusEvent e) {
181 if (normalIcon == null) {
182 normalIcon = toggleButton.getIcon();
183 }
184 toggleButton.setIcon(toggleButton.getSelectedIcon());
185 }
186
187
188
189
190 public void focusLost(FocusEvent e) {
191 String image = getProperty("image");
192 if (image != null) {
193 ImageIcon normalIcon = SwingUtils.getIcon(image, this);
194 if (toggleButton != null) {
195 toggleButton.setIcon(normalIcon);
196 toggleButton.setFocusPainted(true);
197 }
198 }
199 }
200 };
201 toggleButton.addFocusListener(focusListener);
202 }
203
204 }
205 toggleButton.setToolTipText(getProperty("tooltip"));
206
207 toggleButton.repaint();
208 isRefreshing = false;
209 }
210
211
212
213
214 public Object getGuiValue() {
215 if (initialized) {
216 return BooleanUtils.toBooleanObject(toggleButton.isSelected());
217 }
218 return null;
219 }
220
221
222
223
224 public void focus() {
225
226 }
227
228
229
230
231 public boolean isValueEmpty() {
232 return false;
233 }
234
235
236
237
238 public boolean canContainValue() {
239 return true;
240 }
241
242
243
244
245 public Object getValue() {
246 if (getPart().getBean() == null || getField() == null) {
247 Object retValue = super.getValue();
248 if (retValue == null) {
249 retValue = getGuiValue();
250 }
251 return retValue;
252 }
253 IMapping map = XuluxContext.getDictionary().getDefaultProvider().getMapping(getPart().getBean().getClass());
254 if (map != null) {
255 IField field = map.getField(getField());
256 if (field != null) {
257 return field.getValue(getPart().getBean());
258 }
259 }
260 return super.getValue();
261 }
262
263
264
265
266 public void setValue(Object value) {
267 if (this.value == null) {
268 this.value = "false";
269 }
270 this.previousValue = this.value;
271 IMapping map = XuluxContext.getDictionary().getDefaultProvider().getMapping(getPart().getBean());
272 if (map != null) {
273 if (getField() != null) {
274 IField f = map.getField(getField());
275 Class cClass = f.getType();
276 if (cClass == Boolean.class || cClass == Boolean.TYPE) {
277 if (value.getClass() == String.class) {
278 value = BooleanUtils.toBooleanObject((String) value);
279 }
280 } else if (cClass == String.class) {
281 if (value.getClass() == Boolean.class) {
282 value = BooleanUtils.toStringTrueFalse((Boolean) value);
283 }
284 }
285 f.setValue(getPart().getBean(), value);
286 }
287 }
288 this.value = value;
289 if (initialized) {
290 refresh();
291 }
292 }
293
294 public IWidgetInitializer getWidgetInitializer() {
295 return null;
296 }
297 }