From: Artur Signell Date: Thu, 22 Mar 2012 08:25:48 +0000 (+0200) Subject: #8555 Listener for modifying state before it is sent to the client X-Git-Tag: 7.0.0.alpha2~248 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=009a9e074a584d44ae095b27cc7f1170c10e110c;p=vaadin-framework.git #8555 Listener for modifying state before it is sent to the client --- diff --git a/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java b/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java index 8ba17e1c0c..337f6ec3f8 100644 --- a/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java +++ b/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java @@ -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\":["); diff --git a/src/com/vaadin/ui/AbstractComponent.java b/src/com/vaadin/ui/AbstractComponent.java index ef51ed8a25..b1edc0dd43 100644 --- a/src/com/vaadin/ui/AbstractComponent.java +++ b/src/com/vaadin/ui/AbstractComponent.java @@ -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; } /** diff --git a/src/com/vaadin/ui/AbstractField.java b/src/com/vaadin/ui/AbstractField.java index 3fbcfd6b64..9f24bfb1d7 100644 --- a/src/com/vaadin/ui/AbstractField.java +++ b/src/com/vaadin/ui/AbstractField.java @@ -130,13 +130,6 @@ public abstract class AbstractField 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 extends AbstractComponent implements * false. */ public boolean isRequired() { - return required; + return getState().isRequired(); } /** @@ -1425,7 +1418,7 @@ public abstract class AbstractField 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 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 diff --git a/src/com/vaadin/ui/Component.java b/src/com/vaadin/ui/Component.java index 3fe71dfea1..3589b43391 100644 --- a/src/com/vaadin/ui/Component.java +++ b/src/com/vaadin/ui/Component.java @@ -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. + *

+ * This method must not alter the component hierarchy in any way. + *

+ * + * @since 7.0 + */ + public void updateState(); + /* Component event framework */ /**