litesoft
@ 965
litesoft / trunk / GWT_Sandbox / FormEngine / src / com / temp / foundation / client / widget / OptionBox.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 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
package com.temp.foundation.client.widget; import com.google.gwt.i18n.client.HasDirection.*; import com.google.gwt.user.client.ui.*; import com.temp.common.shared.utils.*; import java.util.*; public class OptionBox<T> extends ListBox { public static final String DEFAULT_OPTIONAL_NOTHING_SELECTED_TEXT = "-"; protected String nullOptionTextToAdd = null; protected DisplayStringHandler<T> displayStringHandler; protected NoEqualsHelper<T, String> noEqualsHelper; protected boolean addItemStringCalled; protected Class<?> addOptionTypeAdded; protected Map<String, T> optionsByListBoxItemValue = new HashMap<String, T>(); protected final String baseValuePrefix = "___"; private int lastSeqNumber = 0; public OptionBox<T> addNullOptional() { return setAddNullOption( DEFAULT_OPTIONAL_NOTHING_SELECTED_TEXT ); } public OptionBox<T> addNullOptional( String text ) { return setAddNullOption( text ); } public OptionBox<T> enable() { setEnabled( true ); return this; } public OptionBox<T> disable() { setEnabled( false ); return this; } public OptionBox<T> reset() { setEnabled( true ); if ( !isEmpty() ) { setSelectedIndex( 0 ); } return this; } public OptionBox<T> initialize() { clear(); return this; } public boolean isEmpty() { return (0 == getItemCount()); } public DisplayStringHandler<T> getDisplayStringHandler() { return displayStringHandler; } public OptionBox<T> addSupporter( Object supporter ) { boolean neither = true; if ( supporter instanceof NoEqualsHelper ) { noEqualsHelper = ObjectUtils.cast( supporter ); neither = false; } if ( supporter instanceof DisplayStringHandler ) { displayStringHandler = ObjectUtils.cast( supporter ); neither = false; } if ( neither ) { throw new IllegalArgumentException( "Supporter was neither: NoEqualsHelper nor DisplayStringHandler" ); } return this; } public OptionBox<T> setDisplayStringHandler( DisplayStringHandler<T> handler ) { displayStringHandler = handler; return this; } public OptionBox<T> setNoEqualsHelper( NoEqualsHelper<T, String> helper ) { noEqualsHelper = helper; return this; } public boolean isAddNullOption() { return (nullOptionTextToAdd != null); } public OptionBox<T> setAddNullOption( String nullOptionTextToAdd ) { if ( nullOptionTextToAdd != null ) { // Force there to be at least some surrounding whitespace (too force differentiation from legitimate values) if ( !nullOptionTextToAdd.startsWith( " " ) && !nullOptionTextToAdd.endsWith( " " ) ) { nullOptionTextToAdd += " "; } } this.nullOptionTextToAdd = nullOptionTextToAdd; return this; } public T getOptionAt( int index ) { if ( (0 <= index) && (index < getItemCount()) ) { String selectedValue = getValue( index ); return optionsByListBoxItemValue.get( selectedValue ); } return null; } public T getSelected() { return getOptionAt( getSelectedIndex() ); } public boolean setSelected( T option ) { int index = indexOfValue( findValueForOption( option ) ); if ( 0 <= index ) { setSelectedIndex( index ); return true; } if ( !isEmpty() ) { setSelectedIndex( 0 ); } return false; } public boolean setSelectedByDisplayString( String displayString ) { displayString = StringUtils.noEmpty( displayString ); if ( displayString != null ) { for ( int i = 0; i < getItemCount(); i++ ) { if ( displayString.equals( getItemText( i ) ) ) { setSelectedIndex( i ); return true; } } } if ( !isEmpty() ) { setSelectedIndex( 0 ); } return false; } @Override /** * Method ! Supported */ public void setItemText( int index, String text, Direction dir ) { throw new UnsupportedOperationException(); } @Override /** * Method ! Supported */ public void setValue( int index, String value ) { throw new UnsupportedOperationException(); } @Override /** * **** All addItem(s) & insertItem(s) end up here! **** * * Inserts an item into the list box, specifying its direction and an initial * value for the item. If the index is less than zero, or greater than or * equal to the length of the list, then the item will be appended to the end * of the list. * * @param item the text of the item to be inserted * @param dir the item's direction. If {@code null}, the item is displayed in * the widget's overall direction, or, if a direction estimator has * been set, in the item's estimated direction. * @param value the item's value, to be submitted if it is part of a * {@link FormPanel}. * @param index the index at which to insert it (-1 means the end!) */ public void insertItem( String item, Direction dir, String value, int index ) { if ( (addOptionTypeAdded != null) && (String.class != addOptionTypeAdded) ) { throw new IllegalStateException( "Attempt to add a String Item directly, when the OptionBox Type is probably: " + addOptionTypeAdded.getName() ); } checkAddDefaultNothingSelectedOption(); superInsertItem( item, dir, value, index ); addItemStringCalled = true; T valueAsOption = ObjectUtils.cast( value ); addToMap( value, valueAsOption ); } @Override public void clear() { super.clear(); optionsByListBoxItemValue.clear(); checkAddDefaultNothingSelectedOption(); } @Override /** * Removes the item at the specified index. * * @param index the index of the item to be removed * @throws IndexOutOfBoundsException if the index is out of range */ public void removeItem( int index ) { String mapKey = super.getValue( index ); super.removeItem( index ); optionsByListBoxItemValue.remove( mapKey ); } public OptionBox<T> addOption( T option ) { checkAddDefaultNothingSelectedOption(); postInitialCheckedAdd( option ); return this; } public OptionBox<T> addOption( T option, String displayText ) { checkAddDefaultNothingSelectedOption(); postInitialCheckedAdd( option, displayText ); return this; } public OptionBox<T> addOptions( T... options ) { checkAddDefaultNothingSelectedOption(); if ( options != null ) { for ( T option : options ) { postInitialCheckedAdd( option ); } } return this; } public OptionBox<T> addOptions( List<T> options ) { checkAddDefaultNothingSelectedOption(); if ( options != null ) { for ( T option : options ) { postInitialCheckedAdd( option ); } } return this; } public OptionBox<T> addOptions( LinkedHashMap<T, String> optionsWithDisplayText ) { checkAddDefaultNothingSelectedOption(); if ( optionsWithDisplayText != null ) { for ( Map.Entry<T, String> optionWithDisplayText : optionsWithDisplayText.entrySet() ) { T option = optionWithDisplayText.getKey(); String displayText = optionWithDisplayText.getValue(); postInitialCheckedAdd( option, displayText ); } } return this; } protected int indexOfValue( String valueToFind ) { if ( valueToFind != null ) { for ( int index = 0; index < getItemCount(); index++ ) { if ( valueToFind.equals( getValue( index ) ) ) { return index; } } } return -1; } protected void checkAddDefaultNothingSelectedOption() { if ( isAddNullOption() && isEmpty() ) { superInsertItem( nullOptionTextToAdd, baseValuePrefix ); } } protected String findValueForOption( T optionToFind ) { if ( optionToFind != null ) { for ( Map.Entry<String, T> valueAndOption : optionsByListBoxItemValue.entrySet() ) { T currentOption = valueAndOption.getValue(); if ( areEqual( optionToFind, currentOption ) ) { return valueAndOption.getKey(); // Note: the Entry "Key" is actually the List Box "Value" } } } return null; } protected boolean areEqual( T t1, T t2 ) { if ( noEqualsHelper == null ) { return ObjectUtils.areEqual( t1, t2 ); } return ObjectUtils.areEqual( noEqualsHelper.getProxyValue( t1 ), noEqualsHelper.getProxyValue( t2 ) ); } protected String generateValue() { return baseValuePrefix + (++lastSeqNumber); } protected void superInsertItem( String item, Direction dir, String value, int index ) { boolean wasEmpty = isEmpty(); super.insertItem( item, dir, value, index ); if ( wasEmpty ) { setSelectedIndex( 0 ); } } protected void superInsertItem( String displayForm, String submitValue ) { superInsertItem( displayForm, null, submitValue, -1 ); } protected void addToMap( String mapKeyOrListBoxItemValue, T option ) { if ( null != optionsByListBoxItemValue.put( mapKeyOrListBoxItemValue, option ) ) { throw new IllegalStateException( "Duplicate option '" + mapKeyOrListBoxItemValue + "' for OptionBox named: " + getName() ); } } protected void paramsCheckedAdd( T option, String displayText ) { String uniqueValueForListBox = generateValue(); addToMap( uniqueValueForListBox, option ); superInsertItem( displayText, uniqueValueForListBox ); } protected void postInitialCheckedAdd( T option, String displayText ) { option = checkOption( option ); displayText = Assert.noEmpty( "displayText", displayText ); paramsCheckedAdd( option, displayText ); } protected void postInitialCheckedAdd( T option ) { option = checkOption( option ); paramsCheckedAdd( option, extractDisplayText( option ) ); } protected T checkOption( T option ) { option = Assert.notNullOrEmpty( "option", option ); if ( addItemStringCalled && !(option instanceof String) ) { 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: " + getName() ); } addOptionTypeAdded = option.getClass(); return option; } private String extractDisplayText( T option ) { if ( displayStringHandler != null ) { return Assert.noEmpty( "DisplayStringHandler", displayStringHandler.getDisplayString( option ) ); } if ( option instanceof Displayable ) { return Assert.noEmpty( "Displayable", ((Displayable) option).getDisplayString() ); } return option.toString(); // Fall back } } |
Commits for litesoft/trunk/GWT_Sandbox/FormEngine/src/com/temp/foundation/client/widget/OptionBox.java
Revision | Author | Commited | Message |
---|---|---|---|
965 | GeorgeS | Fri 01 Aug, 2014 03:20:35 +0000 | ! |