From 2a0cbd3bdbebaa25b44a096ae3ac6462a408d92d Mon Sep 17 00:00:00 2001 From: Aleksi Hietanen Date: Thu, 15 Sep 2016 14:07:18 +0300 Subject: [PATCH] Move ItemIconProvider out of ComboBox, rename to IconGenerator Change-Id: I884a52c75b3be5573cf6634f211d72d09de69d80 --- .../src/main/java/com/vaadin/ui/ComboBox.java | 48 ++++++++----------- .../java/com/vaadin/ui/IconGenerator.java | 41 ++++++++++++++++ .../combobox/ComboBoxClickIcon.java | 2 +- .../components/combobox/ComboBoxItemIcon.java | 4 +- .../combobox/ComboBoxLargeIcons.java | 2 +- .../ComboBoxUndefinedWidthAndIcon.java | 2 +- .../tests/components/combobox/Comboboxes.java | 4 +- .../tests/components/combobox/PopUpWidth.java | 2 +- 8 files changed, 69 insertions(+), 36 deletions(-) create mode 100644 server/src/main/java/com/vaadin/ui/IconGenerator.java diff --git a/server/src/main/java/com/vaadin/ui/ComboBox.java b/server/src/main/java/com/vaadin/ui/ComboBox.java index d39d8a7c4e..5ed454d370 100644 --- a/server/src/main/java/com/vaadin/ui/ComboBox.java +++ b/server/src/main/java/com/vaadin/ui/ComboBox.java @@ -82,19 +82,6 @@ public class ComboBox extends AbstractSingleSelect implements HasValue, public interface NewItemHandler extends Consumer, Serializable { } - /** - * ItemIconProvider can be used to add custom icons to combo box items shown - * in the popup. - * - * @see ComboBox#setItemIconProvider(ItemIconProvider) - * @param - * item type in the combo box - */ - @FunctionalInterface - public interface ItemIconProvider - extends Function, Serializable { - } - /** * Filter can be used to customize the filtering of items based on user * input. @@ -150,7 +137,7 @@ public class ComboBox extends AbstractSingleSelect implements HasValue, private ItemCaptionGenerator itemCaptionGenerator = String::valueOf; private StyleGenerator itemStyleGenerator = item -> null; - private ItemIconProvider itemIconProvider = item -> null; + private IconGenerator itemIconGenerator = item -> null; private ItemFilter filter = (filterText, item) -> { if (filterText == null) { @@ -241,7 +228,7 @@ public class ComboBox extends AbstractSingleSelect implements HasValue, if (style != null) { jsonObject.put(ComboBoxConstants.STYLE, style); } - Resource icon = itemIconProvider.apply(data); + Resource icon = itemIconGenerator.apply(data); if (icon != null) { String iconUrl = ResourceReference .create(icon, ComboBox.this, null).getURL(); @@ -477,30 +464,35 @@ public class ComboBox extends AbstractSingleSelect implements HasValue, } /** - * Sets the item icon provider that is used to produce custom icons for - * showing items in the popup. The provider can return null for items with + * Sets the item icon generator that is used to produce custom icons for + * showing items in the popup. The generator can return null for items with * no icon. * - * @param itemIconProvider - * the item icon provider to set, not null + * @see IconGenerator + * + * @param itemIconGenerator + * the item icon generator to set, not null + * @throws NullPointerException + * if {@code itemIconGenerator} is {@code null} */ - public void setItemIconProvider(ItemIconProvider itemIconProvider) { - Objects.requireNonNull(itemIconProvider, - "Item icon providers must not be null"); - this.itemIconProvider = itemIconProvider; + public void setItemIconGenerator(IconGenerator itemIconGenerator) { + Objects.requireNonNull(itemIconGenerator, + "Item icon generator must not be null"); + this.itemIconGenerator = itemIconGenerator; getDataCommunicator().reset(); } /** - * Gets the currently used item icon provider. The default item icon + * Gets the currently used item icon generator. The default item icon * provider returns null for all items, resulting in no icons being used. * - * @see #setItemIconProvider(ItemIconProvider) + * @see IconGenerator + * @see #setItemIconGenerator(IconGenerator) * - * @return the currently used item icon provider, not null + * @return the currently used item icon generator, not null */ - public ItemIconProvider getItemIconProvider() { - return itemIconProvider; + public IconGenerator getItemIconGenerator() { + return itemIconGenerator; } /** diff --git a/server/src/main/java/com/vaadin/ui/IconGenerator.java b/server/src/main/java/com/vaadin/ui/IconGenerator.java new file mode 100644 index 0000000000..41f11dad81 --- /dev/null +++ b/server/src/main/java/com/vaadin/ui/IconGenerator.java @@ -0,0 +1,41 @@ +/* + * 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; + +import com.vaadin.server.Resource; + +/** + * A callback interface for generating icons for an item. + * + * @param + * item type for which the icon is generated + */ +@FunctionalInterface +public interface IconGenerator extends Function, Serializable { + + /** + * Gets an icon resource for the {@code item}. + * + * @param item + * the item for which to generate an icon for + * @return the generated icon resource + */ + @Override + public Resource apply(T item); +} \ No newline at end of file diff --git a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxClickIcon.java b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxClickIcon.java index 886dfc7ae2..d4a0d4f6d4 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxClickIcon.java +++ b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxClickIcon.java @@ -32,7 +32,7 @@ public class ComboBoxClickIcon extends AbstractTestUI { protected void setup(VaadinRequest request) { final ComboBox combo = new ComboBox<>(null, DataSource.create("A", "B", "C")); - combo.setItemIconProvider(item -> FontAwesome.ALIGN_CENTER); + combo.setItemIconGenerator(item -> FontAwesome.ALIGN_CENTER); combo.setTextInputAllowed(false); addComponent(combo); } diff --git a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxItemIcon.java b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxItemIcon.java index 0a29669e1b..a479922b4d 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxItemIcon.java +++ b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxItemIcon.java @@ -21,7 +21,7 @@ public class ComboBoxItemIcon extends TestBase { { ComboBox cb = new ComboBox<>(); cb.setItems("FI", "SE"); - cb.setItemIconProvider(item -> new ThemeResource( + cb.setItemIconGenerator(item -> new ThemeResource( "../tests-tickets/icons/" + item.toLowerCase() + ".gif")); addComponent(cb); @@ -29,7 +29,7 @@ public class ComboBoxItemIcon extends TestBase { { ComboBox cb = new ComboBox<>(); cb.setItems("Finland", "Australia", "Hungary"); - cb.setItemIconProvider( + cb.setItemIconGenerator( item -> new ThemeResource("../tests-tickets/icons/" + item.substring(0, 2).toLowerCase() + ".gif")); diff --git a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxLargeIcons.java b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxLargeIcons.java index a2fb93def9..c262b5f01a 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxLargeIcons.java +++ b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxLargeIcons.java @@ -29,7 +29,7 @@ public class ComboBoxLargeIcons extends TestBase { "document-txt", "document-web", "document")); getLayout().addComponent(cb); // FIXME cb.setNullSelectionAllowed(false); - cb.setItemIconProvider(icon -> new ThemeResource( + cb.setItemIconGenerator(icon -> new ThemeResource( "../runo/icons/32/" + icon + ".png?" + new Date().getTime())); } } diff --git a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxUndefinedWidthAndIcon.java b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxUndefinedWidthAndIcon.java index c5d72528af..5b2e580fc8 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxUndefinedWidthAndIcon.java +++ b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxUndefinedWidthAndIcon.java @@ -15,7 +15,7 @@ public class ComboBoxUndefinedWidthAndIcon extends TestBase { data.add("Item " + i); } ComboBox cb = new ComboBox<>(null, data); - cb.setItemIconProvider( + cb.setItemIconGenerator( item -> new ThemeResource("../runo/icons/16/users.png")); addComponent(cb); diff --git a/uitest/src/main/java/com/vaadin/tests/components/combobox/Comboboxes.java b/uitest/src/main/java/com/vaadin/tests/components/combobox/Comboboxes.java index 711c20a398..a3d6546853 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/combobox/Comboboxes.java +++ b/uitest/src/main/java/com/vaadin/tests/components/combobox/Comboboxes.java @@ -137,9 +137,9 @@ public class Comboboxes extends ComponentTestCase { @Override public void execute(ComboBox c, String value, Object data) { if (value == null) { - c.setItemIconProvider(item -> null); + c.setItemIconGenerator(item -> null); } else { - c.setItemIconProvider(item -> new ThemeResource( + c.setItemIconGenerator(item -> new ThemeResource( value + "?" + new Date().getTime())); } } diff --git a/uitest/src/main/java/com/vaadin/tests/components/combobox/PopUpWidth.java b/uitest/src/main/java/com/vaadin/tests/components/combobox/PopUpWidth.java index c0607d49ed..dbddb27936 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/combobox/PopUpWidth.java +++ b/uitest/src/main/java/com/vaadin/tests/components/combobox/PopUpWidth.java @@ -24,7 +24,7 @@ public class PopUpWidth extends TestBase { items.add(i); } cb.setItems(items); - cb.setItemIconProvider( + cb.setItemIconGenerator( item -> new ThemeResource("../runo/icons/16/users.png")); cb.setItemCaptionGenerator(item -> "Item " + item); return cb; -- 2.39.5