use of org.eclipse.smarthome.model.sitemap.Widget in project smarthome by eclipse.
the class ItemUIRegistryImplTest method getLabel_transformationContainingPercentS.
@Test
public void getLabel_transformationContainingPercentS() throws ItemNotFoundException {
// It doesn't matter that "FOO" doesn't exist - this is to assert it doesn't fail before because of the two "%s"
String testLabel = "Memory [FOO(echo %s):%s]";
Widget w = mock(Widget.class);
Item item = mock(Item.class);
when(w.getLabel()).thenReturn(testLabel);
when(w.getItem()).thenReturn("Item");
when(registry.getItem("Item")).thenReturn(item);
when(item.getState()).thenReturn(new StringType("State"));
String label = uiRegistry.getLabel(w);
assertEquals("Memory [State]", label);
}
use of org.eclipse.smarthome.model.sitemap.Widget in project smarthome by eclipse.
the class ItemUIRegistryImplTest method getLabel_labelWithZonedDate.
@Test
public void getLabel_labelWithZonedDate() throws ItemNotFoundException {
String testLabel = "Label [%1$td.%1$tm.%1$tY]";
Widget w = mock(Widget.class);
Item item = mock(Item.class);
when(w.getLabel()).thenReturn(testLabel);
when(w.getItem()).thenReturn("Item");
when(registry.getItem("Item")).thenReturn(item);
when(item.getState()).thenReturn(new DateTimeType("2011-06-01T00:00:00Z"));
String label = uiRegistry.getLabel(w);
assertEquals("Label [01.06.2011]", label);
}
use of org.eclipse.smarthome.model.sitemap.Widget in project smarthome by eclipse.
the class ItemUIRegistryImplTest method getLabel_widgetWithoutLabelAndItem.
@Test
public void getLabel_widgetWithoutLabelAndItem() throws ItemNotFoundException {
Widget w = mock(Widget.class);
String label = uiRegistry.getLabel(w);
assertEquals("", label);
}
use of org.eclipse.smarthome.model.sitemap.Widget in project smarthome by eclipse.
the class PageRenderer method processChildren.
private void processChildren(StringBuilder sb_pre, StringBuilder sb_post, EList<Widget> children) throws RenderException {
// put a single frame around all children widgets, if there are no explicit frames
if (!children.isEmpty()) {
EObject firstChild = children.get(0);
EObject parent = itemUIRegistry.getParent((Widget) firstChild);
if (!(firstChild instanceof Frame || parent instanceof Frame || parent instanceof Sitemap || parent instanceof org.eclipse.smarthome.model.sitemap.List)) {
String frameSnippet = getSnippet("frame");
frameSnippet = StringUtils.replace(frameSnippet, "%widget_id%", "");
frameSnippet = StringUtils.replace(frameSnippet, "%label%", "");
frameSnippet = StringUtils.replace(frameSnippet, "%frame_class%", "mdl-form--no-label");
String[] parts = frameSnippet.split("%children%");
if (parts.length > 1) {
sb_pre.append(parts[0]);
sb_post.insert(0, parts[1]);
}
if (parts.length > 2) {
logger.error("Snippet 'frame' contains multiple %children% sections, but only one is allowed!");
}
}
}
for (Widget w : children) {
StringBuilder newPre = new StringBuilder();
StringBuilder newPost = new StringBuilder();
StringBuilder widgetSB = new StringBuilder();
EList<Widget> nextChildren = renderWidget(w, widgetSB);
if (nextChildren != null) {
String[] parts = widgetSB.toString().split("%children%");
// no %children% placeholder found or at the end
if (parts.length == 1) {
newPre.append(widgetSB);
}
// %children% section found
if (parts.length > 1) {
newPre.append(parts[0]);
newPost.insert(0, parts[1]);
}
// occurance
if (parts.length > 2) {
String widgetType = w.eClass().getInstanceTypeName().substring(w.eClass().getInstanceTypeName().lastIndexOf(".") + 1);
logger.error("Snippet for widget '{}' contains multiple %children% sections, but only one is allowed!", widgetType);
}
processChildren(newPre, newPost, nextChildren);
sb_pre.append(newPre);
sb_pre.append(newPost);
} else {
sb_pre.append(widgetSB);
}
}
}
use of org.eclipse.smarthome.model.sitemap.Widget in project smarthome by eclipse.
the class WebAppServlet method service.
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
logger.debug("Servlet request received!");
// read request parameters
String sitemapName = req.getParameter("sitemap");
String widgetId = req.getParameter("w");
String subscriptionId = req.getParameter("subscriptionId");
boolean async = "true".equalsIgnoreCase(req.getParameter("__async"));
if (sitemapName == null) {
sitemapName = config.getDefaultSitemap();
}
StringBuilder result = new StringBuilder();
Sitemap sitemap = null;
for (SitemapProvider sitemapProvider : sitemapProviders) {
sitemap = sitemapProvider.getSitemap(sitemapName);
if (sitemap != null) {
break;
}
}
try {
if (sitemap == null) {
showSitemapList(res);
return;
}
logger.debug("reading sitemap {}", sitemap.getName());
if (widgetId == null || widgetId.isEmpty() || widgetId.equals(sitemapName)) {
// we are at the homepage, so we render the children of the sitemap root node
if (subscriptionId != null) {
if (subscriptions.exists(subscriptionId)) {
subscriptions.setPageId(subscriptionId, sitemap.getName(), sitemapName);
} else {
logger.debug("Basic UI requested a non-existing event subscription id ({})", subscriptionId);
}
}
String label = sitemap.getLabel() != null ? sitemap.getLabel() : sitemapName;
EList<Widget> children = renderer.getItemUIRegistry().getChildren(sitemap);
result.append(renderer.processPage(sitemapName, sitemapName, label, children, async));
} else if (!widgetId.equals("Colorpicker")) {
// we are on some subpage, so we have to render the children of the widget that has been selected
if (subscriptionId != null) {
if (subscriptions.exists(subscriptionId)) {
subscriptions.setPageId(subscriptionId, sitemap.getName(), widgetId);
} else {
logger.debug("Basic UI requested a non-existing event subscription id ({})", subscriptionId);
}
}
Widget w = renderer.getItemUIRegistry().getWidget(sitemap, widgetId);
if (w != null) {
String label = renderer.getItemUIRegistry().getLabel(w);
if (label == null) {
label = "undefined";
}
if (!(w instanceof LinkableWidget)) {
throw new RenderException("Widget '" + w + "' can not have any content");
}
EList<Widget> children = renderer.getItemUIRegistry().getChildren((LinkableWidget) w);
result.append(renderer.processPage(renderer.getItemUIRegistry().getWidgetId(w), sitemapName, label, children, async));
}
}
} catch (RenderException e) {
throw new ServletException(e.getMessage(), e);
}
if (async) {
res.setContentType(CONTENT_TYPE_ASYNC);
} else {
res.setContentType(CONTENT_TYPE);
}
res.getWriter().append(result);
res.getWriter().close();
}
Aggregations