Subversion Repository Public Repository

litesoft

Diff Revisions 626 vs 948 for /trunk/GWT_Sandbox/FormEngine/src/com/temp/client/foundation/widget/OptionBox.java

Diff revisions: vs.
  @@ -1,19 +1,10 @@
1 1 package com.temp.client.foundation.widget;
2 2
3 - import java.util.HashMap;
4 - import java.util.LinkedHashMap;
5 - import java.util.List;
6 - import java.util.Map;
7 -
8 - import com.temp.shared.utils.Assert;
9 - import com.temp.shared.utils.DisplayStringHandler;
10 - import com.temp.shared.utils.Displayable;
11 - import com.temp.shared.utils.NoEqualsHelper;
12 - import com.temp.shared.utils.ObjectUtils;
13 - import com.temp.shared.utils.StringUtils;
3 + import com.google.gwt.i18n.client.HasDirection.*;
4 + import com.google.gwt.user.client.ui.*;
5 + import com.temp.shared.utils.*;
14 6
15 - import com.google.gwt.i18n.client.HasDirection.Direction;
16 - import com.google.gwt.user.client.ui.ListBox;
7 + import java.util.*;
17 8
18 9 public class OptionBox<T> extends ListBox {
19 10
  @@ -29,27 +20,27 @@
29 20 private int lastSeqNumber = 0;
30 21
31 22 public OptionBox<T> addNullOptional() {
32 - return setAddNullOption(DEFAULT_OPTIONAL_NOTHING_SELECTED_TEXT);
23 + return setAddNullOption( DEFAULT_OPTIONAL_NOTHING_SELECTED_TEXT );
33 24 }
34 25
35 - public OptionBox<T> addNullOptional(String text) {
36 - return setAddNullOption(text);
26 + public OptionBox<T> addNullOptional( String text ) {
27 + return setAddNullOption( text );
37 28 }
38 29
39 30 public OptionBox<T> enable() {
40 - setEnabled(true);
31 + setEnabled( true );
41 32 return this;
42 33 }
43 34
44 35 public OptionBox<T> disable() {
45 - setEnabled(false);
36 + setEnabled( false );
46 37 return this;
47 38 }
48 39
49 40 public OptionBox<T> reset() {
50 - setEnabled(true);
51 - if (!isEmpty()) {
52 - setSelectedIndex(0);
41 + setEnabled( true );
42 + if ( !isEmpty() ) {
43 + setSelectedIndex( 0 );
53 44 }
54 45 return this;
55 46 }
  @@ -67,28 +58,28 @@
67 58 return displayStringHandler;
68 59 }
69 60
70 - public OptionBox<T> addSupporter(Object supporter) {
61 + public OptionBox<T> addSupporter( Object supporter ) {
71 62 boolean neither = true;
72 - if (supporter instanceof NoEqualsHelper) {
73 - noEqualsHelper = ObjectUtils.cast(supporter);
63 + if ( supporter instanceof NoEqualsHelper ) {
64 + noEqualsHelper = ObjectUtils.cast( supporter );
74 65 neither = false;
75 66 }
76 - if (supporter instanceof DisplayStringHandler) {
77 - displayStringHandler = ObjectUtils.cast(supporter);
67 + if ( supporter instanceof DisplayStringHandler ) {
68 + displayStringHandler = ObjectUtils.cast( supporter );
78 69 neither = false;
79 70 }
80 - if (neither) {
81 - throw new IllegalArgumentException("Supporter was neither: NoEqualsHelper nor DisplayStringHandler");
71 + if ( neither ) {
72 + throw new IllegalArgumentException( "Supporter was neither: NoEqualsHelper nor DisplayStringHandler" );
82 73 }
83 74 return this;
84 75 }
85 76
86 - public OptionBox<T> setDisplayStringHandler(DisplayStringHandler<T> handler) {
77 + public OptionBox<T> setDisplayStringHandler( DisplayStringHandler<T> handler ) {
87 78 displayStringHandler = handler;
88 79 return this;
89 80 }
90 81
91 - public OptionBox<T> setNoEqualsHelper(NoEqualsHelper<T, String> helper) {
82 + public OptionBox<T> setNoEqualsHelper( NoEqualsHelper<T, String> helper ) {
92 83 noEqualsHelper = helper;
93 84 return this;
94 85 }
  @@ -97,10 +88,10 @@
97 88 return (nullOptionTextToAdd != null);
98 89 }
99 90
100 - public OptionBox<T> setAddNullOption(String nullOptionTextToAdd) {
101 - if (nullOptionTextToAdd != null) {
91 + public OptionBox<T> setAddNullOption( String nullOptionTextToAdd ) {
92 + if ( nullOptionTextToAdd != null ) {
102 93 // Force there to be at least some surrounding whitespace (too force differentiation from legitimate values)
103 - if (!nullOptionTextToAdd.startsWith(" ") && !nullOptionTextToAdd.endsWith(" ")) {
94 + if ( !nullOptionTextToAdd.startsWith( " " ) && !nullOptionTextToAdd.endsWith( " " ) ) {
104 95 nullOptionTextToAdd += " ";
105 96 }
106 97 }
  @@ -108,42 +99,42 @@
108 99 return this;
109 100 }
110 101
111 - public T getOptionAt(int index) {
112 - if ((0 <= index) && (index < getItemCount())) {
113 - String selectedValue = getValue(index);
114 - return optionsByListBoxItemValue.get(selectedValue);
102 + public T getOptionAt( int index ) {
103 + if ( (0 <= index) && (index < getItemCount()) ) {
104 + String selectedValue = getValue( index );
105 + return optionsByListBoxItemValue.get( selectedValue );
115 106 }
116 107 return null;
117 108 }
118 109
119 110 public T getSelected() {
120 - return getOptionAt(getSelectedIndex());
111 + return getOptionAt( getSelectedIndex() );
121 112 }
122 113
123 - public boolean setSelected(T option) {
124 - int index = indexOfValue(findValueForOption(option));
125 - if (0 <= index) {
126 - setSelectedIndex(index);
114 + public boolean setSelected( T option ) {
115 + int index = indexOfValue( findValueForOption( option ) );
116 + if ( 0 <= index ) {
117 + setSelectedIndex( index );
127 118 return true;
128 119 }
129 - if (!isEmpty()) {
130 - setSelectedIndex(0);
120 + if ( !isEmpty() ) {
121 + setSelectedIndex( 0 );
131 122 }
132 123 return false;
133 124 }
134 125
135 - public boolean setSelectedByDisplayString(String displayString) {
136 - displayString = StringUtils.noEmpty(displayString);
137 - if (displayString != null) {
138 - for (int i = 0; i < getItemCount(); i++) {
139 - if (displayString.equals(getItemText(i))) {
140 - setSelectedIndex(i);
126 + public boolean setSelectedByDisplayString( String displayString ) {
127 + displayString = StringUtils.noEmpty( displayString );
128 + if ( displayString != null ) {
129 + for ( int i = 0; i < getItemCount(); i++ ) {
130 + if ( displayString.equals( getItemText( i ) ) ) {
131 + setSelectedIndex( i );
141 132 return true;
142 133 }
143 134 }
144 135 }
145 - if (!isEmpty()) {
146 - setSelectedIndex(0);
136 + if ( !isEmpty() ) {
137 + setSelectedIndex( 0 );
147 138 }
148 139 return false;
149 140 }
  @@ -152,7 +143,7 @@
152 143 /**
153 144 * Method ! Supported
154 145 */
155 - public void setItemText(int index, String text, Direction dir) {
146 + public void setItemText( int index, String text, Direction dir ) {
156 147 throw new UnsupportedOperationException();
157 148 }
158 149
  @@ -160,7 +151,7 @@
160 151 /**
161 152 * Method ! Supported
162 153 */
163 - public void setValue(int index, String value) {
154 + public void setValue( int index, String value ) {
164 155 throw new UnsupportedOperationException();
165 156 }
166 157
  @@ -181,15 +172,15 @@
181 172 * {@link FormPanel}.
182 173 * @param index the index at which to insert it (-1 means the end!)
183 174 */
184 - public void insertItem(String item, Direction dir, String value, int index) {
185 - if ((addOptionTypeAdded != null) && (String.class != addOptionTypeAdded)) {
186 - throw new IllegalStateException("Attempt to add a String Item directly, when the OptionBox Type is probably: " + addOptionTypeAdded.getName());
175 + public void insertItem( String item, Direction dir, String value, int index ) {
176 + if ( (addOptionTypeAdded != null) && (String.class != addOptionTypeAdded) ) {
177 + throw new IllegalStateException( "Attempt to add a String Item directly, when the OptionBox Type is probably: " + addOptionTypeAdded.getName() );
187 178 }
188 179 checkAddDefaultNothingSelectedOption();
189 - superInsertItem(item, dir, value, index);
180 + superInsertItem( item, dir, value, index );
190 181 addItemStringCalled = true;
191 - T valueAsOption = ObjectUtils.cast(value);
192 - addToMap(value, valueAsOption);
182 + T valueAsOption = ObjectUtils.cast( value );
183 + addToMap( value, valueAsOption );
193 184 }
194 185
195 186 @Override
  @@ -206,60 +197,60 @@
206 197 * @param index the index of the item to be removed
207 198 * @throws IndexOutOfBoundsException if the index is out of range
208 199 */
209 - public void removeItem(int index) {
210 - String mapKey = super.getValue(index);
211 - super.removeItem(index);
212 - optionsByListBoxItemValue.remove(mapKey);
200 + public void removeItem( int index ) {
201 + String mapKey = super.getValue( index );
202 + super.removeItem( index );
203 + optionsByListBoxItemValue.remove( mapKey );
213 204 }
214 205
215 - public OptionBox<T> addOption(T option) {
206 + public OptionBox<T> addOption( T option ) {
216 207 checkAddDefaultNothingSelectedOption();
217 - postInitialCheckedAdd(option);
208 + postInitialCheckedAdd( option );
218 209 return this;
219 210 }
220 211
221 - public OptionBox<T> addOption(T option, String displayText) {
212 + public OptionBox<T> addOption( T option, String displayText ) {
222 213 checkAddDefaultNothingSelectedOption();
223 - postInitialCheckedAdd(option, displayText);
214 + postInitialCheckedAdd( option, displayText );
224 215 return this;
225 216 }
226 217
227 - public OptionBox<T> addOptions(T... options) {
218 + public OptionBox<T> addOptions( T... options ) {
228 219 checkAddDefaultNothingSelectedOption();
229 - if (options != null) {
230 - for (T option : options) {
231 - postInitialCheckedAdd(option);
220 + if ( options != null ) {
221 + for ( T option : options ) {
222 + postInitialCheckedAdd( option );
232 223 }
233 224 }
234 225 return this;
235 226 }
236 227
237 - public OptionBox<T> addOptions(List<T> options) {
228 + public OptionBox<T> addOptions( List<T> options ) {
238 229 checkAddDefaultNothingSelectedOption();
239 - if (options != null) {
240 - for (T option : options) {
241 - postInitialCheckedAdd(option);
230 + if ( options != null ) {
231 + for ( T option : options ) {
232 + postInitialCheckedAdd( option );
242 233 }
243 234 }
244 235 return this;
245 236 }
246 237
247 - public OptionBox<T> addOptions(LinkedHashMap<T, String> optionsWithDisplayText) {
238 + public OptionBox<T> addOptions( LinkedHashMap<T, String> optionsWithDisplayText ) {
248 239 checkAddDefaultNothingSelectedOption();
249 - if (optionsWithDisplayText != null) {
250 - for (Map.Entry<T, String> optionWithDisplayText : optionsWithDisplayText.entrySet()) {
240 + if ( optionsWithDisplayText != null ) {
241 + for ( Map.Entry<T, String> optionWithDisplayText : optionsWithDisplayText.entrySet() ) {
251 242 T option = optionWithDisplayText.getKey();
252 243 String displayText = optionWithDisplayText.getValue();
253 - postInitialCheckedAdd(option, displayText);
244 + postInitialCheckedAdd( option, displayText );
254 245 }
255 246 }
256 247 return this;
257 248 }
258 249
259 - protected int indexOfValue(String valueToFind) {
260 - if (valueToFind != null) {
261 - for (int index = 0; index < getItemCount(); index++) {
262 - if (valueToFind.equals(getValue(index))) {
250 + protected int indexOfValue( String valueToFind ) {
251 + if ( valueToFind != null ) {
252 + for ( int index = 0; index < getItemCount(); index++ ) {
253 + if ( valueToFind.equals( getValue( index ) ) ) {
263 254 return index;
264 255 }
265 256 }
  @@ -268,16 +259,16 @@
268 259 }
269 260
270 261 protected void checkAddDefaultNothingSelectedOption() {
271 - if (isAddNullOption() && isEmpty()) {
272 - superInsertItem(nullOptionTextToAdd, baseValuePrefix);
262 + if ( isAddNullOption() && isEmpty() ) {
263 + superInsertItem( nullOptionTextToAdd, baseValuePrefix );
273 264 }
274 265 }
275 266
276 - protected String findValueForOption(T optionToFind) {
277 - if (optionToFind != null) {
278 - for (Map.Entry<String, T> valueAndOption : optionsByListBoxItemValue.entrySet()) {
267 + protected String findValueForOption( T optionToFind ) {
268 + if ( optionToFind != null ) {
269 + for ( Map.Entry<String, T> valueAndOption : optionsByListBoxItemValue.entrySet() ) {
279 270 T currentOption = valueAndOption.getValue();
280 - if (areEqual(optionToFind, currentOption)) {
271 + if ( areEqual( optionToFind, currentOption ) ) {
281 272 return valueAndOption.getKey(); // Note: the Entry "Key" is actually the List Box "Value"
282 273 }
283 274 }
  @@ -285,68 +276,69 @@
285 276 return null;
286 277 }
287 278
288 - protected boolean areEqual(T t1, T t2) {
289 - if (noEqualsHelper == null) {
290 - return ObjectUtils.areEqual(t1, t2);
279 + protected boolean areEqual( T t1, T t2 ) {
280 + if ( noEqualsHelper == null ) {
281 + return ObjectUtils.areEqual( t1, t2 );
291 282 }
292 - return ObjectUtils.areEqual(noEqualsHelper.getProxyValue(t1), noEqualsHelper.getProxyValue(t2));
283 + return ObjectUtils.areEqual( noEqualsHelper.getProxyValue( t1 ), noEqualsHelper.getProxyValue( t2 ) );
293 284 }
294 285
295 286 protected String generateValue() {
296 287 return baseValuePrefix + (++lastSeqNumber);
297 288 }
298 289
299 - protected void superInsertItem(String item, Direction dir, String value, int index) {
290 + protected void superInsertItem( String item, Direction dir, String value, int index ) {
300 291 boolean wasEmpty = isEmpty();
301 - super.insertItem(item, dir, value, index);
302 - if (wasEmpty) {
303 - setSelectedIndex(0);
292 + super.insertItem( item, dir, value, index );
293 + if ( wasEmpty ) {
294 + setSelectedIndex( 0 );
304 295 }
305 296 }
306 297
307 - protected void superInsertItem(String displayForm, String submitValue) {
308 - superInsertItem(displayForm, null, submitValue, -1);
298 + protected void superInsertItem( String displayForm, String submitValue ) {
299 + superInsertItem( displayForm, null, submitValue, -1 );
309 300 }
310 301
311 - protected void addToMap(String mapKeyOrListBoxItemValue, T option) {
312 - if (null != optionsByListBoxItemValue.put(mapKeyOrListBoxItemValue, option)) {
313 - throw new IllegalStateException("Duplicate option '" + mapKeyOrListBoxItemValue + "' for OptionBox named: " + getName());
302 + protected void addToMap( String mapKeyOrListBoxItemValue, T option ) {
303 + if ( null != optionsByListBoxItemValue.put( mapKeyOrListBoxItemValue, option ) ) {
304 + throw new IllegalStateException( "Duplicate option '" + mapKeyOrListBoxItemValue + "' for OptionBox named: " + getName() );
314 305 }
315 306 }
316 307
317 - protected void paramsCheckedAdd(T option, String displayText) {
308 + protected void paramsCheckedAdd( T option, String displayText ) {
318 309 String uniqueValueForListBox = generateValue();
319 - addToMap(uniqueValueForListBox, option);
320 - superInsertItem(displayText, uniqueValueForListBox);
310 + addToMap( uniqueValueForListBox, option );
311 + superInsertItem( displayText, uniqueValueForListBox );
321 312 }
322 313
323 - protected void postInitialCheckedAdd(T option, String displayText) {
324 - option = checkOption(option);
325 - displayText = Assert.noEmpty("displayText", displayText);
326 - paramsCheckedAdd(option, displayText);
314 + protected void postInitialCheckedAdd( T option, String displayText ) {
315 + option = checkOption( option );
316 + displayText = Assert.noEmpty( "displayText", displayText );
317 + paramsCheckedAdd( option, displayText );
327 318 }
328 319
329 - protected void postInitialCheckedAdd(T option) {
330 - option = checkOption(option);
331 - paramsCheckedAdd(option, extractDisplayText(option));
320 + protected void postInitialCheckedAdd( T option ) {
321 + option = checkOption( option );
322 + paramsCheckedAdd( option, extractDisplayText( option ) );
332 323 }
333 324
334 - protected T checkOption(T option) {
335 - option = Assert.notNullOrEmpty("option", option);
336 - if (addItemStringCalled && !(option instanceof String)) {
337 - throw new IllegalStateException("Attempt to add a non-String type 'T' of '" + option.getClass().getName() + "' when a String type was explicitly already added for OptionBox named: "
338 - + getName());
325 + protected T checkOption( T option ) {
326 + option = Assert.notNullOrEmpty( "option", option );
327 + if ( addItemStringCalled && !(option instanceof String) ) {
328 + throw new IllegalStateException( "Attempt to add a non-String type 'T' of '" + option.getClass().getName() +
329 + "' when a String type was explicitly already added for OptionBox named: "
330 + + getName() );
339 331 }
340 332 addOptionTypeAdded = option.getClass();
341 333 return option;
342 334 }
343 335
344 - private String extractDisplayText(T option) {
345 - if (displayStringHandler != null) {
346 - return Assert.noEmpty("DisplayStringHandler", displayStringHandler.getDisplayString(option));
336 + private String extractDisplayText( T option ) {
337 + if ( displayStringHandler != null ) {
338 + return Assert.noEmpty( "DisplayStringHandler", displayStringHandler.getDisplayString( option ) );
347 339 }
348 - if (option instanceof Displayable) {
349 - return Assert.noEmpty("Displayable", ((Displayable) option).getDisplayString());
340 + if ( option instanceof Displayable ) {
341 + return Assert.noEmpty( "Displayable", ((Displayable) option).getDisplayString() );
350 342 }
351 343 return option.toString(); // Fall back
352 344 }