From 276490d6bdc0e0e834841a65c6236ea6e7acdc5e Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Tue, 3 Apr 2012 23:19:01 +0300 Subject: [PATCH] Fixed FormLayoutConnector to use state and hierarchy listeners Added AbstractOrderedLayoutState for spacing info --- .../ui/AbstractOrderedLayoutConnector.java | 20 +- .../gwt/client/ui/FormLayoutConnector.java | 87 +++++++-- .../terminal/gwt/client/ui/VFormLayout.java | 180 +++++++++--------- src/com/vaadin/ui/AbstractOrderedLayout.java | 6 + 4 files changed, 180 insertions(+), 113 deletions(-) diff --git a/src/com/vaadin/terminal/gwt/client/ui/AbstractOrderedLayoutConnector.java b/src/com/vaadin/terminal/gwt/client/ui/AbstractOrderedLayoutConnector.java index c4c017920c..188b66e10f 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/AbstractOrderedLayoutConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/AbstractOrderedLayoutConnector.java @@ -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); } diff --git a/src/com/vaadin/terminal/gwt/client/ui/FormLayoutConnector.java b/src/com/vaadin/terminal/gwt/client/ui/FormLayoutConnector.java index 782cfd62af..2b2f129246 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/FormLayoutConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/FormLayoutConnector.java @@ -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 diff --git a/src/com/vaadin/terminal/gwt/client/ui/VFormLayout.java b/src/com/vaadin/terminal/gwt/client/ui/VFormLayout.java index 7188869af1..4a9bc8ca66 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VFormLayout.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VFormLayout.java @@ -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 widgetToCaption = new HashMap(); - HashMap widgetToError = new HashMap(); + private HashMap widgetToCaption = new HashMap(); + private HashMap widgetToError = new HashMap(); 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(); diff --git a/src/com/vaadin/ui/AbstractOrderedLayout.java b/src/com/vaadin/ui/AbstractOrderedLayout.java index 4a1df6c9bc..d911579216 100644 --- a/src/com/vaadin/ui/AbstractOrderedLayout.java +++ b/src/com/vaadin/ui/AbstractOrderedLayout.java @@ -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. -- 2.39.5