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.
this.hideErrors = hideErrors;
}
+ /**
+ * Is the field required. Required fields must filled by the user.
+ *
+ * See AbstractField#isRequired() for more information.
+ *
+ * @return <code>true</code> if the field is required, otherwise
+ * <code>false</code>.
+ */
+ 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;
+ }
+
}
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;
private int maxWidth = -1;
- protected static final String ATTRIBUTE_REQUIRED = AbstractComponentConnector.ATTRIBUTE_REQUIRED;
-
private enum InsertPosition {
ICON, CAPTION, REQUIRED, ERROR
}
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) {
return true;
}
}
- if (uidl.hasAttribute(AbstractComponentConnector.ATTRIBUTE_REQUIRED)) {
- return true;
- }
return false;
}
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 */
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);
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();
+ }
+
}
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, "*");
sharedState.setWidth("");
}
+ // TODO this should be in a listener called before sending state
ErrorMessage error = getErrorMessage();
if (null != error) {
sharedState.setErrorMessage(error.getFormattedHtmlMessage());
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;
/**
* <p>
/**
* 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;
/**
if (isModified()) {
target.addAttribute("modified", true);
}
-
- // Adds the required attribute
- if (!isReadOnly() && isRequired()) {
- target.addAttribute(AbstractComponentConnector.ATTRIBUTE_REQUIRED,
- true);
- }
}
/**
* 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;
}
/**
* field isEmpty() regardless of any attached validators.
*
*
- * @return <code>true</code> if the field is required .otherwise
+ * @return <code>true</code> if the field is required, otherwise
* <code>false</code>.
*/
public boolean isRequired() {
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;
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;
/**
target.addAttribute("modified", true);
}
- // Adds the required attribute
- if (!isReadOnly() && isRequired()) {
- target.addAttribute(AbstractComponentConnector.ATTRIBUTE_REQUIRED,
- true);
- }
-
if (isNewItemsAllowed()) {
target.addAttribute("allownewitem", true);
}