public abstract class SwingComponentDecorationFactory
extends java.lang.Object
ValidationUI
instances that can decorate
a Swing GUI-component when it has a Problem.
By default, one instance of a class implementing this interface is used to create a ValidationUI for all components handled by the simplevalidation framework. This instance can be replaced with a custom one, as described below.
For custom decoration, simply pass a different
decorator factory in ValidationGroup.add() (and if necessary, proxy the
default decorator factory for all but some specific kind of component).
A rudimentary example of writing a component decorator: The code and
description below show how to replace the default
SwingComponentDecorationFactory with one that will create ValidationUI
instances that draws a thick colored border around the component
when there is an error.
Ourpackage com.foo.myapp; public class MySwingComponentDecorationFactory extends SwingComponentDecorationFactory { public ValidationUI decorationFor(final JComponent c) { return new ValidationUI() { private javax.swing.border.Border origBorder = c.getBorder(); public void showProblem(Problem problem) { if( problem == null ) { c.setBorder(origBorder); } else { c.setBorder(javax.swing.BorderFactory.createLineBorder(problem.severity().color(), 3)); } } }; } };
MySwingComponentDecorationFactory
is then registered so that
it can be found using JDK 6's ServiceLoader
(or NetBeans'
Lookup
): Create a file named org.netbeans.validation.api.ui.swing.SwingComponentDecorationFactory
in the folder META-INF/services
in your source root (so that
it will be included in the JAR file). Add one line of text to this file -
the fully qualified name of your class, e.g.
com.foo.myapp.MySwingComponentDecorationFactory
Constructor and Description |
---|
SwingComponentDecorationFactory() |
Modifier and Type | Method and Description |
---|---|
abstract ValidationUI |
decorationFor(javax.swing.JComponent c)
Factory method that creates a
ValidationUI visually attached to
the Swing GUI-component when there is a Problem . |
static SwingComponentDecorationFactory |
getDefault()
Get the current application wide component decorator
|
static SwingComponentDecorationFactory |
getNoOpDecorationFactory()
Special decorator that does not decorate at all -- even if
there is a problem -- a "null" decorator.
|
public static final SwingComponentDecorationFactory getNoOpDecorationFactory()
Or just for one specific group of components, here a SwingValidationGroup:SwingComponentDecorationFactory.set(SwingComponentDecorationFactory.getNoOpDecorationFactory());
SwingValidationGroup group = SwingValidationGroup.create(SwingComponentDecorationFactory.getNoOpDecorationFactory());
public abstract ValidationUI decorationFor(javax.swing.JComponent c)
ValidationUI
visually attached to
the Swing GUI-component when there is a Problem
. When a
Problem
occurs in a component, this ValidationUI
needs to be
updated using ValidationUI.showProblem(org.netbeans.validation.api.Problem)
(this is
typically done from within a ValidationListener
) and
will then apply some visual mark to the component. When the
problem disappears, ValidationListener
will pass null
to ValidationUI#showProblem
,
which makes sure that the visual cue is removed, so that the
components is restored to its original visual state.c
- The componentpublic static final SwingComponentDecorationFactory getDefault()