From 5f1c86d93e5b295c0909d792ef20a53c405aa0a1 Mon Sep 17 00:00:00 2001 From: Henri Sara Date: Fri, 16 Mar 2012 16:29:01 +0200 Subject: [PATCH] Transfer required attribute in shared state (#8436). The flag should be used directly from shared state, but that requires error message etc. related updates to be done in a listener before sending states. --- .../gwt/client/AbstractFieldState.java | 25 +++++++++++++++++++ .../vaadin/terminal/gwt/client/VCaption.java | 19 +++++++------- .../client/ui/AbstractComponentConnector.java | 12 ++------- .../gwt/client/ui/AbstractFieldConnector.java | 12 +++++++++ .../terminal/gwt/client/ui/VFormLayout.java | 4 ++- src/com/vaadin/ui/AbstractComponent.java | 1 + src/com/vaadin/ui/AbstractField.java | 21 ++++++++-------- src/com/vaadin/ui/Select.java | 7 ------ 8 files changed, 63 insertions(+), 38 deletions(-) diff --git a/src/com/vaadin/terminal/gwt/client/AbstractFieldState.java b/src/com/vaadin/terminal/gwt/client/AbstractFieldState.java index bf84e2e606..ba67902fae 100644 --- a/src/com/vaadin/terminal/gwt/client/AbstractFieldState.java +++ b/src/com/vaadin/terminal/gwt/client/AbstractFieldState.java @@ -16,6 +16,7 @@ import com.vaadin.ui.AbstractField; public class AbstractFieldState extends ComponentState { private boolean propertyReadOnly = false; private boolean hideErrors = false; + private boolean required = false; /** * Checks if the property data source for the Field is in read only mode. @@ -62,4 +63,28 @@ public class AbstractFieldState extends ComponentState { this.hideErrors = hideErrors; } + /** + * Is the field required. Required fields must filled by the user. + * + * See AbstractField#isRequired() for more information. + * + * @return true if the field is required, otherwise + * false. + */ + public boolean isRequired() { + return required; + } + + /** + * Sets the field required. Required fields must filled by the user. + * + * See AbstractField#setRequired(boolean) for more information. + * + * @param required + * Is the field required. + */ + public void setRequired(boolean required) { + this.required = required; + } + } diff --git a/src/com/vaadin/terminal/gwt/client/VCaption.java b/src/com/vaadin/terminal/gwt/client/VCaption.java index 56af03b8d1..62c6604c58 100644 --- a/src/com/vaadin/terminal/gwt/client/VCaption.java +++ b/src/com/vaadin/terminal/gwt/client/VCaption.java @@ -8,7 +8,7 @@ import com.google.gwt.user.client.DOM; import com.google.gwt.user.client.Element; import com.google.gwt.user.client.Event; import com.google.gwt.user.client.ui.HTML; -import com.vaadin.terminal.gwt.client.ui.AbstractComponentConnector; +import com.vaadin.terminal.gwt.client.ui.AbstractFieldConnector; import com.vaadin.terminal.gwt.client.ui.Icon; import com.vaadin.terminal.gwt.client.ui.TabsheetBaseConnector; @@ -32,8 +32,6 @@ public class VCaption extends HTML { private int maxWidth = -1; - protected static final String ATTRIBUTE_REQUIRED = AbstractComponentConnector.ATTRIBUTE_REQUIRED; - private enum InsertPosition { ICON, CAPTION, REQUIRED, ERROR } @@ -113,12 +111,16 @@ public class VCaption extends HTML { setStyleName(style); boolean hasIcon = owner.getState().getIcon() != null; - boolean showRequired = uidl - .getBooleanAttribute(AbstractComponentConnector.ATTRIBUTE_REQUIRED); + boolean showRequired = false; boolean showError = owner.getState().getErrorMessage() != null; if (owner.getState() instanceof AbstractFieldState) { - showError = showError - && !((AbstractFieldState) owner.getState()).isHideErrors(); + AbstractFieldState abstractFieldState = (AbstractFieldState) owner + .getState(); + showError = showError && !abstractFieldState.isHideErrors(); + } + if (owner instanceof AbstractFieldConnector) { + showRequired = ((AbstractFieldConnector) owner) + .isRequired(); } if (hasIcon) { @@ -391,9 +393,6 @@ public class VCaption extends HTML { return true; } } - if (uidl.hasAttribute(AbstractComponentConnector.ATTRIBUTE_REQUIRED)) { - return true; - } return false; } diff --git a/src/com/vaadin/terminal/gwt/client/ui/AbstractComponentConnector.java b/src/com/vaadin/terminal/gwt/client/ui/AbstractComponentConnector.java index f26aad9d2a..de85f597df 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/AbstractComponentConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/AbstractComponentConnector.java @@ -27,15 +27,6 @@ public abstract class AbstractComponentConnector extends AbstractConnector private ComponentContainerConnector parent; - // Generic UIDL parameter names, to be moved to shared state. - // Attributes are here mainly if they apply to all paintable widgets or - // affect captions - otherwise, they are in the relevant subclasses. - // For e.g. item or context specific attributes, subclasses may use separate - // constants, which may refer to these. - // Not all references to the string literals have been converted to use - // these! - public static final String ATTRIBUTE_REQUIRED = "required"; - private Widget widget; /* State variables */ @@ -358,7 +349,8 @@ public abstract class AbstractComponentConnector extends AbstractConnector styleBuf.append(ApplicationConnection.ERROR_CLASSNAME_EXT); } // add required style to required components - if (uidl.hasAttribute(ATTRIBUTE_REQUIRED)) { + if (connector instanceof AbstractFieldConnector + && ((AbstractFieldConnector) connector).isRequired()) { styleBuf.append(" "); styleBuf.append(primaryStyleName); styleBuf.append(ApplicationConnection.REQUIRED_CLASSNAME_EXT); diff --git a/src/com/vaadin/terminal/gwt/client/ui/AbstractFieldConnector.java b/src/com/vaadin/terminal/gwt/client/ui/AbstractFieldConnector.java index 7bdd4e0255..b2c7101c15 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/AbstractFieldConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/AbstractFieldConnector.java @@ -24,4 +24,16 @@ public abstract class AbstractFieldConnector extends AbstractComponentConnector return super.isReadOnly() || getState().isPropertyReadOnly(); } + /** + * Checks whether the required indicator should be shown for the field. + * + * Required indicators are hidden if the field or its data source is + * read-only. + * + * @return true if required indicator should be shown + */ + public boolean isRequired() { + return getState().isRequired() && !isReadOnly(); + } + } diff --git a/src/com/vaadin/terminal/gwt/client/ui/VFormLayout.java b/src/com/vaadin/terminal/gwt/client/ui/VFormLayout.java index 92d15df618..3ed63b9702 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VFormLayout.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VFormLayout.java @@ -289,7 +289,9 @@ public class VFormLayout extends SimplePanel { removeStyleDependentName("hasdescription"); } - if (uidl.getBooleanAttribute(AbstractComponentConnector.ATTRIBUTE_REQUIRED)) { + boolean required = owner instanceof AbstractFieldConnector + && ((AbstractFieldConnector) owner).isRequired(); + if (required) { if (requiredFieldIndicator == null) { requiredFieldIndicator = DOM.createSpan(); DOM.setInnerText(requiredFieldIndicator, "*"); diff --git a/src/com/vaadin/ui/AbstractComponent.java b/src/com/vaadin/ui/AbstractComponent.java index 4da172d395..ef51ed8a25 100644 --- a/src/com/vaadin/ui/AbstractComponent.java +++ b/src/com/vaadin/ui/AbstractComponent.java @@ -859,6 +859,7 @@ public abstract class AbstractComponent implements Component, MethodEventSource sharedState.setWidth(""); } + // TODO this should be in a listener called before sending state ErrorMessage error = getErrorMessage(); if (null != error) { sharedState.setErrorMessage(error.getFormattedHtmlMessage()); diff --git a/src/com/vaadin/ui/AbstractField.java b/src/com/vaadin/ui/AbstractField.java index 4380506505..3fbcfd6b64 100644 --- a/src/com/vaadin/ui/AbstractField.java +++ b/src/com/vaadin/ui/AbstractField.java @@ -32,7 +32,6 @@ import com.vaadin.terminal.ErrorMessage; import com.vaadin.terminal.PaintException; import com.vaadin.terminal.PaintTarget; import com.vaadin.terminal.gwt.client.AbstractFieldState; -import com.vaadin.terminal.gwt.client.ui.AbstractComponentConnector; /** *

@@ -134,6 +133,8 @@ public abstract class AbstractField extends AbstractComponent implements /** * 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; /** @@ -172,12 +173,6 @@ public abstract class AbstractField extends AbstractComponent implements if (isModified()) { target.addAttribute("modified", true); } - - // Adds the required attribute - if (!isReadOnly() && isRequired()) { - target.addAttribute(AbstractComponentConnector.ATTRIBUTE_REQUIRED, - true); - } } /** @@ -192,8 +187,9 @@ public abstract class AbstractField extends AbstractComponent implements * to show it when there are errors */ protected boolean shouldHideErrors() { - return isRequired() && isEmpty() && getComponentError() == null - && getErrorMessage() != null; + // getErrorMessage() can still return something else than null based on + // validation etc. + return isRequired() && isEmpty() && getComponentError() == null; } /** @@ -1406,7 +1402,7 @@ public abstract class AbstractField extends AbstractComponent implements * field isEmpty() regardless of any attached validators. * * - * @return true if the field is required .otherwise + * @return true if the field is required, otherwise * false. */ public boolean isRequired() { @@ -1621,7 +1617,12 @@ public abstract class AbstractField extends AbstractComponent implements public AbstractFieldState getState() { AbstractFieldState state = (AbstractFieldState) super.getState(); + // TODO should be directly in state when listener for updates before + // sending state is implemented + state.setRequired(isRequired()); + // Hide the error indicator if needed + // TODO these should be in a listener called before sending state state.setHideErrors(shouldHideErrors()); return state; diff --git a/src/com/vaadin/ui/Select.java b/src/com/vaadin/ui/Select.java index 173e96c16d..3bbfa29f2b 100644 --- a/src/com/vaadin/ui/Select.java +++ b/src/com/vaadin/ui/Select.java @@ -23,7 +23,6 @@ import com.vaadin.event.FieldEvents.FocusListener; import com.vaadin.terminal.PaintException; import com.vaadin.terminal.PaintTarget; import com.vaadin.terminal.Resource; -import com.vaadin.terminal.gwt.client.ui.AbstractComponentConnector; import com.vaadin.terminal.gwt.client.ui.ComboBoxConnector; /** @@ -148,12 +147,6 @@ public class Select extends AbstractSelect implements AbstractSelect.Filtering, target.addAttribute("modified", true); } - // Adds the required attribute - if (!isReadOnly() && isRequired()) { - target.addAttribute(AbstractComponentConnector.ATTRIBUTE_REQUIRED, - true); - } - if (isNewItemsAllowed()) { target.addAttribute("allownewitem", true); } -- 2.39.5