]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fixed FormLayoutConnector to use state and hierarchy listeners
authorArtur Signell <artur@vaadin.com>
Tue, 3 Apr 2012 20:19:01 +0000 (23:19 +0300)
committerArtur Signell <artur@vaadin.com>
Wed, 4 Apr 2012 21:09:23 +0000 (00:09 +0300)
Added AbstractOrderedLayoutState for spacing info

src/com/vaadin/terminal/gwt/client/ui/AbstractOrderedLayoutConnector.java
src/com/vaadin/terminal/gwt/client/ui/FormLayoutConnector.java
src/com/vaadin/terminal/gwt/client/ui/VFormLayout.java
src/com/vaadin/ui/AbstractOrderedLayout.java

index c4c017920c420f298a8711c4e812a0af2509e094..188b66e10fa5f343fcfeec6ad499543faccf474c 100644 (file)
@@ -25,6 +25,19 @@ import com.vaadin.terminal.gwt.client.ui.layout.VLayoutSlot;
 public abstract class AbstractOrderedLayoutConnector extends
         AbstractLayoutConnector implements Paintable, DirectionalManagedLayout {
 
+    public static class AbstractOrderedLayoutState extends AbstractLayoutState {
+        private boolean spacing = true;
+
+        public boolean isSpacing() {
+            return spacing;
+        }
+
+        public void setSpacing(boolean spacing) {
+            this.spacing = spacing;
+        }
+
+    }
+
     public interface AbstractOrderedLayoutServerRPC extends LayoutClickRPC,
             ServerRpc {
 
@@ -55,6 +68,11 @@ public abstract class AbstractOrderedLayoutConnector extends
                 getWidget().spacingMeasureElement);
     }
 
+    @Override
+    public AbstractOrderedLayoutState getState() {
+        return (AbstractOrderedLayoutState) super.getState();
+    }
+
     public void updateCaption(ComponentConnector component) {
         VMeasuringOrderedLayout layout = getWidget();
         if (VCaption.isNeeded(component.getState())) {
@@ -115,7 +133,7 @@ public abstract class AbstractOrderedLayoutConnector extends
         layout.updateMarginStyleNames(new VMarginInfo(getState()
                 .getMarginsBitmask()));
 
-        layout.updateSpacingStyleName(uidl.getBooleanAttribute("spacing"));
+        layout.updateSpacingStyleName(getState().isSpacing());
 
         getLayoutManager().setNeedsUpdate(this);
     }
index 782cfd62af651bc15dd0ce55cf2262f90801a261..2b2f1292468826719a22eb4ce5f69649c1174207 100644 (file)
@@ -5,38 +5,87 @@ package com.vaadin.terminal.gwt.client.ui;
 
 import com.google.gwt.core.client.GWT;
 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.Paintable;
-import com.vaadin.terminal.gwt.client.UIDL;
+import com.vaadin.terminal.gwt.client.ConnectorHierarchyChangeEvent;
+import com.vaadin.terminal.gwt.client.communication.StateChangeEvent;
+import com.vaadin.terminal.gwt.client.ui.AbstractOrderedLayoutConnector.AbstractOrderedLayoutState;
 import com.vaadin.terminal.gwt.client.ui.VFormLayout.Caption;
 import com.vaadin.terminal.gwt.client.ui.VFormLayout.ErrorFlag;
+import com.vaadin.terminal.gwt.client.ui.VFormLayout.VFormLayoutTable;
 import com.vaadin.ui.FormLayout;
 
 @Component(FormLayout.class)
-public class FormLayoutConnector extends AbstractComponentContainerConnector
-        implements Paintable {
-    public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
-        getWidget().client = client;
+public class FormLayoutConnector extends AbstractLayoutConnector {
 
-        if (!isRealUpdate(uidl)) {
-            return;
+    @Override
+    public AbstractOrderedLayoutState getState() {
+        return (AbstractOrderedLayoutState) super.getState();
+    }
+
+    @Override
+    public void onStateChanged(StateChangeEvent stateChangeEvent) {
+        super.onStateChanged(stateChangeEvent);
+
+        VFormLayoutTable formLayoutTable = getWidget().table;
+
+        formLayoutTable.setMargins(new VMarginInfo(getState()
+                .getMarginsBitmask()));
+        formLayoutTable.setSpacing(getState().isSpacing());
+
+    }
+
+    @Override
+    public void onConnectorHierarchyChange(ConnectorHierarchyChangeEvent event) {
+        super.onConnectorHierarchyChange(event);
+
+        VFormLayout formLayout = getWidget();
+        VFormLayoutTable formLayoutTable = getWidget().table;
+
+        int childId = 0;
+
+        formLayoutTable.setRowCount(getChildren().size());
+
+        for (ComponentConnector child : getChildren()) {
+            Widget childWidget = child.getWidget();
+
+            Caption caption = formLayoutTable.getCaption(childWidget);
+            if (caption == null) {
+                caption = formLayout.new Caption(child);
+                caption.addClickHandler(formLayoutTable);
+            }
+
+            ErrorFlag error = formLayoutTable.getError(childWidget);
+            if (error == null) {
+                error = formLayout.new ErrorFlag(child);
+            }
+
+            formLayoutTable.setChild(childId, childWidget, caption, error);
+            childId++;
+        }
+
+        for (ComponentConnector oldChild : event.getOldChildren()) {
+            if (oldChild.getParent() == this) {
+                continue;
+            }
+
+            formLayoutTable.cleanReferences(oldChild.getWidget());
         }
 
-        getWidget().table.updateFromUIDL(uidl, client);
     }
 
     public void updateCaption(ComponentConnector component) {
-        final Caption c = getWidget().table.widgetToCaption.get(component
-                .getWidget());
-        if (c != null) {
-            c.updateCaption(component.getState(), component.isEnabled());
-        }
-        final ErrorFlag e = getWidget().table.widgetToError.get(component
-                .getWidget());
-        if (e != null) {
-            e.updateFromUIDL(component);
+        getWidget().table.updateCaption(component.getWidget(),
+                component.getState(), component.isEnabled());
+        boolean hideErrors = false;
+
+        // FIXME This incorrectly depends on AbstractFieldConnector
+        if (component instanceof AbstractFieldConnector) {
+            hideErrors = ((AbstractFieldConnector) component).getState()
+                    .isHideErrors();
         }
+
+        getWidget().table.updateError(component.getWidget(), component
+                .getState().getErrorMessage(), hideErrors);
     }
 
     @Override
index 7188869af16a57889602083292bcf9ead7ed7255..4a9bc8ca669f5ce264a9e9a7177a32f4a59076a3 100644 (file)
@@ -6,7 +6,6 @@ package com.vaadin.terminal.gwt.client.ui;
 
 import java.util.ArrayList;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.List;
 
 import com.google.gwt.event.dom.client.ClickEvent;
@@ -18,15 +17,12 @@ import com.google.gwt.user.client.ui.FlexTable;
 import com.google.gwt.user.client.ui.HTML;
 import com.google.gwt.user.client.ui.SimplePanel;
 import com.google.gwt.user.client.ui.Widget;
-import com.vaadin.terminal.gwt.client.AbstractFieldState;
 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.ConnectorMap;
 import com.vaadin.terminal.gwt.client.Focusable;
 import com.vaadin.terminal.gwt.client.StyleConstants;
-import com.vaadin.terminal.gwt.client.UIDL;
 import com.vaadin.terminal.gwt.client.VTooltip;
 
 /**
@@ -75,18 +71,34 @@ public class VFormLayout extends SimplePanel {
         private static final int COLUMN_ERRORFLAG = 1;
         private static final int COLUMN_WIDGET = 2;
 
-        HashMap<Widget, Caption> widgetToCaption = new HashMap<Widget, Caption>();
-        HashMap<Widget, ErrorFlag> widgetToError = new HashMap<Widget, ErrorFlag>();
+        private HashMap<Widget, Caption> widgetToCaption = new HashMap<Widget, Caption>();
+        private HashMap<Widget, ErrorFlag> widgetToError = new HashMap<Widget, ErrorFlag>();
 
         public VFormLayoutTable() {
             DOM.setElementProperty(getElement(), "cellPadding", "0");
             DOM.setElementProperty(getElement(), "cellSpacing", "0");
         }
 
-        public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
-            final VMarginInfo margins = new VMarginInfo(
-                    uidl.getIntAttribute("margins"));
+        /*
+         * (non-Javadoc)
+         * 
+         * @see
+         * com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt
+         * .event.dom.client.ClickEvent)
+         */
+        public void onClick(ClickEvent event) {
+            Caption caption = (Caption) event.getSource();
+            if (caption.getOwner() != null) {
+                if (caption.getOwner() instanceof Focusable) {
+                    ((Focusable) caption.getOwner()).focus();
+                } else if (caption.getOwner() instanceof com.google.gwt.user.client.ui.Focusable) {
+                    ((com.google.gwt.user.client.ui.Focusable) caption
+                            .getOwner()).setFocus(true);
+                }
+            }
+        }
 
+        public void setMargins(VMarginInfo margins) {
             Element margin = getElement();
             setStyleName(margin, CLASSNAME + "-" + StyleConstants.MARGIN_TOP,
                     margins.hasTop());
@@ -98,96 +110,85 @@ public class VFormLayout extends SimplePanel {
             setStyleName(margin, CLASSNAME + "-" + StyleConstants.MARGIN_LEFT,
                     margins.hasLeft());
 
-            setStyleName(margin, CLASSNAME + "-" + "spacing",
-                    uidl.hasAttribute("spacing"));
+        }
 
-            int i = 0;
-            for (final Iterator<?> it = uidl.getChildIterator(); it.hasNext(); i++) {
-                prepareCell(i, 1);
-                final UIDL childUidl = (UIDL) it.next();
-                final ComponentConnector childPaintable = client
-                        .getPaintable(childUidl);
-                Widget childWidget = childPaintable.getWidget();
-                Caption caption = widgetToCaption.get(childWidget);
-                if (caption == null) {
-                    caption = new Caption(childPaintable, client);
-                    caption.addClickHandler(this);
-                    widgetToCaption.put(childWidget, caption);
-                }
-                ErrorFlag error = widgetToError.get(childWidget);
-                if (error == null) {
-                    error = new ErrorFlag();
-                    widgetToError.put(childWidget, error);
-                }
-                prepareCell(i, COLUMN_WIDGET);
-
-                Widget oldWidget = getWidget(i, COLUMN_WIDGET);
-                if (oldWidget == null) {
-                    setWidget(i, COLUMN_WIDGET, childWidget);
-                } else if (oldWidget != childWidget) {
-                    final ComponentConnector oldPaintable = ConnectorMap.get(
-                            client).getConnector(oldWidget);
-                    client.unregisterPaintable(oldPaintable);
-                    setWidget(i, COLUMN_WIDGET, childWidget);
-                }
-                getCellFormatter().setStyleName(i, COLUMN_WIDGET,
-                        CLASSNAME + "-contentcell");
+        public void setSpacing(boolean spacing) {
+            setStyleName(getElement(), CLASSNAME + "-" + "spacing", spacing);
+
+        }
+
+        public void setRowCount(int rowNr) {
+            for (int i = 0; i < rowNr; i++) {
+                prepareCell(i, COLUMN_CAPTION);
                 getCellFormatter().setStyleName(i, COLUMN_CAPTION,
                         CLASSNAME + "-captioncell");
-                setWidget(i, COLUMN_CAPTION, caption);
 
+                prepareCell(i, 1);
                 getCellFormatter().setStyleName(i, COLUMN_ERRORFLAG,
                         CLASSNAME + "-errorcell");
-                setWidget(i, COLUMN_ERRORFLAG, error);
+
+                prepareCell(i, 2);
+                getCellFormatter().setStyleName(i, COLUMN_WIDGET,
+                        CLASSNAME + "-contentcell");
 
                 String rowstyles = CLASSNAME + "-row";
                 if (i == 0) {
                     rowstyles += " " + CLASSNAME + "-firstrow";
                 }
-                if (!it.hasNext()) {
+                if (i == rowNr - 1) {
                     rowstyles += " " + CLASSNAME + "-lastrow";
                 }
 
                 getRowFormatter().setStyleName(i, rowstyles);
 
             }
-
-            while (getRowCount() > i) {
-                Widget w = getWidget(i, COLUMN_WIDGET);
-                final ComponentConnector p = ConnectorMap.get(client)
-                        .getConnector(w);
-                client.unregisterPaintable(p);
-                widgetToCaption.remove(w);
-                removeRow(i);
+            while (getRowCount() != rowNr) {
+                removeRow(rowNr);
             }
+        }
+
+        public void setChild(int rowNr, Widget childWidget, Caption caption,
+                ErrorFlag error) {
+            setWidget(rowNr, COLUMN_WIDGET, childWidget);
+            setWidget(rowNr, COLUMN_CAPTION, caption);
+            setWidget(rowNr, COLUMN_ERRORFLAG, error);
 
-            /*
-             * Must update relative sized fields last when it is clear how much
-             * space they are allowed to use
-             */
-            for (Widget p : widgetToCaption.keySet()) {
-                client.handleComponentRelativeSize(p);
+            widgetToCaption.put(childWidget, caption);
+            widgetToError.put(childWidget, error);
+
+        }
+
+        public Caption getCaption(Widget childWidget) {
+            return widgetToCaption.get(childWidget);
+        }
+
+        public ErrorFlag getError(Widget childWidget) {
+            return widgetToError.get(childWidget);
+        }
+
+        public void cleanReferences(Widget oldChildWidget) {
+            widgetToError.remove(oldChildWidget);
+            widgetToCaption.remove(oldChildWidget);
+
+        }
+
+        public void updateCaption(Widget widget, ComponentState state,
+                boolean enabled) {
+            final Caption c = widgetToCaption.get(widget);
+            if (c != null) {
+                c.updateCaption(state, enabled);
             }
         }
 
-        /*
-         * (non-Javadoc)
-         * 
-         * @see
-         * com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt
-         * .event.dom.client.ClickEvent)
-         */
-        public void onClick(ClickEvent event) {
-            Caption caption = (Caption) event.getSource();
-            if (caption.getOwner() != null) {
-                if (caption.getOwner() instanceof Focusable) {
-                    ((Focusable) caption.getOwner()).focus();
-                } else if (caption.getOwner() instanceof com.google.gwt.user.client.ui.Focusable) {
-                    ((com.google.gwt.user.client.ui.Focusable) caption
-                            .getOwner()).setFocus(true);
-                }
+        public void updateError(Widget widget, String errorMessage,
+                boolean hideErrors) {
+            final ErrorFlag e = widgetToError.get(widget);
+            if (e != null) {
+                e.updateError(errorMessage, hideErrors);
             }
+
         }
+
     }
 
     // TODO why duplicated here?
@@ -203,19 +204,14 @@ public class VFormLayout extends SimplePanel {
 
         private Element captionText;
 
-        private final ApplicationConnection client;
-
         /**
          * 
          * @param component
          *            optional owner of caption. If not set, getOwner will
          *            return null
-         * @param client
          */
-        public Caption(ComponentConnector component,
-                ApplicationConnection client) {
+        public Caption(ComponentConnector component) {
             super();
-            this.client = client;
             owner = component;
 
             sinkEvents(VTooltip.TOOLTIP_EVENTS);
@@ -247,7 +243,7 @@ public class VFormLayout extends SimplePanel {
 
             if (state.getIcon() != null) {
                 if (icon == null) {
-                    icon = new Icon(client);
+                    icon = new Icon(owner.getConnection());
 
                     DOM.insertChild(getElement(), icon.getElement(), 0);
                 }
@@ -330,9 +326,7 @@ public class VFormLayout extends SimplePanel {
         @Override
         public void onBrowserEvent(Event event) {
             super.onBrowserEvent(event);
-            if (client != null) {
-                client.handleTooltipEvent(event, owner);
-            }
+            owner.getConnection().handleTooltipEvent(event, owner);
         }
     }
 
@@ -340,21 +334,21 @@ public class VFormLayout extends SimplePanel {
         private static final String CLASSNAME = VFormLayout.CLASSNAME
                 + "-error-indicator";
         Element errorIndicatorElement;
+
         private ComponentConnector owner;
 
-        public ErrorFlag() {
+        public ErrorFlag(ComponentConnector owner) {
             setStyleName(CLASSNAME);
             sinkEvents(VTooltip.TOOLTIP_EVENTS);
+            this.owner = owner;
         }
 
-        public void updateFromUIDL(ComponentConnector component) {
-            owner = component;
-            boolean showError = null != owner.getState().getErrorMessage();
-            if (owner.getState() instanceof AbstractFieldState) {
-                showError = showError
-                        && !((AbstractFieldState) owner.getState())
-                                .isHideErrors();
+        public void updateError(String errorMessage, boolean hideErrors) {
+            boolean showError = null != errorMessage;
+            if (hideErrors) {
+                showError = false;
             }
+
             if (showError) {
                 if (errorIndicatorElement == null) {
                     errorIndicatorElement = DOM.createDiv();
index 4a1df6c9bc78f54f2e10a6869b03cb2ad203dc36..d9115792167444d669a27bcf52096b6173cc011f 100644 (file)
@@ -18,6 +18,7 @@ import com.vaadin.terminal.Sizeable;
 import com.vaadin.terminal.gwt.client.Connector;
 import com.vaadin.terminal.gwt.client.MouseEventDetails;
 import com.vaadin.terminal.gwt.client.ui.AbstractOrderedLayoutConnector.AbstractOrderedLayoutServerRPC;
+import com.vaadin.terminal.gwt.client.ui.AbstractOrderedLayoutConnector.AbstractOrderedLayoutState;
 import com.vaadin.terminal.gwt.client.ui.LayoutClickEventHandler;
 
 @SuppressWarnings("serial")
@@ -58,6 +59,11 @@ public abstract class AbstractOrderedLayout extends AbstractLayout implements
         registerRpc(rpc);
     }
 
+    @Override
+    public AbstractOrderedLayoutState getState() {
+        return (AbstractOrderedLayoutState) super.getState();
+    }
+
     /**
      * Add a component into this container. The component is added to the right
      * or under the previous component.