Search in sources :

Example 11 with Widget

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

Example 12 with Widget

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);
}
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 13 with Widget

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);
}
Also used : Widget(org.eclipse.smarthome.model.sitemap.Widget) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 14 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()) {
        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);
        }
    }
}
Also used : Sitemap(org.eclipse.smarthome.model.sitemap.Sitemap) Frame(org.eclipse.smarthome.model.sitemap.Frame) EObject(org.eclipse.emf.ecore.EObject) Widget(org.eclipse.smarthome.model.sitemap.Widget)

Example 15 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");
    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();
}
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.basic.render.RenderException) 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