]> source.dussan.org Git - vaadin-framework.git/commitdiff
#8304 First steps towards supporting serialization of any bean
authorArtur Signell <artur@vaadin.com>
Tue, 21 Feb 2012 13:27:42 +0000 (15:27 +0200)
committerArtur Signell <artur@vaadin.com>
Tue, 21 Feb 2012 13:27:42 +0000 (15:27 +0200)
20 files changed:
src/com/vaadin/terminal/gwt/client/ApplicationConnection.java
src/com/vaadin/terminal/gwt/client/ComponentState.java
src/com/vaadin/terminal/gwt/client/ComponentState_Serializer.java [new file with mode: 0644]
src/com/vaadin/terminal/gwt/client/Util.java
src/com/vaadin/terminal/gwt/client/VPaintable.java
src/com/vaadin/terminal/gwt/client/VPaintableWidget.java
src/com/vaadin/terminal/gwt/client/communication/JsonDecoder.java
src/com/vaadin/terminal/gwt/client/communication/SharedState.java
src/com/vaadin/terminal/gwt/client/communication/VaadinSerializer.java [new file with mode: 0644]
src/com/vaadin/terminal/gwt/client/ui/VAbstractPaintableWidget.java
src/com/vaadin/terminal/gwt/client/ui/VButtonState.java [new file with mode: 0644]
src/com/vaadin/terminal/gwt/client/ui/VButtonState_Serializer.java [new file with mode: 0644]
src/com/vaadin/terminal/gwt/client/ui/VEmbeddedPaintable.java
src/com/vaadin/terminal/gwt/client/ui/VGridLayout.java
src/com/vaadin/terminal/gwt/client/ui/VMenuBarPaintable.java
src/com/vaadin/terminal/gwt/client/ui/VWindowPaintable.java
src/com/vaadin/terminal/gwt/client/ui/layout/CellBasedLayoutPaintable.java
src/com/vaadin/terminal/gwt/server/JsonCodec.java
src/com/vaadin/ui/AbstractComponent.java
src/com/vaadin/ui/Button.java

index c8a676d6fada911f8be9f0e2f07c412149463b4d..1737f82e557bc8bbe73ce1b5f9d934e25815c07a 100644 (file)
@@ -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<String, Object> stateMap = (Map<String, Object>) 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 "
index 955833e48f1207e4380d873a3d8db27fe644cf4e..14b5e3189144be8648b922358b49fb5ab4cc3428 100644 (file)
@@ -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 (file)
index 0000000..075b7f9
--- /dev/null
@@ -0,0 +1,28 @@
+package com.vaadin.terminal.gwt.client;\r
+\r
+import com.google.gwt.core.client.GWT;\r
+import com.google.gwt.json.client.JSONArray;\r
+import com.google.gwt.json.client.JSONObject;\r
+import com.vaadin.terminal.gwt.client.communication.JsonDecoder;\r
+import com.vaadin.terminal.gwt.client.communication.VaadinSerializer;\r
+\r
+//TODO This should be autogenerated\r
+public class ComponentState_Serializer implements VaadinSerializer {\r
+\r
+    public ComponentState deserialize(JSONObject jsonValue,\r
+            VPaintableMap idMapper) {\r
+        ComponentState state = GWT.create(ComponentState.class);\r
+\r
+        // For (Field f : fields) {\r
+        // state.setState((Map<String, Object>)\r
+        // JsonDecoder.convertMap(jsonValue,\r
+        // idMapper));\r
+        JSONArray jsonHeight = (JSONArray) jsonValue.get("height");\r
+        state.setHeight((String) JsonDecoder.convertValue(jsonHeight, idMapper));\r
+\r
+        JSONArray jsonWidth = (JSONArray) jsonValue.get("width");\r
+        state.setWidth((String) JsonDecoder.convertValue(jsonWidth, idMapper));\r
+\r
+        return state;\r
+    }\r
+}\r
index 0c4181066a1f4db5c9d75d30c297f7c67fa3ba3e..be78959830d101a5fe16ed2696888f7170c7db87 100644 (file)
@@ -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<String, Object> 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;
index 666b620d82c7cf86c89a5082688e527bbafe1169..9d1dd0fdbccf3efb3591e9559dd32340a4c18dea 100644 (file)
@@ -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
index 4b8720b1325af30fd9e90266b28928de23c538bb..bb944ae4d6b6c6dd8e6fb0f53850f863ef14e3d7 100644 (file)
@@ -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
      */
index c66dc8a0361b95c045f0d0c5cec62b9fc8ec3926..4239845e4eaedb4eb0d0dff74ce4079f53973770 100644 (file)
@@ -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<String, Object> convertMap(JSONObject jsonMap,
+            VPaintableMap idMapper) {
         HashMap<String, Object> map = new HashMap<String, Object>();
         Iterator<String> 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<Object> tokens = new ArrayList<Object>();
         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);
+    }
 }
index 38183ba50d5a2a7899748935d7440b16d5d09c11..86c710c92783d62af2e54a135ae4d2cab7de8276 100644 (file)
@@ -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<String, Object> state = new HashMap<String, Object>();
-
-    // TODO temporary until reflection based serialization is implemented
-    public Map<String, Object> getState() {
-        return state;
-    }
-
-    // TODO temporary until generator based deserialization is implemented
-    public void setState(Map<String, Object> 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 (file)
index 0000000..809cf15
--- /dev/null
@@ -0,0 +1,11 @@
+package com.vaadin.terminal.gwt.client.communication;\r
+\r
+import com.google.gwt.json.client.JSONObject;\r
+import com.vaadin.terminal.gwt.client.VPaintableMap;\r
+\r
+public interface VaadinSerializer {\r
+\r
+    // TODO Object -> something\r
+    Object deserialize(JSONObject jsonValue, VPaintableMap idMapper);\r
+\r
+}\r
index 2c948dfba8022c6bd25f710f47070a6d2fb5f111..23c8f6ca0bcc1220c8fc3c9dc3b8811e05cd31eb 100644 (file)
@@ -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 (file)
index 0000000..b065818
--- /dev/null
@@ -0,0 +1,16 @@
+package com.vaadin.terminal.gwt.client.ui;\r
+\r
+import com.vaadin.terminal.gwt.client.ComponentState;\r
+\r
+public class VButtonState extends ComponentState {\r
+    private boolean disableOnClick = false;\r
+\r
+    public boolean isDisableOnClick() {\r
+        return disableOnClick;\r
+    }\r
+\r
+    public void setDisableOnClick(boolean disableOnClick) {\r
+        this.disableOnClick = disableOnClick;\r
+    }\r
+\r
+}\r
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 (file)
index 0000000..9ac1fab
--- /dev/null
@@ -0,0 +1,29 @@
+package com.vaadin.terminal.gwt.client.ui;\r
+\r
+import com.google.gwt.core.client.GWT;\r
+import com.google.gwt.json.client.JSONArray;\r
+import com.google.gwt.json.client.JSONObject;\r
+import com.vaadin.terminal.gwt.client.VPaintableMap;\r
+import com.vaadin.terminal.gwt.client.communication.JsonDecoder;\r
+import com.vaadin.terminal.gwt.client.communication.VaadinSerializer;\r
+\r
+//TODO This should be autogenerated\r
+public class VButtonState_Serializer implements VaadinSerializer {\r
+\r
+    public VButtonState deserialize(JSONObject jsonValue, VPaintableMap idMapper) {\r
+        VButtonState state = GWT.create(VButtonState.class);\r
+\r
+        JSONArray jsonHeight = (JSONArray) jsonValue.get("height");\r
+        state.setHeight((String) JsonDecoder.convertValue(jsonHeight, idMapper));\r
+\r
+        JSONArray jsonWidth = (JSONArray) jsonValue.get("width");\r
+        state.setWidth((String) JsonDecoder.convertValue(jsonWidth, idMapper));\r
+\r
+        JSONArray jsonDisableOnClick = (JSONArray) jsonValue\r
+                .get("disableOnClick");\r
+        state.setDisableOnClick((Boolean) JsonDecoder.convertValue(\r
+                jsonDisableOnClick, idMapper));\r
+\r
+        return state;\r
+    }\r
+}\r
index 0c64b28ab1c51b044fc59be3911ca38a7dfdc038..585241ef21f1c6d3bcde5a67b7121865baf8861f 100644 (file)
@@ -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
index 6e84d636fce2568d5a47d99f30c805bfd62f76d9..cdde126d3aaf1e29cf392029aa74f8162d26d282 100644 (file)
@@ -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;
                     }
index 246986f174db876268f2d9d6551dea0cb0f333d5..690dd5692cb92d320c30a3e6ee1d53d16e6e203c 100644 (file)
@@ -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();
 
index 7803931ecd04971e559b1975a9b1fe8c2de9c668..402b7fd42dca3f78c769513802cd3c16828a2f6c 100644 (file)
@@ -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<String, Object> 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");
index 819ac90cdf8eb744756e660947e18182715eb468..e41a1175b9abba112e165d6eaef1e5d9eaa6e8c8 100644 (file)
@@ -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) {
index e525c690c01a0b8a3bc1ae994da620008d3ed836..348f4a6f0ffc7b0c966e5f7ed6857a7decca5dea 100644 (file)
@@ -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<String, Object> 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();
index 9654f12ea3dfe09397760982cbf8b4fd5484a88c..b8c54e864a4a596ccd4ce25e68385ada63c33356 100644 (file)
@@ -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<String, Object> state = new HashMap<String, Object>();
+        // Map<String, Object> state = new HashMap<String, Object>();
 
         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;
     }
index 9910f3a4c43e8f09139d85cce550102af0b9830c..b88cab81d90605698e69130f2a22085cab416835 100644 (file)
@@ -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();
+    }
 }