Class SwingComponentDecorationFactory

java.lang.Object
org.netbeans.validation.api.ui.swing.SwingComponentDecorationFactory

public abstract class SwingComponentDecorationFactory extends Object
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.

 package 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));
                }
             }
        };
     }
  };
 
Our 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 Details

    • SwingComponentDecorationFactory

      public SwingComponentDecorationFactory()
  • Method Details

    • getNoOpDecorationFactory

      public static final SwingComponentDecorationFactory 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:
      SwingComponentDecorationFactory.set(SwingComponentDecorationFactory.getNoOpDecorationFactory());
      
      Or just for one specific group of components, here a SwingValidationGroup:
      SwingValidationGroup group = SwingValidationGroup.create(SwingComponentDecorationFactory.getNoOpDecorationFactory());
      
    • decorationFor

      public abstract ValidationUI decorationFor(JComponent c)
      Factory method that creates a 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(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.
      Parameters:
      c - The component
      Returns:
      A ValidationUI, visually attached to the component.
    • getDefault

      public static final SwingComponentDecorationFactory getDefault()
      Get the current application wide component decorator