public final class ValidationComponentUtils extends Object
JComponent
client property
mechanism as a backing store.ValidationMessage
,
ValidationMessage.key()
,
ValidationResult
,
ValidationResult.subResult(Object)
,
ValidationResult.keyMap()
Modifier and Type | Class and Description |
---|---|
static interface |
ValidationComponentUtils.Visitor
Describes visitors that visit a component tree.
|
Modifier and Type | Method and Description |
---|---|
static ValidationResult |
getAssociatedResult(JComponent comp,
Map<Object,ValidationResult> keyMap)
Returns the ValidationResult associated with the given component
using the specified validation result key map,
or
null if the component has no message key set,
or ValidationResult.EMPTY if the key map contains
no result for the component. |
static Color |
getErrorBackground()
Returns the error background color used to mark components
that have an associated validation error.
|
static Object |
getInputHint(JComponent comp)
Returns the component's input hint that is stored in a client property.
|
static Color |
getMandatoryBackground()
Returns a default background color that can be used as the component
background for components with mandatory content.
|
static Border |
getMandatoryBorder()
Lazily creates and returns a
Border instance that is used
to indicate that a component's content is mandatory. |
static Color |
getMandatoryForeground()
Returns a default foreground color that can be used as the component
foreground for components with mandatory content.
|
static Object[] |
getMessageKeys(JComponent comp)
Returns the message key that has been set to associate the given
component with a set of ValidationMessages.
|
static Severity |
getSeverity(JComponent comp)
Returns the component's
Severity if it has been set before. |
static Color |
getWarningBackground()
Returns the warning background color used to mark components
that have an associated validation warning.
|
static boolean |
hasError(JComponent comp,
ValidationResult result)
Checks and answers if the specified component is associated with an
error message in the given validation result.
|
static boolean |
hasWarning(JComponent comp,
ValidationResult result)
Checks and answers if the specified component is associated with a
warning message in the given validation result.
|
static boolean |
isMandatory(JComponent comp)
Returns if the component has been marked as mandatory.
|
static boolean |
isMandatoryAndBlank(JComponent comp)
Returns if the component is a
JTextComponent with blank content
and has been marked as mandatory. |
static void |
setErrorBackground(JTextComponent comp)
Sets the text component's background to a color that shall indicate
that the component's content has is invalid with error severity.
|
static void |
setInputHint(JComponent comp,
Object hint)
Sets the input hint for the given component.
|
static void |
setMandatory(JComponent comp,
boolean mandatory)
Marks the given component as mandatory or optional.
|
static void |
setMandatoryBackground(JTextComponent comp)
Sets the text component's background to a color that shall indicate
that the component's content is mandatory.
|
static void |
setMandatoryBorder(JTextComponent comp)
Sets the text component's border to use a new border that shall indicate
that the component's content is mandatory.
|
static void |
setMessageKey(JComponent comp,
Object messageKey)
Associates the given component with the specified message key.
|
static void |
setMessageKeys(JComponent comp,
Object... messageKeys)
Associates the given component with the specified message keys.
|
static void |
setSeverity(JComponent comp,
Severity severity)
Marks the given component with the specified severity.
|
static void |
setWarningBackground(JTextComponent comp)
Sets the text component's background to a color that shall indicate
that the component's content is invalid with warning severity.
|
static void |
updateComponentTreeMandatoryAndBlankBackground(Container container)
Traverses a component tree and sets mandatory backgrounds
to text components that have blank content and have been marked
as mandatory with
setMandatory(JComponent, boolean) before. |
static void |
updateComponentTreeMandatoryBackground(Container container)
Traverses a component tree and sets mandatory backgrounds
to text components that have been marked as mandatory
with
setMandatory(JComponent, boolean) before. |
static void |
updateComponentTreeMandatoryBorder(Container container)
Traverses a component tree and sets mandatory borders
to text components that have been marked as mandatory
with
setMandatory(JComponent, boolean) before. |
static void |
updateComponentTreeSeverity(Container container,
ValidationResult result)
Traverses a component tree and sets the severity for all text components.
|
static void |
updateComponentTreeSeverityBackground(Container container,
ValidationResult result)
Traverses a component tree and sets the text component backgrounds
according to the severity of an associated validation result - if any.
|
static void |
visitComponentTree(Container container,
Map<Object,ValidationResult> keyMap,
ValidationComponentUtils.Visitor visitor)
Traverses the component tree starting at the given container and invokes
the given visitor's
#visit method on each instance of
JTextComponent . |
public static boolean isMandatory(JComponent comp)
comp
- the component to be checkedisMandatoryAndBlank(JComponent)
,
setMandatory(JComponent, boolean)
,
setMandatoryBackground(JTextComponent)
,
setMandatoryBorder(JTextComponent)
public static boolean isMandatoryAndBlank(JComponent comp)
JTextComponent
with blank content
and has been marked as mandatory.comp
- the component to be checkedisMandatory(JComponent)
,
setMandatory(JComponent, boolean)
,
setMandatoryBackground(JTextComponent)
,
setMandatoryBorder(JTextComponent)
public static void setMandatory(JComponent comp, boolean mandatory)
comp
- the component to be markedmandatory
- true for mandatory, false for optionalisMandatory(JComponent)
,
isMandatoryAndBlank(JComponent)
,
setMandatoryBackground(JTextComponent)
,
setMandatoryBorder(JTextComponent)
public static Severity getSeverity(JComponent comp)
Severity
if it has been set before.
Useful for validation-aware containers that render the component's
validation state.comp
- the component to be readSeverity
as set beforesetSeverity(JComponent, Severity)
,
updateComponentTreeSeverity(Container, ValidationResult)
,
updateComponentTreeSeverityBackground(Container, ValidationResult)
public static void setSeverity(JComponent comp, Severity severity)
comp
- the component that shall be markedseverity
- the component's severitygetSeverity(JComponent)
,
updateComponentTreeSeverity(Container, ValidationResult)
,
updateComponentTreeSeverityBackground(Container, ValidationResult)
public static Object[] getMessageKeys(JComponent comp)
comp
- the component to be requestedsetMessageKey(JComponent, Object)
,
ValidationMessage
,
ValidationMessage.key()
,
ValidationResult.subResult(Object)
,
ValidationResult.keyMap()
public static void setMessageKey(JComponent comp, Object messageKey)
comp
- the component that shall be associated with the keymessageKey
- the key to be setgetMessageKeys(JComponent)
,
ValidationMessage
,
ValidationMessage.key()
,
ValidationResult.subResult(Object)
,
ValidationResult.keyMap()
public static void setMessageKeys(JComponent comp, Object... messageKeys)
Since version 2.0.1 this method uses varargs instead of an Object array.
comp
- the component that shall be associated with the keysmessageKeys
- the keys to be setgetMessageKeys(JComponent)
,
ValidationMessage
,
ValidationMessage.key()
,
ValidationResult.subResult(Object)
,
ValidationResult.keyMap()
public static Object getInputHint(JComponent comp)
String
or a
compound object, for example that is able to localize the input hint
for the active Locale
.To make use of this information an editor should register a listener with the focus management. Whenever the focused component changes, the mechanism can request the input hint for the focus owner using this service and can display the result hint in the user interface.
comp
- the component to be requestedsetInputHint(JComponent, Object)
public static void setInputHint(JComponent comp, Object hint)
comp
- the component to set a hint forhint
- the input hint to be associated with the componentgetInputHint(JComponent)
public static boolean hasError(JComponent comp, ValidationResult result)
setMessageKey(JComponent, Object)
or
setMessageKeys(JComponent, Object[])
.
Note: This method may become slow if invoked for larger
validation results and multiple components. In this case,
it is recommended to use ValidationResult.keyMap()
instead.
The latter iterates once over the validation result and can be used later
to request the severity for multiple components in almost linear time.
comp
- used to get the association key fromresult
- used to lookup the validation messages fromNullPointerException
- if the component or validation result
is null
hasWarning(JComponent, ValidationResult)
,
getMessageKeys(JComponent)
public static boolean hasWarning(JComponent comp, ValidationResult result)
setMessageKey(JComponent, Object)
or
setMessageKeys(JComponent, Object[])
.
Note: This method may become slow if invoked for larger
validation results and multiple components. In this case,
it is recommended to use ValidationResult.keyMap()
instead.
The latter iterates once over the validation result and can be used later
to request the severity for multiple components in almost linear time.
comp
- used to get the association key fromresult
- used to lookup the validation messages fromNullPointerException
- if the component or validation result
is null
hasError(JComponent, ValidationResult)
,
getMessageKeys(JComponent)
public static Color getMandatoryBackground()
JTextComponent
.Note: The component background colors are managed by the look&feel implementation. Many l&fs will honor a custom background color. However, some l&fs may ignore custom background colors. It is recommended to check the appearance in all l&fs available in an application.
getMandatoryForeground()
public static Color getMandatoryForeground()
JTextComponent
.Note: The component foreground and border colors are managed by the look&feel implementation. Many l&fs will honor a custom foreground color and custom border configuration. However, some l&fs may ignore these custom settings. It is recommended to check the appearance in all l&fs available in an application.
getMandatoryBackground()
,
getMandatoryBorder()
public static void setMandatoryBackground(JTextComponent comp)
Note: The component background colors are managed by the look&feel implementation. Many l&fs will honor a custom foreground color and custom border configuration. However, some l&fs may ignore these custom settings. It is recommended to check the appearance in all l&fs available in an application.
comp
- the text component that shall get a new backgroundsetMandatoryBorder(JTextComponent)
,
setErrorBackground(JTextComponent)
,
setWarningBackground(JTextComponent)
public static Color getErrorBackground()
getWarningBackground()
,
setErrorBackground(JTextComponent)
,
updateComponentTreeSeverityBackground(Container, ValidationResult)
public static void setErrorBackground(JTextComponent comp)
Note: The component background colors are managed by the look&feel implementation. Many l&fs will honor a custom foreground color and custom border configuration. However, some l&fs may ignore these custom settings. It is recommended to check the appearance in all l&fs available in an application.
comp
- the text component that shall get a new backgroundsetMandatoryBackground(JTextComponent)
,
setWarningBackground(JTextComponent)
public static Color getWarningBackground()
getErrorBackground()
,
setWarningBackground(JTextComponent)
,
updateComponentTreeSeverityBackground(Container, ValidationResult)
public static void setWarningBackground(JTextComponent comp)
Note: The component background colors are managed by the look&feel implementation. Many l&fs will honor a custom foreground color and custom border configuration. However, some l&fs may ignore these custom settings. It is recommended to check the appearance in all l&fs available in an application.
comp
- the text component that shall get a new backgroundsetMandatoryBackground(JTextComponent)
,
setErrorBackground(JTextComponent)
public static void setMandatoryBorder(JTextComponent comp)
Note: The component foreground and border colors are managed by the look&feel implementation. Many l&fs will honor a custom foreground color and custom border configuration. However, some l&fs may ignore these custom settings. It is recommended to check the appearance in all l&fs available in an application.
comp
- the component that gets a new bordersetMandatoryBackground(JTextComponent)
,
getMandatoryBorder()
public static Border getMandatoryBorder()
Border
instance that is used
to indicate that a component's content is mandatory.Border
that is used to indicate that
a component's content is mandatorypublic static void updateComponentTreeMandatoryBackground(Container container)
setMandatory(JComponent, boolean)
before.
The iteration starts at the given container.container
- the component tree rootsetMandatory(JComponent, boolean)
,
setMandatoryBackground(JTextComponent)
public static void updateComponentTreeMandatoryAndBlankBackground(Container container)
setMandatory(JComponent, boolean)
before.
The iteration starts at the given container.container
- the component tree rootsetMandatory(JComponent, boolean)
,
setMandatoryBackground(JTextComponent)
public static void updateComponentTreeMandatoryBorder(Container container)
setMandatory(JComponent, boolean)
before.
The iteration starts at the given container.container
- the component tree rootsetMandatory(JComponent, boolean)
,
setMandatoryBorder(JTextComponent)
public static void updateComponentTreeSeverityBackground(Container container, ValidationResult result)
The message keys used to associate components with validation messages
should be set using setMessageKey(JComponent, Object)
before
you call this method.
container
- the component tree rootresult
- the validation result used to lookup the severitiessetMandatory(JComponent, boolean)
,
setMessageKey(JComponent, Object)
,
setMandatoryBackground(JTextComponent)
,
setErrorBackground(JTextComponent)
,
setWarningBackground(JTextComponent)
public static void updateComponentTreeSeverity(Container container, ValidationResult result)
null
. The severity is set using
setSeverity(JComponent, Severity)
.
Before you use this method, associate components with validation
messages using setMessageKey(JComponent, Object)
.
container
- the component tree rootresult
- the validation result that provides the associated messagessetSeverity(JComponent, Severity)
public static void visitComponentTree(Container container, Map<Object,ValidationResult> keyMap, ValidationComponentUtils.Visitor visitor)
#visit
method on each instance of
JTextComponent
. Useful to perform custom component tree updates
that are not already provided by the #updateComponentTreeXXX
methods.
The arguments passed to the #visit method are the visited component and
its associated validation subresult. This subresult is requested from
the specified keyMap
using the component's message key.
Before you use this method, associate text component with validation
messages using setMessageKey(JComponent, Object)
.
container
- the component tree rootkeyMap
- maps messages keys to associated validation resultsvisitor
- the visitor that is applied to all text componentssetMessageKey(JComponent, Object)
,
ValidationComponentUtils.Visitor
public static ValidationResult getAssociatedResult(JComponent comp, Map<Object,ValidationResult> keyMap)
null
if the component has no message key set,
or ValidationResult.EMPTY
if the key map contains
no result for the component.comp
- the component may be marked with a validation message keyskeyMap
- maps validation message keys to ValidationResultsnull
if the component has no message key set,
or ValidationResult.EMPTY
if no result is associated
with the componentCopyright © 2003-2011 JGoodies Karsten Lentzsch. All Rights Reserved.