]> source.dussan.org Git - vaadin-framework.git/commitdiff
#8555 Listener for modifying state before it is sent to the client
authorArtur Signell <artur@vaadin.com>
Thu, 22 Mar 2012 08:25:48 +0000 (10:25 +0200)
committerArtur Signell <artur@vaadin.com>
Thu, 22 Mar 2012 11:47:19 +0000 (13:47 +0200)
src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java
src/com/vaadin/ui/AbstractComponent.java
src/com/vaadin/ui/AbstractField.java
src/com/vaadin/ui/Component.java

index 8ba17e1c0c0cab8395469b3377f60ee22ec29d20..337f6ec3f8d5bb3950935a35d70a6200356c7b3e 100644 (file)
@@ -763,6 +763,11 @@ public abstract class AbstractCommunicationManager implements Serializable {
 
         System.out.println("* Found " + dirtyVisibleConnectors.size()
                 + " dirty connectors to paint");
+        for (ClientConnector connector : dirtyVisibleConnectors) {
+            if (connector instanceof Component) {
+                ((Component) connector).updateState();
+            }
+        }
         rootConnectorTracker.markAllComponentsClean();
 
         outWriter.print("\"changes\":[");
index ef51ed8a25b7162bf84f9ee9c4b0d855ffb161d5..b1edc0dd4350367f3a205d7df289ec81b22780fd 100644 (file)
@@ -841,33 +841,39 @@ public abstract class AbstractComponent implements Component, MethodEventSource
         if (null == sharedState) {
             sharedState = createState();
         }
+        return sharedState;
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see com.vaadin.ui.Component#updateState()
+     */
+    public void updateState() {
         // TODO This logic should be on the client side and the state should
         // simply be a data object with "width" and "height".
         if (getHeight() >= 0
                 && (getHeightUnits() != Unit.PERCENTAGE || ComponentSizeValidator
                         .parentCanDefineHeight(this))) {
-            sharedState.setHeight("" + getCSSHeight());
+            getState().setHeight("" + getCSSHeight());
         } else {
-            sharedState.setHeight("");
+            getState().setHeight("");
         }
 
         if (getWidth() >= 0
                 && (getWidthUnits() != Unit.PERCENTAGE || ComponentSizeValidator
                         .parentCanDefineWidth(this))) {
-            sharedState.setWidth("" + getCSSWidth());
+            getState().setWidth("" + getCSSWidth());
         } else {
-            sharedState.setWidth("");
+            getState().setWidth("");
         }
 
-        // TODO this should be in a listener called before sending state
         ErrorMessage error = getErrorMessage();
         if (null != error) {
-            sharedState.setErrorMessage(error.getFormattedHtmlMessage());
+            getState().setErrorMessage(error.getFormattedHtmlMessage());
         } else {
-            sharedState.setErrorMessage(null);
+            getState().setErrorMessage(null);
         }
-
-        return sharedState;
     }
 
     /**
index 3fbcfd6b64a04b095cd78d9cd15a38c24c3ff318..9f24bfb1d703f8e354c7679db5def98f224189c2 100644 (file)
@@ -130,13 +130,6 @@ public abstract class AbstractField<T> extends AbstractComponent implements
      */
     private int tabIndex = 0;
 
-    /**
-     * Required field.
-     */
-    // TODO should be used directly from shared state, but requires a listener
-    // for updating state before it is sent
-    private boolean required = false;
-
     /**
      * The error message for the exception that is thrown when the field is
      * required but empty.
@@ -1406,7 +1399,7 @@ public abstract class AbstractField<T> extends AbstractComponent implements
      *         <code>false</code>.
      */
     public boolean isRequired() {
-        return required;
+        return getState().isRequired();
     }
 
     /**
@@ -1425,7 +1418,7 @@ public abstract class AbstractField<T> extends AbstractComponent implements
      *            Is the field required.
      */
     public void setRequired(boolean required) {
-        this.required = required;
+        getState().setRequired(required);
         requestRepaint();
     }
 
@@ -1615,17 +1608,15 @@ public abstract class AbstractField<T> extends AbstractComponent implements
 
     @Override
     public AbstractFieldState getState() {
-        AbstractFieldState state = (AbstractFieldState) super.getState();
+        return (AbstractFieldState) super.getState();
+    }
 
-        // TODO should be directly in state when listener for updates before
-        // sending state is implemented
-        state.setRequired(isRequired());
+    @Override
+    public void updateState() {
+        super.updateState();
 
         // Hide the error indicator if needed
-        // TODO these should be in a listener called before sending state
-        state.setHideErrors(shouldHideErrors());
-
-        return state;
+        getState().setHideErrors(shouldHideErrors());
     }
 
     @Override
index 3fe71dfea1de1cfbfe445fad55790b801a4a8662..3589b43391f368f21b8027ff6148e12d3669179a 100644 (file)
@@ -706,6 +706,18 @@ public interface Component extends ClientConnector, Paintable, VariableOwner,
      */
     public ComponentState getState();
 
+    /**
+     * Called before the shared state is sent to the client. Gives the component
+     * an opportunity to set computed/dynamic state values e.g. state values
+     * that depend on other component features.
+     * <p>
+     * This method must not alter the component hierarchy in any way.
+     * </p>
+     * 
+     * @since 7.0
+     */
+    public void updateState();
+
     /* Component event framework */
 
     /**