net.sf.swinglib.field
Class FieldWatcher

java.lang.Object
  extended by net.sf.swinglib.field.FieldWatcher

public class FieldWatcher
extends Object

Tracks input field changes. This is normally used by dialogs, to automatically enable/disable an "OK" button based on whether the user has made changes.

To use, create a single instance for the dialog, and attach all fields of interest. When the dialog is displayed, call reset(). You can either manually interrogate the listener for changes, or have it automatically enable/disable one or more buttons.

An alternate usage is to create a watcher on a single field (normally a checkbox), and use it to enable/disable a group of related fields. For example, add a checkbox to enter a billing address, then enable/disable the address fields depending on whether the box is checked.

At present, this watcher knows how to track the following field types:

text fields (any subclass of JTextComponent
will look for changes to the underlying document;
stateful buttons (any subclass of JToggleButton)
will look for changes in the button's state
JList
will look for changes in the list's selection; does not consider changes to the underlying list model
In all cases, the watcher will track the initial value of the component, and recognize when its current state returns to that initial value. The initial value will be updated whenever reset() is called.


Constructor Summary
FieldWatcher(Action... controlled)
          Creates an instance with zero or more controlled actions.
FieldWatcher(JComponent... controlled)
          Creates an instance with zero or more controlled components.
 
Method Summary
 FieldWatcher addControlled(Action controlled)
          Adds a controlled action after construction.
 FieldWatcher addControlled(JComponent controlled)
          Adds a controlled component after construction.
 FieldWatcher addValidatedField(JTextComponent theField, FieldValidator validator)
          Adds a validated field to the watch list.
 FieldWatcher addWatchedField(JComponent theField)
          Adds a field to the watch list.
 Collection<JComponent> getChangedComponents()
          Returns the components that have changed since construction or the last call to reset().
 void reset()
          Resets this watcher: clears the list of changed fields, and disables any controlled components.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FieldWatcher

public FieldWatcher(JComponent... controlled)
Creates an instance with zero or more controlled components. These will be disabled when reset() is called, enabled when a watched field changes.


FieldWatcher

public FieldWatcher(Action... controlled)
Creates an instance with zero or more controlled actions. These will be disabled when reset() is called, enabled when a watched field changes.

Method Detail

addControlled

public FieldWatcher addControlled(JComponent controlled)
Adds a controlled component after construction. This is useful if you want to mix components and actions.


addControlled

public FieldWatcher addControlled(Action controlled)
Adds a controlled action after construction. This is useful if you want to mix components and actions.


addWatchedField

public FieldWatcher addWatchedField(JComponent theField)
Adds a field to the watch list. Will attach a component-appropriate listener to watch for changes (note that there is no way to remove this listener).

Returns:
The watcher, allowing multiple components to be added using chained calls.

addValidatedField

public FieldWatcher addValidatedField(JTextComponent theField,
                                      FieldValidator validator)
Adds a validated field to the watch list. Changes to the field will only be recorded if the validator claims that the field is valid.


getChangedComponents

public Collection<JComponent> getChangedComponents()
Returns the components that have changed since construction or the last call to reset().


reset

public void reset()
Resets this watcher: clears the list of changed fields, and disables any controlled components.