From 0835300873c192a5f052af34bc9366b0f4c521da Mon Sep 17 00:00:00 2001 From: lolodomo Date: Thu, 7 Jun 2018 16:02:00 +0200 Subject: [PATCH] Classic UI: consider item options for selection widget when no mapping is defined (#5666) Relative to issue #4942 Signed-off-by: Laurent Garnier --- .../internal/render/SelectionRenderer.java | 87 ++++++++++++------- 1 file changed, 55 insertions(+), 32 deletions(-) diff --git a/extensions/ui/org.eclipse.smarthome.ui.classic/src/main/java/org/eclipse/smarthome/ui/classic/internal/render/SelectionRenderer.java b/extensions/ui/org.eclipse.smarthome.ui.classic/src/main/java/org/eclipse/smarthome/ui/classic/internal/render/SelectionRenderer.java index cb3aa7ac775..b9503b5ab4d 100644 --- a/extensions/ui/org.eclipse.smarthome.ui.classic/src/main/java/org/eclipse/smarthome/ui/classic/internal/render/SelectionRenderer.java +++ b/extensions/ui/org.eclipse.smarthome.ui.classic/src/main/java/org/eclipse/smarthome/ui/classic/internal/render/SelectionRenderer.java @@ -20,6 +20,7 @@ import org.eclipse.smarthome.core.library.items.NumberItem; import org.eclipse.smarthome.core.library.types.QuantityType; import org.eclipse.smarthome.core.types.State; +import org.eclipse.smarthome.core.types.StateOption; import org.eclipse.smarthome.core.types.util.UnitUtils; import org.eclipse.smarthome.model.sitemap.Mapping; import org.eclipse.smarthome.model.sitemap.Selection; @@ -60,6 +61,7 @@ public EList renderWidget(Widget w, StringBuilder sb) throws RenderExcep State state = itemUIRegistry.getState(w); Selection selection = (Selection) w; String mappedValue = ""; + String rowMappedValue; Item item = null; try { @@ -69,40 +71,20 @@ public EList renderWidget(Widget w, StringBuilder sb) throws RenderExcep } StringBuilder rowSB = new StringBuilder(); - for (Mapping mapping : selection.getMappings()) { - String rowSnippet = getSnippet("selection_row"); - - String command = mapping.getCmd() != null ? mapping.getCmd() : ""; - String label = mapping.getLabel(); - - if (item instanceof NumberItem && ((NumberItem) item).getDimension() != null) { - String unit = getUnitForWidget(w); - command = StringUtils.replace(command, UnitUtils.UNIT_PLACEHOLDER, unit); - label = StringUtils.replace(label, UnitUtils.UNIT_PLACEHOLDER, unit); - - // Special treatment for °C since uom library uses a single character: ℃ - // This will ensure the current state matches the cmd and the buttonClass is set accordingly. - command = StringUtils.replace(command, "°C", "℃"); - } - - rowSnippet = StringUtils.replace(rowSnippet, "%item%", w.getItem() != null ? w.getItem() : ""); - rowSnippet = StringUtils.replace(rowSnippet, "%cmd%", StringEscapeUtils.escapeHtml(command)); - rowSnippet = StringUtils.replace(rowSnippet, "%label%", - label != null ? StringEscapeUtils.escapeHtml(label) : ""); - - State compareMappingState = state; - if (state instanceof QuantityType) { // convert the item state to the command value for proper - // comparison and "checked" attribute calculation - compareMappingState = convertStateToLabelUnit((QuantityType) state, command); + if (selection.getMappings().size() == 0 && item != null && item.getStateDescription() != null) { + for (StateOption option : item.getStateDescription().getOptions()) { + rowMappedValue = buildRow(selection, option.getLabel(), option.getValue(), item, state, rowSB); + if (rowMappedValue != null) { + mappedValue = rowMappedValue; + } } - - if (compareMappingState.toString().equals(command)) { - rowSnippet = StringUtils.replace(rowSnippet, "%checked%", "checked=\"true\""); - mappedValue = (label != null) ? label : command; - } else { - rowSnippet = StringUtils.replace(rowSnippet, "%checked%", ""); + } else { + for (Mapping mapping : selection.getMappings()) { + rowMappedValue = buildRow(selection, mapping.getLabel(), mapping.getCmd(), item, state, rowSB); + if (rowMappedValue != null) { + mappedValue = rowMappedValue; + } } - rowSB.append(rowSnippet); } snippet = StringUtils.replace(snippet, "%label_header%", getLabel(w, mappedValue)); snippet = StringUtils.replace(snippet, "%rows%", rowSB.toString()); @@ -114,6 +96,47 @@ public EList renderWidget(Widget w, StringBuilder sb) throws RenderExcep return null; } + private String buildRow(Selection w, String lab, String cmd, Item item, State state, StringBuilder rowSB) + throws RenderException { + String mappedValue = null; + String rowSnippet = getSnippet("selection_row"); + + String command = cmd != null ? cmd : ""; + String label = lab; + + if (item instanceof NumberItem && ((NumberItem) item).getDimension() != null) { + String unit = getUnitForWidget(w); + command = StringUtils.replace(command, UnitUtils.UNIT_PLACEHOLDER, unit); + label = StringUtils.replace(label, UnitUtils.UNIT_PLACEHOLDER, unit); + + // Special treatment for °C since uom library uses a single character: ℃ + // This will ensure the current state matches the cmd and the buttonClass is set accordingly. + command = StringUtils.replace(command, "°C", "℃"); + } + + rowSnippet = StringUtils.replace(rowSnippet, "%item%", w.getItem() != null ? w.getItem() : ""); + rowSnippet = StringUtils.replace(rowSnippet, "%cmd%", StringEscapeUtils.escapeHtml(command)); + rowSnippet = StringUtils.replace(rowSnippet, "%label%", + label != null ? StringEscapeUtils.escapeHtml(label) : ""); + + State compareMappingState = state; + if (state instanceof QuantityType) { // convert the item state to the command value for proper + // comparison and "checked" attribute calculation + compareMappingState = convertStateToLabelUnit((QuantityType) state, command); + } + + if (compareMappingState.toString().equals(command)) { + mappedValue = label; + rowSnippet = StringUtils.replace(rowSnippet, "%checked%", "checked=\"true\""); + } else { + rowSnippet = StringUtils.replace(rowSnippet, "%checked%", ""); + } + + rowSB.append(rowSnippet); + + return mappedValue; + } + @Override @Reference protected void setItemUIRegistry(ItemUIRegistry ItemUIRegistry) {