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