From 5bc65cc0096071994b57b9c6f215001974470fba Mon Sep 17 00:00:00 2001 From: Laurent Garnier Date: Sat, 23 Sep 2023 10:09:49 +0200 Subject: [PATCH 1/2] [Sitemap] Accept an optional icon for each value/label mapping When set by the user, the icon can be used by UIs for switch element with mappings to render a button with the icon rather than the label. Related to #3441 Signed-off-by: Laurent Garnier --- .../openhab/core/io/rest/sitemap/internal/MappingDTO.java | 4 +++- .../core/io/rest/sitemap/internal/SitemapResource.java | 2 ++ .../src/org/openhab/core/model/sitemap/Sitemap.xtext | 2 +- .../internal/components/UIComponentSitemapProvider.java | 8 ++++++-- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/bundles/org.openhab.core.io.rest.sitemap/src/main/java/org/openhab/core/io/rest/sitemap/internal/MappingDTO.java b/bundles/org.openhab.core.io.rest.sitemap/src/main/java/org/openhab/core/io/rest/sitemap/internal/MappingDTO.java index e5a3f15c197..11c3b3609ff 100644 --- a/bundles/org.openhab.core.io.rest.sitemap/src/main/java/org/openhab/core/io/rest/sitemap/internal/MappingDTO.java +++ b/bundles/org.openhab.core.io.rest.sitemap/src/main/java/org/openhab/core/io/rest/sitemap/internal/MappingDTO.java @@ -14,13 +14,15 @@ /** * This is a data transfer object that is used to serialize command mappings. - * + * * @author Kai Kreuzer - Initial contribution + * @author Laurent Garnier - Added icon */ public class MappingDTO { public String command; public String label; + public String icon; public MappingDTO() { } diff --git a/bundles/org.openhab.core.io.rest.sitemap/src/main/java/org/openhab/core/io/rest/sitemap/internal/SitemapResource.java b/bundles/org.openhab.core.io.rest.sitemap/src/main/java/org/openhab/core/io/rest/sitemap/internal/SitemapResource.java index 2874f5a7654..3f11363805b 100644 --- a/bundles/org.openhab.core.io.rest.sitemap/src/main/java/org/openhab/core/io/rest/sitemap/internal/SitemapResource.java +++ b/bundles/org.openhab.core.io.rest.sitemap/src/main/java/org/openhab/core/io/rest/sitemap/internal/SitemapResource.java @@ -131,6 +131,7 @@ * @author Wouter Born - Migrated to OpenAPI annotations * @author Laurent Garnier - Added support for icon color * @author Mark Herwege - Added pattern and unit fields + * @author Laurent Garnier - Added icon field for mappings used for switch element */ @Component(service = { RESTResource.class, EventSubscriber.class }) @JaxrsResource @@ -555,6 +556,7 @@ private PageDTO createPageBean(String sitemapName, @Nullable String title, @Null MappingDTO mappingBean = new MappingDTO(); mappingBean.command = mapping.getCmd(); mappingBean.label = mapping.getLabel(); + mappingBean.icon = mapping.getIcon(); bean.mappings.add(mappingBean); } } diff --git a/bundles/org.openhab.core.model.sitemap/src/org/openhab/core/model/sitemap/Sitemap.xtext b/bundles/org.openhab.core.model.sitemap/src/org/openhab/core/model/sitemap/Sitemap.xtext index 2a3088db948..a98bebfba1e 100644 --- a/bundles/org.openhab.core.model.sitemap/src/org/openhab/core/model/sitemap/Sitemap.xtext +++ b/bundles/org.openhab.core.model.sitemap/src/org/openhab/core/model/sitemap/Sitemap.xtext @@ -159,7 +159,7 @@ Default: ('visibility=[' (Visibility+=VisibilityRule (',' Visibility+=VisibilityRule)* ']'))?); Mapping: - cmd=Command '=' label=(ID | STRING); + cmd=Command '=' label=(ID | STRING) ('=' icon=Icon)?; VisibilityRule: (item=ID) (condition=('==' | '>' | '<' | '>=' | '<=' | '!=')) (sign=('-' | '+'))? (state=XState); diff --git a/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/components/UIComponentSitemapProvider.java b/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/components/UIComponentSitemapProvider.java index 39c0f47dce5..fba2676cb30 100644 --- a/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/components/UIComponentSitemapProvider.java +++ b/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/components/UIComponentSitemapProvider.java @@ -75,6 +75,7 @@ * * @author Yannick Schaus - Initial contribution * @author Laurent Garnier - icon color support for all widgets + * @author Laurent Garnier - Added icon field for mappings */ @NonNullByDefault @Component(service = SitemapProvider.class) @@ -321,11 +322,14 @@ private void addWidgetMappings(EList mappings, UIComponent component) { if (component.getConfig().get("mappings") instanceof Collection) { for (Object sourceMapping : (Collection) component.getConfig().get("mappings")) { if (sourceMapping instanceof String) { - String cmd = sourceMapping.toString().split("=")[0].trim(); - String label = sourceMapping.toString().split("=")[1].trim(); + String[] splittedMapping = sourceMapping.toString().split("="); + String cmd = splittedMapping[0].trim(); + String label = splittedMapping[1].trim(); + String icon = splittedMapping.length < 3 ? null : splittedMapping[2].trim(); MappingImpl mapping = (MappingImpl) SitemapFactory.eINSTANCE.createMapping(); mapping.setCmd(cmd); mapping.setLabel(label); + mapping.setIcon(icon); mappings.add(mapping); } } From 42ce697c3401f350b4dc76d86c7c8356eac67536 Mon Sep 17 00:00:00 2001 From: Laurent Garnier Date: Sun, 1 Oct 2023 23:09:42 +0200 Subject: [PATCH 2/2] Review comment: splitMapping Signed-off-by: Laurent Garnier --- .../internal/components/UIComponentSitemapProvider.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/components/UIComponentSitemapProvider.java b/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/components/UIComponentSitemapProvider.java index 7f3937d9582..2f59e2ed531 100644 --- a/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/components/UIComponentSitemapProvider.java +++ b/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/components/UIComponentSitemapProvider.java @@ -332,10 +332,10 @@ private void addWidgetMappings(EList mappings, UIComponent component) { if (component.getConfig().get("mappings") instanceof Collection) { for (Object sourceMapping : (Collection) component.getConfig().get("mappings")) { if (sourceMapping instanceof String) { - String[] splittedMapping = sourceMapping.toString().split("="); - String cmd = splittedMapping[0].trim(); - String label = splittedMapping[1].trim(); - String icon = splittedMapping.length < 3 ? null : splittedMapping[2].trim(); + String[] splitMapping = sourceMapping.toString().split("="); + String cmd = splitMapping[0].trim(); + String label = splitMapping[1].trim(); + String icon = splitMapping.length < 3 ? null : splitMapping[2].trim(); MappingImpl mapping = (MappingImpl) SitemapFactory.eINSTANCE.createMapping(); mapping.setCmd(cmd); mapping.setLabel(label);