]> source.dussan.org Git - vaadin-framework.git/commitdiff
Add helper for updating the diffstate of a component
authorLeif Åstrand <leif@vaadin.com>
Tue, 22 Nov 2016 07:22:56 +0000 (09:22 +0200)
committerIlia Motornyi <elmot@vaadin.com>
Wed, 23 Nov 2016 13:49:51 +0000 (13:49 +0000)
Change-Id: I7a7068bd6fcc86a8bbbc8693d8be4a388ce55a25

server/src/main/java/com/vaadin/server/AbstractClientConnector.java
server/src/main/java/com/vaadin/ui/AbstractTextField.java
server/src/main/java/com/vaadin/ui/Button.java
server/src/main/java/com/vaadin/ui/RichTextArea.java
server/src/main/java/com/vaadin/ui/Slider.java

index 9f5416968c9403a52ecde03d743e7e6fe96afbf7..32f62edb8e6bd83ea08af7dfb9c8beb237af4571 100644 (file)
@@ -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);
+    }
 }
index 831b0ba6c6fed65ca589d2688a10f2203edbd468..07044bf47228d6f6a5c45e024475cfb6b913053c 100644 (file)
@@ -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<String>
 
         @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);
         }
index f18ad276d991e8233ec1a1d4760fbc17d404728c..4a8e9b4b928e51b2fc9b872ef97fcfa84864b5b0 100644 (file)
@@ -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));
         }
     };
 
index 96a639e285b3d7ff94874d87bbf98c4f30809278..44ecb7aae05d745ede3dd6f978225db7e8f1d457 100644 (file)
@@ -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<String>
     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<String>
      * 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
index f6cc37f61acc9056ca9f58e2e76330f0fa3d0807..16c287d92c5dfc2e14f3dd95d8e4cda5353fd0cd 100644 (file)
@@ -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<Double> {
          *
          * See #12133.
          */
-        getUI().getConnectorTracker().getDiffState(Slider.this).put("value",
-                value);
+        updateDiffstate("value", Json.create(value));
 
         try {
             setValue(value, true);