Subversion Repository Public Repository

litesoft

Diff Revisions 634 vs 635 for /trunk/GWT_Sandbox/FormEngine/src/com/temp/client/foundation/widget/input/RadioButtonGroupInputField.java

Diff revisions: vs.
  @@ -1,15 +1,16 @@
1 1 package com.temp.client.foundation.widget.input;
2 2
3 + import com.google.gwt.user.client.ui.HasEnabled;
3 4 import com.google.gwt.user.client.ui.Panel;
4 - import com.google.gwt.user.client.ui.Widget;
5 5 import com.temp.client.foundation.widget.NameToID;
6 6 import com.temp.client.foundation.widget.input.fieldsupport.AbstractInputField;
7 - import com.temp.client.foundation.widget.input.fieldsupport.InputWidgetChangeFilter;
8 - import com.temp.client.foundation.widget.input.fieldsupport.InputWidgetValidator;
7 + import com.temp.client.foundation.widget.input.fieldsupport.FocusChangedListener;
8 + import com.temp.client.foundation.widget.input.fieldsupport.StateInputField;
9 9 import com.temp.shared.externalization.E13nData;
10 10 import com.temp.shared.externalization.E13nResolver;
11 11 import com.temp.shared.externalization.OptionallyPrefixingE13nResolver;
12 - import com.temp.shared.utils.Enableable;
12 + import com.temp.shared.utils.ObjectUtils;
13 + import com.temp.shared.utils.StringUtils;
13 14
14 15 import java.util.ArrayList;
15 16 import java.util.List;
  @@ -17,11 +18,10 @@
17 18 /**
18 19 * This is not a "real" Widget backed InputField, but a Proxy to a number of RadioButtonInputFields
19 20 */
20 - public class RadioButtonGroupInputField<T> extends AbstractInputField<T, RadioButtonGroupInputField<T>> {
21 + public class RadioButtonGroupInputField extends AbstractInputField<String, RadioButtonGroupInputField> {
21 22
22 23 private final Panel container;
23 - private final String groupName;
24 - private final List<RadioButtonInputField> radioButtons = new ArrayList<RadioButtonInputField>();
24 + private final FieldAccessor fieldAccessor;
25 25
26 26 private E13nResolver resolver = null;
27 27
  @@ -30,32 +30,35 @@
30 30 RadioButtonInputField radioButton2, //
31 31 RadioButtonInputField... additionalRadioButtons) {
32 32 this.container = container;
33 - groupName = radioButton1.getGroup();
34 -
35 - ChangeListener<Boolean> listener = new ChangeListener<Boolean>() {
36 - @Override
37 - public void changed(InputField<Boolean> radioButton) {
38 - System.out.println("RadioButtonGroupInputField.changed: " + radioButton.getName());
39 - }
40 - };
41 -
42 - add(radioButton1, listener);
43 - add(radioButton2, listener);
44 - if (additionalRadioButtons != null) {
45 - for (RadioButtonInputField radioButton : additionalRadioButtons) {
46 - add(radioButton, listener);
47 - }
48 - }
33 + fieldAccessor = new FieldAccessor(radioButton1, radioButton2, additionalRadioButtons, //
34 + new ChangeListener<Boolean>() {
35 + @Override
36 + public void changed(InputField<Boolean> radioButton) {
37 + processDataChanged();
38 + }
39 + }, //
40 + new FocusChangedListener() {
41 + @Override
42 + public void focusChanged() {
43 + processFocusChanged();
44 + }
45 + } //
46 + );
49 47 }
50 48
51 - private void add(RadioButtonInputField radioButton, ChangeListener<Boolean> listener) {
49 + @Override
50 + protected Panel getContainer() {
51 + return container;
52 + }
52 53
53 - //To change body of created methods use File | Settings | File Templates.
54 + @Override
55 + protected HasEnabled getEnableable() {
56 + return fieldAccessor;
54 57 }
55 58
56 59 @Override
57 60 public String getName() {
58 - return groupName;
61 + return fieldAccessor.groupName;
59 62 }
60 63
61 64 @Override
  @@ -65,111 +68,263 @@
65 68
66 69 @Override
67 70 public void init(E13nResolver parentResolver) {
68 - resolver = new OptionallyPrefixingE13nResolver(groupName, parentResolver);
69 -
70 - for (RadioButtonInputField radioButton : radioButtons) {
71 - radioButton.init(resolver);
72 - }
71 + resolver = new OptionallyPrefixingE13nResolver(getName(), parentResolver);
72 + fieldAccessor.init(resolver);
73 73 }
74 74
75 75 @Override
76 76 public boolean isRunMode() {
77 - return false; //To change body of implemented methods use File | Settings | File Templates.
77 + return (resolver != null);
78 78 }
79 79
80 - @Override
81 - public void reset() {
82 - //To change body of implemented methods use File | Settings | File Templates.
83 - }
80 + // Only callable in Run Mode
84 81
85 82 @Override
86 - public void revert() {
87 - //To change body of implemented methods use File | Settings | File Templates.
83 + public final void reset() {
84 + checkRunMode("reset").setValue(null);
88 85 }
89 86
90 87 @Override
91 - public boolean isChanged() {
92 - return false; //To change body of implemented methods use File | Settings | File Templates.
88 + public final String setCurrentValue(String value) {
89 + assertRunMode("setCurrentValue");
90 + fieldAccessor.setValue(value);
91 + return fieldAccessor.getValue();
93 92 }
94 93
95 94 @Override
96 - public Boolean isFocused() {
97 - return null; //To change body of implemented methods use File | Settings | File Templates.
95 + public final String getCurrentValue() {
96 + return checkRunMode("getCurrentValue").getValue();
98 97 }
99 98
100 99 @Override
101 - public boolean isErrored() {
102 - return false; //To change body of implemented methods use File | Settings | File Templates.
100 + public final String getBaseValue() {
101 + return checkRunMode("getBaseValue").getBaseValue();
103 102 }
104 103
105 104 @Override
106 - public boolean validate() {
107 - return false; //To change body of implemented methods use File | Settings | File Templates.
105 + public final void revert() {
106 + assertRunMode("revert");
107 + fieldAccessor.setValue(fieldAccessor.getBaseValue());
108 108 }
109 109
110 110 @Override
111 - public boolean setFocused() {
112 - return false; //To change body of implemented methods use File | Settings | File Templates.
111 + public final boolean isChanged() {
112 + return checkRunMode("isChanged").isChanged();
113 113 }
114 114
115 115 @Override
116 - public E13nResolver getE13nResolver() {
117 - assertBuildMode("E13nResolver");
118 - return resolver;
116 + public final Boolean isFocused() {
117 + assertRunMode("isFocused");
118 + Boolean focused = fieldAccessor.isFocused();
119 + return Boolean.TRUE.equals(focused) ? isVisible() : focused;
119 120 }
120 121
121 122 @Override
122 - public NameToID addNamedWidgetsTo(NameToID nameToID) {
123 - return null; //To change body of implemented methods use File | Settings | File Templates.
123 + public final boolean isErrored() {
124 + assertRunMode("isErrored");
125 + return false;
124 126 }
125 127
126 128 @Override
127 - protected Enableable getEnableable() {
128 - return null; //To change body of implemented methods use File | Settings | File Templates.
129 + public final E13nData getErrorData() {
130 + assertRunMode("getErrorData");
131 + return null;
129 132 }
130 133
131 134 @Override
132 - protected Panel getContainer() {
133 - return container;
135 + public final boolean validate() {
136 + assertRunMode("validate");
137 + updateStyle(fieldAccessor);
138 + return true;
134 139 }
135 140
136 141 @Override
137 - protected void updateErrorLabel(Widget errorLabel, E13nData error) {
138 - //To change body of implemented methods use File | Settings | File Templates.
142 + public final boolean setFocused() {
143 + assertRunMode("setFocused");
144 + return isVisible() && fieldAccessor.setFocused();
139 145 }
140 146
141 147 @Override
142 - public T setCurrentValue(T value) {
143 - return null; //To change body of implemented methods use File | Settings | File Templates.
148 + public final E13nResolver getE13nResolver() {
149 + assertRunMode("E13nResolver");
150 + return resolver;
144 151 }
145 152
146 153 @Override
147 - public T getCurrentValue() {
148 - return null; //To change body of implemented methods use File | Settings | File Templates.
154 + public NameToID addNamedWidgetsTo(NameToID nameToID) {
155 + return checkRunMode("addNamedWidgetsTo").addNamedWidgetsTo(nameToID);
149 156 }
150 157
151 - @Override
152 - public T getBaseValue() {
153 - return null; //To change body of implemented methods use File | Settings | File Templates.
158 + // Support Methods...
159 +
160 + protected void processDataChanged() {
161 + updateStyle(checkRunMode("processDataChanged"));
162 + notifyChangeListeners();
154 163 }
155 164
156 - @Override
157 - public InputWidgetChangeFilter<T> getInput() {
158 - return null; //To change body of implemented methods use File | Settings | File Templates.
165 + protected void processFocusChanged() {
166 + updateStyle(checkRunMode("processFocusChanged"));
167 + notifyFocusChangeListeners();
159 168 }
160 169
161 - @Override
162 - public InputWidgetValidator<T> getValidator() {
163 - return null; //To change body of implemented methods use File | Settings | File Templates.
170 + protected void updateStyle(FieldAccessor fieldAccessor) {
171 + updateStyle(StateInputField.BASE //
172 + .focused(Boolean.TRUE.equals(fieldAccessor.isFocused())) //
173 + .changed(fieldAccessor.isChanged()));
164 174 }
165 175
166 - @Override
167 - public Widget getErrorLabel() {
168 - return null; //To change body of implemented methods use File | Settings | File Templates.
176 + protected final FieldAccessor checkRunMode(String method) {
177 + assertRunMode(method);
178 + return fieldAccessor;
169 179 }
170 180
171 - @Override
172 - public Widget getHelpWidget() {
173 - return null; //To change body of implemented methods use File | Settings | File Templates.
181 + private static class FieldAccessor implements HasEnabled {
182 + private final String groupName;
183 + private final List<RadioButtonInputField> radioButtons = new ArrayList<RadioButtonInputField>();
184 + private RadioButtonInputField nullValue;
185 + private RadioButtonInputField baseValue;
186 +
187 + public FieldAccessor(RadioButtonInputField radioButton1, //
188 + RadioButtonInputField radioButton2, //
189 + RadioButtonInputField[] additionalRadioButtons, //
190 + ChangeListener<Boolean> changeListener,
191 + FocusChangedListener focusChangedListener) {
192 +
193 + groupName = getGroup(radioButton1);
194 +
195 + add(radioButton1, changeListener, focusChangedListener);
196 + add(radioButton2, changeListener, focusChangedListener);
197 + if (additionalRadioButtons != null) {
198 + for (RadioButtonInputField radioButton : additionalRadioButtons) {
199 + add(radioButton, changeListener, focusChangedListener);
200 + }
201 + }
202 + }
203 +
204 + private static String getGroup(RadioButtonInputField radioButton) {
205 + String groupName = StringUtils.noEmpty(radioButton.getGroup());
206 + if (groupName != null) {
207 + return groupName;
208 + }
209 + throw new IllegalStateException("RadioButton named '" + radioButton.getName() + "', had No Group (name)");
210 + }
211 +
212 + private void add(RadioButtonInputField radioButton, ChangeListener<Boolean> changeListener, FocusChangedListener focusChangedListener) {
213 + String group = getGroup(radioButton);
214 + if (!groupName.equals(group)) {
215 + throw new IllegalArgumentException("RadioButton named '" + radioButton.getName() + "', " + //
216 + "Group (name) mismatch, expected '" + groupName + "', but got '" + group + "'");
217 + }
218 + radioButtons.add(radioButton);
219 + radioButton.add(changeListener);
220 + radioButton.add(focusChangedListener);
221 + }
222 +
223 + public void init(E13nResolver resolver) {
224 + for (RadioButtonInputField radioButton : radioButtons) {
225 + radioButton.init(resolver);
226 + if (Boolean.TRUE.equals(radioButton.getCurrentValue())) {
227 + if (nullValue == null) {
228 + nullValue = radioButton;
229 + } else {
230 + radioButton.setCurrentValue(false);
231 + }
232 + }
233 + }
234 + if (nullValue == null) {
235 + nullValue = radioButtons.get(0);
236 + nullValue.setCurrentValue(true);
237 + }
238 + baseValue = nullValue;
239 + }
240 +
241 + public boolean isChanged() {
242 + return (baseValue != findSelected());
243 + }
244 +
245 + public String getBaseValue() {
246 + return baseValue.getName();
247 + }
248 +
249 + public String getValue() {
250 + return findSelected().getName();
251 + }
252 +
253 + public void setValue(String radioButtonName) {
254 + baseValue = ObjectUtils.deNull(findByName(radioButtonName), nullValue);
255 + baseValue.setCurrentValue(true);
256 + }
257 +
258 + private RadioButtonInputField findSelected() {
259 + for (RadioButtonInputField radioButton : radioButtons) {
260 + if (Boolean.TRUE.equals(radioButton.getCurrentValue())) {
261 + return radioButton;
262 + }
263 + }
264 + nullValue.setCurrentValue(true);
265 + return nullValue;
266 + }
267 +
268 + private RadioButtonInputField findByName(String radioButtonName) {
269 + for (RadioButtonInputField radioButton : radioButtons) {
270 + if (radioButton.getName().equals(radioButtonName)) {
271 + return radioButton;
272 + }
273 + }
274 + return null;
275 + }
276 +
277 + public Boolean isFocused() {
278 + boolean anyUnableToDetermineFocus = false;
279 + for (RadioButtonInputField radioButton : radioButtons) {
280 + Boolean focused = radioButton.isFocused();
281 + if (focused == null) {
282 + anyUnableToDetermineFocus = true;
283 + } else if (focused) {
284 + return true;
285 + }
286 + }
287 + return anyUnableToDetermineFocus ? null : false;
288 + }
289 +
290 + public boolean setFocused() {
291 + for (RadioButtonInputField radioButton : radioButtons) {
292 + Boolean focused = radioButton.isFocused();
293 + if (Boolean.TRUE.equals(focused)) {
294 + return true;
295 + }
296 + }
297 + for (RadioButtonInputField radioButton : radioButtons) {
298 + if (radioButton.setFocused()) {
299 + return true;
300 + }
301 + }
302 + return false;
303 + }
304 +
305 + public NameToID addNamedWidgetsTo(NameToID nameToID) {
306 + for (RadioButtonInputField radioButton : radioButtons) {
307 + radioButton.addNamedWidgetsTo(nameToID);
308 + }
309 + return nameToID;
310 + }
311 +
312 + @Override
313 + public boolean isEnabled() {
314 + int enabled = 0;
315 + for (RadioButtonInputField radioButton : radioButtons) {
316 + if (radioButton.isEnabled()) {
317 + enabled++;
318 + }
319 + }
320 + return (enabled > 1);
321 + }
322 +
323 + @Override
324 + public void setEnabled(boolean enabled) {
325 + for (RadioButtonInputField radioButton : radioButtons) {
326 + radioButton.setEnabled(enabled);
327 + }
328 + }
174 329 }
175 330 }