From 30fd83615cc3ac8a3879e2380458742698448231 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Tue, 24 Jan 2023 17:02:03 +0200 Subject: [PATCH 1/4] feat: Allow changing the icon --- .../com/vaadin/flow/component/icon/Icon.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/vaadin-icons-flow-parent/vaadin-icons-flow/src/main/java/com/vaadin/flow/component/icon/Icon.java b/vaadin-icons-flow-parent/vaadin-icons-flow/src/main/java/com/vaadin/flow/component/icon/Icon.java index ad9de7620d0..5e7c64ecda4 100644 --- a/vaadin-icons-flow-parent/vaadin-icons-flow/src/main/java/com/vaadin/flow/component/icon/Icon.java +++ b/vaadin-icons-flow-parent/vaadin-icons-flow/src/main/java/com/vaadin/flow/component/icon/Icon.java @@ -97,6 +97,40 @@ public Icon(String icon) { * the icon name */ public Icon(String collection, String icon) { + setIcon(collection, icon); + } + + /** + * Sets the icon to the given icon from the vaadin-icons collection. + * + * @param icon + * the icon name + */ + public void setIcon(String icon) { + setIcon(ICON_COLLECTION_NAME, icon); + } + + /** + * Sets the icon to the given {@code icon} from the given + * {@code collection}. + * + * If you want to use a custom {@code } -based icon set, you + * also need to add a dependency and an import for it, example: + * + *
+     * 
+     * @NpmPackage(value = "custom-icons", version = "1.0.0")
+     * @JsModule("custom-icons/iconset.js")
+     * public class MyView extends Div {
+     * 
+     * 
+ * + * @param collection + * the icon collection + * @param icon + * the icon name + */ + public void setIcon(String collection, String icon) { getElement().setAttribute(ICON_ATTRIBUTE_NAME, collection + ':' + icon); } From 42aaf77c4790d55a3ac34c9b95df79acf4e1eb8c Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Wed, 8 May 2024 15:35:41 +0300 Subject: [PATCH 2/4] Fix review comments --- .../com/vaadin/flow/component/icon/Icon.java | 65 ++++++++++++++++--- .../flow/component/icon/tests/IconTest.java | 38 +++++++++++ 2 files changed, 95 insertions(+), 8 deletions(-) diff --git a/vaadin-icons-flow-parent/vaadin-icons-flow/src/main/java/com/vaadin/flow/component/icon/Icon.java b/vaadin-icons-flow-parent/vaadin-icons-flow/src/main/java/com/vaadin/flow/component/icon/Icon.java index 241c3f9f3b0..df1120203a9 100644 --- a/vaadin-icons-flow-parent/vaadin-icons-flow/src/main/java/com/vaadin/flow/component/icon/Icon.java +++ b/vaadin-icons-flow-parent/vaadin-icons-flow/src/main/java/com/vaadin/flow/component/icon/Icon.java @@ -31,14 +31,13 @@ public class Icon extends AbstractIcon { private static final String ICON_ATTRIBUTE_NAME = "icon"; - private static final String ICON_COLLECTION_NAME = "vaadin"; + private static final String VAADIN_ICON_COLLECTION_NAME = "vaadin"; private static final String STYLE_FILL = "fill"; /** - * Creates an Icon component that displays a Vaadin logo. + * Creates an empty Icon. */ public Icon() { - this(VaadinIcon.VAADIN_H); } /** @@ -49,8 +48,7 @@ public Icon() { * the icon to display */ public Icon(VaadinIcon icon) { - this(ICON_COLLECTION_NAME, - icon.name().toLowerCase(Locale.ENGLISH).replace('_', '-')); + setIcon(icon); } /** @@ -61,7 +59,7 @@ public Icon(VaadinIcon icon) { * the icon name */ public Icon(String icon) { - this(ICON_COLLECTION_NAME, icon); + setIcon(icon); } /** @@ -89,13 +87,50 @@ public Icon(String collection, String icon) { } /** - * Sets the icon to the given icon from the vaadin-icons collection. + * Sets the icon to the given icon. + *

+ * If the icon name contains a ":", the first part is used as the collection + * and the second part as the icon name. If the icon name does not contain a + * ":", the icon is assumed to be from the "vaadin" collection. * * @param icon * the icon name */ public void setIcon(String icon) { - setIcon(ICON_COLLECTION_NAME, icon); + if (icon.contains(":")) { + String[] parts = icon.split(":", 2); + setIcon(parts[0], parts[1]); + } else { + String collection = getCollection(); + if (collection == null) { + collection = VAADIN_ICON_COLLECTION_NAME; + } + setIcon(collection, icon); + } + } + + /** + * /** Sets the icon to the given Vaadin icon. + *

+ * If the icon name contains a ":", the first part is used as the collection + * and the second part as the icon name. If the icon name does not contain a + * ":", the icon is assumed to be from the "vaadin" collection. + * + * @param icon + * the icon name + */ + public void setIcon(VaadinIcon icon) { + setIcon(VAADIN_ICON_COLLECTION_NAME, + icon.name().toLowerCase(Locale.ENGLISH).replace('_', '-')); + } + + /** + * Gets the full icon name, including the collection. + * + * @return the icon name or {@code null} if no icon is set + */ + public String getIcon() { + return getElement().getAttribute(ICON_ATTRIBUTE_NAME); } /** @@ -122,6 +157,20 @@ public void setIcon(String collection, String icon) { getElement().setAttribute(ICON_ATTRIBUTE_NAME, collection + ':' + icon); } + /** + * Gets the collection of the icon (the part before {@literal :}). + * + * @return the collection of the icon or {@code null} if no collection is + * set + */ + public String getCollection() { + String icon = getIcon(); + if (icon != null && icon.contains(":")) { + return icon.substring(0, icon.indexOf(':')); + } + return null; + } + @Override public void setColor(String color) { if (color == null) { diff --git a/vaadin-icons-flow-parent/vaadin-icons-flow/src/test/java/com/vaadin/flow/component/icon/tests/IconTest.java b/vaadin-icons-flow-parent/vaadin-icons-flow/src/test/java/com/vaadin/flow/component/icon/tests/IconTest.java index a8676bf1c93..10ef3e11fbf 100644 --- a/vaadin-icons-flow-parent/vaadin-icons-flow/src/test/java/com/vaadin/flow/component/icon/tests/IconTest.java +++ b/vaadin-icons-flow-parent/vaadin-icons-flow/src/test/java/com/vaadin/flow/component/icon/tests/IconTest.java @@ -19,12 +19,50 @@ import org.junit.Test; import com.vaadin.flow.component.icon.Icon; +import com.vaadin.flow.component.icon.VaadinIcon; import com.vaadin.flow.component.shared.HasTooltip; public class IconTest { + @Test public void implementsHasTooltip() { Icon icon = new Icon(); Assert.assertTrue(icon instanceof HasTooltip); } + + @Test + public void emptyIconIsEmpty() { + Assert.assertNull(new Icon().getIcon()); + } + + @Test + public void usesVaadinCollectionByDefault() { + Assert.assertEquals("vaadin:foo", new Icon("foo").getIcon()); + } + + @Test + public void canDefineCollectionInConstructor() { + Assert.assertEquals("bar:foo", new Icon("bar:foo").getIcon()); + } + + @Test + public void canDefineCollectionInSetter() { + Icon icon = new Icon(); + icon.setIcon("bar:foo"); + Assert.assertEquals("bar:foo", icon.getIcon()); + } + + @Test + public void setterUsesCurrentCollection() { + Icon icon = new Icon("bar:foo"); + icon.setIcon("baz"); + Assert.assertEquals("bar:baz", icon.getIcon()); + } + + @Test + public void canSetNewVaadinIcon() { + Icon icon = new Icon("bar:foo"); + icon.setIcon(VaadinIcon.ABSOLUTE_POSITION); + Assert.assertEquals("vaadin:absolute-position", icon.getIcon()); + } } From 56889649f4d500891e127cc24ac9b95310aad1cb Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Fri, 10 May 2024 10:20:59 +0300 Subject: [PATCH 3/4] javadoc --- .../src/main/java/com/vaadin/flow/component/icon/Icon.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/vaadin-icons-flow-parent/vaadin-icons-flow/src/main/java/com/vaadin/flow/component/icon/Icon.java b/vaadin-icons-flow-parent/vaadin-icons-flow/src/main/java/com/vaadin/flow/component/icon/Icon.java index df1120203a9..5586a153769 100644 --- a/vaadin-icons-flow-parent/vaadin-icons-flow/src/main/java/com/vaadin/flow/component/icon/Icon.java +++ b/vaadin-icons-flow-parent/vaadin-icons-flow/src/main/java/com/vaadin/flow/component/icon/Icon.java @@ -91,7 +91,7 @@ public Icon(String collection, String icon) { *

* If the icon name contains a ":", the first part is used as the collection * and the second part as the icon name. If the icon name does not contain a - * ":", the icon is assumed to be from the "vaadin" collection. + * ":", the current collection is used (vaadin by default). * * @param icon * the icon name @@ -111,10 +111,6 @@ public void setIcon(String icon) { /** * /** Sets the icon to the given Vaadin icon. - *

- * If the icon name contains a ":", the first part is used as the collection - * and the second part as the icon name. If the icon name does not contain a - * ":", the icon is assumed to be from the "vaadin" collection. * * @param icon * the icon name From dac31a6004a833e26d95094c15170954639dad5d Mon Sep 17 00:00:00 2001 From: Artur Date: Fri, 10 May 2024 10:30:40 +0300 Subject: [PATCH 4/4] Update vaadin-icons-flow-parent/vaadin-icons-flow/src/main/java/com/vaadin/flow/component/icon/Icon.java Co-authored-by: Sergey Vinogradov --- .../src/main/java/com/vaadin/flow/component/icon/Icon.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vaadin-icons-flow-parent/vaadin-icons-flow/src/main/java/com/vaadin/flow/component/icon/Icon.java b/vaadin-icons-flow-parent/vaadin-icons-flow/src/main/java/com/vaadin/flow/component/icon/Icon.java index 5586a153769..3222ac1fb4c 100644 --- a/vaadin-icons-flow-parent/vaadin-icons-flow/src/main/java/com/vaadin/flow/component/icon/Icon.java +++ b/vaadin-icons-flow-parent/vaadin-icons-flow/src/main/java/com/vaadin/flow/component/icon/Icon.java @@ -110,7 +110,7 @@ public void setIcon(String icon) { } /** - * /** Sets the icon to the given Vaadin icon. + * Sets the icon to the given Vaadin icon. * * @param icon * the icon name