Class NullableField<T>

Type Parameters:
T - field value type
All Implemented Interfaces:
AttachNotifier, BlurNotifier<CustomField<T>>, DetachNotifier, Focusable<CustomField<T>>, FocusNotifier<CustomField<T>>, HasElement, HasEnabled, HasHelper, HasLabel, HasSize, HasStyle, HasTheme, HasValidation, HasValue<AbstractField.ComponentValueChangeEvent<CustomField<T>,T>,T>, HasValueAndElement<AbstractField.ComponentValueChangeEvent<CustomField<T>,T>,T>, HasTooltip, HasValidationProperties, InputField<AbstractField.ComponentValueChangeEvent<CustomField<T>,T>,T>, Serializable, ValidatingField<AbstractField.ComponentValueChangeEvent<CustomField<T>,T>,T>

public class NullableField<T> extends CustomField<T> implements ValidatingField<AbstractField.ComponentValueChangeEvent<CustomField<T>,T>,T>
A CustomField that wraps an inner field and prepends a checkbox (or other boolean field) in front of it that controls whether the value is null or not.

When the checkbox is checked, the inner field functions normally and provides this field's value. When the checkbox is unchecked, the inner field is completely disabled and this field's value is null.

Resetting On Disable

When the checkbox is unchecked, by default the inner (wrapped) field is automatically reset to its empty value. To have the inner field retain its value instead, so the value reappears if the checkbox is unchecked, use setResetOnDisable(false).

Validation

This class implements ValidatingField, delegating to the inner field (for non-null values only) if it also implements ValidatingField.

See Also:
  • Field Details

    • DEFAULT_ENABLED_BUT_NULL_ERROR

      public static final String DEFAULT_ENABLED_BUT_NULL_ERROR
      See Also:
    • innerField

      protected final HasValue<?,T> innerField
      The inner field.
    • enabledField

      protected final HasValue<?,Boolean> enabledField
      The checkbox (or other boolean field).
    • component

      protected final Component component
      The inner field component.
  • Constructor Details

    • NullableField

      public NullableField(AbstractField<?,T> innerField, String checkboxLabel)
      Create an instance using the given AbstractField and a new Checkbox with the given label.
      Parameters:
      innerField - the inner field
      checkboxLabel - label for the checkbox, or null for none
    • NullableField

      public NullableField(AbstractField<?,T> innerField, HasValue<?,Boolean> enabledField)
      Create an instance using the given AbstractField and the given enablement field.
      Parameters:
      innerField - the inner field
      enabledField - field that controls nullability
      Throws:
      IllegalArgumentException - if innerField is null
      IllegalArgumentException - if enabledField is null
    • NullableField

      public NullableField(HasValue<?,T> innerField, Component component, HasValue<?,Boolean> enabledField)
      Create an instance using the given field, component, and enablement field.
      Parameters:
      innerField - the inner field
      component - the component corresponding to innerField (typically this is same object)
      enabledField - field that controls nullability
      Throws:
      IllegalArgumentException - if any parameter is null
  • Method Details

    • getInnerField

      public HasValue<?,T> getInnerField()
    • getEnabledField

      public HasValue<?,Boolean> getEnabledField()
    • getComponent

      public Component getComponent()
    • isResetOnDisable

      public boolean isResetOnDisable()
      Determine whether the inner field forgets its value when the checkbox is unchecked.

      Default is true.

      Returns:
      true if the inner field is reset when the checkbox is unchecked
    • setResetOnDisable

      public void setResetOnDisable(boolean resetOnDisable)
    • isDisplayErrorMessages

      public boolean isDisplayErrorMessages()
      Determine whether to display error messages.

      This method is invoked by setErrorMessage().

      Default is true.

      Returns:
      true to display error messages, false to not display
    • setDisplayErrorMessages

      public void setDisplayErrorMessages(boolean displayErrorMessages)
    • setErrorMessage

      public void setErrorMessage(String errorMessage)
      Sets an error message to the component.

      The implementation in NullableField delegates to the overridden superclass method only if isDisplayErrorMessages() returns true.

      Specified by:
      setErrorMessage in interface HasValidation
      Specified by:
      setErrorMessage in interface HasValidationProperties
      See Also:
    • validate

      public ValidationResult validate(T value, ValueContext ctx)
      Description copied from interface: ValidatingField
      Validate this instance.
      Specified by:
      validate in interface ValidatingField<AbstractField.ComponentValueChangeEvent<CustomField<T>,T>,T>
      Parameters:
      value - field value to validate
      ctx - the value context for validation
      Returns:
      the validation result
    • handleEnabledFieldChange

      protected void handleEnabledFieldChange(boolean enabled)
    • enabledButNullValidationResult

      protected ValidationResult enabledButNullValidationResult()
      Build a ValidationResult to be returned by validate() in the situation where the checkbox is checked, but the inner field is returning a value of null.

      This would typically indicate that the inner field is incompletely filled-in or otherwise invalid.

      In this situation, validate() should return a validation error, because the user has explicitly said they want a non-null value by checking the checkbox.

      The implementation in NullableField returns a "Required value" error.

      Returns:
      validation error
      See Also:
    • buildLayout

      protected void buildLayout()
      Build the layout for this field.

      The implementation in NullableField returns a HorizontalLayout with the field that controls nullability (if it's a Component) followed by the inner component.

    • setComponentEnabled

      protected void setComponentEnabled(boolean enabled)
      Set whether the inner component is enabled, if possible.
      Parameters:
      enabled - true to enable, false to disable
    • generateModelValue

      protected T generateModelValue()
      Specified by:
      generateModelValue in class CustomField<T>
    • setPresentationValue

      protected void setPresentationValue(T value)
      Specified by:
      setPresentationValue in class CustomField<T>