Class ColorFieldMarker

  • All Implemented Interfaces:
    EventListener, FormControllerFieldStatusListener, FormControllerValidationListener

    public class ColorFieldMarker
    extends Object
    implements FormControllerValidationListener, FormControllerFieldStatusListener

    A specialized FormControllerValidationListener implementation that can change the colors of fields depending on their validation status.

    An instance of this class can be registered at a FormController as FormControllerValidationListener and FormControllerFieldStatusListener. Whenever an event is received from the FormController the current status of the affected field(s) is checked. The instance can be configured with multiple colors corresponding to the possible field states:

    • not visited, invalid
    • visited, invalid
    • not visited, validation warning
    • visited, validation warning
    The foreground and background colors of the field(s) are set to the corresponding colors.

    Valid fields do not change their colors. If colors for a certain field status are undefined, the default colors are used, too. For instance, if fields for which a validation warning exists should not be highlighted in a special way, just leave the properties for these colors undefined. This class implements a very simple means for marking invalid input fields. If the background color of a text field is changed to, say, red, it will be pretty obvious that something is wrong with this field.

    Instances of this class can be associated with a single form controller only and should not be reused (because they keep an internal state about the input fields involved). If they are declared in a bean definition file of the dependency injection framework, the singleton attribute should be set to false.

    Version:
    $Id: ColorFieldMarker.java 205 2012-01-29 18:29:57Z oheger $
    Author:
    Oliver Heger
    • Constructor Detail

      • ColorFieldMarker

        public ColorFieldMarker()
        Creates a new instance of ColorFieldMarker.
    • Method Detail

      • getInvalidBackground

        public Color getInvalidBackground()
        Returns the background color to be set for invalid input fields.
        Returns:
        the background color for invalid input fields
      • setInvalidBackground

        public void setInvalidBackground​(Color invalidBackground)
        Sets the background color for invalid input fields. If an input field changes to the state invalid, it is assigned this background color. (If this property is not set, the background color of invalid input fields won't be changed.)
        Parameters:
        invalidBackground - the background color for invalid input fields
      • getInvalidForeground

        public Color getInvalidForeground()
        Returns the foreground color to be set for invalid input fields.
        Returns:
        the foreground color for invalid input fields
      • setInvalidForeground

        public void setInvalidForeground​(Color invalidForeground)
        Sets the foreground color for invalid input fields. If an input field changes to the state invalid, it is assigned this foreground color. (If this property is not set, the foreground color of invalid input fields won't be changed.)
        Parameters:
        invalidForeground - the foreground color for invalid input fields
      • getNotVisitedInvalidBackground

        public Color getNotVisitedInvalidBackground()
        Returns the background color to be set for input fields in the state not visited, invalid.
        Returns:
        the background color for not visited invalid input fields
      • setNotVisitedInvalidBackground

        public void setNotVisitedInvalidBackground​(Color c)
        Sets the background color for input fields in the state not visited, invalid. If an input field changes to this state, it is assigned this background color. (If this property is not set, the background color of not visited, invalid input fields won't be changed.)
        Parameters:
        c - the background color for not visited invalid input fields
      • getNotVisitedInvalidForeground

        public Color getNotVisitedInvalidForeground()
        Returns the foreground color to be set for input fields in the state not visited, invalid.
        Returns:
        the foreground color for not visited invalid input fields
      • setNotVisitedInvalidForeground

        public void setNotVisitedInvalidForeground​(Color c)
        Sets the foreground color for input fields in the state not visited, invalid. If an input field changes to this state, it is assigned this foreground color. (If this property is not set, the foreground color of not visited, invalid input fields won't be changed.)
        Parameters:
        c - the foreground color for not visited invalid input fields
      • getWarningForeground

        public Color getWarningForeground()
        Returns the foreground color for input fields in the state warning.
        Returns:
        the foreground color for input fields with a validation warning
      • setWarningForeground

        public void setWarningForeground​(Color c)
        Sets the foreground color for input fields in the state warning. If an input field changes to this state, it is assigned this foreground color. (If this property is not set, the foreground color for fields with a validation warning won't be changed.)
        Parameters:
        c - the foreground color for fields with a validation warning
      • getWarningBackground

        public Color getWarningBackground()
        Returns the background color for input fields in the state warning.
        Returns:
        the background color for input fields with a validation warning
      • setWarningBackground

        public void setWarningBackground​(Color c)
        Sets the background color for input fields in the state warning. If an input field changes to this state, it is assigned this background color. (If this property is not set, the background color for fields with a validation warning won't be changed.)
        Parameters:
        c - the background color for fields with a validation warning
      • getNotVisitedWarningForeground

        public Color getNotVisitedWarningForeground()
        Returns the foreground color for input fields in the state not visited, warning.
        Returns:
        the foreground color for fields with a validation warning that have not yet been visited
      • setNotVisitedWarningForeground

        public void setNotVisitedWarningForeground​(Color c)
        Sets the foreground color for input fields in the state not visited, warning. If an input field changes to this state, it is assigned this foreground color. (If this property is not set, the foreground color for fields with a validation warning won't be changed.)
        Parameters:
        c - the foreground color for fields with a validation warning that have not yet been visited
      • getNotVisitedWarningBackground

        public Color getNotVisitedWarningBackground()
        Returns the background color for input fields in the state not visited, warning.
        Returns:
        the background color for fields with a validation warning that have not yet been visited
      • setNotVisitedWarningBackground

        public void setNotVisitedWarningBackground​(Color c)
        Sets the background color for input fields in the state not visited, warning. If an input field changes to this state, it is assigned this background color. (If this property is not set, the background color for fields with a validation warning won't be changed.)
        Parameters:
        c - the background color for fields with a validation warning that have not yet been visited
      • getForegroundColor

        public Color getForegroundColor​(FieldValidationStatus status)
        Returns the foreground color for fields with the specified FieldValidationStatus.
        Parameters:
        status - the FieldValidationStatus
        Returns:
        the corresponding foreground color (null if undefined)
      • setForegroundColor

        public void setForegroundColor​(FieldValidationStatus status,
                                       Color c)
        Sets the foreground color for fields with the specified FieldValidationStatus. This is a generic method for setting foreground colors. Internally, the other set methods for foreground colors delegate to this method.
        Parameters:
        status - the FieldValidationStatus (must not be null)
        c - the new foreground color
        Throws:
        IllegalArgumentException - if the FieldValidationStatus is null
      • getBackgroundColor

        public Color getBackgroundColor​(FieldValidationStatus status)
        Returns the background color for fields with the specified FieldValidationStatus.
        Parameters:
        status - the FieldValidationStatus
        Returns:
        the corresponding background color (null if undefined)
      • setBackgroundColor

        public void setBackgroundColor​(FieldValidationStatus status,
                                       Color c)
        Sets the background color for fields with the specified FieldValidationStatus. This is a generic method for setting background colors. Internally, the other set methods for background colors delegate to this method.
        Parameters:
        status - the FieldValidationStatus (must not be null)
        c - the new background color
        Throws:
        IllegalArgumentException - if the ValidationMessageLevel is null
      • getLastValidationStatus

        protected FieldValidationStatus getLastValidationStatus​(String field)
        Returns the last known validation status of the specified field. This method is used to find out whether the status of a field has changed. If no status is known, the field is considered valid and not visited.
        Parameters:
        field - the name of the field in question
        Returns:
        the last FieldValidationStatus of this field
      • setLastValidationStatus

        protected void setLastValidationStatus​(String field,
                                               FieldValidationStatus status)
        Sets the last FieldValidationStatus for the specified field. With this method the status of a field can be updated.
        Parameters:
        field - the name of the field in question
        status - the new FieldValidationStatus for this field
      • getWidgetForegroundColor

        protected Color getWidgetForegroundColor​(FormController controller,
                                                 String field)
        Returns the default foreground color of the specified widget. This color is set if no specific color is defined for the validation status this field is in. It is obtained once on first access and then cached.
        Parameters:
        controller - the FormController
        field - the name of the field
        Returns:
        the default foreground color of this field
      • getWidgetBackgroundColor

        protected Color getWidgetBackgroundColor​(FormController controller,
                                                 String field)
        Returns the default background color of the specified widget. This color is set if no specific color is defined for the validation status this field is in. It is obtained once on first access and then cached.
        Parameters:
        controller - the FormController
        field - the name of the field
        Returns:
        the default background color of this field
      • updateField

        protected void updateField​(FormController controller,
                                   String field,
                                   FormValidatorResults validationResults,
                                   boolean visited)
        Updates the colors for the given field. This method is called whenever an event is received from the FormController that indicates a status change of a field. This implementation checks whether the status of the given field has actually changed. If this is the case, the colors for the new status are obtained and set.
        Parameters:
        controller - the FormController
        field - the name of the field affected by the change
        validationResults - the current FormValidatorResults object
        visited - the visited state of the current field
        Throws:
        FormBuilderRuntimeException - if the field name cannot be resolved