From fdc80972f55adc3bc2b987d40d3b4e628635f42f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leif=20=C3=85strand?= Date: Tue, 22 Nov 2016 09:22:56 +0200 Subject: [PATCH] Add helper for updating the diffstate of a component Change-Id: I7a7068bd6fcc86a8bbbc8693d8be4a388ce55a25 --- .../server/AbstractClientConnector.java | 29 +++++++++++++++++++ .../java/com/vaadin/ui/AbstractTextField.java | 6 ++-- .../src/main/java/com/vaadin/ui/Button.java | 5 ++-- .../main/java/com/vaadin/ui/RichTextArea.java | 7 +++-- .../src/main/java/com/vaadin/ui/Slider.java | 5 ++-- 5 files changed, 43 insertions(+), 9 deletions(-) diff --git a/server/src/main/java/com/vaadin/server/AbstractClientConnector.java b/server/src/main/java/com/vaadin/server/AbstractClientConnector.java index 9f5416968c..32f62edb8e 100644 --- a/server/src/main/java/com/vaadin/server/AbstractClientConnector.java +++ b/server/src/main/java/com/vaadin/server/AbstractClientConnector.java @@ -47,6 +47,7 @@ import com.vaadin.ui.LegacyComponent; import com.vaadin.ui.UI; import elemental.json.JsonObject; +import elemental.json.JsonValue; /** * An abstract base class for ClientConnector implementations. This class @@ -1083,4 +1084,32 @@ public abstract class AbstractClientConnector public int hashCode() { return super.hashCode(); } + + /** + * Sets the expected value of a state property so that changes can be + * properly sent to the client. This needs to be done in cases where a state + * change originates from the client, since otherwise the server-side would + * fail to recognize if the value is changed back to its previous value. + * + * @param propertyName + * the name of the shared state property to update + * @param newValue + * the new diffstate reference value + */ + protected void updateDiffstate(String propertyName, JsonValue newValue) { + if (!isAttached()) { + return; + } + + JsonObject diffState = getUI().getConnectorTracker().getDiffState(this); + if (diffState == null) { + return; + } + + assert diffState.hasKey(propertyName) : "Diffstate for " + + getClass().getName() + " has no property named " + + propertyName; + + diffState.put(propertyName, newValue); + } } diff --git a/server/src/main/java/com/vaadin/ui/AbstractTextField.java b/server/src/main/java/com/vaadin/ui/AbstractTextField.java index 831b0ba6c6..07044bf472 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractTextField.java +++ b/server/src/main/java/com/vaadin/ui/AbstractTextField.java @@ -35,6 +35,8 @@ import com.vaadin.shared.ui.textfield.AbstractTextFieldState; import com.vaadin.ui.declarative.DesignAttributeHandler; import com.vaadin.ui.declarative.DesignContext; +import elemental.json.Json; + /** * Abstract base class for text input components. * @@ -49,8 +51,8 @@ public abstract class AbstractTextField extends AbstractField @Override public void setText(String text, int cursorPosition) { - getUI().getConnectorTracker().getDiffState(AbstractTextField.this) - .put("text", text); + updateDiffstate("text", Json.create(text)); + lastKnownCursorPosition = cursorPosition; setValue(text, true); } diff --git a/server/src/main/java/com/vaadin/ui/Button.java b/server/src/main/java/com/vaadin/ui/Button.java index f18ad276d9..4a8e9b4b92 100644 --- a/server/src/main/java/com/vaadin/ui/Button.java +++ b/server/src/main/java/com/vaadin/ui/Button.java @@ -38,6 +38,8 @@ import com.vaadin.ui.declarative.DesignContext; import com.vaadin.ui.declarative.DesignFormatter; import com.vaadin.util.ReflectTools; +import elemental.json.Json; + /** * A generic button component. * @@ -61,8 +63,7 @@ public class Button extends AbstractFocusable // Makes sure the enabled=false state is noticed at once - otherwise // a following setEnabled(true) call might have no effect. see // ticket #10030 - getUI().getConnectorTracker().getDiffState(Button.this) - .put("enabled", false); + updateDiffstate("enabled", Json.create(false)); } }; diff --git a/server/src/main/java/com/vaadin/ui/RichTextArea.java b/server/src/main/java/com/vaadin/ui/RichTextArea.java index 96a639e285..44ecb7aae0 100644 --- a/server/src/main/java/com/vaadin/ui/RichTextArea.java +++ b/server/src/main/java/com/vaadin/ui/RichTextArea.java @@ -26,6 +26,8 @@ import com.vaadin.shared.ui.richtextarea.RichTextAreaServerRpc; import com.vaadin.shared.ui.richtextarea.RichTextAreaState; import com.vaadin.ui.declarative.DesignContext; +import elemental.json.Json; + /** * A simple RichTextArea to edit HTML format text. */ @@ -35,8 +37,7 @@ public class RichTextArea extends AbstractField private class RichTextAreaServerRpcImpl implements RichTextAreaServerRpc { @Override public void setText(String text) { - getUI().getConnectorTracker().getDiffState(RichTextArea.this) - .put("value", text); + updateDiffstate("value", Json.create(text)); if (!setValue(text, true)) { // The value was not updated, this could happen if the field has // been set to readonly on the server and the client does not @@ -106,7 +107,7 @@ public class RichTextArea extends AbstractField * Sets the value of this object. If the new value is not equal to * {@code getValue()}, fires a {@link ValueChangeEvent}. Throws * {@code NullPointerException} if the value is null. - * + * * @param value * the new value, not {@code null} * @throws NullPointerException diff --git a/server/src/main/java/com/vaadin/ui/Slider.java b/server/src/main/java/com/vaadin/ui/Slider.java index f6cc37f61a..16c287d92c 100644 --- a/server/src/main/java/com/vaadin/ui/Slider.java +++ b/server/src/main/java/com/vaadin/ui/Slider.java @@ -28,6 +28,8 @@ import com.vaadin.shared.ui.slider.SliderState; import com.vaadin.ui.declarative.DesignAttributeHandler; import com.vaadin.ui.declarative.DesignContext; +import elemental.json.Json; + /** * A component for selecting a numerical value within a range. * @@ -45,8 +47,7 @@ public class Slider extends AbstractField { * * See #12133. */ - getUI().getConnectorTracker().getDiffState(Slider.this).put("value", - value); + updateDiffstate("value", Json.create(value)); try { setValue(value, true); -- 2.39.5