From 6dae5cd35f3811146d3cf8a8d67baaa637d9af4c Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Tue, 21 Feb 2012 15:27:42 +0200 Subject: [PATCH] #8304 First steps towards supporting serialization of any bean --- .../gwt/client/ApplicationConnection.java | 38 ++++++++----------- .../terminal/gwt/client/ComponentState.java | 35 ++++++++++++++++- .../gwt/client/ComponentState_Serializer.java | 28 ++++++++++++++ src/com/vaadin/terminal/gwt/client/Util.java | 26 ++----------- .../terminal/gwt/client/VPaintable.java | 14 ++++--- .../terminal/gwt/client/VPaintableWidget.java | 7 ++++ .../gwt/client/communication/JsonDecoder.java | 33 +++++++++++++--- .../gwt/client/communication/SharedState.java | 13 ------- .../communication/VaadinSerializer.java | 11 ++++++ .../client/ui/VAbstractPaintableWidget.java | 19 +++++++--- .../terminal/gwt/client/ui/VButtonState.java | 16 ++++++++ .../client/ui/VButtonState_Serializer.java | 29 ++++++++++++++ .../gwt/client/ui/VEmbeddedPaintable.java | 22 ++--------- .../terminal/gwt/client/ui/VGridLayout.java | 16 ++++---- .../gwt/client/ui/VMenuBarPaintable.java | 5 +-- .../gwt/client/ui/VWindowPaintable.java | 15 +------- .../ui/layout/CellBasedLayoutPaintable.java | 28 +------------- .../vaadin/terminal/gwt/server/JsonCodec.java | 31 +++++++++++++-- src/com/vaadin/ui/AbstractComponent.java | 10 ++--- src/com/vaadin/ui/Button.java | 20 +++++++--- 20 files changed, 255 insertions(+), 161 deletions(-) create mode 100644 src/com/vaadin/terminal/gwt/client/ComponentState_Serializer.java create mode 100644 src/com/vaadin/terminal/gwt/client/communication/VaadinSerializer.java create mode 100644 src/com/vaadin/terminal/gwt/client/ui/VButtonState.java create mode 100644 src/com/vaadin/terminal/gwt/client/ui/VButtonState_Serializer.java diff --git a/src/com/vaadin/terminal/gwt/client/ApplicationConnection.java b/src/com/vaadin/terminal/gwt/client/ApplicationConnection.java index c8a676d6fa..1737f82e55 100644 --- a/src/com/vaadin/terminal/gwt/client/ApplicationConnection.java +++ b/src/com/vaadin/terminal/gwt/client/ApplicationConnection.java @@ -1034,17 +1034,14 @@ public class ApplicationConnection { VPaintable paintable = paintableMap .getPaintable(paintableId); if (null != paintable) { - // TODO handle as a ValueMap or similar object and - // native JavaScript processing? - JavaScriptObject value = states - .getJavaScriptObject(paintableId); - // TODO implement with shared state subclasses - SharedState state = GWT.create(SharedState.class); - Map stateMap = (Map) JsonDecoder - .convertValue(new JSONArray(value), - getPaintableMap()); - state.setState(stateMap); - paintable.updateState(state); + + JSONArray stateDataAndType = new JSONArray( + states.getJavaScriptObject(paintableId)); + + Object state = JsonDecoder.deserialize( + stateDataAndType, paintableMap); + + paintable.setState((SharedState) state); } } catch (final Throwable e) { VConsole.error(e); @@ -1597,19 +1594,14 @@ public class ApplicationConnection { SharedState state = paintable.getState(); String w = ""; String h = ""; - if (null != state) { + if (null != state || !(state instanceof ComponentState)) { + ComponentState componentState = (ComponentState) state; // TODO move logging to VUIDLBrowser and VDebugConsole - VConsole.log("Paintable state for " - + getPaintableMap().getPid(paintable) + ": " - + String.valueOf(state.getState())); - if (state.getState().containsKey(ComponentState.STATE_WIDTH)) { - w = String.valueOf(state.getState().get( - ComponentState.STATE_WIDTH)); - } - if (state.getState().containsKey(ComponentState.STATE_HEIGHT)) { - h = String.valueOf(state.getState().get( - ComponentState.STATE_HEIGHT)); - } + // VConsole.log("Paintable state for " + // + getPaintableMap().getPid(paintable) + ": " + // + String.valueOf(state.getState())); + h = componentState.getHeight(); + w = componentState.getWidth(); } else { // TODO move logging to VUIDLBrowser and VDebugConsole VConsole.log("No state for paintable " diff --git a/src/com/vaadin/terminal/gwt/client/ComponentState.java b/src/com/vaadin/terminal/gwt/client/ComponentState.java index 955833e48f..14b5e31891 100644 --- a/src/com/vaadin/terminal/gwt/client/ComponentState.java +++ b/src/com/vaadin/terminal/gwt/client/ComponentState.java @@ -14,11 +14,42 @@ import com.vaadin.terminal.gwt.client.communication.SharedState; * @since 7.0 */ public class ComponentState extends SharedState { + private String height = ""; + private String width = ""; + // TODO more javadoc + public String getHeight() { + if (height == null) { + return ""; + } + return height; + } + + public void setHeight(String height) { + this.height = height; + } + + public boolean isUndefinedHeight() { + return "".equals(getHeight()); + } + + public String getWidth() { + if (width == null) { + return ""; + } + return width; + } + + public void setWidth(String width) { + this.width = width; + } + + public boolean isUndefinedWidth() { + return "".equals(getWidth()); + } + // TODO constants for the state attributes for now - public static final String STATE_HEIGHT = "height"; - public static final String STATE_WIDTH = "width"; public static final String STATE_STYLE = "style"; public static final String STATE_READONLY = "readonly"; public static final String STATE_IMMEDIATE = "immediate"; diff --git a/src/com/vaadin/terminal/gwt/client/ComponentState_Serializer.java b/src/com/vaadin/terminal/gwt/client/ComponentState_Serializer.java new file mode 100644 index 0000000000..075b7f9daa --- /dev/null +++ b/src/com/vaadin/terminal/gwt/client/ComponentState_Serializer.java @@ -0,0 +1,28 @@ +package com.vaadin.terminal.gwt.client; + +import com.google.gwt.core.client.GWT; +import com.google.gwt.json.client.JSONArray; +import com.google.gwt.json.client.JSONObject; +import com.vaadin.terminal.gwt.client.communication.JsonDecoder; +import com.vaadin.terminal.gwt.client.communication.VaadinSerializer; + +//TODO This should be autogenerated +public class ComponentState_Serializer implements VaadinSerializer { + + public ComponentState deserialize(JSONObject jsonValue, + VPaintableMap idMapper) { + ComponentState state = GWT.create(ComponentState.class); + + // For (Field f : fields) { + // state.setState((Map) + // JsonDecoder.convertMap(jsonValue, + // idMapper)); + JSONArray jsonHeight = (JSONArray) jsonValue.get("height"); + state.setHeight((String) JsonDecoder.convertValue(jsonHeight, idMapper)); + + JSONArray jsonWidth = (JSONArray) jsonValue.get("width"); + state.setWidth((String) JsonDecoder.convertValue(jsonWidth, idMapper)); + + return state; + } +} diff --git a/src/com/vaadin/terminal/gwt/client/Util.java b/src/com/vaadin/terminal/gwt/client/Util.java index 0c4181066a..be78959830 100644 --- a/src/com/vaadin/terminal/gwt/client/Util.java +++ b/src/com/vaadin/terminal/gwt/client/Util.java @@ -32,7 +32,6 @@ 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 { @@ -572,30 +571,13 @@ public class Util { * @param state * @return */ - public static FloatSize parseRelativeSize(SharedState state) { - if (null == state) { + public static FloatSize parseRelativeSize(ComponentState state) { + if (state.isUndefinedHeight() && state.isUndefinedWidth()) { return null; } - boolean hasAttribute = false; - String w = ""; - String h = ""; - Map stateMap = state.getState(); - if (stateMap.containsKey("width")) { - hasAttribute = true; - w = String.valueOf(stateMap.get("width")); - } - if (stateMap.containsKey("height")) { - hasAttribute = true; - h = String.valueOf(stateMap.get("height")); - } - - if (!hasAttribute) { - return null; - } - - float relativeWidth = Util.parseRelativeSize(w); - float relativeHeight = Util.parseRelativeSize(h); + float relativeWidth = Util.parseRelativeSize(state.getWidth()); + float relativeHeight = Util.parseRelativeSize(state.getHeight()); FloatSize relativeSize = new FloatSize(relativeWidth, relativeHeight); return relativeSize; diff --git a/src/com/vaadin/terminal/gwt/client/VPaintable.java b/src/com/vaadin/terminal/gwt/client/VPaintable.java index 666b620d82..9d1dd0fdbc 100644 --- a/src/com/vaadin/terminal/gwt/client/VPaintable.java +++ b/src/com/vaadin/terminal/gwt/client/VPaintable.java @@ -24,18 +24,20 @@ public interface VPaintable { public void updateFromUIDL(UIDL uidl, ApplicationConnection client); /** - * Sets the shared state for the paintable. + * Gets the current shared state of the paintable. * - * @param state + * @return state */ - public void updateState(SharedState state); + public SharedState getState(); /** - * Gets the current shared state of the paintable. + * Sets a new state for the paintable. + * + * @param state + * The new state * - * @return state */ - public SharedState getState(); + public void setState(SharedState state); /** * Returns the id for this VPaintable. This must always be what has been set diff --git a/src/com/vaadin/terminal/gwt/client/VPaintableWidget.java b/src/com/vaadin/terminal/gwt/client/VPaintableWidget.java index 4b8720b132..bb944ae4d6 100644 --- a/src/com/vaadin/terminal/gwt/client/VPaintableWidget.java +++ b/src/com/vaadin/terminal/gwt/client/VPaintableWidget.java @@ -16,6 +16,13 @@ import com.google.gwt.user.client.ui.Widget; */ public interface VPaintableWidget extends VPaintable { + /* + * (non-Javadoc) + * + * @see com.vaadin.terminal.gwt.client.VPaintable#getState() + */ + public ComponentState getState(); + /** * TODO: Rename to getWidget */ diff --git a/src/com/vaadin/terminal/gwt/client/communication/JsonDecoder.java b/src/com/vaadin/terminal/gwt/client/communication/JsonDecoder.java index c66dc8a036..4239845e4e 100644 --- a/src/com/vaadin/terminal/gwt/client/communication/JsonDecoder.java +++ b/src/com/vaadin/terminal/gwt/client/communication/JsonDecoder.java @@ -8,10 +8,13 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; +import java.util.Map; +import com.google.gwt.core.client.GWT; import com.google.gwt.json.client.JSONArray; import com.google.gwt.json.client.JSONObject; import com.google.gwt.json.client.JSONString; +import com.vaadin.terminal.gwt.client.ComponentState_Serializer; import com.vaadin.terminal.gwt.client.VPaintable; import com.vaadin.terminal.gwt.client.VPaintableMap; @@ -72,15 +75,19 @@ public class JsonDecoder { } else if (JsonEncoder.VTYPE_PAINTABLE.equals(variableType)) { // TODO handle properly val = idMapper.getPaintable(String.valueOf(value)); - } else if (JsonEncoder.VTYPE_SHAREDSTATE.equals(variableType)) { - val = convertMap((JSONObject) value, idMapper); - // TODO convert to a SharedState instance + } else { + // object, class name as type + VaadinSerializer serializer = getSerializer(variableType); + Object object = serializer + .deserialize((JSONObject) value, idMapper); + return object; } return val; } - private static Object convertMap(JSONObject jsonMap, VPaintableMap idMapper) { + public static Map convertMap(JSONObject jsonMap, + VPaintableMap idMapper) { HashMap map = new HashMap(); Iterator it = jsonMap.keySet().iterator(); while (it.hasNext()) { @@ -99,7 +106,7 @@ public class JsonDecoder { return tokens.toArray(new String[tokens.size()]); } - private static Object convertArray(JSONArray jsonArray, + private static Object[] convertArray(JSONArray jsonArray, VPaintableMap idMapper) { List tokens = new ArrayList(); for (int i = 0; i < jsonArray.size(); ++i) { @@ -110,4 +117,20 @@ public class JsonDecoder { return tokens.toArray(new Object[tokens.size()]); } + public static Object deserialize(JSONArray jsonArray, VPaintableMap idMapper) { + // jsonArray always contains two items + // 1. type (String) + // 2. data (Serialized) + String type = ((JSONString) jsonArray.get(0)).stringValue(); + VaadinSerializer serializer = getSerializer(type); + Object object = serializer.deserialize((JSONObject) jsonArray.get(1), + idMapper); + return object; + } + + private static VaadinSerializer getSerializer(String type) { + // TODO This should be in a separate class and constructed by a + // generator + return GWT.create(ComponentState_Serializer.class); + } } diff --git a/src/com/vaadin/terminal/gwt/client/communication/SharedState.java b/src/com/vaadin/terminal/gwt/client/communication/SharedState.java index 38183ba50d..86c710c927 100644 --- a/src/com/vaadin/terminal/gwt/client/communication/SharedState.java +++ b/src/com/vaadin/terminal/gwt/client/communication/SharedState.java @@ -5,8 +5,6 @@ package com.vaadin.terminal.gwt.client.communication; import java.io.Serializable; -import java.util.HashMap; -import java.util.Map; import com.google.gwt.core.client.GWT; import com.vaadin.terminal.gwt.client.ui.VAbstractPaintableWidget; @@ -55,15 +53,4 @@ import com.vaadin.terminal.gwt.client.ui.VAbstractPaintableWidget; * @since 7.0 */ public class SharedState implements Serializable { - private Map state = new HashMap(); - - // TODO temporary until reflection based serialization is implemented - public Map getState() { - return state; - } - - // TODO temporary until generator based deserialization is implemented - public void setState(Map stateMap) { - state = stateMap; - } } diff --git a/src/com/vaadin/terminal/gwt/client/communication/VaadinSerializer.java b/src/com/vaadin/terminal/gwt/client/communication/VaadinSerializer.java new file mode 100644 index 0000000000..809cf152a5 --- /dev/null +++ b/src/com/vaadin/terminal/gwt/client/communication/VaadinSerializer.java @@ -0,0 +1,11 @@ +package com.vaadin.terminal.gwt.client.communication; + +import com.google.gwt.json.client.JSONObject; +import com.vaadin.terminal.gwt.client.VPaintableMap; + +public interface VaadinSerializer { + + // TODO Object -> something + Object deserialize(JSONObject jsonValue, VPaintableMap idMapper); + +} diff --git a/src/com/vaadin/terminal/gwt/client/ui/VAbstractPaintableWidget.java b/src/com/vaadin/terminal/gwt/client/ui/VAbstractPaintableWidget.java index 2c948dfba8..23c8f6ca0b 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VAbstractPaintableWidget.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VAbstractPaintableWidget.java @@ -3,11 +3,13 @@ */ package com.vaadin.terminal.gwt.client.ui; +import com.google.gwt.core.client.GWT; import com.google.gwt.user.client.DOM; import com.google.gwt.user.client.ui.FocusWidget; import com.google.gwt.user.client.ui.Focusable; 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.TooltipInfo; import com.vaadin.terminal.gwt.client.UIDL; import com.vaadin.terminal.gwt.client.VPaintableMap; @@ -44,7 +46,7 @@ public abstract class VAbstractPaintableWidget implements VPaintableWidget { private boolean visible = true; // shared state from the server to the client - private SharedState state; + private ComponentState state; /** * Default constructor @@ -108,14 +110,18 @@ public abstract class VAbstractPaintableWidget implements VPaintableWidget { this.id = id; } - public void updateState(SharedState state) { - this.state = state; - } + public ComponentState getState() { + if (state == null) { + state = createState(); + } - public SharedState getState() { return state; } + protected ComponentState createState() { + return GWT.create(ComponentState.class); + } + public VPaintableWidgetContainer getParent() { // FIXME: Hierarchy should be set by framework instead of looked up here VPaintableMap paintableMap = VPaintableMap.get(getConnection()); @@ -343,4 +349,7 @@ public abstract class VAbstractPaintableWidget implements VPaintableWidget { return styleBuf.toString(); } + public final void setState(SharedState state) { + this.state = (ComponentState) state; + } } diff --git a/src/com/vaadin/terminal/gwt/client/ui/VButtonState.java b/src/com/vaadin/terminal/gwt/client/ui/VButtonState.java new file mode 100644 index 0000000000..b065818040 --- /dev/null +++ b/src/com/vaadin/terminal/gwt/client/ui/VButtonState.java @@ -0,0 +1,16 @@ +package com.vaadin.terminal.gwt.client.ui; + +import com.vaadin.terminal.gwt.client.ComponentState; + +public class VButtonState extends ComponentState { + private boolean disableOnClick = false; + + public boolean isDisableOnClick() { + return disableOnClick; + } + + public void setDisableOnClick(boolean disableOnClick) { + this.disableOnClick = disableOnClick; + } + +} diff --git a/src/com/vaadin/terminal/gwt/client/ui/VButtonState_Serializer.java b/src/com/vaadin/terminal/gwt/client/ui/VButtonState_Serializer.java new file mode 100644 index 0000000000..9ac1fab836 --- /dev/null +++ b/src/com/vaadin/terminal/gwt/client/ui/VButtonState_Serializer.java @@ -0,0 +1,29 @@ +package com.vaadin.terminal.gwt.client.ui; + +import com.google.gwt.core.client.GWT; +import com.google.gwt.json.client.JSONArray; +import com.google.gwt.json.client.JSONObject; +import com.vaadin.terminal.gwt.client.VPaintableMap; +import com.vaadin.terminal.gwt.client.communication.JsonDecoder; +import com.vaadin.terminal.gwt.client.communication.VaadinSerializer; + +//TODO This should be autogenerated +public class VButtonState_Serializer implements VaadinSerializer { + + public VButtonState deserialize(JSONObject jsonValue, VPaintableMap idMapper) { + VButtonState state = GWT.create(VButtonState.class); + + JSONArray jsonHeight = (JSONArray) jsonValue.get("height"); + state.setHeight((String) JsonDecoder.convertValue(jsonHeight, idMapper)); + + JSONArray jsonWidth = (JSONArray) jsonValue.get("width"); + state.setWidth((String) JsonDecoder.convertValue(jsonWidth, idMapper)); + + JSONArray jsonDisableOnClick = (JSONArray) jsonValue + .get("disableOnClick"); + state.setDisableOnClick((Boolean) JsonDecoder.convertValue( + jsonDisableOnClick, idMapper)); + + return state; + } +} diff --git a/src/com/vaadin/terminal/gwt/client/ui/VEmbeddedPaintable.java b/src/com/vaadin/terminal/gwt/client/ui/VEmbeddedPaintable.java index 0c64b28ab1..585241ef21 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VEmbeddedPaintable.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VEmbeddedPaintable.java @@ -20,7 +20,6 @@ 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; @@ -70,22 +69,8 @@ public class VEmbeddedPaintable extends VAbstractPaintableWidget { // Set attributes Style style = el.getStyle(); - 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); + style.setProperty("width", getState().getWidth()); + style.setProperty("height", getState().getHeight()); DOM.setElementProperty(el, "src", getWidgetForPaintable() .getSrc(uidl, client)); @@ -203,4 +188,5 @@ public class VEmbeddedPaintable extends VAbstractPaintableWidget { } }; -} + +} \ No newline at end of file diff --git a/src/com/vaadin/terminal/gwt/client/ui/VGridLayout.java b/src/com/vaadin/terminal/gwt/client/ui/VGridLayout.java index 6e84d636fc..cdde126d3a 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VGridLayout.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VGridLayout.java @@ -19,6 +19,7 @@ import com.google.gwt.user.client.ui.AbsolutePanel; import com.google.gwt.user.client.ui.SimplePanel; 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.Container; import com.vaadin.terminal.gwt.client.RenderSpace; import com.vaadin.terminal.gwt.client.StyleConstants; @@ -26,7 +27,6 @@ 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; @@ -936,20 +936,18 @@ public class VGridLayout extends SimplePanel implements Container { } } - protected void updateRelSizeStatus(SharedState state, boolean cached) { + protected void updateRelSizeStatus(ComponentState 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("%")) { + boolean widthDefined = !state.isUndefinedWidth(); + boolean heightDefined = !state.isUndefinedHeight(); + if (heightDefined && state.getHeight().contains("%")) { relHeight = true; } else { relHeight = false; } if (widthDefined) { - widthCanAffectHeight = relWidth = String.valueOf( - state.getState().get("width")).contains("%"); + widthCanAffectHeight = relWidth = state.getWidth() + .contains("%"); if (heightDefined) { widthCanAffectHeight = false; } diff --git a/src/com/vaadin/terminal/gwt/client/ui/VMenuBarPaintable.java b/src/com/vaadin/terminal/gwt/client/ui/VMenuBarPaintable.java index 246986f174..690dd5692c 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VMenuBarPaintable.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VMenuBarPaintable.java @@ -10,7 +10,6 @@ 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; @@ -52,9 +51,7 @@ public class VMenuBarPaintable extends VAbstractPaintableWidget { UIDL options = uidl.getChildUIDL(0); - if (null != getState() - && getState().getState() - .containsKey(ComponentState.STATE_WIDTH)) { + if (null != getState() && !getState().isUndefinedWidth()) { UIDL moreItemUIDL = options.getChildUIDL(0); StringBuffer itemHTML = new StringBuffer(); diff --git a/src/com/vaadin/terminal/gwt/client/ui/VWindowPaintable.java b/src/com/vaadin/terminal/gwt/client/ui/VWindowPaintable.java index 7803931ecd..402b7fd42d 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VWindowPaintable.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VWindowPaintable.java @@ -3,8 +3,6 @@ */ 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; @@ -15,7 +13,6 @@ 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; @@ -155,16 +152,8 @@ public class VWindowPaintable extends VAbstractPaintableWidgetContainer getWidgetForPaintable().layout = lo; } - 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().dynamicWidth = getState().isUndefinedWidth(); + getWidgetForPaintable().dynamicHeight = getState().isUndefinedHeight(); 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 819ac90cdf..e41a1175b9 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/layout/CellBasedLayoutPaintable.java +++ b/src/com/vaadin/terminal/gwt/client/ui/layout/CellBasedLayoutPaintable.java @@ -4,7 +4,6 @@ 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; @@ -37,31 +36,8 @@ public abstract class CellBasedLayoutPaintable extends } 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; - } else { - getWidgetForPaintable().dynamicWidth = false; - } - - if (h.equals("")) { - getWidgetForPaintable().dynamicHeight = true; - } else { - getWidgetForPaintable().dynamicHeight = false; - } - + getWidgetForPaintable().dynamicWidth = getState().isUndefinedWidth(); + getWidgetForPaintable().dynamicHeight = getState().isUndefinedHeight(); } void updateMarginAndSpacingInfo(UIDL uidl) { diff --git a/src/com/vaadin/terminal/gwt/server/JsonCodec.java b/src/com/vaadin/terminal/gwt/server/JsonCodec.java index e525c690c0..348f4a6f0f 100644 --- a/src/com/vaadin/terminal/gwt/server/JsonCodec.java +++ b/src/com/vaadin/terminal/gwt/server/JsonCodec.java @@ -4,7 +4,10 @@ package com.vaadin.terminal.gwt.server; +import java.beans.Introspector; +import java.beans.PropertyDescriptor; import java.io.Serializable; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; @@ -151,10 +154,8 @@ public class JsonCodec implements Serializable { return combineTypeAndValue(JsonEncoder.VTYPE_UNDEFINED, JSONObject.NULL); } else if (value instanceof SharedState) { - // TODO implement by encoding the bean - Map map = ((SharedState) value).getState(); - return combineTypeAndValue(JsonEncoder.VTYPE_SHAREDSTATE, - encodeMapContents(map, idMapper)); + return combineTypeAndValue(value.getClass().getName(), + encodeObject(value, idMapper)); } else if (value instanceof String[]) { String[] array = (String[]) value; JSONArray jsonArray = new JSONArray(); @@ -184,6 +185,28 @@ public class JsonCodec implements Serializable { } } + private static Object encodeObject(Object value, PaintableIdMapper idMapper) + throws JSONException { + JSONObject jsonMap = new JSONObject(); + + try { + for (PropertyDescriptor pd : Introspector.getBeanInfo( + value.getClass()).getPropertyDescriptors()) { + String fieldName = pd.getName(); + if (pd.getReadMethod() == null || pd.getWriteMethod() == null) { + continue; + } + Method getterMethod = pd.getReadMethod(); + Object fieldValue = getterMethod.invoke(value, null); + jsonMap.put(fieldName, encode(fieldValue, idMapper)); + } + } catch (Exception e) { + // TODO: Should exceptions be handled in a different way? + throw new JSONException(e); + } + return jsonMap; + } + private static JSONArray encodeArrayContents(Object[] array, PaintableIdMapper idMapper) throws JSONException { JSONArray jsonArray = new JSONArray(); diff --git a/src/com/vaadin/ui/AbstractComponent.java b/src/com/vaadin/ui/AbstractComponent.java index 9654f12ea3..b8c54e864a 100644 --- a/src/com/vaadin/ui/AbstractComponent.java +++ b/src/com/vaadin/ui/AbstractComponent.java @@ -890,18 +890,18 @@ public abstract class AbstractComponent implements Component, MethodEventSource // TODO for now, this superclass always recreates the state from // scratch, whereas subclasses should only modify it - Map state = new HashMap(); + // Map state = new HashMap(); if (getHeight() >= 0 && (getHeightUnits() != Unit.PERCENTAGE || ComponentSizeValidator .parentCanDefineHeight(this))) { - state.put(ComponentState.STATE_HEIGHT, "" + getCSSHeight()); + sharedState.setHeight("" + getCSSHeight()); } if (getWidth() >= 0 && (getWidthUnits() != Unit.PERCENTAGE || ComponentSizeValidator .parentCanDefineWidth(this))) { - state.put(ComponentState.STATE_WIDTH, "" + getCSSWidth()); + sharedState.setWidth("" + getCSSWidth()); } // if (getCaption() != null) { @@ -927,8 +927,8 @@ public abstract class AbstractComponent implements Component, MethodEventSource // if (getDescription() != null && getDescription().length() > 0) { // state.put(ComponentState.STATE_DESCRIPTION, getDescription()); // } - - sharedState.setState(state); + // + // sharedState.setState(state); return sharedState; } diff --git a/src/com/vaadin/ui/Button.java b/src/com/vaadin/ui/Button.java index 9910f3a4c4..b88cab81d9 100644 --- a/src/com/vaadin/ui/Button.java +++ b/src/com/vaadin/ui/Button.java @@ -21,10 +21,12 @@ import com.vaadin.event.ShortcutAction.ModifierKey; import com.vaadin.event.ShortcutListener; import com.vaadin.terminal.PaintException; import com.vaadin.terminal.PaintTarget; +import com.vaadin.terminal.gwt.client.ComponentState; import com.vaadin.terminal.gwt.client.MouseEventDetails; import com.vaadin.terminal.gwt.client.ui.VButton; import com.vaadin.terminal.gwt.client.ui.VButton.ButtonClientToServerRpc; import com.vaadin.terminal.gwt.client.ui.VButtonPaintable; +import com.vaadin.terminal.gwt.client.ui.VButtonState; import com.vaadin.terminal.gwt.server.RpcTarget; import com.vaadin.tools.ReflectTools; import com.vaadin.ui.ClientWidget.LoadStyle; @@ -44,10 +46,6 @@ public class Button extends AbstractComponent implements FieldEvents.BlurNotifier, FieldEvents.FocusNotifier, Focusable, Action.ShortcutNotifier, RpcTarget { - /* Private members */ - - boolean disableOnClick = false; - /** * Creates a new push button. */ @@ -485,7 +483,7 @@ public class Button extends AbstractComponent implements * @return true if the button is disabled when clicked, false otherwise */ public boolean isDisableOnClick() { - return disableOnClick; + return getState().isDisableOnClick(); } /** @@ -497,7 +495,7 @@ public class Button extends AbstractComponent implements * true to disable button when it is clicked, false otherwise */ public void setDisableOnClick(boolean disableOnClick) { - this.disableOnClick = disableOnClick; + getState().setDisableOnClick(disableOnClick); requestRepaint(); } @@ -515,4 +513,14 @@ public class Button extends AbstractComponent implements // Overridden only to make public super.focus(); } + + @Override + protected ComponentState createState() { + return new VButtonState(); + } + + @Override + public VButtonState getState() { + return (VButtonState) super.getState(); + } } -- 2.39.5