]> source.dussan.org Git - vaadin-framework.git/commitdiff
#8304 Changed Button to solely use shared state
authorArtur Signell <artur@vaadin.com>
Tue, 21 Feb 2012 14:17:57 +0000 (16:17 +0200)
committerArtur Signell <artur@vaadin.com>
Tue, 21 Feb 2012 14:17:57 +0000 (16:17 +0200)
src/com/vaadin/terminal/gwt/client/ApplicationConnection.java
src/com/vaadin/terminal/gwt/client/communication/JsonDecoder.java
src/com/vaadin/terminal/gwt/client/communication/SerializerMap.java [new file with mode: 0644]
src/com/vaadin/terminal/gwt/client/communication/SerializerMapImpl.java [new file with mode: 0644]
src/com/vaadin/terminal/gwt/client/ui/VButton.java
src/com/vaadin/terminal/gwt/client/ui/VButtonPaintable.java
src/com/vaadin/terminal/gwt/client/ui/VButtonState.java
src/com/vaadin/terminal/gwt/client/ui/VButtonState_Serializer.java
src/com/vaadin/terminal/gwt/client/ui/VNativeButtonPaintable.java
src/com/vaadin/terminal/gwt/server/JsonCodec.java
src/com/vaadin/ui/Button.java

index 1737f82e557bc8bbe73ce1b5f9d934e25815c07a..ee6091e925b3e274443e6467a3594289efcd926d 100644 (file)
@@ -1038,7 +1038,7 @@ public class ApplicationConnection {
                             JSONArray stateDataAndType = new JSONArray(
                                     states.getJavaScriptObject(paintableId));
 
-                            Object state = JsonDecoder.deserialize(
+                            Object state = JsonDecoder.convertValue(
                                     stateDataAndType, paintableMap);
 
                             paintable.setState((SharedState) state);
index 4239845e4eaedb4eb0d0dff74ce4079f53973770..e87db8259f43cf7673395d5b6000b592c8ef341c 100644 (file)
@@ -14,7 +14,6 @@ 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;
 
@@ -30,19 +29,23 @@ import com.vaadin.terminal.gwt.client.VPaintableMap;
  * @since 7.0
  */
 public class JsonDecoder {
+    private static SerializerMap serializerMap = GWT
+            .create(SerializerMap.class);
+
     /**
      * Convert a JSON array with two elements (type and value) into a
      * client-side type, recursively if necessary.
      * 
-     * @param value
+     * @param jsonArray
      *            JSON array with two elements
      * @param idMapper
      *            mapper between paintable ID and {@link VPaintable} objects
      * @return converted value (does not contain JSON types)
      */
-    public static Object convertValue(JSONArray value, VPaintableMap idMapper) {
-        return convertValue(((JSONString) value.get(0)).stringValue(),
-                value.get(1), idMapper);
+    public static Object convertValue(JSONArray jsonArray,
+            VPaintableMap idMapper) {
+        String type = ((JSONString) jsonArray.get(0)).stringValue();
+        return convertValue(type, jsonArray.get(1), idMapper);
     }
 
     private static Object convertValue(String variableType, Object value,
@@ -77,7 +80,8 @@ public class JsonDecoder {
             val = idMapper.getPaintable(String.valueOf(value));
         } else {
             // object, class name as type
-            VaadinSerializer serializer = getSerializer(variableType);
+            VaadinSerializer serializer = serializerMap
+                    .getSerializer(variableType);
             Object object = serializer
                     .deserialize((JSONObject) value, idMapper);
             return object;
@@ -86,7 +90,7 @@ public class JsonDecoder {
         return val;
     }
 
-    public static Map<String, Object> convertMap(JSONObject jsonMap,
+    private static Map<String, Object> convertMap(JSONObject jsonMap,
             VPaintableMap idMapper) {
         HashMap<String, Object> map = new HashMap<String, Object>();
         Iterator<String> it = jsonMap.keySet().iterator();
@@ -117,20 +121,4 @@ 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/SerializerMap.java b/src/com/vaadin/terminal/gwt/client/communication/SerializerMap.java
new file mode 100644 (file)
index 0000000..8fd791d
--- /dev/null
@@ -0,0 +1,7 @@
+package com.vaadin.terminal.gwt.client.communication;\r
+\r
+public interface SerializerMap {\r
+\r
+    public VaadinSerializer getSerializer(String type);\r
+\r
+}\r
diff --git a/src/com/vaadin/terminal/gwt/client/communication/SerializerMapImpl.java b/src/com/vaadin/terminal/gwt/client/communication/SerializerMapImpl.java
new file mode 100644 (file)
index 0000000..eb37db3
--- /dev/null
@@ -0,0 +1,19 @@
+package com.vaadin.terminal.gwt.client.communication;\r
+\r
+import com.google.gwt.core.client.GWT;\r
+import com.vaadin.terminal.gwt.client.ComponentState_Serializer;\r
+import com.vaadin.terminal.gwt.client.ui.VButtonState_Serializer;\r
+\r
+public class SerializerMapImpl implements SerializerMap {\r
+\r
+    public VaadinSerializer getSerializer(String type) {\r
+        // TODO This should be in a separate class and constructed by a\r
+        // generator\r
+        if (type.equals("com.vaadin.terminal.gwt.client.ui.VButtonState")) {\r
+            return GWT.create(VButtonState_Serializer.class);\r
+        }\r
+\r
+        return GWT.create(ComponentState_Serializer.class);\r
+    }\r
+\r
+}\r
index e04403d00ea3453404345ae01b86af4a773d834f..9797349d9e94864a31ec9930c389926eb29b7389 100644 (file)
@@ -56,8 +56,6 @@ public class VButton extends FocusWidget implements ClickHandler, FocusHandler,
         public void disableOnClick();
     }
 
-    public static final String ATTR_DISABLE_ON_CLICK = "dc";
-
     // mouse movement is checked before synthesizing click event on mouseout
     protected static int MOVE_THRESHOLD = 3;
     protected int mousedownX = 0;
index ea227b1854eca23318afff6c04832fcfb3438afb..f934571ac5ba7472a7f3c5e179a7fee3bbe576ea 100644 (file)
@@ -43,8 +43,7 @@ public class VButtonPaintable extends VAbstractPaintableWidget {
         getWidgetForPaintable().setText(
                 uidl.getStringAttribute(ATTRIBUTE_CAPTION));
 
-        getWidgetForPaintable().disableOnClick = uidl
-                .hasAttribute(VButton.ATTR_DISABLE_ON_CLICK);
+        getWidgetForPaintable().disableOnClick = getState().isDisableOnClick();
 
         // handle error
         if (uidl.hasAttribute("error")) {
@@ -81,10 +80,8 @@ public class VButtonPaintable extends VAbstractPaintableWidget {
             }
         }
 
-        if (uidl.hasAttribute("keycode")) {
-            getWidgetForPaintable().clickShortcut = uidl
-                    .getIntAttribute("keycode");
-        }
+        getWidgetForPaintable().clickShortcut = getState()
+                .getClickShortcutKeyCode();
     }
 
     @Override
@@ -96,4 +93,9 @@ public class VButtonPaintable extends VAbstractPaintableWidget {
     public VButton getWidgetForPaintable() {
         return (VButton) super.getWidgetForPaintable();
     }
+
+    @Override
+    public VButtonState getState() {
+        return (VButtonState) super.getState();
+    }
 }
index b0658180406967b3d9772a0676816ce7cdd25644..f4ed071bef1a1a9129975d7e06af3e862b8baece 100644 (file)
@@ -4,6 +4,7 @@ import com.vaadin.terminal.gwt.client.ComponentState;
 \r
 public class VButtonState extends ComponentState {\r
     private boolean disableOnClick = false;\r
+    private int clickShortcutKeyCode = 0;\r
 \r
     public boolean isDisableOnClick() {\r
         return disableOnClick;\r
@@ -13,4 +14,12 @@ public class VButtonState extends ComponentState {
         this.disableOnClick = disableOnClick;\r
     }\r
 \r
+    public int getClickShortcutKeyCode() {\r
+        return clickShortcutKeyCode;\r
+    }\r
+\r
+    public void setClickShortcutKeyCode(int clickShortcutKeyCode) {\r
+        this.clickShortcutKeyCode = clickShortcutKeyCode;\r
+    }\r
+\r
 }\r
index 9ac1fab83629e04fc47c684b9d24f2945421e76c..31cb9ab70fbb9a199f56701ddd21de1fb2099f9d 100644 (file)
@@ -24,6 +24,11 @@ public class VButtonState_Serializer implements VaadinSerializer {
         state.setDisableOnClick((Boolean) JsonDecoder.convertValue(\r
                 jsonDisableOnClick, idMapper));\r
 \r
+        JSONArray jsonClickShortcutKeyCode = (JSONArray) jsonValue\r
+                .get("clickShortcutKeyCode");\r
+        state.setClickShortcutKeyCode((Integer) JsonDecoder.convertValue(\r
+                jsonClickShortcutKeyCode, idMapper));\r
+\r
         return state;\r
     }\r
 }\r
index 4257597e2dbbea8e3177dd3b0a95be3b9eb4c27b..2ee71658b5307735a41dbaf39394b00c8802f528 100644 (file)
@@ -27,9 +27,7 @@ public class VNativeButtonPaintable extends VAbstractPaintableWidget {
             return;
         }
 
-        getWidgetForPaintable().disableOnClick = uidl
-                .hasAttribute(VButton.ATTR_DISABLE_ON_CLICK);
-
+        getWidgetForPaintable().disableOnClick = getState().isDisableOnClick();
         getWidgetForPaintable().focusHandlerRegistration = EventHelper
                 .updateFocusHandler(this, client,
                         getWidgetForPaintable().focusHandlerRegistration);
@@ -91,4 +89,9 @@ public class VNativeButtonPaintable extends VAbstractPaintableWidget {
     public VNativeButton getWidgetForPaintable() {
         return (VNativeButton) super.getWidgetForPaintable();
     }
+
+    @Override
+    public VButtonState getState() {
+        return (VButtonState) super.getState();
+    }
 }
\ No newline at end of file
index 348f4a6f0ffc7b0c966e5f7ed6857a7decca5dea..d8964f85a644f8ced1fb16e55f568760bce675e0 100644 (file)
@@ -136,8 +136,8 @@ public class JsonCodec implements Serializable {
     }
 
     /**
-     * Encode a value to a JSON representation for transport from the client to
-     * the server.
+     * Encode a value to a JSON representation for transport from the server to
+     * the client.
      * 
      * @param value
      *            value to convert
@@ -167,6 +167,8 @@ public class JsonCodec implements Serializable {
             return combineTypeAndValue(JsonEncoder.VTYPE_STRING, value);
         } else if (value instanceof Boolean) {
             return combineTypeAndValue(JsonEncoder.VTYPE_BOOLEAN, value);
+        } else if (value instanceof Number) {
+            return combineTypeAndValue(getTransportType(value), value);
         } else if (value instanceof Object[]) {
             Object[] array = (Object[]) value;
             JSONArray jsonArray = encodeArrayContents(array, idMapper);
index b88cab81d90605698e69130f2a22085cab416835..4ca274abf94b8ddbb95a100ee8c59c0813a5ae2c 100644 (file)
@@ -4,7 +4,6 @@
 
 package com.vaadin.ui;
 
-import java.io.IOException;
 import java.io.Serializable;
 import java.lang.reflect.Method;
 import java.util.Map;
@@ -19,11 +18,8 @@ import com.vaadin.event.ShortcutAction;
 import com.vaadin.event.ShortcutAction.KeyCode;
 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;
@@ -88,28 +84,6 @@ public class Button extends AbstractComponent implements
         addListener(listener);
     }
 
-    /**
-     * Paints the content of this component.
-     * 
-     * @param event
-     *            the PaintEvent.
-     * @throws IOException
-     *             if the writing failed due to input/output error.
-     * @throws PaintException
-     *             if the paint operation failed.
-     */
-    @Override
-    public void paintContent(PaintTarget target) throws PaintException {
-        super.paintContent(target);
-
-        if (isDisableOnClick()) {
-            target.addAttribute(VButton.ATTR_DISABLE_ON_CLICK, true);
-        }
-        if (clickShortcut != null) {
-            target.addAttribute("keycode", clickShortcut.getKeyCode());
-        }
-    }
-
     /**
      * Invoked when the value of a variable has changed. Button listeners are
      * notified if the button is clicked.
@@ -404,6 +378,7 @@ public class Button extends AbstractComponent implements
         }
         clickShortcut = new ClickShortcut(this, keyCode, modifiers);
         addShortcutListener(clickShortcut);
+        getState().setClickShortcutKeyCode(clickShortcut.getKeyCode());
     }
 
     /**
@@ -414,6 +389,7 @@ public class Button extends AbstractComponent implements
         if (clickShortcut != null) {
             removeShortcutListener(clickShortcut);
             clickShortcut = null;
+            getState().setClickShortcutKeyCode(0);
         }
     }