From 1cc4ad371d2517f9ad49bd0746c883568fa5ff62 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Wed, 11 Apr 2012 10:15:52 +0300 Subject: [PATCH] Updated Root to use hierarchy change event for content and windows --- .../terminal/gwt/client/ComponentLocator.java | 41 +++--- .../terminal/gwt/client/VDebugConsole.java | 7 +- .../terminal/gwt/client/ui/RootConnector.java | 127 ++++++++++-------- .../vaadin/terminal/gwt/client/ui/VView.java | 19 --- src/com/vaadin/ui/Root.java | 48 +++---- 5 files changed, 111 insertions(+), 131 deletions(-) diff --git a/src/com/vaadin/terminal/gwt/client/ComponentLocator.java b/src/com/vaadin/terminal/gwt/client/ComponentLocator.java index f954105120..9e5ed709c7 100644 --- a/src/com/vaadin/terminal/gwt/client/ComponentLocator.java +++ b/src/com/vaadin/terminal/gwt/client/ComponentLocator.java @@ -5,6 +5,7 @@ package com.vaadin.terminal.gwt.client; import java.util.ArrayList; import java.util.Iterator; +import java.util.List; import com.google.gwt.user.client.DOM; import com.google.gwt.user.client.Element; @@ -17,6 +18,7 @@ import com.vaadin.terminal.gwt.client.ui.VMeasuringOrderedLayout; import com.vaadin.terminal.gwt.client.ui.VTabsheetPanel; import com.vaadin.terminal.gwt.client.ui.VView; import com.vaadin.terminal.gwt.client.ui.VWindow; +import com.vaadin.terminal.gwt.client.ui.WindowConnector; /** * ComponentLocator provides methods for generating a String locator for a given @@ -368,18 +370,14 @@ public class ComponentLocator { return null; } - String pid = ConnectorMap.get(client).getConnectorId(w.getElement()); - if (isStaticPid(pid)) { - return pid; - } - if (w instanceof VView) { return ""; } else if (w instanceof VWindow) { - VWindow win = (VWindow) w; - ArrayList subWindowList = client.getView().getWidget() - .getSubWindowList(); - int indexOfSubWindow = subWindowList.indexOf(win); + Connector windowConnector = ConnectorMap.get(client) + .getConnector(w); + List subWindowList = client.getView() + .getSubWindows(); + int indexOfSubWindow = subWindowList.indexOf(windowConnector); return PARENTCHILD_SEPARATOR + "VWindow[" + indexOfSubWindow + "]"; } else if (w instanceof RootPanel) { return ROOT_ID; @@ -543,8 +541,14 @@ public class ComponentLocator { * compatibility */ if (widgetClassName.equals("VWindow")) { - iterator = client.getView().getWidget().getSubWindowList() - .iterator(); + List windows = client.getView() + .getSubWindows(); + List windowWidgets = new ArrayList( + windows.size()); + for (WindowConnector wc : windows) { + windowWidgets.add(wc.getWidget()); + } + iterator = windowWidgets.iterator(); } else if (widgetClassName.equals("VContextMenu")) { return client.getContextMenu(); } else { @@ -603,19 +607,4 @@ public class ComponentLocator { return null; } - /** - * Checks if the given pid is a static pid. - * - * @param pid - * The pid to check - * @return true if the pid is a static pid, false otherwise - */ - private boolean isStaticPid(String pid) { - if (pid == null) { - return false; - } - - return pid.startsWith("PID_S"); - } - } diff --git a/src/com/vaadin/terminal/gwt/client/VDebugConsole.java b/src/com/vaadin/terminal/gwt/client/VDebugConsole.java index c457044ea9..f815dc29fc 100644 --- a/src/com/vaadin/terminal/gwt/client/VDebugConsole.java +++ b/src/com/vaadin/terminal/gwt/client/VDebugConsole.java @@ -56,7 +56,7 @@ import com.vaadin.terminal.gwt.client.ui.RootConnector; import com.vaadin.terminal.gwt.client.ui.VLazyExecutor; import com.vaadin.terminal.gwt.client.ui.VNotification; import com.vaadin.terminal.gwt.client.ui.VOverlay; -import com.vaadin.terminal.gwt.client.ui.VWindow; +import com.vaadin.terminal.gwt.client.ui.WindowConnector; /** * A helper console for client side development. The debug console can also be @@ -858,9 +858,8 @@ public class VDebugConsole extends VOverlay implements Console { .getConnectors(); log("Sub windows:"); Set subWindowHierarchyConnectors = new HashSet(); - for (VWindow w : root.getWidget().getSubWindowList()) { - SimpleTree windowHierachy = dumpConnectorHierarchy( - connectorMap.getConnector(w), "", + for (WindowConnector wc : root.getSubWindows()) { + SimpleTree windowHierachy = dumpConnectorHierarchy(wc, "", subWindowHierarchyConnectors); if (panel.isAttached()) { windowHierachy.open(true); diff --git a/src/com/vaadin/terminal/gwt/client/ui/RootConnector.java b/src/com/vaadin/terminal/gwt/client/ui/RootConnector.java index 392230aa7f..21942ed96b 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/RootConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/RootConnector.java @@ -3,8 +3,9 @@ */ package com.vaadin.terminal.gwt.client.ui; -import java.util.HashSet; +import java.util.ArrayList; import java.util.Iterator; +import java.util.List; import com.google.gwt.core.client.GWT; import com.google.gwt.core.client.Scheduler; @@ -22,6 +23,8 @@ import com.google.gwt.user.client.ui.Widget; import com.vaadin.terminal.gwt.client.ApplicationConnection; import com.vaadin.terminal.gwt.client.BrowserInfo; import com.vaadin.terminal.gwt.client.ComponentConnector; +import com.vaadin.terminal.gwt.client.ComponentState; +import com.vaadin.terminal.gwt.client.Connector; import com.vaadin.terminal.gwt.client.ConnectorHierarchyChangeEvent; import com.vaadin.terminal.gwt.client.ConnectorMap; import com.vaadin.terminal.gwt.client.Focusable; @@ -41,6 +44,19 @@ import com.vaadin.ui.Root; public class RootConnector extends AbstractComponentContainerConnector implements Paintable { + public static class RootState extends ComponentState { + private Connector content; + + public Connector getContent() { + return content; + } + + public void setContent(Connector content) { + this.content = content; + } + + } + public interface RootServerRPC extends ClickRPC, ServerRpc { } @@ -158,39 +174,8 @@ public class RootConnector extends AbstractComponentContainerConnector return; } - // Draw this application level window - UIDL childUidl = uidl.getChildUIDL(childIndex); - final ComponentConnector lo = client.getPaintable(childUidl); - - boolean layoutChanged = getWidget().layout != lo; - if (getWidget().layout != null) { - if (layoutChanged) { - // remove old - if (childStateChangeHandlerRegistration != null) { - childStateChangeHandlerRegistration.removeHandler(); - childStateChangeHandlerRegistration = null; - } - // add new - getWidget().setWidget(lo.getWidget()); - getWidget().layout = lo; - } - } else { - getWidget().setWidget(lo.getWidget()); - getWidget().layout = lo; - if (layoutChanged) { - childStateChangeHandlerRegistration = lo - .addStateChangeHandler(childStateChangeHandler); - // Must handle new child here as state change events are already - // fired - onChildSizeChange(); - } - } - - // Save currently open subwindows to track which will need to be closed - final HashSet removedSubWindows = new HashSet( - getWidget().subWindows); - // Handle other UIDL children + UIDL childUidl; while ((childUidl = uidl.getChildUIDL(++childIndex)) != null) { String tag = childUidl.getTag().intern(); if (tag == "actions") { @@ -208,28 +193,9 @@ public class RootConnector extends AbstractComponentContainerConnector final UIDL notification = (UIDL) it.next(); VNotification.showNotification(client, notification); } - } else { - // subwindows - final WindowConnector w = (WindowConnector) client - .getPaintable(childUidl); - VWindow windowWidget = w.getWidget(); - if (getWidget().subWindows.contains(windowWidget)) { - removedSubWindows.remove(windowWidget); - } else { - getWidget().subWindows.add(windowWidget); - } - w.updateFromUIDL(childUidl, client); } } - // Close old windows which where not in UIDL anymore - for (final Iterator rem = removedSubWindows.iterator(); rem - .hasNext();) { - final VWindow w = rem.next(); - getWidget().subWindows.remove(w); - w.hide(); - } - if (uidl.hasAttribute("focused")) { // set focused component when render phase is finished Scheduler.get().scheduleDeferred(new Command() { @@ -356,7 +322,7 @@ public class RootConnector extends AbstractComponentContainerConnector } protected void onChildSizeChange() { - ComponentConnector child = getWidget().layout; + ComponentConnector child = (ComponentConnector) getState().getContent(); Style childStyle = child.getWidget().getElement().getStyle(); /* * Must set absolute position if the child has relative height and @@ -382,17 +348,70 @@ public class RootConnector extends AbstractComponentContainerConnector */ @Deprecated public boolean hasSubWindow(WindowConnector wc) { - return getWidget().subWindows.contains(wc.getWidget()); + return getChildren().contains(wc); + } + + /** + * Return an iterator for current subwindows. This method is meant for + * testing purposes only. + * + * @return + */ + public List getSubWindows() { + ArrayList windows = new ArrayList(); + for (ComponentConnector child : getChildren()) { + if (child instanceof WindowConnector) { + windows.add((WindowConnector) child); + } + } + return windows; + } + + @Override + public RootState getState() { + return (RootState) super.getState(); } @Override public void onConnectorHierarchyChange(ConnectorHierarchyChangeEvent event) { super.onConnectorHierarchyChange(event); + + ComponentConnector oldChild = null; + ComponentConnector newChild = (ComponentConnector) getState() + .getContent(); + + for (ComponentConnector c : event.getOldChildren()) { + if (!(c instanceof WindowConnector)) { + oldChild = c; + break; + } + } + + if (oldChild != newChild) { + if (childStateChangeHandlerRegistration != null) { + childStateChangeHandlerRegistration.removeHandler(); + childStateChangeHandlerRegistration = null; + } + getWidget().setWidget(newChild.getWidget()); + childStateChangeHandlerRegistration = newChild + .addStateChangeHandler(childStateChangeHandler); + // Must handle new child here as state change events are already + // fired + onChildSizeChange(); + } + for (ComponentConnector c : getChildren()) { if (c instanceof WindowConnector) { WindowConnector wc = (WindowConnector) c; wc.setWindowOrderAndPosition(); } } + + // Close removed sub windows + for (ComponentConnector c : event.getOldChildren()) { + if (c.getParent() != this && c instanceof WindowConnector) { + ((WindowConnector) c).getWidget().hide(); + } + } } } diff --git a/src/com/vaadin/terminal/gwt/client/ui/VView.java b/src/com/vaadin/terminal/gwt/client/ui/VView.java index 014a016410..2cf446ae41 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VView.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VView.java @@ -5,7 +5,6 @@ package com.vaadin.terminal.gwt.client.ui; import java.util.ArrayList; -import java.util.LinkedHashSet; import com.google.gwt.core.client.Scheduler; import com.google.gwt.core.client.Scheduler.ScheduledCommand; @@ -44,10 +43,6 @@ public class VView extends SimplePanel implements ResizeHandler, String theme; - ComponentConnector layout; - - final LinkedHashSet subWindows = new LinkedHashSet(); - String id; ShortcutActionHandler actionHandler; @@ -329,20 +324,6 @@ public class VView extends SimplePanel implements ResizeHandler, } }-*/; - /** - * Return an iterator for current subwindows. This method is meant for - * testing purposes only. - * - * @return - */ - public ArrayList getSubWindowList() { - ArrayList windows = new ArrayList(subWindows.size()); - for (VWindow widget : subWindows) { - windows.add(widget); - } - return windows; - } - public ShortcutActionHandler getShortcutActionHandler() { return actionHandler; } diff --git a/src/com/vaadin/ui/Root.java b/src/com/vaadin/ui/Root.java index 32c40836c0..3fcf70b922 100644 --- a/src/com/vaadin/ui/Root.java +++ b/src/com/vaadin/ui/Root.java @@ -27,10 +27,12 @@ import com.vaadin.event.MouseEvents.ClickListener; import com.vaadin.terminal.PaintException; import com.vaadin.terminal.PaintTarget; import com.vaadin.terminal.Resource; +import com.vaadin.terminal.Vaadin6Component; import com.vaadin.terminal.WrappedRequest; import com.vaadin.terminal.WrappedRequest.BrowserDetails; import com.vaadin.terminal.gwt.client.MouseEventDetails; import com.vaadin.terminal.gwt.client.ui.RootConnector.RootServerRPC; +import com.vaadin.terminal.gwt.client.ui.RootConnector.RootState; import com.vaadin.terminal.gwt.client.ui.VNotification; import com.vaadin.terminal.gwt.client.ui.VView; import com.vaadin.tools.ReflectTools; @@ -74,7 +76,7 @@ import com.vaadin.ui.Window.CloseListener; * @since 7.0 */ public abstract class Root extends AbstractComponentContainer implements - Action.Container, Action.Notifier { + Action.Container, Action.Notifier, Vaadin6Component { /** * Listener that gets notified when the size of the browser window @@ -343,11 +345,6 @@ public abstract class Root extends AbstractComponentContainer implements */ public static final int BORDER_DEFAULT = 2; - /** - * The container in which the component hierarchy of the root starts. - */ - private ComponentContainer content; - /** * The application to which this root belongs */ @@ -422,8 +419,7 @@ public abstract class Root extends AbstractComponentContainer implements * {@link VerticalLayout} with margins enabled as its content. */ public Root() { - registerRpc(rpc); - setSizeFull(); + this((ComponentContainer) null); } /** @@ -435,7 +431,8 @@ public abstract class Root extends AbstractComponentContainer implements * @see #setContent(ComponentContainer) */ public Root(ComponentContainer content) { - this(); + registerRpc(rpc); + setSizeFull(); setContent(content); } @@ -450,7 +447,7 @@ public abstract class Root extends AbstractComponentContainer implements * @see #setCaption(String) */ public Root(String caption) { - this(); + this((ComponentContainer) null); setCaption(caption); } @@ -471,6 +468,11 @@ public abstract class Root extends AbstractComponentContainer implements setCaption(caption); } + @Override + public RootState getState() { + return (RootState) super.getState(); + } + /** * Overridden to return a value instead of referring to the parent. * @@ -505,17 +507,6 @@ public abstract class Root extends AbstractComponentContainer implements } } - ComponentContainer content = getContent(); - if (content != null) { - content.paint(target); - } - - // Paint subwindows - for (final Iterator i = windows.iterator(); i.hasNext();) { - final Window w = i.next(); - w.paint(target); - } - // Paint notifications if (notifications != null) { target.startTag("notifications"); @@ -1035,10 +1026,7 @@ public abstract class Root extends AbstractComponentContainer implements * @see #createDefaultLayout() */ public ComponentContainer getContent() { - if (content == null) { - setContent(createDefaultLayout()); - } - return content; + return (ComponentContainer) getState().getContent(); } /** @@ -1066,10 +1054,14 @@ public abstract class Root extends AbstractComponentContainer implements * @see #createDefaultLayout() */ public void setContent(ComponentContainer content) { - if (this.content != null) { - super.removeComponent(this.content); + if (content == null) { + content = createDefaultLayout(); + } + + if (getState().getContent() != null) { + super.removeComponent((Component) getState().getContent()); } - this.content = content; + getState().setContent(content); if (content != null) { super.addComponent(content); } -- 2.39.5