From bc8914066b68e1938ac63606f205672d688be0f1 Mon Sep 17 00:00:00 2001 From: Henri Sara Date: Thu, 16 Feb 2012 16:23:33 +0200 Subject: [PATCH] Communicate component width and height only via shared state (#8304). Also removes old fix for #4608, needs to be redesigned together with layout changes. --- src/com/vaadin/terminal/gwt/client/Util.java | 24 ++++++++++------ .../terminal/gwt/client/VUIDLBrowser.java | 4 ++- .../client/ui/VAbstractPaintableWidget.java | 4 +++ .../gwt/client/ui/VEmbeddedPaintable.java | 28 +++++++++++-------- .../terminal/gwt/client/ui/VGridLayout.java | 25 ++++++++++------- .../gwt/client/ui/VGridLayoutPaintable.java | 3 +- .../gwt/client/ui/VMenuBarPaintable.java | 5 +++- .../client/ui/VOrderedLayoutPaintable.java | 5 ++-- .../gwt/client/ui/VWindowPaintable.java | 15 ++++++++-- .../ui/layout/CellBasedLayoutPaintable.java | 22 ++++++++++----- .../ui/layout/ChildComponentContainer.java | 12 -------- src/com/vaadin/ui/AbstractComponent.java | 13 ++------- 12 files changed, 94 insertions(+), 66 deletions(-) diff --git a/src/com/vaadin/terminal/gwt/client/Util.java b/src/com/vaadin/terminal/gwt/client/Util.java index 27717ad63a..591016d54d 100644 --- a/src/com/vaadin/terminal/gwt/client/Util.java +++ b/src/com/vaadin/terminal/gwt/client/Util.java @@ -32,6 +32,7 @@ import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.Widget; import com.vaadin.terminal.gwt.client.RenderInformation.FloatSize; import com.vaadin.terminal.gwt.client.communication.MethodInvocation; +import com.vaadin.terminal.gwt.client.communication.SharedState; public class Util { @@ -563,25 +564,30 @@ public class Util { } /** - * Parses the UIDL parameter and fetches the relative size of the component. - * If a dimension is not specified as relative it will return -1. If the - * UIDL does not contain width or height specifications this will return + * Parses shared state and fetches the relative size of the component. If a + * dimension is not specified as relative it will return -1. If the shared + * state does not contain width or height specifications this will return * null. * - * @param uidl + * @param state * @return */ - public static FloatSize parseRelativeSize(UIDL uidl) { + public static FloatSize parseRelativeSize(SharedState state) { + if (null == state) { + return null; + } + boolean hasAttribute = false; String w = ""; String h = ""; - if (uidl.hasAttribute("width")) { + Map stateMap = state.getState(); + if (stateMap.containsKey("width")) { hasAttribute = true; - w = uidl.getStringAttribute("width"); + w = String.valueOf(stateMap.get("width")); } - if (uidl.hasAttribute("height")) { + if (stateMap.containsKey("height")) { hasAttribute = true; - h = uidl.getStringAttribute("height"); + h = String.valueOf(stateMap.get("height")); } if (!hasAttribute) { diff --git a/src/com/vaadin/terminal/gwt/client/VUIDLBrowser.java b/src/com/vaadin/terminal/gwt/client/VUIDLBrowser.java index e63bcf98d9..e83cfcc4fe 100644 --- a/src/com/vaadin/terminal/gwt/client/VUIDLBrowser.java +++ b/src/com/vaadin/terminal/gwt/client/VUIDLBrowser.java @@ -46,7 +46,9 @@ public class VUIDLBrowser extends SimpleTree { } Set keySet = u.getKeySet(); for (String key : keySet) { - if (key.equals("changes")) { + if (key.equals("state")) { + // TODO print updated shared states + } else if (key.equals("changes")) { JsArray jsValueMapArray = u.getJSValueMapArray("changes") .cast(); for (int i = 0; i < jsValueMapArray.length(); i++) { diff --git a/src/com/vaadin/terminal/gwt/client/ui/VAbstractPaintableWidget.java b/src/com/vaadin/terminal/gwt/client/ui/VAbstractPaintableWidget.java index 3cbe8da00f..3d186d2759 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VAbstractPaintableWidget.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VAbstractPaintableWidget.java @@ -151,6 +151,10 @@ public abstract class VAbstractPaintableWidget implements VPaintableWidget { return; } + if (!isRealUpdate(uidl)) { + return; + } + /* * Disabled state may affect (override) tabindex so the order must be * first setting tabindex, then enabled state. diff --git a/src/com/vaadin/terminal/gwt/client/ui/VEmbeddedPaintable.java b/src/com/vaadin/terminal/gwt/client/ui/VEmbeddedPaintable.java index f4a3df7615..0c64b28ab1 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VEmbeddedPaintable.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VEmbeddedPaintable.java @@ -20,6 +20,7 @@ import com.google.gwt.user.client.Element; 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.ComponentState; import com.vaadin.terminal.gwt.client.UIDL; import com.vaadin.terminal.gwt.client.VConsole; import com.vaadin.terminal.gwt.client.VTooltip; @@ -69,18 +70,23 @@ public class VEmbeddedPaintable extends VAbstractPaintableWidget { // Set attributes Style style = el.getStyle(); - String w = uidl.getStringAttribute("width"); - if (w != null) { - style.setProperty("width", w); - } else { - style.setProperty("width", ""); - } - String h = uidl.getStringAttribute("height"); - if (h != null) { - style.setProperty("height", h); - } else { - style.setProperty("height", ""); + String w = ""; + String h = ""; + if (null != getState()) { + if (getState().getState().containsKey( + ComponentState.STATE_WIDTH)) { + w = String.valueOf(getState().getState().get( + ComponentState.STATE_WIDTH)); + } + if (getState().getState().containsKey( + ComponentState.STATE_HEIGHT)) { + h = String.valueOf(getState().getState().get( + ComponentState.STATE_HEIGHT)); + } } + style.setProperty("width", w); + style.setProperty("height", h); + DOM.setElementProperty(el, "src", getWidgetForPaintable() .getSrc(uidl, client)); diff --git a/src/com/vaadin/terminal/gwt/client/ui/VGridLayout.java b/src/com/vaadin/terminal/gwt/client/ui/VGridLayout.java index 5302fa6f67..f894b49d87 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VGridLayout.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VGridLayout.java @@ -26,6 +26,7 @@ import com.vaadin.terminal.gwt.client.UIDL; import com.vaadin.terminal.gwt.client.Util; import com.vaadin.terminal.gwt.client.VPaintableMap; import com.vaadin.terminal.gwt.client.VPaintableWidget; +import com.vaadin.terminal.gwt.client.communication.SharedState; import com.vaadin.terminal.gwt.client.ui.layout.CellBasedLayout; import com.vaadin.terminal.gwt.client.ui.layout.ChildComponentContainer; @@ -928,25 +929,29 @@ public class VGridLayout extends SimplePanel implements Container { } } childUidl = c; - updateRelSizeStatus(c); + updateRelSizeStatus(client.getPaintable(c).getState(), + c.getBooleanAttribute("cached")); } - protected void updateRelSizeStatus(UIDL uidl) { - if (uidl != null && !uidl.getBooleanAttribute("cached")) { - if (uidl.hasAttribute("height") - && uidl.getStringAttribute("height").contains("%")) { + protected void updateRelSizeStatus(SharedState state, boolean cached) { + if (state != null && !cached) { + boolean widthDefined = state.getState().containsKey("width"); + boolean heightDefined = state.getState().containsKey("height"); + if (heightDefined + && String.valueOf(state.getState().get("height")) + .contains("%")) { relHeight = true; } else { relHeight = false; } - if (uidl.hasAttribute("width")) { - widthCanAffectHeight = relWidth = uidl.getStringAttribute( - "width").contains("%"); - if (uidl.hasAttribute("height")) { + if (widthDefined) { + widthCanAffectHeight = relWidth = String.valueOf( + state.getState().get("width")).contains("%"); + if (heightDefined) { widthCanAffectHeight = false; } } else { - widthCanAffectHeight = !uidl.hasAttribute("height"); + widthCanAffectHeight = !heightDefined; relWidth = false; } } diff --git a/src/com/vaadin/terminal/gwt/client/ui/VGridLayoutPaintable.java b/src/com/vaadin/terminal/gwt/client/ui/VGridLayoutPaintable.java index c7e9f79d9a..0df932a45b 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VGridLayoutPaintable.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VGridLayoutPaintable.java @@ -175,8 +175,9 @@ public class VGridLayoutPaintable extends VAbstractPaintableWidgetContainer { } if (!getWidgetForPaintable().rendering) { // ensure rel size details are updated + boolean cached = uidl.getBooleanAttribute("cached"); getWidgetForPaintable().widgetToCell.get(widget) - .updateRelSizeStatus(uidl); + .updateRelSizeStatus(paintable.getState(), cached); /* * This was a component-only update and the possible size change * must be propagated to the layout diff --git a/src/com/vaadin/terminal/gwt/client/ui/VMenuBarPaintable.java b/src/com/vaadin/terminal/gwt/client/ui/VMenuBarPaintable.java index f3e4609540..4b2fdff819 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VMenuBarPaintable.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VMenuBarPaintable.java @@ -10,6 +10,7 @@ import com.google.gwt.core.client.GWT; import com.google.gwt.user.client.Command; import com.google.gwt.user.client.ui.Widget; import com.vaadin.terminal.gwt.client.ApplicationConnection; +import com.vaadin.terminal.gwt.client.ComponentState; import com.vaadin.terminal.gwt.client.UIDL; import com.vaadin.terminal.gwt.client.Util; import com.vaadin.terminal.gwt.client.ui.VMenuBar.CustomMenuItem; @@ -50,7 +51,9 @@ public class VMenuBarPaintable extends VAbstractPaintableWidget { UIDL options = uidl.getChildUIDL(0); - if (uidl.hasAttribute("width")) { + if (null != getState() + && getState().getState() + .containsKey(ComponentState.STATE_WIDTH)) { UIDL moreItemUIDL = options.getChildUIDL(0); StringBuffer itemHTML = new StringBuffer(); diff --git a/src/com/vaadin/terminal/gwt/client/ui/VOrderedLayoutPaintable.java b/src/com/vaadin/terminal/gwt/client/ui/VOrderedLayoutPaintable.java index 0d9f5c6263..58d93fccc6 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VOrderedLayoutPaintable.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VOrderedLayoutPaintable.java @@ -105,8 +105,9 @@ public abstract class VOrderedLayoutPaintable extends CellBasedLayoutPaintable { * the layout are rendered later when it is clear how much space * they can use */ - if (isRealUpdate(childUIDL)) { - FloatSize relativeSize = Util.parseRelativeSize(childUIDL); + if (null != childPaintable.getState()) { + FloatSize relativeSize = Util.parseRelativeSize(childPaintable + .getState()); childComponentContainer.setRelativeSize(relativeSize); } diff --git a/src/com/vaadin/terminal/gwt/client/ui/VWindowPaintable.java b/src/com/vaadin/terminal/gwt/client/ui/VWindowPaintable.java index e77aefc25b..afcd95a48b 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VWindowPaintable.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VWindowPaintable.java @@ -3,6 +3,8 @@ */ package com.vaadin.terminal.gwt.client.ui; +import java.util.Map; + import com.google.gwt.core.client.GWT; import com.google.gwt.event.dom.client.DomEvent.Type; import com.google.gwt.event.shared.EventHandler; @@ -13,6 +15,7 @@ import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.Frame; import com.google.gwt.user.client.ui.Widget; import com.vaadin.terminal.gwt.client.ApplicationConnection; +import com.vaadin.terminal.gwt.client.ComponentState; import com.vaadin.terminal.gwt.client.UIDL; import com.vaadin.terminal.gwt.client.Util; import com.vaadin.terminal.gwt.client.VPaintableWidget; @@ -151,8 +154,16 @@ public class VWindowPaintable extends VAbstractPaintableWidgetContainer getWidgetForPaintable().layout = lo; } - getWidgetForPaintable().dynamicWidth = !uidl.hasAttribute("width"); - getWidgetForPaintable().dynamicHeight = !uidl.hasAttribute("height"); + if (null != getState()) { + Map state = getState().getState(); + getWidgetForPaintable().dynamicWidth = !state + .containsKey(ComponentState.STATE_WIDTH); + getWidgetForPaintable().dynamicHeight = !state + .containsKey(ComponentState.STATE_HEIGHT); + } else { + getWidgetForPaintable().dynamicWidth = true; + getWidgetForPaintable().dynamicHeight = true; + } getWidgetForPaintable().layoutRelativeWidth = uidl .hasAttribute("layoutRelativeWidth"); diff --git a/src/com/vaadin/terminal/gwt/client/ui/layout/CellBasedLayoutPaintable.java b/src/com/vaadin/terminal/gwt/client/ui/layout/CellBasedLayoutPaintable.java index 3a5030e606..819ac90cdf 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/layout/CellBasedLayoutPaintable.java +++ b/src/com/vaadin/terminal/gwt/client/ui/layout/CellBasedLayoutPaintable.java @@ -4,6 +4,7 @@ package com.vaadin.terminal.gwt.client.ui.layout; import com.vaadin.terminal.gwt.client.ApplicationConnection; +import com.vaadin.terminal.gwt.client.ComponentState; import com.vaadin.terminal.gwt.client.UIDL; import com.vaadin.terminal.gwt.client.ui.VAbstractPaintableWidgetContainer; import com.vaadin.terminal.gwt.client.ui.VMarginInfo; @@ -31,16 +32,23 @@ public abstract class CellBasedLayoutPaintable extends super.updateFromUIDL(uidl, client); if (isRealUpdate(uidl)) { - handleDynamicDimensions(uidl); + handleDynamicDimensions(); } } - private void handleDynamicDimensions(UIDL uidl) { - String w = uidl.hasAttribute("width") ? uidl - .getStringAttribute("width") : ""; - - String h = uidl.hasAttribute("height") ? uidl - .getStringAttribute("height") : ""; + private void handleDynamicDimensions() { + String w = ""; + String h = ""; + if (null != getState()) { + if (getState().getState().containsKey(ComponentState.STATE_WIDTH)) { + w = String.valueOf(getState().getState().get( + ComponentState.STATE_WIDTH)); + } + if (getState().getState().containsKey(ComponentState.STATE_HEIGHT)) { + h = String.valueOf(getState().getState().get( + ComponentState.STATE_HEIGHT)); + } + } if (w.equals("")) { getWidgetForPaintable().dynamicWidth = true; diff --git a/src/com/vaadin/terminal/gwt/client/ui/layout/ChildComponentContainer.java b/src/com/vaadin/terminal/gwt/client/ui/layout/ChildComponentContainer.java index 92464cd459..3cba88b30d 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/layout/ChildComponentContainer.java +++ b/src/com/vaadin/terminal/gwt/client/ui/layout/ChildComponentContainer.java @@ -438,18 +438,6 @@ public class ChildComponentContainer extends Panel { } updateCaptionSize(); - - if (relativeSize == null) { - /* - * relativeSize may be null if component is updated via independent - * update, after it has initially been hidden. See #4608 - * - * It might also change in which case there would be similar issues. - * - * Yes, it is an ugly hack. Don't come telling me about it. - */ - setRelativeSize(Util.parseRelativeSize(uidl)); - } } public void updateCaptionSize() { diff --git a/src/com/vaadin/ui/AbstractComponent.java b/src/com/vaadin/ui/AbstractComponent.java index 8d9e3be932..122a6bd470 100644 --- a/src/com/vaadin/ui/AbstractComponent.java +++ b/src/com/vaadin/ui/AbstractComponent.java @@ -768,17 +768,8 @@ public abstract class AbstractComponent implements Component, MethodEventSource // Only paint content of visible components. if (isVisible()) { - if (getHeight() >= 0 - && (getHeightUnits() != Unit.PERCENTAGE || ComponentSizeValidator - .parentCanDefineHeight(this))) { - target.addAttribute("height", "" + getCSSHeight()); - } + // width and height are only in shared state - if (getWidth() >= 0 - && (getWidthUnits() != Unit.PERCENTAGE || ComponentSizeValidator - .parentCanDefineWidth(this))) { - target.addAttribute("width", "" + getCSSWidth()); - } if (styles != null && styles.size() > 0) { target.addAttribute("style", getStyle()); } @@ -902,6 +893,8 @@ public abstract class AbstractComponent implements Component, MethodEventSource state.put(ComponentState.STATE_WIDTH, "" + getCSSWidth()); } + // TODO use the rest on the client side + if (styles != null && styles.size() > 0) { state.put(ComponentState.STATE_STYLE, getStyle()); } -- 2.39.5