Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
Classic UI: consider item options for selection widget when no mappin…
Browse files Browse the repository at this point in the history
…g is defined (#5666)

Relative to issue #4942

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
  • Loading branch information
lolodomo authored and kaikreuzer committed Jun 7, 2018
1 parent 4649232 commit 1c97d88
Showing 1 changed file with 55 additions and 32 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -60,6 +61,7 @@ public EList<Widget> renderWidget(Widget w, StringBuilder sb) throws RenderExcep
State state = itemUIRegistry.getState(w);
Selection selection = (Selection) w;
String mappedValue = "";
String rowMappedValue;

Item item = null;
try {
Expand All @@ -69,40 +71,20 @@ public EList<Widget> 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());
Expand All @@ -114,6 +96,47 @@ public EList<Widget> 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) {
Expand Down

0 comments on commit 1c97d88

Please sign in to comment.