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.IField;
24 import org.xulux.api.dataprovider.IMapping;
25 import org.xulux.api.gui.IWidgetInitializer;
26 import org.xulux.core.XuluxContext;
27 import org.xulux.gui.XuluxWidget;
28 import org.xulux.gui.utils.ColorUtils;
29 import org.xulux.guilayer.swing.extensions.NyxJRadioButton;
30 import org.xulux.guilayer.swing.listeners.PrePostFieldListener;
31 import org.xulux.utils.BooleanUtils;
32
33
34
35
36
37
38
39 public class RadioButton extends XuluxWidget {
40
41
42
43
44 protected NyxJRadioButton radioButton;
45
46
47
48 protected PrePostFieldListener itemListener;
49
50
51
52
53 public RadioButton(String name) {
54 super(name);
55 }
56
57
58
59
60 public void destroy() {
61 if (!initialized) {
62 return;
63 }
64 processDestroy();
65 removeAllRules();
66 if (radioButton != null) {
67 if (itemListener != null) {
68 radioButton.removeItemListener(itemListener);
69 }
70 radioButton.removeAll();
71 Container container = radioButton.getParent();
72 if (container != null) {
73 container.remove(radioButton);
74 }
75 radioButton = null;
76 }
77 removeAllRules();
78 getPart().removeWidget(this, this);
79
80 }
81
82
83
84
85 public Object getNativeWidget() {
86 initialize();
87 return this.radioButton;
88 }
89
90
91
92
93 public void initialize() {
94 if (initialized) {
95 return;
96 }
97 radioButton = new NyxJRadioButton();
98
99 radioButton.setIcon((Icon) UIManager.get("RadioButton.icon"));
100 radioButton.setSelectedIcon((Icon) UIManager.get("RadioButton.icon"));
101 itemListener = new PrePostFieldListener(this);
102 radioButton.addItemListener(this.itemListener);
103 radioButton.setSelected(BooleanUtils.toBoolean(getValue()));
104 initialized = true;
105 refresh();
106 processInit();
107
108 }
109
110
111
112
113 public void refresh() {
114 if (isRefreshing()) {
115 return;
116 }
117 isRefreshing = true;
118 initialize();
119 if (getProperty("text") != null) {
120 radioButton.setText(getProperty("text"));
121 }
122 if (getValue() instanceof Boolean) {
123 radioButton.setSelected(BooleanUtils.toBoolean((Boolean) getValue()));
124 } else if (getValue() instanceof String) {
125 radioButton.setSelected(BooleanUtils.toBoolean((String) getValue()));
126 } else {
127 radioButton.setSelected(BooleanUtils.toBoolean(getProperty("selected")));
128 }
129 radioButton.setEnabled(isEnabled());
130 String backgroundColor = null;
131 if (isRequired() && isEnabled()) {
132 backgroundColor = getProperty("required-background-color");
133 } else if (!isEnabled()) {
134 backgroundColor = getProperty("disabled-background-color");
135 } else {
136 backgroundColor = getProperty("default-background-color");
137 }
138 if (backgroundColor != null) {
139 radioButton.setRealBackground(ColorUtils.getSwingColor(backgroundColor));
140 }
141 isRefreshing = false;
142 }
143
144
145
146
147 public Object getGuiValue() {
148 if (initialized) {
149 return BooleanUtils.toBooleanObject(radioButton.isSelected());
150 }
151 return null;
152 }
153
154
155
156
157 public void focus() {
158
159 }
160
161
162
163
164 public boolean isValueEmpty() {
165 return false;
166 }
167
168
169
170
171 public boolean canContainValue() {
172 return true;
173 }
174
175
176
177
178 public Object getValue() {
179 if (getPart().getBean() == null || getField() == null) {
180 Object retValue = getGuiValue();
181 if (retValue == null) {
182 retValue = super.getValue();
183 }
184 return retValue;
185 }
186 IMapping map = XuluxContext.getDictionary().getDefaultProvider().getMapping(getPart().getBean().getClass());
187 if (map != null) {
188 IField field = map.getField(getField());
189 if (field != null) {
190 return field.getValue(getPart().getBean());
191 }
192 }
193 return super.getValue();
194 }
195
196
197
198
199 public void setValue(Object value) {
200 if (this.value == null) {
201 this.value = "false";
202 }
203 this.previousValue = this.value;
204 IMapping map = XuluxContext.getDictionary().getDefaultProvider().getMapping(getPart().getBean());
205 if (map != null) {
206 if (getField() != null) {
207 IField f = map.getField(getField());
208 Class cClass = f.getType();
209 if (cClass == Boolean.class || cClass == Boolean.TYPE) {
210 if (value.getClass() == String.class) {
211 value = BooleanUtils.toBooleanObject((String) value);
212 }
213 } else if (cClass == String.class) {
214 if (value.getClass() == Boolean.class) {
215 value = BooleanUtils.toStringTrueFalse((Boolean) value);
216 }
217 }
218 f.setValue(getPart().getBean(), value);
219 }
220 }
221 this.value = value;
222 if (initialized) {
223 refresh();
224 }
225 }
226
227 public IWidgetInitializer getWidgetInitializer() {
228 return null;
229 }
230
231 }