/** * Get command-label map for a Selection widget * * @return String representing JSON object */ private String getMappingsJSON(Selection w) { JsonObject resultObject = new JsonObject(); for (Mapping mapping : w.getMappings()) { resultObject.addProperty(mapping.getCmd(), mapping.getLabel()); } String result = resultObject.toString(); result = StringEscapeUtils.escapeHtml(result); return result; }
/** {@inheritDoc} */ @Override public EList<Widget> renderWidget(Widget w, StringBuilder sb) throws RenderException { String snippet = getSnippet("selection"); snippet = StringUtils.replace(snippet, "%category%", getCategory(w)); snippet = StringUtils.replace(snippet, "%icon_type%", config.getIconType()); snippet = StringUtils.replace(snippet, "%state%", getState(w)); snippet = StringUtils.replace(snippet, "%value_map%", getMappingsJSON((Selection) w)); snippet = StringUtils.replace(snippet, "%label_header%", getLabel(w)); snippet = StringUtils.replace(snippet, "%item%", w.getItem() != null ? w.getItem() : ""); String state = itemUIRegistry.getState(w).toString(); Selection selection = (Selection) w; String mappingLabel = null; StringBuilder rowSB = new StringBuilder(); for (Mapping mapping : selection.getMappings()) { String rowSnippet = getSnippet("selection_row"); String command = mapping.getCmd() != null ? mapping.getCmd() : ""; rowSnippet = StringUtils.replace(rowSnippet, "%item%", w.getItem() != null ? w.getItem() : ""); rowSnippet = StringUtils.replace(rowSnippet, "%cmd%", escapeHtml(command)); rowSnippet = StringUtils.replace( rowSnippet, "%label%", mapping.getLabel() != null ? mapping.getLabel() : ""); if (state.equals(mapping.getCmd())) { mappingLabel = mapping.getLabel(); rowSnippet = StringUtils.replace(rowSnippet, "%checked%", "checked=\"true\""); } else { rowSnippet = StringUtils.replace(rowSnippet, "%checked%", ""); } rowSB.append(rowSnippet); } snippet = StringUtils.replace(snippet, "%rows%", rowSB.toString()); snippet = StringUtils.replace(snippet, "%value%", mappingLabel != null ? mappingLabel : ""); // Process the color tags snippet = processColor(w, snippet); sb.append(snippet); return null; }
private WidgetDTO createWidgetBean( String sitemapName, Widget widget, boolean drillDown, URI uri, String widgetId, Locale locale) { // Test visibility if (itemUIRegistry.getVisiblity(widget) == false) { return null; } WidgetDTO bean = new WidgetDTO(); if (widget.getItem() != null) { try { Item item = itemUIRegistry.getItem(widget.getItem()); if (item != null) { bean.item = EnrichedItemDTOMapper.map(item, false, UriBuilder.fromUri(uri).build(), locale); } } catch (ItemNotFoundException e) { logger.debug(e.getMessage()); } } bean.widgetId = widgetId; bean.icon = itemUIRegistry.getCategory(widget); bean.labelcolor = itemUIRegistry.getLabelColor(widget); bean.valuecolor = itemUIRegistry.getValueColor(widget); bean.label = itemUIRegistry.getLabel(widget); bean.type = widget.eClass().getName(); if (widget instanceof LinkableWidget) { LinkableWidget linkableWidget = (LinkableWidget) widget; EList<Widget> children = itemUIRegistry.getChildren(linkableWidget); if (widget instanceof Frame) { int cntWidget = 0; for (Widget child : children) { widgetId += "_" + cntWidget; WidgetDTO subWidget = createWidgetBean(sitemapName, child, drillDown, uri, widgetId, locale); if (subWidget != null) { bean.widgets.add(subWidget); cntWidget++; } } } else if (children.size() > 0) { String pageName = itemUIRegistry.getWidgetId(linkableWidget); bean.linkedPage = createPageBean( sitemapName, itemUIRegistry.getLabel(widget), itemUIRegistry.getCategory(widget), pageName, drillDown ? children : null, drillDown, isLeaf(children), uri, locale); } } if (widget instanceof Switch) { Switch switchWidget = (Switch) widget; for (Mapping mapping : switchWidget.getMappings()) { MappingDTO mappingBean = new MappingDTO(); mappingBean.command = mapping.getCmd(); mappingBean.label = mapping.getLabel(); bean.mappings.add(mappingBean); } } if (widget instanceof Selection) { Selection selectionWidget = (Selection) widget; for (Mapping mapping : selectionWidget.getMappings()) { MappingDTO mappingBean = new MappingDTO(); mappingBean.command = mapping.getCmd(); mappingBean.label = mapping.getLabel(); bean.mappings.add(mappingBean); } } if (widget instanceof Slider) { Slider sliderWidget = (Slider) widget; bean.sendFrequency = sliderWidget.getFrequency(); bean.switchSupport = sliderWidget.isSwitchEnabled(); } if (widget instanceof List) { List listWidget = (List) widget; bean.separator = listWidget.getSeparator(); } if (widget instanceof Image) { Image imageWidget = (Image) widget; String wId = itemUIRegistry.getWidgetId(widget); if (uri.getPort() < 0 || uri.getPort() == 80) { bean.url = uri.getScheme() + "://" + uri.getHost() + "/proxy?sitemap=" + sitemapName + ".sitemap&widgetId=" + wId; } else { bean.url = uri.getScheme() + "://" + uri.getHost() + ":" + uri.getPort() + "/proxy?sitemap=" + sitemapName + ".sitemap&widgetId=" + wId; } if (imageWidget.getRefresh() > 0) { bean.refresh = imageWidget.getRefresh(); } } if (widget instanceof Video) { Video videoWidget = (Video) widget; String wId = itemUIRegistry.getWidgetId(widget); if (videoWidget.getEncoding() != null) { bean.encoding = videoWidget.getEncoding(); } if (uri.getPort() < 0 || uri.getPort() == 80) { bean.url = uri.getScheme() + "://" + uri.getHost() + "/proxy?sitemap=" + sitemapName + ".sitemap&widgetId=" + wId; } else { bean.url = uri.getScheme() + "://" + uri.getHost() + ":" + uri.getPort() + "/proxy?sitemap=" + sitemapName + ".sitemap&widgetId=" + wId; } } if (widget instanceof Webview) { Webview webViewWidget = (Webview) widget; bean.url = webViewWidget.getUrl(); bean.height = webViewWidget.getHeight(); } if (widget instanceof Mapview) { Mapview mapViewWidget = (Mapview) widget; bean.height = mapViewWidget.getHeight(); } if (widget instanceof Chart) { Chart chartWidget = (Chart) widget; bean.service = chartWidget.getService(); bean.period = chartWidget.getPeriod(); if (chartWidget.getRefresh() > 0) { bean.refresh = chartWidget.getRefresh(); } } if (widget instanceof Setpoint) { Setpoint setpointWidget = (Setpoint) widget; bean.minValue = setpointWidget.getMinValue(); bean.maxValue = setpointWidget.getMaxValue(); bean.step = setpointWidget.getStep(); } return bean; }