]> source.dussan.org Git - vaadin-framework.git/commitdiff
#8500 Made SplitPanel use hierarchy change events
authorArtur Signell <artur@vaadin.com>
Tue, 13 Mar 2012 18:41:56 +0000 (20:41 +0200)
committerArtur Signell <artur@vaadin.com>
Wed, 14 Mar 2012 14:00:58 +0000 (16:00 +0200)
src/com/vaadin/terminal/gwt/client/ui/AbstractSplitPanelConnector.java
src/com/vaadin/ui/AbstractSplitPanel.java

index 54e9763650339cc44fa0b11cb527613c810a4fa4..3363aa792bf1ccf9455e1ec76230089c0baef4a1 100644 (file)
@@ -5,6 +5,7 @@ package com.vaadin.terminal.gwt.client.ui;
 
 import java.util.LinkedList;
 
+import com.google.gwt.core.client.GWT;
 import com.google.gwt.dom.client.NativeEvent;
 import com.google.gwt.event.dom.client.DomEvent.Type;
 import com.google.gwt.event.shared.EventHandler;
@@ -14,12 +15,44 @@ import com.google.gwt.user.client.Event;
 import com.google.gwt.user.client.ui.Widget;
 import com.vaadin.terminal.gwt.client.ApplicationConnection;
 import com.vaadin.terminal.gwt.client.ComponentConnector;
-import com.vaadin.terminal.gwt.client.ConnectorMap;
+import com.vaadin.terminal.gwt.client.ComponentState;
+import com.vaadin.terminal.gwt.client.Connector;
+import com.vaadin.terminal.gwt.client.ConnectorHierarchyChangedEvent;
 import com.vaadin.terminal.gwt.client.UIDL;
 
 public abstract class AbstractSplitPanelConnector extends
         AbstractComponentContainerConnector implements SimpleManagedLayout {
 
+    public static class AbstractSplitPanelState extends ComponentState {
+        private Connector firstChild = null;
+        private Connector secondChild = null;
+
+        public boolean hasFirstChild() {
+            return firstChild != null;
+        }
+
+        public boolean hasSecondChild() {
+            return secondChild != null;
+        }
+
+        public Connector getFirstChild() {
+            return firstChild;
+        }
+
+        public void setFirstChild(Connector firstChild) {
+            this.firstChild = firstChild;
+        }
+
+        public Connector getSecondChild() {
+            return secondChild;
+        }
+
+        public void setSecondChild(Connector secondChild) {
+            this.secondChild = secondChild;
+        }
+
+    }
+
     public static final String SPLITTER_CLICK_EVENT_IDENTIFIER = "sp_click";
 
     public void updateCaption(ComponentConnector component, UIDL uidl) {
@@ -94,29 +127,10 @@ public abstract class AbstractSplitPanelConnector extends
 
         getWidget().position = uidl.getStringAttribute("position");
 
-        final ComponentConnector newFirstChildPaintable = client
-                .getPaintable(uidl.getChildUIDL(0));
-        final ComponentConnector newSecondChildPaintable = client
-                .getPaintable(uidl.getChildUIDL(1));
-        Widget newFirstChild = newFirstChildPaintable.getWidget();
-        Widget newSecondChild = newSecondChildPaintable.getWidget();
-
-        if (getWidget().firstChild != newFirstChild) {
-            if (getWidget().firstChild != null) {
-                client.unregisterPaintable(ConnectorMap.get(client)
-                        .getConnector(getWidget().firstChild));
-            }
-            getWidget().setFirstWidget(newFirstChild);
-        }
-        if (getWidget().secondChild != newSecondChild) {
-            if (getWidget().secondChild != null) {
-                client.unregisterPaintable(ConnectorMap.get(client)
-                        .getConnector(getWidget().secondChild));
-            }
-            getWidget().setSecondWidget(newSecondChild);
+        for (int childIndex = 0; childIndex < getChildren().size(); childIndex++) {
+            getChildren().get(childIndex).updateFromUIDL(
+                    uidl.getChildUIDL(childIndex++), client);
         }
-        newFirstChildPaintable.updateFromUIDL(uidl.getChildUIDL(0), client);
-        newSecondChildPaintable.updateFromUIDL(uidl.getChildUIDL(1), client);
 
         // This is needed at least for cases like #3458 to take
         // appearing/disappearing scrollbars into account.
@@ -139,4 +153,38 @@ public abstract class AbstractSplitPanelConnector extends
     @Override
     protected abstract VAbstractSplitPanel createWidget();
 
+    @Override
+    public AbstractSplitPanelState getState() {
+        return (AbstractSplitPanelState) super.getState();
+    }
+
+    @Override
+    protected AbstractSplitPanelState createState() {
+        return GWT.create(AbstractSplitPanelState.class);
+    }
+
+    private ComponentConnector getFirstChild() {
+        return (ComponentConnector) getState().getFirstChild();
+    }
+
+    private ComponentConnector getSecondChild() {
+        return (ComponentConnector) getState().getSecondChild();
+    }
+
+    @Override
+    public void connectorHierarchyChanged(ConnectorHierarchyChangedEvent event) {
+        super.connectorHierarchyChanged(event);
+
+        Widget newFirstChildWidget = null;
+        if (getFirstChild() != null) {
+            newFirstChildWidget = getFirstChild().getWidget();
+        }
+        getWidget().setFirstWidget(newFirstChildWidget);
+
+        Widget newSecondChildWidget = null;
+        if (getSecondChild() != null) {
+            newSecondChildWidget = getSecondChild().getWidget();
+        }
+        getWidget().setSecondWidget(newSecondChildWidget);
+    }
 }
index 6cbbcaf2e4f647d8a518be162af1c1b4ed350985..f8f6381dae21e27e2ec90c9eb39e25c5dafac05e 100644 (file)
@@ -14,8 +14,10 @@ import com.vaadin.event.MouseEvents.ClickEvent;
 import com.vaadin.terminal.PaintException;
 import com.vaadin.terminal.PaintTarget;
 import com.vaadin.terminal.Sizeable;
+import com.vaadin.terminal.gwt.client.ComponentState;
 import com.vaadin.terminal.gwt.client.MouseEventDetails;
 import com.vaadin.terminal.gwt.client.ui.AbstractSplitPanelConnector;
+import com.vaadin.terminal.gwt.client.ui.AbstractSplitPanelConnector.AbstractSplitPanelState;
 import com.vaadin.tools.ReflectTools;
 
 /**
@@ -31,10 +33,6 @@ import com.vaadin.tools.ReflectTools;
  */
 public abstract class AbstractSplitPanel extends AbstractLayout {
 
-    private Component firstComponent;
-
-    private Component secondComponent;
-
     private int pos = 50;
 
     private Unit posUnit = Unit.PERCENTAGE;
@@ -66,18 +64,19 @@ public abstract class AbstractSplitPanel extends AbstractLayout {
                 return null;
             }
             i++;
+            AbstractSplitPanelState state = getState();
             if (i == 1) {
-                return firstComponent == null ? secondComponent
-                        : firstComponent;
+                return (getFirstComponent() == null ? getSecondComponent()
+                        : getFirstComponent());
             } else if (i == 2) {
-                return secondComponent;
+                return (Component) state.getSecondChild();
             }
             return null;
         }
 
         public void remove() {
             if (i == 1) {
-                if (firstComponent != null) {
+                if (getFirstComponent() != null) {
                     setFirstComponent(null);
                     i = 0;
                 } else {
@@ -98,60 +97,83 @@ public abstract class AbstractSplitPanel extends AbstractLayout {
      */
     @Override
     public void addComponent(Component c) {
-        if (firstComponent == null) {
-            firstComponent = c;
-        } else if (secondComponent == null) {
-            secondComponent = c;
+        if (getFirstComponent() == null) {
+            setFirstComponent(c);
+        } else if (getSecondComponent() == null) {
+            setSecondComponent(c);
         } else {
             throw new UnsupportedOperationException(
                     "Split panel can contain only two components");
         }
-        super.addComponent(c);
-        requestRepaint();
     }
 
+    /**
+     * Sets the first component of this split panel. Depending on the direction
+     * the first component is shown at the top or to the left.
+     * 
+     * @param c
+     *            The component to use as first component
+     */
     public void setFirstComponent(Component c) {
-        if (firstComponent == c) {
+        if (getFirstComponent() == c) {
             // Nothing to do
             return;
         }
 
-        if (firstComponent != null) {
+        if (getFirstComponent() != null) {
             // detach old
-            removeComponent(firstComponent);
+            removeComponent(getFirstComponent());
+        }
+        getState().setFirstChild(c);
+        if (c != null) {
+            super.addComponent(c);
         }
-        firstComponent = c;
-        super.addComponent(c);
+
         requestRepaint();
     }
 
+    /**
+     * Sets the second component of this split panel. Depending on the direction
+     * the second component is shown at the bottom or to the left.
+     * 
+     * @param c
+     *            The component to use as first component
+     */
     public void setSecondComponent(Component c) {
-        if (c == secondComponent) {
+        if (getSecondComponent() == c) {
             // Nothing to do
             return;
         }
 
-        if (secondComponent != null) {
+        if (getSecondComponent() != null) {
             // detach old
-            removeComponent(secondComponent);
+            removeComponent(getSecondComponent());
+        }
+        getState().setSecondChild(c);
+        if (c != null) {
+            super.addComponent(c);
         }
-        secondComponent = c;
-        super.addComponent(c);
         requestRepaint();
     }
 
     /**
-     * @return the first component of this SplitPanel.
+     * Gets the first component of this split panel. Depending on the direction
+     * this is either the component shown at the top or to the left.
+     * 
+     * @return the first component of this split panel
      */
     public Component getFirstComponent() {
-        return firstComponent;
+        return (Component) getState().getFirstChild();
     }
 
     /**
-     * @return the second component of this SplitPanel.
+     * Gets the second component of this split panel. Depending on the direction
+     * this is either the component shown at the top or to the left.
+     * 
+     * @return the second component of this split panel
      */
     public Component getSecondComponent() {
-        return secondComponent;
+        return (Component) getState().getSecondChild();
     }
 
     /**
@@ -163,10 +185,10 @@ public abstract class AbstractSplitPanel extends AbstractLayout {
     @Override
     public void removeComponent(Component c) {
         super.removeComponent(c);
-        if (c == firstComponent) {
-            firstComponent = null;
-        } else if (c == secondComponent) {
-            secondComponent = null;
+        if (c == getFirstComponent()) {
+            getState().setFirstChild(null);
+        } else if (c == getSecondComponent()) {
+            getState().setSecondChild(null);
         }
         requestRepaint();
     }
@@ -188,10 +210,10 @@ public abstract class AbstractSplitPanel extends AbstractLayout {
      */
     public int getComponentCount() {
         int count = 0;
-        if (firstComponent != null) {
+        if (getFirstComponent() != null) {
             count++;
         }
-        if (secondComponent != null) {
+        if (getSecondComponent() != null) {
             count++;
         }
         return count;
@@ -219,27 +241,19 @@ public abstract class AbstractSplitPanel extends AbstractLayout {
 
         target.addAttribute("reversed", posReversed);
 
-        if (firstComponent != null) {
-            firstComponent.paint(target);
-        } else {
-            VerticalLayout temporaryComponent = new VerticalLayout();
-            temporaryComponent.setParent(this);
-            temporaryComponent.paint(target);
+        if (getFirstComponent() != null) {
+            getFirstComponent().paint(target);
         }
-        if (secondComponent != null) {
-            secondComponent.paint(target);
-        } else {
-            VerticalLayout temporaryComponent = new VerticalLayout();
-            temporaryComponent.setParent(this);
-            temporaryComponent.paint(target);
+        if (getSecondComponent() != null) {
+            getSecondComponent().paint(target);
         }
     }
 
     /* Documented in superclass */
     public void replaceComponent(Component oldComponent, Component newComponent) {
-        if (oldComponent == firstComponent) {
+        if (oldComponent == getFirstComponent()) {
             setFirstComponent(newComponent);
-        } else if (oldComponent == secondComponent) {
+        } else if (oldComponent == getSecondComponent()) {
             setSecondComponent(newComponent);
         }
         requestRepaint();
@@ -435,4 +449,13 @@ public abstract class AbstractSplitPanel extends AbstractLayout {
         removeListener(SPLITTER_CLICK_EVENT, SplitterClickEvent.class, listener);
     }
 
+    @Override
+    public AbstractSplitPanelState getState() {
+        return (AbstractSplitPanelState) super.getState();
+    }
+
+    @Override
+    protected ComponentState createState() {
+        return new AbstractSplitPanelState();
+    }
 }