From 42277efe719a99da9ee316fabfccccebc091cb0f Mon Sep 17 00:00:00 2001 From: lolodomo Date: Sat, 20 May 2017 13:43:24 +0200 Subject: [PATCH] Classic UI: fix dynamic widget visibility and dynamic update of the page title Also fix update of title when linked to a group item Fix #3488 Fix #3176 Signed-off-by: Laurent Garnier --- .../internal/servlet/WebAppServlet.java | 66 ++++++++++++------- 1 file changed, 41 insertions(+), 25 deletions(-) diff --git a/extensions/ui/org.eclipse.smarthome.ui.classic/src/main/java/org/eclipse/smarthome/ui/classic/internal/servlet/WebAppServlet.java b/extensions/ui/org.eclipse.smarthome.ui.classic/src/main/java/org/eclipse/smarthome/ui/classic/internal/servlet/WebAppServlet.java index 6b591a91bc8..2ce0bc61701 100644 --- a/extensions/ui/org.eclipse.smarthome.ui.classic/src/main/java/org/eclipse/smarthome/ui/classic/internal/servlet/WebAppServlet.java +++ b/extensions/ui/org.eclipse.smarthome.ui.classic/src/main/java/org/eclipse/smarthome/ui/classic/internal/servlet/WebAppServlet.java @@ -19,8 +19,10 @@ import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; +import org.eclipse.emf.common.util.BasicEList; import org.eclipse.emf.common.util.EList; import org.eclipse.smarthome.core.items.GenericItem; +import org.eclipse.smarthome.core.items.GroupItem; import org.eclipse.smarthome.core.items.Item; import org.eclipse.smarthome.core.items.ItemNotFoundException; import org.eclipse.smarthome.core.items.StateChangeListener; @@ -29,6 +31,7 @@ import org.eclipse.smarthome.model.sitemap.LinkableWidget; import org.eclipse.smarthome.model.sitemap.Sitemap; import org.eclipse.smarthome.model.sitemap.SitemapProvider; +import org.eclipse.smarthome.model.sitemap.VisibilityRule; import org.eclipse.smarthome.model.sitemap.Widget; import org.eclipse.smarthome.ui.classic.internal.WebAppConfig; import org.eclipse.smarthome.ui.classic.internal.render.PageRenderer; @@ -129,7 +132,7 @@ public void service(ServletRequest req, ServletResponse res) throws ServletExcep if (sitemap == null) { throw new RenderException("Sitemap '" + sitemapName + "' could not be found"); } - logger.debug("reading sitemap {}", sitemap.getName()); + 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; @@ -144,19 +147,23 @@ public void service(ServletRequest req, ServletResponse res) throws ServletExcep // 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) { - 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 children = renderer.getItemUIRegistry().getChildren((LinkableWidget) w); - if (poll && waitForChanges(children) == false) { + LinkableWidget lw = (LinkableWidget) w; + EList children = renderer.getItemUIRegistry().getChildren(lw); + EList parentAndChildren = new BasicEList(); + 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)); } @@ -219,29 +226,34 @@ private boolean waitForChanges(EList widgets) { */ private Set getAllItems(EList widgets) { Set items = new HashSet(); - if (itemRegistry != null) { + if (renderer.getItemUIRegistry() != null) { for (Widget widget : widgets) { - String itemName = widget.getItem(); - if (itemName != null) { - try { - Item item = itemRegistry.getItem(itemName); - if (item instanceof GenericItem) { - final GenericItem gItem = (GenericItem) item; - items.add(gItem); - } - } catch (ItemNotFoundException e) { - // ignore - } - } else { - if (widget instanceof Frame) { - items.addAll(getAllItems(((Frame) widget).getChildren())); - } + addItemWithName(items, widget.getItem()); + if (widget instanceof Frame) { + items.addAll(getAllItems(((Frame) widget).getChildren())); + } + for (VisibilityRule vr : widget.getVisibility()) { + addItemWithName(items, vr.getItem()); } } } return items; } + private void addItemWithName(Set items, String itemName) { + if (itemName != null) { + try { + Item item = renderer.getItemUIRegistry().getItem(itemName); + if (item instanceof GenericItem) { + final GenericItem gItem = (GenericItem) item; + items.add(gItem); + } + } catch (ItemNotFoundException e) { + // ignore + } + } + } + /** * This is a state change listener, which is merely used to determine, if a state * change has occurred on one of a list of items. @@ -258,7 +270,9 @@ private static class BlockingStateChangeListener implements StateChangeListener */ @Override public void stateChanged(Item item, State oldState, State newState) { - changed = true; + if (!(item instanceof GroupItem)) { + changed = true; + } } /** @@ -275,7 +289,9 @@ public boolean hasChangeOccurred() { */ @Override public void stateUpdated(Item item, State state) { - // ignore if the state did not change + if (item instanceof GroupItem) { + changed = true; + } } }