Class ColorFieldMarker
- java.lang.Object
-
- net.sf.jguiraffe.gui.builder.window.ctrl.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
asFormControllerValidationListener
andFormControllerFieldStatusListener
. Whenever an event is received from theFormController
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
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 Summary
Constructors Constructor Description ColorFieldMarker()
Creates a new instance ofColorFieldMarker
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
fieldStatusChanged(FormControllerFieldStatusEvent event)
The visited status of a field managed by theFormController
has changed.Color
getBackgroundColor(FieldValidationStatus status)
Returns the background color for fields with the specifiedFieldValidationStatus
.Color
getForegroundColor(FieldValidationStatus status)
Returns the foreground color for fields with the specifiedFieldValidationStatus
.Color
getInvalidBackground()
Returns the background color to be set for invalid input fields.Color
getInvalidForeground()
Returns the foreground color to be set for invalid input fields.protected FieldValidationStatus
getLastValidationStatus(String field)
Returns the last known validation status of the specified field.Color
getNotVisitedInvalidBackground()
Returns the background color to be set for input fields in the state not visited, invalid.Color
getNotVisitedInvalidForeground()
Returns the foreground color to be set for input fields in the state not visited, invalid.Color
getNotVisitedWarningBackground()
Returns the background color for input fields in the state not visited, warning.Color
getNotVisitedWarningForeground()
Returns the foreground color for input fields in the state not visited, warning.Color
getWarningBackground()
Returns the background color for input fields in the state warning.Color
getWarningForeground()
Returns the foreground color for input fields in the state warning.protected Color
getWidgetBackgroundColor(FormController controller, String field)
Returns the default background color of the specified widget.protected Color
getWidgetForegroundColor(FormController controller, String field)
Returns the default foreground color of the specified widget.void
setBackgroundColor(FieldValidationStatus status, Color c)
Sets the background color for fields with the specifiedFieldValidationStatus
.void
setForegroundColor(FieldValidationStatus status, Color c)
Sets the foreground color for fields with the specifiedFieldValidationStatus
.void
setInvalidBackground(Color invalidBackground)
Sets the background color for invalid input fields.void
setInvalidForeground(Color invalidForeground)
Sets the foreground color for invalid input fields.protected void
setLastValidationStatus(String field, FieldValidationStatus status)
Sets the lastFieldValidationStatus
for the specified field.void
setNotVisitedInvalidBackground(Color c)
Sets the background color for input fields in the state not visited, invalid.void
setNotVisitedInvalidForeground(Color c)
Sets the foreground color for input fields in the state not visited, invalid.void
setNotVisitedWarningBackground(Color c)
Sets the background color for input fields in the state not visited, warning.void
setNotVisitedWarningForeground(Color c)
Sets the foreground color for input fields in the state not visited, warning.void
setWarningBackground(Color c)
Sets the background color for input fields in the state warning.void
setWarningForeground(Color c)
Sets the foreground color for input fields in the state warning.protected void
updateField(FormController controller, String field, FormValidatorResults validationResults, boolean visited)
Updates the colors for the given field.void
validationPerformed(FormControllerValidationEvent event)
A validation operation was performed by theFormController
.
-
-
-
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 specifiedFieldValidationStatus
.- Parameters:
status
- theFieldValidationStatus
- Returns:
- the corresponding foreground color (null if undefined)
-
setForegroundColor
public void setForegroundColor(FieldValidationStatus status, Color c)
Sets the foreground color for fields with the specifiedFieldValidationStatus
. This is a generic method for setting foreground colors. Internally, the other set methods for foreground colors delegate to this method.- Parameters:
status
- theFieldValidationStatus
(must not be null)c
- the new foreground color- Throws:
IllegalArgumentException
- if theFieldValidationStatus
is null
-
getBackgroundColor
public Color getBackgroundColor(FieldValidationStatus status)
Returns the background color for fields with the specifiedFieldValidationStatus
.- Parameters:
status
- theFieldValidationStatus
- Returns:
- the corresponding background color (null if undefined)
-
setBackgroundColor
public void setBackgroundColor(FieldValidationStatus status, Color c)
Sets the background color for fields with the specifiedFieldValidationStatus
. This is a generic method for setting background colors. Internally, the other set methods for background colors delegate to this method.- Parameters:
status
- theFieldValidationStatus
(must not be null)c
- the new background color- Throws:
IllegalArgumentException
- if theValidationMessageLevel
is null
-
validationPerformed
public void validationPerformed(FormControllerValidationEvent event)
A validation operation was performed by theFormController
. This implementation checks which fields need to be updated and sets the corresponding new colors.- Specified by:
validationPerformed
in interfaceFormControllerValidationListener
- Parameters:
event
- the validation event
-
fieldStatusChanged
public void fieldStatusChanged(FormControllerFieldStatusEvent event)
The visited status of a field managed by theFormController
has changed. This implementation checks whether new colors need to be set for this field.- Specified by:
fieldStatusChanged
in interfaceFormControllerFieldStatusListener
- Parameters:
event
- theFormControllerFieldStatusEvent
-
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 lastFieldValidationStatus
for the specified field. With this method the status of a field can be updated.- Parameters:
field
- the name of the field in questionstatus
- the newFieldValidationStatus
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
- theFormController
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
- theFormController
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 theFormController
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
- theFormController
field
- the name of the field affected by the changevalidationResults
- the currentFormValidatorResults
objectvisited
- the visited state of the current field- Throws:
FormBuilderRuntimeException
- if the field name cannot be resolved
-
-