Skip to content

Commit ce4f225

Browse files
authored
Merge pull request #440 from GwtMaterialDesign/release_1.6.1
Emergency patch for the MaterialListValueBox.
2 parents fcf5ebe + a302072 commit ce4f225

File tree

5 files changed

+88
-41
lines changed

5 files changed

+88
-41
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ We created <a href="http://gwtmaterialdesign.github.io/gwt-material-demo/apidocs
1616

1717

1818
## Maven
19-
### Current Version 1.6.0
19+
### Current Version 1.6.1
2020
```xml
2121
<dependency>
2222
<groupId>com.github.gwtmaterialdesign</groupId>
2323
<artifactId>gwt-material</artifactId>
24-
<version>1.6.0</version>
24+
<version>1.6.1</version>
2525
</dependency>
2626
```
2727
### Snapshot Version 2.0-SNAPSHOT

gwt-material/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<artifactId>gwt-material-parent</artifactId>
55
<groupId>com.github.gwtmaterialdesign</groupId>
6-
<version>1.6.0</version>
6+
<version>1.6.1</version>
77
</parent>
88
<modelVersion>4.0.0</modelVersion>
99

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package gwt.material.design.client.base;
2+
3+
/*
4+
* #%L
5+
* GwtMaterial
6+
* %%
7+
* Copyright (C) 2015 - 2016 GwtMaterialDesign
8+
* %%
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* #L%
21+
*/
22+
23+
24+
public interface KeyFactory<T, R> {
25+
26+
/**
27+
* Generate a unique key based on an object and given return type.
28+
* @param object the object the key is representing.
29+
* @return a unique key Object signifying the given object.
30+
*/
31+
R generateKey(T object);
32+
}

gwt-material/src/main/java/gwt/material/design/client/ui/MaterialListValueBox.java

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
import gwt.material.design.client.base.validator.ValidationChangedEvent.ValidationChangedHandler;
5050
import gwt.material.design.client.base.validator.Validator;
5151
import gwt.material.design.client.ui.html.Label;
52-
import gwt.material.design.client.ui.html.Option;
5352

5453
import 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
}

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<groupId>com.github.gwtmaterialdesign</groupId>
66
<artifactId>gwt-material-parent</artifactId>
77
<packaging>pom</packaging>
8-
<version>1.6.0</version>
8+
<version>1.6.1</version>
99

1010
<modules>
1111
<module>gwt-material</module>
@@ -62,7 +62,7 @@
6262
<connection>scm:git:[email protected]:GwtMaterialDesign/gwt-material.git</connection>
6363
<developerConnection>scm:git:[email protected]:GwtMaterialDesign/gwt-material.git</developerConnection>
6464
<url>http://github.com/GwtMaterialDesign/gwt-material</url>
65-
<tag>v1.6.0</tag>
65+
<tag>v1.6.1</tag>
6666
</scm>
6767

6868
<licenses>

0 commit comments

Comments
 (0)