From: Leif Åstrand Date: Wed, 30 May 2012 12:13:26 +0000 (+0300) Subject: Wrap variable change data in UidlValue wrapper with type info (#8878) X-Git-Tag: 7.0.0.alpha3~208 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=f633581ab653be38bef09d1f68b361409cdea108;p=vaadin-framework.git Wrap variable change data in UidlValue wrapper with type info (#8878) This is the first step towards dropping type info from the sent data for everything except changeVariables data --- diff --git a/src/com/vaadin/terminal/gwt/client/ApplicationConnection.java b/src/com/vaadin/terminal/gwt/client/ApplicationConnection.java index c3bdc405aa..530fd546db 100644 --- a/src/com/vaadin/terminal/gwt/client/ApplicationConnection.java +++ b/src/com/vaadin/terminal/gwt/client/ApplicationConnection.java @@ -45,6 +45,7 @@ import com.vaadin.terminal.gwt.client.communication.MethodInvocation; import com.vaadin.terminal.gwt.client.communication.RpcManager; import com.vaadin.terminal.gwt.client.communication.SerializerMap; import com.vaadin.terminal.gwt.client.communication.StateChangeEvent; +import com.vaadin.terminal.gwt.client.communication.UidlValue; import com.vaadin.terminal.gwt.client.ui.AbstractComponentConnector; import com.vaadin.terminal.gwt.client.ui.VContextMenu; import com.vaadin.terminal.gwt.client.ui.dd.VDragAndDropManager; @@ -1600,7 +1601,7 @@ public class ApplicationConnection { // TODO could eliminate invocations of same shared variable setter addMethodInvocationToQueue(new MethodInvocation(connectorId, UPDATE_VARIABLE_INTERFACE, UPDATE_VARIABLE_METHOD, - new Object[] { variableName, value }), immediate); + new Object[] { variableName, new UidlValue(value) }), immediate); } /** diff --git a/src/com/vaadin/terminal/gwt/client/communication/JsonEncoder.java b/src/com/vaadin/terminal/gwt/client/communication/JsonEncoder.java index f09536a9f7..edd900174a 100644 --- a/src/com/vaadin/terminal/gwt/client/communication/JsonEncoder.java +++ b/src/com/vaadin/terminal/gwt/client/communication/JsonEncoder.java @@ -46,6 +46,12 @@ public class JsonEncoder { public static final String VTYPE_SET = "q"; public static final String VTYPE_NULL = "n"; + /** + * Temporary hack to get variable changes decoded as internal types in + * JsonCodec. + */ + public static final String VTYPE_UIDL_VALUE = "v"; + /** * Encode a value to a JSON representation for transport from the client to * the server. @@ -98,6 +104,9 @@ public class JsonEncoder { } else if (value instanceof Collection) { return encodeCollection((Collection) value, restrictToInternalTypes, connectorMap, connection); + } else if (value instanceof UidlValue) { + return encodeVariableChange((UidlValue) value, connectorMap, + connection); } else { String transportType = getTransportType(value); if (transportType != null) { @@ -117,6 +126,14 @@ public class JsonEncoder { } } + private static JSONValue encodeVariableChange(UidlValue value, + ConnectorMap connectorMap, ApplicationConnection connection) { + JSONArray encodedValue = encode(value.getValue(), true, connectorMap, + connection).isArray(); + + return combineTypeAndValue(VTYPE_UIDL_VALUE, encodedValue); + } + private static JSONValue encodeMap(Map map, boolean restrictToInternalTypes, ConnectorMap connectorMap, ApplicationConnection connection) { diff --git a/src/com/vaadin/terminal/gwt/client/communication/UidlValue.java b/src/com/vaadin/terminal/gwt/client/communication/UidlValue.java new file mode 100644 index 0000000000..2a21074037 --- /dev/null +++ b/src/com/vaadin/terminal/gwt/client/communication/UidlValue.java @@ -0,0 +1,25 @@ +/* +@VaadinApache2LicenseForJavaFiles@ + */ + +package com.vaadin.terminal.gwt.client.communication; + +import java.io.Serializable; + +public class UidlValue implements Serializable { + private Object value; + + public UidlValue(Object value) { + this.value = value; + } + + public Object getValue() { + return value; + } + + @Override + public String toString() { + return "" + value; + } + +} diff --git a/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java b/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java index 15b148ce7f..5ead96c42e 100644 --- a/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java +++ b/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java @@ -68,6 +68,7 @@ import com.vaadin.terminal.gwt.client.ApplicationConnection; import com.vaadin.terminal.gwt.client.Connector; import com.vaadin.terminal.gwt.client.communication.MethodInvocation; import com.vaadin.terminal.gwt.client.communication.SharedState; +import com.vaadin.terminal.gwt.client.communication.UidlValue; import com.vaadin.terminal.gwt.server.BootstrapHandler.BootstrapContext; import com.vaadin.terminal.gwt.server.ComponentSizeValidator.InvalidLayout; import com.vaadin.ui.AbstractComponent; @@ -1630,9 +1631,11 @@ public abstract class AbstractCommunicationManager implements Serializable { String variableName = (String) JsonCodec .decodeInternalType(String.class, true, parametersJson.getJSONArray(0), application); - Object value = JsonCodec.decodeInternalType( + UidlValue uidlValue = (UidlValue) JsonCodec.decodeInternalType( parametersJson.getJSONArray(1), application); + Object value = uidlValue.getValue(); + if (previousInvocation != null && previousInvocation.getConnectorId().equals(connectorId)) { previousInvocation.setVariableChange(variableName, value); diff --git a/src/com/vaadin/terminal/gwt/server/JsonCodec.java b/src/com/vaadin/terminal/gwt/server/JsonCodec.java index e082eca47e..9c7abc53b4 100644 --- a/src/com/vaadin/terminal/gwt/server/JsonCodec.java +++ b/src/com/vaadin/terminal/gwt/server/JsonCodec.java @@ -13,6 +13,7 @@ import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; @@ -27,6 +28,7 @@ import com.vaadin.external.json.JSONException; import com.vaadin.external.json.JSONObject; import com.vaadin.terminal.gwt.client.Connector; import com.vaadin.terminal.gwt.client.communication.JsonEncoder; +import com.vaadin.terminal.gwt.client.communication.UidlValue; import com.vaadin.ui.Component; /** @@ -64,6 +66,7 @@ public class JsonCodec implements Serializable { registerType(HashMap.class, JsonEncoder.VTYPE_MAP); registerType(List.class, JsonEncoder.VTYPE_LIST); registerType(Set.class, JsonEncoder.VTYPE_SET); + registerType(UidlValue.class, JsonEncoder.VTYPE_UIDL_VALUE); } private static void registerType(Class type, String transportType) { @@ -195,6 +198,13 @@ public class JsonCodec implements Serializable { if (JsonEncoder.VTYPE_NULL.equals(encodedTransportType)) { return null; } + + // VariableChange + if (JsonEncoder.VTYPE_UIDL_VALUE.contentEquals(transportType)) { + return decodeVariableChange((JSONArray) encodedJsonValue, + application); + } + // Collections if (JsonEncoder.VTYPE_LIST.equals(transportType)) { return decodeList(targetType, restrictToInternalTypes, @@ -244,6 +254,17 @@ public class JsonCodec implements Serializable { throw new JSONException("Unknown type " + transportType); } + private static UidlValue decodeVariableChange(JSONArray encodedJsonValue, + Application application) throws JSONException { + String type = encodedJsonValue.getString(0); + + // Fake format used by decode method + JSONArray valueAndType = new JSONArray(Arrays.asList(type, + encodedJsonValue.get(1))); + Object decodedValue = decodeInternalType(valueAndType, application); + return new UidlValue(decodedValue); + } + private static boolean transportTypesCompatible( String encodedTransportType, String transportType) { if (encodedTransportType == null) {