Search in sources :

Example 16 with Widget

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);
        }
    }
}
Also used : Frame(org.eclipse.smarthome.model.sitemap.Frame) EObject(org.eclipse.emf.ecore.EObject) Widget(org.eclipse.smarthome.model.sitemap.Widget) EList(org.eclipse.emf.common.util.EList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 17 with Widget

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);
}
Also used : Sitemap(org.eclipse.smarthome.model.sitemap.Sitemap) Widget(org.eclipse.smarthome.model.sitemap.Widget) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException) Test(org.junit.Test)

Example 18 with Widget

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);
}
Also used : ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) Item(org.eclipse.smarthome.core.items.Item) DateTimeType(org.eclipse.smarthome.core.library.types.DateTimeType) Widget(org.eclipse.smarthome.model.sitemap.Widget) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 19 with Widget

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;
    }
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) CallItem(org.eclipse.smarthome.core.library.items.CallItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) DateTimeItem(org.eclipse.smarthome.core.library.items.DateTimeItem) RollershutterItem(org.eclipse.smarthome.core.library.items.RollershutterItem) GroupItem(org.eclipse.smarthome.core.items.GroupItem) ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) LocationItem(org.eclipse.smarthome.core.library.items.LocationItem) ContactItem(org.eclipse.smarthome.core.library.items.ContactItem) GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) StringItem(org.eclipse.smarthome.core.library.items.StringItem) PlayerItem(org.eclipse.smarthome.core.library.items.PlayerItem) ImageItem(org.eclipse.smarthome.core.library.items.ImageItem) DimmerItem(org.eclipse.smarthome.core.library.items.DimmerItem) Widget(org.eclipse.smarthome.model.sitemap.Widget) LinkableWidget(org.eclipse.smarthome.model.sitemap.LinkableWidget) Default(org.eclipse.smarthome.model.sitemap.Default)

Example 20 with Widget

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();
}
Also used : LinkableWidget(org.eclipse.smarthome.model.sitemap.LinkableWidget) ServletException(javax.servlet.ServletException) Sitemap(org.eclipse.smarthome.model.sitemap.Sitemap) RenderException(org.eclipse.smarthome.ui.classic.render.RenderException) BasicEList(org.eclipse.emf.common.util.BasicEList) EList(org.eclipse.emf.common.util.EList) SitemapProvider(org.eclipse.smarthome.model.sitemap.SitemapProvider) Widget(org.eclipse.smarthome.model.sitemap.Widget) LinkableWidget(org.eclipse.smarthome.model.sitemap.LinkableWidget)

Aggregations

Widget (org.eclipse.smarthome.model.sitemap.Widget)24 LinkableWidget (org.eclipse.smarthome.model.sitemap.LinkableWidget)14 Sitemap (org.eclipse.smarthome.model.sitemap.Sitemap)9 Item (org.eclipse.smarthome.core.items.Item)8 BasicEList (org.eclipse.emf.common.util.BasicEList)6 Frame (org.eclipse.smarthome.model.sitemap.Frame)6 GenericItem (org.eclipse.smarthome.core.items.GenericItem)5 ColorItem (org.eclipse.smarthome.core.library.items.ColorItem)5 Test (org.junit.Test)5 EList (org.eclipse.emf.common.util.EList)4 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)4 EObject (org.eclipse.emf.ecore.EObject)3 GroupItem (org.eclipse.smarthome.core.items.GroupItem)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 ServletException (javax.servlet.ServletException)2 CallItem (org.eclipse.smarthome.core.library.items.CallItem)2 ContactItem (org.eclipse.smarthome.core.library.items.ContactItem)2 DateTimeItem (org.eclipse.smarthome.core.library.items.DateTimeItem)2