From 23bca2af5ed2080e7bbe2e36791c2208f91d8084 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Tue, 21 Feb 2012 16:17:57 +0200 Subject: [PATCH] #8304 Changed Button to solely use shared state --- .../gwt/client/ApplicationConnection.java | 2 +- .../gwt/client/communication/JsonDecoder.java | 34 ++++++------------- .../client/communication/SerializerMap.java | 7 ++++ .../communication/SerializerMapImpl.java | 19 +++++++++++ .../terminal/gwt/client/ui/VButton.java | 2 -- .../gwt/client/ui/VButtonPaintable.java | 14 ++++---- .../terminal/gwt/client/ui/VButtonState.java | 9 +++++ .../client/ui/VButtonState_Serializer.java | 5 +++ .../gwt/client/ui/VNativeButtonPaintable.java | 9 +++-- .../vaadin/terminal/gwt/server/JsonCodec.java | 6 ++-- src/com/vaadin/ui/Button.java | 28 ++------------- 11 files changed, 72 insertions(+), 63 deletions(-) create mode 100644 src/com/vaadin/terminal/gwt/client/communication/SerializerMap.java create mode 100644 src/com/vaadin/terminal/gwt/client/communication/SerializerMapImpl.java diff --git a/src/com/vaadin/terminal/gwt/client/ApplicationConnection.java b/src/com/vaadin/terminal/gwt/client/ApplicationConnection.java index 1737f82e55..ee6091e925 100644 --- a/src/com/vaadin/terminal/gwt/client/ApplicationConnection.java +++ b/src/com/vaadin/terminal/gwt/client/ApplicationConnection.java @@ -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); diff --git a/src/com/vaadin/terminal/gwt/client/communication/JsonDecoder.java b/src/com/vaadin/terminal/gwt/client/communication/JsonDecoder.java index 4239845e4e..e87db8259f 100644 --- a/src/com/vaadin/terminal/gwt/client/communication/JsonDecoder.java +++ b/src/com/vaadin/terminal/gwt/client/communication/JsonDecoder.java @@ -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 convertMap(JSONObject jsonMap, + private static Map convertMap(JSONObject jsonMap, VPaintableMap idMapper) { HashMap map = new HashMap(); Iterator 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 index 0000000000..8fd791d047 --- /dev/null +++ b/src/com/vaadin/terminal/gwt/client/communication/SerializerMap.java @@ -0,0 +1,7 @@ +package com.vaadin.terminal.gwt.client.communication; + +public interface SerializerMap { + + public VaadinSerializer getSerializer(String type); + +} 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 index 0000000000..eb37db3eb0 --- /dev/null +++ b/src/com/vaadin/terminal/gwt/client/communication/SerializerMapImpl.java @@ -0,0 +1,19 @@ +package com.vaadin.terminal.gwt.client.communication; + +import com.google.gwt.core.client.GWT; +import com.vaadin.terminal.gwt.client.ComponentState_Serializer; +import com.vaadin.terminal.gwt.client.ui.VButtonState_Serializer; + +public class SerializerMapImpl implements SerializerMap { + + public VaadinSerializer getSerializer(String type) { + // TODO This should be in a separate class and constructed by a + // generator + if (type.equals("com.vaadin.terminal.gwt.client.ui.VButtonState")) { + return GWT.create(VButtonState_Serializer.class); + } + + return GWT.create(ComponentState_Serializer.class); + } + +} diff --git a/src/com/vaadin/terminal/gwt/client/ui/VButton.java b/src/com/vaadin/terminal/gwt/client/ui/VButton.java index e04403d00e..9797349d9e 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VButton.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VButton.java @@ -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; diff --git a/src/com/vaadin/terminal/gwt/client/ui/VButtonPaintable.java b/src/com/vaadin/terminal/gwt/client/ui/VButtonPaintable.java index ea227b1854..f934571ac5 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VButtonPaintable.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VButtonPaintable.java @@ -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(); + } } diff --git a/src/com/vaadin/terminal/gwt/client/ui/VButtonState.java b/src/com/vaadin/terminal/gwt/client/ui/VButtonState.java index b065818040..f4ed071bef 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VButtonState.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VButtonState.java @@ -4,6 +4,7 @@ import com.vaadin.terminal.gwt.client.ComponentState; public class VButtonState extends ComponentState { private boolean disableOnClick = false; + private int clickShortcutKeyCode = 0; public boolean isDisableOnClick() { return disableOnClick; @@ -13,4 +14,12 @@ public class VButtonState extends ComponentState { this.disableOnClick = disableOnClick; } + public int getClickShortcutKeyCode() { + return clickShortcutKeyCode; + } + + public void setClickShortcutKeyCode(int clickShortcutKeyCode) { + this.clickShortcutKeyCode = clickShortcutKeyCode; + } + } diff --git a/src/com/vaadin/terminal/gwt/client/ui/VButtonState_Serializer.java b/src/com/vaadin/terminal/gwt/client/ui/VButtonState_Serializer.java index 9ac1fab836..31cb9ab70f 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VButtonState_Serializer.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VButtonState_Serializer.java @@ -24,6 +24,11 @@ public class VButtonState_Serializer implements VaadinSerializer { state.setDisableOnClick((Boolean) JsonDecoder.convertValue( jsonDisableOnClick, idMapper)); + JSONArray jsonClickShortcutKeyCode = (JSONArray) jsonValue + .get("clickShortcutKeyCode"); + state.setClickShortcutKeyCode((Integer) JsonDecoder.convertValue( + jsonClickShortcutKeyCode, idMapper)); + return state; } } diff --git a/src/com/vaadin/terminal/gwt/client/ui/VNativeButtonPaintable.java b/src/com/vaadin/terminal/gwt/client/ui/VNativeButtonPaintable.java index 4257597e2d..2ee71658b5 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VNativeButtonPaintable.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VNativeButtonPaintable.java @@ -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 diff --git a/src/com/vaadin/terminal/gwt/server/JsonCodec.java b/src/com/vaadin/terminal/gwt/server/JsonCodec.java index 348f4a6f0f..d8964f85a6 100644 --- a/src/com/vaadin/terminal/gwt/server/JsonCodec.java +++ b/src/com/vaadin/terminal/gwt/server/JsonCodec.java @@ -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); diff --git a/src/com/vaadin/ui/Button.java b/src/com/vaadin/ui/Button.java index b88cab81d9..4ca274abf9 100644 --- a/src/com/vaadin/ui/Button.java +++ b/src/com/vaadin/ui/Button.java @@ -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); } } -- 2.39.5