Class SwingComponentDecorationFactory
java.lang.Object
org.netbeans.validation.api.ui.swing.SwingComponentDecorationFactory
Factory class for creating
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
- Author:
- Tim Boudreau, Hugo Heden
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract ValidationUIFactory method that creates aValidationUIvisually attached to the Swing GUI-component when there is aProblem.static final SwingComponentDecorationFactoryGet the current application wide component decoratorstatic final SwingComponentDecorationFactorySpecial decorator that does not decorate at all -- even if there is a problem -- a "null" decorator.
-
Constructor Details
-
SwingComponentDecorationFactory
public SwingComponentDecorationFactory()
-
-
Method Details
-
getNoOpDecorationFactory
Special decorator that does not decorate at all -- even if there is a problem -- a "null" decorator. This is useful if no component decorations are desired. For example application wide:
Or just for one specific group of components, here a SwingValidationGroup:SwingComponentDecorationFactory.set(SwingComponentDecorationFactory.getNoOpDecorationFactory());SwingValidationGroup group = SwingValidationGroup.create(SwingComponentDecorationFactory.getNoOpDecorationFactory()); -
decorationFor
Factory method that creates aValidationUIvisually attached to the Swing GUI-component when there is aProblem. When aProblemoccurs in a component, thisValidationUIneeds to be updated usingValidationUI.showProblem(Problem)(this is typically done from within aValidationListener) and will then apply some visual mark to the component. When the problem disappears,ValidationListenerwill passnulltoValidationUI#showProblem, which makes sure that the visual cue is removed, so that the components is restored to its original visual state.- Parameters:
c- The component- Returns:
- A ValidationUI, visually attached to the component.
-
getDefault
Get the current application wide component decorator
-