4949import gwt .material .design .client .base .validator .ValidationChangedEvent .ValidationChangedHandler ;
5050import gwt .material .design .client .base .validator .Validator ;
5151import gwt .material .design .client .ui .html .Label ;
52- import gwt .material .design .client .ui .html .Option ;
5352
5453import java .util .*;
5554
@@ -92,6 +91,14 @@ public class MaterialListValueBox<T> extends MaterialWidget implements HasId, Ha
9291
9392 protected final List <T > values = new ArrayList <>();
9493
94+ // By default the key is generated using toString
95+ private KeyFactory <T , String > keyFactory = new KeyFactory <T , String >() {
96+ @ Override
97+ public String generateKey (T object ) {
98+ return object .toString ();
99+ }
100+ };
101+
95102 private ToggleStyleMixin <ListBox > toggleOldMixin ;
96103 private final ErrorHandlerMixin <T > errorHandlerMixin = new ErrorHandlerMixin <>(this );
97104 private final BlankValidatorMixin <MaterialListValueBox <T >, T > validatorMixin = new BlankValidatorMixin <>(this ,
@@ -234,7 +241,7 @@ public void setAcceptableValues(Collection<T> values) {
234241 clear ();
235242
236243 for (T value : values ) {
237- addValue (value );
244+ addItem (value );
238245 }
239246 }
240247
@@ -261,18 +268,6 @@ public void setValue(T value, boolean fireEvents) {
261268 }
262269 }
263270
264- public Option addValue (T value ) {
265- if (!values .contains (value )) {
266- values .add (value );
267- Option opt = new Option (value .toString ());
268- add (opt );
269- return opt ;
270- } else {
271- GWT .log ("Cannot add duplicate value: " + value );
272- }
273- return null ;
274- }
275-
276271 public boolean isOld () {
277272 return toggleOldMixin .isOn ();
278273 }
@@ -301,8 +296,9 @@ public void setOld(boolean old) {
301296 * @param index
302297 * the index at which to insert it
303298 */
304- public void insertItem (String item , Direction dir , String value , int index ) {
305- listBox .insertItem (item , dir , value , index );
299+ public void insertItem (String item , Direction dir , T value , int index ) {
300+ values .add (value );
301+ listBox .insertItem (item , dir , keyFactory .generateKey (value ), index );
306302 if (initialized ) {
307303 // reinitialize
308304 initializeMaterial (listBox .getElement ());
@@ -351,8 +347,9 @@ public void setTitle(String title) {
351347 * @param dir
352348 * the item's direction
353349 */
354- public void addItem (String item , Direction dir ) {
355- listBox .addItem (item , dir );
350+ public void addItem (T item , Direction dir ) {
351+ values .add (item );
352+ listBox .addItem (keyFactory .generateKey (item ), dir );
356353 if (initialized ) {
357354 // reinitialize
358355 initializeMaterial (listBox .getElement ());
@@ -369,8 +366,9 @@ public void addItem(String item, Direction dir) {
369366 * @param item
370367 * the text of the item to be added
371368 */
372- public void addItem (String item ) {
373- listBox .addItem (item );
369+ public void addItem (T item ) {
370+ values .add (item );
371+ listBox .addItem (keyFactory .generateKey (item ));
374372 if (initialized ) {
375373 // reinitialize
376374 initializeMaterial (listBox .getElement ());
@@ -386,8 +384,9 @@ public void addItem(String item) {
386384 * the item's value, to be submitted if it is part of a
387385 * {@link FormPanel}; cannot be <code>null</code>
388386 */
389- public void addItem (String item , String value ) {
390- listBox .addItem (item , value );
387+ public void addItem (T item , String value ) {
388+ values .add (item );
389+ listBox .addItem (keyFactory .generateKey (item ), value );
391390 if (initialized ) {
392391 // reinitialize
393392 initializeMaterial (listBox .getElement ());
@@ -406,8 +405,9 @@ public void addItem(String item, String value) {
406405 * the item's value, to be submitted if it is part of a
407406 * {@link FormPanel}; cannot be <code>null</code>
408407 */
409- public void addItem (String item , Direction dir , String value ) {
410- listBox .addItem (item , dir , value );
408+ public void addItem (T item , Direction dir , String value ) {
409+ values .add (item );
410+ listBox .addItem (keyFactory .generateKey (item ), dir , value );
411411 if (initialized ) {
412412 // reinitialize
413413 initializeMaterial (listBox .getElement ());
@@ -426,8 +426,9 @@ public void addItem(String item, Direction dir, String value) {
426426 * @param index
427427 * the index at which to insert it
428428 */
429- public void insertItem (String item , int index ) {
430- listBox .insertItem (item , index );
429+ public void insertItem (T item , int index ) {
430+ values .add (item );
431+ listBox .insertItem (keyFactory .generateKey (item ), index );
431432 if (initialized ) {
432433 // reinitialize
433434 initializeMaterial (listBox .getElement ());
@@ -449,8 +450,9 @@ public void insertItem(String item, int index) {
449450 * @param index
450451 * the index at which to insert it
451452 */
452- public void insertItem (String item , Direction dir , int index ) {
453- listBox .insertItem (item , dir , index );
453+ public void insertItem (T item , Direction dir , int index ) {
454+ values .add (item );
455+ listBox .insertItem (keyFactory .generateKey (item ), dir , index );
454456 if (initialized ) {
455457 // reinitialize
456458 initializeMaterial (listBox .getElement ());
@@ -473,8 +475,9 @@ public void insertItem(String item, Direction dir, int index) {
473475 * @param index
474476 * the index at which to insert it
475477 */
476- public void insertItem (String item , String value , int index ) {
477- listBox .insertItem (item , value , index );
478+ public void insertItem (T item , String value , int index ) {
479+ values .add (item );
480+ listBox .insertItem (keyFactory .generateKey (item ), value , index );
478481 if (initialized ) {
479482 // reinitialize
480483 initializeMaterial (listBox .getElement ());
@@ -635,8 +638,8 @@ public int getSelectedIndex() {
635638 * @throws IndexOutOfBoundsException
636639 * if the index is out of range
637640 */
638- public String getValue (int index ) {
639- return listBox . getValue (index );
641+ public T getValue (int index ) {
642+ return values . get (index );
640643 }
641644
642645 /**
@@ -645,8 +648,12 @@ public String getValue(int index) {
645648 *
646649 * @return the value for selected item, or {@code null} if none is selected
647650 */
648- public String getSelectedValue () {
649- return listBox .getSelectedValue ();
651+ public T getSelectedValue () {
652+ try {
653+ return values .get (getSelectedIndex ());
654+ } catch (IndexOutOfBoundsException ex ) {
655+ return null ;
656+ }
650657 }
651658
652659 /**
@@ -681,6 +688,7 @@ public boolean isItemSelected(int index) {
681688 * if the index is out of range
682689 */
683690 public void removeItem (int index ) {
691+ values .remove (index );
684692 listBox .removeItem (index );
685693 if (initialized ) {
686694 initializeMaterial (listBox .getElement ());
@@ -732,7 +740,7 @@ public void setSelectedValue(String value) {
732740 public int getIndex (String value ) {
733741 int count = getItemCount ();
734742 for (int i = 0 ; i < count ; i ++) {
735- String v = getValue (i );
743+ T v = getValue (i );
736744 if (v .equals (value )) {
737745 return i ;
738746 }
@@ -772,7 +780,7 @@ public void removeValue(String value) {
772780 @ Override
773781 public void setEnabled (boolean enabled ) {
774782 listBox .setEnabled (enabled );
775- if (initialized ) {
783+ if (initialized ) {
776784 // reinitialize
777785 initializeMaterial (listBox .getElement ());
778786 }
@@ -859,4 +867,11 @@ public void onBlur(BlurEvent event) {
859867 }
860868 }, BlurEvent .getType ());
861869 }
870+
871+ /**
872+ * Use your own key factory for value keys.
873+ */
874+ public void setKeyFactory (KeyFactory <T , String > keyFactory ) {
875+ this .keyFactory = keyFactory ;
876+ }
862877}
0 commit comments