]> source.dussan.org Git - vaadin-framework.git/commitdiff
Move ItemCaptionProvider out of ComboBox (#184).
authorDenis Anisimov <denis@vaadin.com>
Thu, 15 Sep 2016 06:02:56 +0000 (09:02 +0300)
committerDenis Anisimov <denis@vaadin.com>
Tue, 20 Sep 2016 10:17:13 +0000 (13:17 +0300)
Change-Id: I7dc98de04127c7495aed81b9e0cd2be8cb12b10c

server/src/main/java/com/vaadin/ui/ComboBox.java
server/src/main/java/com/vaadin/ui/ItemCaptionGenerator.java [new file with mode: 0644]
uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxDuplicateCaption.java
uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxIdenticalItems.java
uitest/src/main/java/com/vaadin/tests/components/combobox/Comboboxes.java
uitest/src/main/java/com/vaadin/tests/components/combobox/PopUpWidth.java
uitest/src/main/java/com/vaadin/tests/components/notification/NotificationsWaiAria.java
uitest/src/main/java/com/vaadin/tests/components/select/EnumSelect.java
uitest/src/main/java/com/vaadin/tests/components/window/WindowMaximizeRestoreTest.java

index 0b1b4923f85a6725ce20b031c9769183a17e5ecc..d39d8a7c4e01600b6a952846dc4eba7abfcda54a 100644 (file)
@@ -67,7 +67,7 @@ public class ComboBox<T> extends AbstractSingleSelect<T> implements HasValue<T>,
             String selectedCaption = null;
             T value = getDataCommunicator().getKeyMapper().get(key);
             if (value != null) {
-                selectedCaption = getItemCaptionProvider().apply(value);
+                selectedCaption = getItemCaptionGenerator().apply(value);
             }
             getState().selectedItemCaption = selectedCaption;
         }
@@ -82,19 +82,6 @@ public class ComboBox<T> extends AbstractSingleSelect<T> implements HasValue<T>,
     public interface NewItemHandler extends Consumer<String>, Serializable {
     }
 
-    /**
-     * ItemCaptionProvider can be used to customize the string shown to the user
-     * for an item.
-     *
-     * @see ComboBox#setItemCaptionProvider(ItemCaptionProvider)
-     * @param <T>
-     *            item type in the combo box
-     */
-    @FunctionalInterface
-    public interface ItemCaptionProvider<T>
-            extends Function<T, String>, Serializable {
-    }
-
     /**
      * ItemIconProvider can be used to add custom icons to combo box items shown
      * in the popup.
@@ -160,7 +147,7 @@ public class ComboBox<T> extends AbstractSingleSelect<T> implements HasValue<T>,
      */
     private NewItemHandler newItemHandler;
 
-    private ItemCaptionProvider<T> itemCaptionProvider = String::valueOf;
+    private ItemCaptionGenerator<T> itemCaptionGenerator = String::valueOf;
 
     private StyleGenerator<T> itemStyleGenerator = item -> null;
     private ItemIconProvider<T> itemIconProvider = item -> null;
@@ -169,7 +156,8 @@ public class ComboBox<T> extends AbstractSingleSelect<T> implements HasValue<T>,
         if (filterText == null) {
             return true;
         } else {
-            return getItemCaptionProvider().apply(item).toLowerCase(getLocale())
+            return getItemCaptionGenerator().apply(item)
+                    .toLowerCase(getLocale())
                     .contains(filterText.toLowerCase(getLocale()));
         }
     };
@@ -248,7 +236,7 @@ public class ComboBox<T> extends AbstractSingleSelect<T> implements HasValue<T>,
 
         addDataGenerator((T data, JsonObject jsonObject) -> {
             jsonObject.put(DataCommunicatorConstants.NAME,
-                    getItemCaptionProvider().apply(data));
+                    getItemCaptionGenerator().apply(data));
             String style = itemStyleGenerator.apply(data);
             if (style != null) {
                 jsonObject.put(ComboBoxConstants.STYLE, style);
@@ -429,28 +417,28 @@ public class ComboBox<T> extends AbstractSingleSelect<T> implements HasValue<T>,
     }
 
     /**
-     * Gets the item caption provider that is used to produce the strings shown
+     * Gets the item caption generator that is used to produce the strings shown
      * in the combo box for each item.
      *
-     * @return the item caption provider used, not null
+     * @return the item caption generator used, not null
      */
-    public ItemCaptionProvider<T> getItemCaptionProvider() {
-        return itemCaptionProvider;
+    public ItemCaptionGenerator<T> getItemCaptionGenerator() {
+        return itemCaptionGenerator;
     }
 
     /**
-     * Sets the item caption provider that is used to produce the strings shown
+     * Sets the item caption generator that is used to produce the strings shown
      * in the combo box for each item. By default,
      * {@link String#valueOf(Object)} is used.
      *
-     * @param itemCaptionProvider
+     * @param itemCaptionGenerator
      *            the item caption provider to use, not null
      */
-    public void setItemCaptionProvider(
-            ItemCaptionProvider<T> itemCaptionProvider) {
-        Objects.requireNonNull(itemCaptionProvider,
-                "Item caption providers must not be null");
-        this.itemCaptionProvider = itemCaptionProvider;
+    public void setItemCaptionGenerator(
+            ItemCaptionGenerator<T> itemCaptionGenerator) {
+        Objects.requireNonNull(itemCaptionGenerator,
+                "Item caption generators must not be null");
+        this.itemCaptionGenerator = itemCaptionGenerator;
         getDataCommunicator().reset();
     }
 
@@ -580,7 +568,7 @@ public class ComboBox<T> extends AbstractSingleSelect<T> implements HasValue<T>,
             HasValue.ValueChangeListener<? super T> listener) {
         return addSelectionListener(event -> {
             ((ValueChangeListener<T>) listener)
-                    .accept(new ValueChange<T>(event.getConnector(),
+                    .accept(new ValueChange<>(event.getConnector(),
                             event.getValue(), event.isUserOriginated()));
         });
     }
diff --git a/server/src/main/java/com/vaadin/ui/ItemCaptionGenerator.java b/server/src/main/java/com/vaadin/ui/ItemCaptionGenerator.java
new file mode 100644 (file)
index 0000000..dc699c0
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2000-2016 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.ui;
+
+import java.io.Serializable;
+import java.util.function.Function;
+
+/**
+ * {@link ItemCaptionGenerator} can be used to customize the string shown to the
+ * user for an item.
+ *
+ * @see ComboBox#setItemCaptionGenerator(ItemCaptionProvider)
+ * @param <T>
+ *            item type
+ * @since 8.0
+ * @author Vaadin Ltd
+ */
+@FunctionalInterface
+public interface ItemCaptionGenerator<T>
+        extends Function<T, String>, Serializable {
+
+    /**
+     * Gets a caption for the {@code item}.
+     *
+     * @param item
+     *            the item to get caption for
+     * @return the caption of the item
+     */
+    @Override
+    String apply(T item);
+}
index df093a297605ccd848ac929662af87ed6476d0c6..1f4d67c1d5a986e0843a7c428b7044a1772313cc 100644 (file)
@@ -33,7 +33,7 @@ public class ComboBoxDuplicateCaption extends TestBase {
             log.log("Person = " + p.getFirstName() + " " + p.getLastName());
         });
         box.setItems(list);
-        box.setItemCaptionProvider(Person::getLastName);
+        box.setItemCaptionGenerator(Person::getLastName);
 
         addComponent(log);
 
index 3de97dc46cfcedc7fdec9815bfbcc5f91621cba4..1f0ebf2f1c09e2cd7f0c229327c989cc9e82a0e6 100644 (file)
@@ -12,7 +12,7 @@ public class ComboBoxIdenticalItems extends AbstractTestUI {
     @Override
     protected void setup(VaadinRequest request) {
         final ComboBox<String> select = new ComboBox<>("ComboBox");
-        select.setItemCaptionProvider(
+        select.setItemCaptionGenerator(
                 item -> item.startsWith("one") ? "One" : "Two");
         select.setItems("one-1", "one-2", "two");
         select.setEmptySelectionAllowed(false);
index 4cc8ca7919708c9302171e0e5fda05670f8132b3..711c20a398a05c4968d8790b823081ebed3d4524 100644 (file)
@@ -50,7 +50,7 @@ public class Comboboxes extends ComponentTestCase<ComboBox> {
                 "Undefined wide select with 50 items");
         s4.setWidth(null);
         populate(s4, 50);
-        s4.setItemCaptionProvider(StringBean::getValue);
+        s4.setItemCaptionGenerator(StringBean::getValue);
         s4.setScrollToSelectedItem(true);
         addTestComponent(s4);
 
@@ -72,14 +72,14 @@ public class Comboboxes extends ComponentTestCase<ComboBox> {
                 "200px wide select with 50 items");
         s8.setWidth("200px");
         populate(s8, 50);
-        s8.setItemCaptionProvider(StringBean::getValue);
+        s8.setItemCaptionGenerator(StringBean::getValue);
         addTestComponent(s8);
 
         ComboBox<StringBean> s9 = new PageLength0ComboBox();
         s9.setImmediate(true);
         s9.setCaption("Pagelength 0");
         populate(s9, 15);
-        s9.setItemCaptionProvider(StringBean::getValue);
+        s9.setItemCaptionGenerator(StringBean::getValue);
         addTestComponent(s9);
     }
 
index 6cbfc42ab86d2ffb095294d27f7742cb00c7851a..c0607d49ed6f6bd50389e8f18150fbe6b07ea754 100644 (file)
@@ -26,7 +26,7 @@ public class PopUpWidth extends TestBase {
         cb.setItems(items);
         cb.setItemIconProvider(
                 item -> new ThemeResource("../runo/icons/16/users.png"));
-        cb.setItemCaptionProvider(item -> "Item " + item);
+        cb.setItemCaptionGenerator(item -> "Item " + item);
         return cb;
     }
 
index c2df9a2cbe295d7d87722dbfdf6f78e10abc6f0e..e35304aa740e3af4219baccf87401fd7809f45db 100644 (file)
@@ -69,7 +69,7 @@ public class NotificationsWaiAria extends AbstractTestUI {
         items.put(Notification.Type.TRAY_NOTIFICATION, "Tray");
         items.put(Notification.Type.ASSISTIVE_NOTIFICATION, "Assistive");
 
-        type.setItemCaptionProvider(item -> items.get(item));
+        type.setItemCaptionGenerator(item -> items.get(item));
         type.setItems(items.keySet());
 
         type.setValue(items.keySet().iterator().next());
index ad98a4b38a8665fcfda5b0e297909052d6c66046..399913778ec47da863509b5aa6d6a096ff428c28 100644 (file)
@@ -37,7 +37,7 @@ public class EnumSelect extends AbstractTestUIWithLog {
         setLocale(new Locale("fi", "FI"));
         ComboBox<Constant> cb = new ComboBox<>(null,
                 Arrays.asList(Constant.values()));
-        cb.setItemCaptionProvider(value -> StringToEnumConverter
+        cb.setItemCaptionGenerator(value -> StringToEnumConverter
                 .enumToString(value, getLocale()));
         addComponent(cb);
 
index 0288eafad529010470b881ec50bf1c9938984e4c..ec735cfa3c4991f1815754cb4861922651ea2cd7 100644 (file)
@@ -29,7 +29,7 @@ public class WindowMaximizeRestoreTest extends AbstractTestUI {
 
         addWindowAgain = new ComboBox<>("Add Window Again");
         addWindowAgain
-                .setItemCaptionProvider(window -> window.getData().toString());
+                .setItemCaptionGenerator(window -> window.getData().toString());
         addWindowAgain.addValueChangeListener(event -> {
             Object value = event.getValue();
             if (value != null && value instanceof Window) {