Search in sources :

Example 6 with Sitemap

use of org.eclipse.smarthome.model.sitemap.Sitemap 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 7 with Sitemap

use of org.eclipse.smarthome.model.sitemap.Sitemap 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)

Example 8 with Sitemap

use of org.eclipse.smarthome.model.sitemap.Sitemap in project smarthome by eclipse.

the class SitemapProviderImpl method getSitemap.

@Override
public Sitemap getSitemap(String sitemapName) {
    String filename = sitemapName + SITEMAP_FILEEXT;
    Sitemap sitemap = sitemapModelCache.get(filename);
    if (sitemap != null) {
        if (!sitemap.getName().equals(sitemapName)) {
            logger.warn("Filename `{}` does not match the name `{}` of the sitemap - please fix this as you might see unexpected behavior otherwise.", filename, sitemap.getName());
        }
        return sitemap;
    } else {
        logger.trace("Sitemap {} cannot be found", sitemapName);
        return null;
    }
}
Also used : Sitemap(org.eclipse.smarthome.model.sitemap.Sitemap)

Example 9 with Sitemap

use of org.eclipse.smarthome.model.sitemap.Sitemap 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 10 with Sitemap

use of org.eclipse.smarthome.model.sitemap.Sitemap 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

Sitemap (org.eclipse.smarthome.model.sitemap.Sitemap)11 Widget (org.eclipse.smarthome.model.sitemap.Widget)9 LinkableWidget (org.eclipse.smarthome.model.sitemap.LinkableWidget)6 SitemapProvider (org.eclipse.smarthome.model.sitemap.SitemapProvider)3 ServletException (javax.servlet.ServletException)2 BasicEList (org.eclipse.emf.common.util.BasicEList)2 EList (org.eclipse.emf.common.util.EList)2 EObject (org.eclipse.emf.ecore.EObject)2 Frame (org.eclipse.smarthome.model.sitemap.Frame)2 URI (java.net.URI)1 LinkedList (java.util.LinkedList)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)1 StringType (org.eclipse.smarthome.core.library.types.StringType)1 State (org.eclipse.smarthome.core.types.State)1 Image (org.eclipse.smarthome.model.sitemap.Image)1 Video (org.eclipse.smarthome.model.sitemap.Video)1 RenderException (org.eclipse.smarthome.ui.basic.render.RenderException)1 RenderException (org.eclipse.smarthome.ui.classic.render.RenderException)1 Test (org.junit.Test)1