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()) {
boolean frameRequired = false;
for (Widget w : children) {
EObject parent = itemUIRegistry.getParent(w);
if (!(w instanceof Frame || parent instanceof Frame || parent instanceof List)) {
frameRequired = true;
}
}
if (frameRequired) {
String frameSnippet = getSnippet("frame");
frameSnippet = StringUtils.replace(frameSnippet, "%label%", "");
String[] parts = frameSnippet.split("%children%");
if (parts.length > 1) {
sb_pre.append(parts[0]);
}
if (parts.length > 2) {
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 ItemUIRegistryImplTest method getWidget_UnknownPageId.
@Test
public void getWidget_UnknownPageId() throws ItemNotFoundException {
Sitemap sitemap = SitemapFactory.eINSTANCE.createSitemap();
when(registry.getItem("unknown")).thenThrow(new ItemNotFoundException("unknown"));
Widget w = uiRegistry.getWidget(sitemap, "unknown");
assertNull(w);
}
use of org.eclipse.smarthome.model.sitemap.Widget in project smarthome by eclipse.
the class ItemUIRegistryImplTest method getLabel_labelWithZonedTime.
@Test
public void getLabel_labelWithZonedTime() throws ItemNotFoundException {
String testLabel = "Label [%1$tT]";
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-01T15:30:59Z"));
String label = uiRegistry.getLabel(w);
assertEquals("Label [15:30:59]", label);
}
use of org.eclipse.smarthome.model.sitemap.Widget in project smarthome by eclipse.
the class ItemUIRegistryImpl method resolveDefault.
private Widget resolveDefault(Widget widget) {
if (!(widget instanceof Default)) {
return widget;
} else {
String itemName = widget.getItem();
if (itemName != null) {
Item item = itemRegistry.get(itemName);
if (item != null) {
Widget defaultWidget = getDefaultWidget(item.getClass(), item.getName());
if (defaultWidget != null) {
copyProperties(widget, defaultWidget);
defaultWidgets.put(defaultWidget, widget);
return defaultWidget;
}
}
}
return null;
}
}
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");
boolean async = "true".equalsIgnoreCase(req.getParameter("__async"));
boolean poll = "true".equalsIgnoreCase(req.getParameter("poll"));
// if there are no parameters, display the "default" sitemap
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) {
throw new RenderException("Sitemap '" + sitemapName + "' could not be found");
}
logger.debug("reading sitemap {} widgetId {} async {} poll {}", sitemap.getName(), widgetId, async, poll);
if (widgetId == null || widgetId.isEmpty() || widgetId.equals("Home")) {
// we are at the homepage, so we render the children of the sitemap root node
String label = sitemap.getLabel() != null ? sitemap.getLabel() : sitemapName;
EList<Widget> children = renderer.getItemUIRegistry().getChildren(sitemap);
if (poll && waitForChanges(children) == false) {
// we have reached the timeout, so we do not return any content as nothing has changed
res.getWriter().append(getTimeoutResponse()).close();
return;
}
result.append(renderer.processPage("Home", 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
Widget w = renderer.getItemUIRegistry().getWidget(sitemap, widgetId);
if (w != null) {
if (!(w instanceof LinkableWidget)) {
throw new RenderException("Widget '" + w + "' can not have any content");
}
LinkableWidget lw = (LinkableWidget) w;
EList<Widget> children = renderer.getItemUIRegistry().getChildren(lw);
EList<Widget> parentAndChildren = new BasicEList<Widget>();
parentAndChildren.add(lw);
parentAndChildren.addAll(children);
if (poll && waitForChanges(parentAndChildren) == false) {
// we have reached the timeout, so we do not return any content as nothing has changed
res.getWriter().append(getTimeoutResponse()).close();
return;
}
String label = renderer.getItemUIRegistry().getLabel(w);
if (label == null) {
label = "undefined";
}
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("application/xml;charset=UTF-8");
} else {
res.setContentType("text/html;charset=UTF-8");
}
res.getWriter().append(result);
res.getWriter().close();
}
Aggregations