diff options
author | Leif Åstrand <legioth@gmail.com> | 2017-01-23 14:59:11 +0200 |
---|---|---|
committer | Ilia Motornyi <elmot@vaadin.com> | 2017-01-23 14:59:11 +0200 |
commit | 838c1d949b6a902fc1bdcabe107158d401d27425 (patch) | |
tree | 07c96dbf9556874acee5db98008f525abcab547a | |
parent | 89abdbf02df913c36550f97385a5cc12b929866a (diff) | |
download | vaadin-framework-838c1d949b6a902fc1bdcabe107158d401d27425.tar.gz vaadin-framework-838c1d949b6a902fc1bdcabe107158d401d27425.zip |
Add shorthands to ComboBox for setting a ListDataProvider
-rw-r--r-- | server/src/main/java/com/vaadin/ui/ComboBox.java | 84 | ||||
-rw-r--r-- | server/src/test/java/com/vaadin/tests/server/component/combobox/ComboBoxFilteringTest.java | 44 |
2 files changed, 115 insertions, 13 deletions
diff --git a/server/src/main/java/com/vaadin/ui/ComboBox.java b/server/src/main/java/com/vaadin/ui/ComboBox.java index 20cc14b2da..4cfcca813d 100644 --- a/server/src/main/java/com/vaadin/ui/ComboBox.java +++ b/server/src/main/java/com/vaadin/ui/ComboBox.java @@ -22,6 +22,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Objects; import java.util.Set; +import java.util.stream.Stream; import org.jsoup.nodes.Element; @@ -30,6 +31,7 @@ import com.vaadin.data.HasValue; import com.vaadin.data.provider.DataCommunicator; import com.vaadin.data.provider.DataKeyMapper; import com.vaadin.data.provider.DataProvider; +import com.vaadin.data.provider.ListDataProvider; import com.vaadin.event.FieldEvents; import com.vaadin.event.FieldEvents.BlurEvent; import com.vaadin.event.FieldEvents.BlurListener; @@ -209,15 +211,64 @@ public class ComboBox<T> extends AbstractSingleSelect<T> }); } + /** + * {@inheritDoc} + * <p> + * Filtering will use a case insensitive match to show all items where the + * filter text is a substring of the caption displayed for that item. + */ @Override public void setItems(Collection<T> items) { + ListDataProvider<T> listDataProvider = DataProvider.create(items); + + setDataProvider(listDataProvider); + } + + /** + * {@inheritDoc} + * <p> + * Filtering will use a case insensitive match to show all items where the + * filter text is a substring of the caption displayed for that item. + */ + @Override + public void setItems(Stream<T> streamOfItems) { + // Overridden only to add clarification to javadocs + super.setItems(streamOfItems); + } + + /** + * {@inheritDoc} + * <p> + * Filtering will use a case insensitive match to show all items where the + * filter text is a substring of the caption displayed for that item. + */ + @Override + public void setItems(T... items) { + // Overridden only to add clarification to javadocs + super.setItems(items); + } + + /** + * Sets a list data provider as the data provider of this combo box. + * Filtering will use a case insensitive match to show all items where the + * filter text is a substring of the caption displayed for that item. + * <p> + * Note that this is a shorthand that calls + * {@link #setDataProvider(DataProvider)} with a wrapper of the provided + * list data provider. This means that {@link #getDataProvider()} will + * return the wrapper instead of the original list data provider. + * + * @param listDataProvider + * the list data provider to use, not <code>null</code> + */ + public void setDataProvider(ListDataProvider<T> listDataProvider) { // Cannot use the case insensitive contains shorthand from // ListDataProvider since it wouldn't react to locale changes CaptionFilter defaultCaptionFilter = (itemText, filterText) -> itemText .toLowerCase(getLocale()) .contains(filterText.toLowerCase(getLocale())); - setItems(defaultCaptionFilter, items); + setDataProvider(defaultCaptionFilter, listDataProvider); } /** @@ -234,11 +285,36 @@ public class ComboBox<T> extends AbstractSingleSelect<T> * the data items to display */ public void setItems(CaptionFilter captionFilter, Collection<T> items) { + ListDataProvider<T> listDataProvider = DataProvider.create(items); + + setDataProvider(captionFilter, listDataProvider); + } + + /** + * Sets a list data provider with an item caption filter as the data + * provider of this combo box. The caption filter is used to compare the + * displayed caption of each item to the filter text entered by the user. + * <p> + * Note that this is a shorthand that calls + * {@link #setDataProvider(DataProvider)} with a wrapper of the provided + * list data provider. This means that {@link #getDataProvider()} will + * return the wrapper instead of the original list data provider. + * + * @param captionFilter + * filter to check if an item is shown when user typed some text + * into the ComboBox + * @param listDataProvider + * the list data provider to use, not <code>null</code> + */ + public void setDataProvider(CaptionFilter captionFilter, + ListDataProvider<T> listDataProvider) { + Objects.requireNonNull(listDataProvider, + "List data provider cannot be null"); + // Must do getItemCaptionGenerator() for each operation since it might // not be the same as when this method was invoked - DataProvider<T, String> provider = DataProvider.create(items) - .filteringBy(item -> getItemCaptionGenerator().apply(item), - captionFilter); + DataProvider<T, String> provider = listDataProvider.filteringBy( + item -> getItemCaptionGenerator().apply(item), captionFilter); setDataProvider(provider); } diff --git a/server/src/test/java/com/vaadin/tests/server/component/combobox/ComboBoxFilteringTest.java b/server/src/test/java/com/vaadin/tests/server/component/combobox/ComboBoxFilteringTest.java index b1f350f447..d4681151d5 100644 --- a/server/src/test/java/com/vaadin/tests/server/component/combobox/ComboBoxFilteringTest.java +++ b/server/src/test/java/com/vaadin/tests/server/component/combobox/ComboBoxFilteringTest.java @@ -27,6 +27,7 @@ import org.junit.Test; import com.vaadin.data.provider.DataProvider; import com.vaadin.data.provider.ListDataProvider; import com.vaadin.data.provider.Query; +import com.vaadin.tests.data.bean.Address; import com.vaadin.tests.data.bean.Person; import com.vaadin.tests.data.bean.Sex; import com.vaadin.ui.ComboBox; @@ -98,29 +99,54 @@ public class ComboBoxFilteringTest { public void setItems_array_customFiltering() { comboBox.setItemCaptionGenerator(Person::getFirstName); - // Result: typing "en" into the search field finds "Enrique Iglesias" - // and "Henry Dunant", but not "Erwin Engelbrecht" + // Result: typing "En" into the search field finds "Enrique Iglesias" + // but not "Henry Dunant" or "Erwin Engelbrecht" comboBox.setItems(String::startsWith, getPersonArray()); - checkFiltering("Er", "er", 3, 1); + checkFiltering("En", "en", 3, 1); } @Test public void setItems_collection_customFiltering() { comboBox.setItemCaptionGenerator(Person::getFirstName); + // Result: typing "En" into the search field finds "Enrique Iglesias" + // but not "Henry Dunant" or "Erwin Engelbrecht" + comboBox.setItems(String::startsWith, getPersonCollection()); + + checkFiltering("En", "en", 3, 1); + } + + @Test + public void setListDataProvider_defaultFiltering() { + comboBox.setItemCaptionGenerator(Person::getFirstName); + // Result: typing "en" into the search field finds "Enrique Iglesias" // and "Henry Dunant", but not "Erwin Engelbrecht" - comboBox.setItems(String::startsWith, getPersonCollection()); + comboBox.setDataProvider(DataProvider.create(getPersonCollection())); - checkFiltering("Er", "er", 3, 1); + checkFiltering("en", "ennen", 3, 2); + } + + @Test + public void setListDataProvider_customFiltering() { + comboBox.setItemCaptionGenerator(Person::getFirstName); + + // Result: typing "En" into the search field finds "Enrique Iglesias" + // but not "Henry Dunant" or "Erwin Engelbrecht" + comboBox.setDataProvider(String::startsWith, + DataProvider.create(getPersonCollection())); + + checkFiltering("En", "en", 3, 1); } public void invalid_dataProvider_compile_error() { - // uncommenting this causes a compile time error - ListDataProvider<Person> ldp = DataProvider.create(getPersonArray()); - // Compile error, because of invalid data provider filter type - // comboBox.setDataProvider(ldp); + DataProvider<Person, Address> dp = DataProvider.create(getPersonArray()) + .filteringByEquals(Person::getAddress); + + // uncommenting this causes a compile time error because of invalid data + // provider filter type + // comboBox.setDataProvider(dp); } @Test |