com.jgoodies.binding.util
Class ChangeTracker

java.lang.Object
  extended by com.jgoodies.binding.beans.Model
      extended by com.jgoodies.binding.util.ChangeTracker
All Implemented Interfaces:
Observable, java.io.Serializable

public final class ChangeTracker
extends Model

Tracks changes in a set of bound bean properties. The tracker itself provides a read-only bound bean property changed that indicates whether one of the observed properties has changed. The changed state can be reset to false using #reset.

The tracker can observe readable bound bean properties if and only if the bean provides the optional support for listening on named properties as described in section 7.4.5 of the Java Bean Specification. The bean class must provide the following pair of methods:

 public void addPropertyChangeListener(String name, PropertyChangeListener l);
 public void removePropertyChangeListener(String name, PropertyChangeListener l);
 

Example:

 ChangeTracker tracker = new ChangeTracker();
 tracker.observe(address, "street");
 tracker.observe(address, "city");
 tracker.addPropertyChangeListener(new PropertyChangeListener() {

     public void propertyChange(PropertyChangeEvent evt) {
         System.out.println("Change state: " + evt.getNewValue());
     }
 });

 // Change the first ValueModel
 System.out.println(tracker.isChanged()); // Prints "false"
 address.setStreet("Belsenplatz");        // Prints "Change state: true"
 System.out.println(tracker.isChanged()); // Prints "true"
 tracker.reset();                         // Prints "Change state: false"
 System.out.println(tracker.isChanged()); // Prints "false"
 

Note: The classes BeanAdapter and PresentationModel already provide support for tracking changes. Typical binding code can use these classes and there seems to be no need to use the ChangeTracker.

Version:
$Revision: 1.5 $
Author:
Karsten Lentzsch
See Also:
ValueModel, Serialized Form

Field Summary
static java.lang.String PROPERTYNAME_CHANGED
          The name of the read-only bound bean property that indicates whether one of the observed properties has changed.
 
Constructor Summary
ChangeTracker()
          Constructs a change tracker with change state set to false.
 
Method Summary
 boolean isChanged()
          Answers whether one of the registered ValueModels has changed since this tracker has been reset last time.
 void observe(java.lang.Object bean, java.lang.String propertyName)
          Observes the specified readable bound bean property in the given bean.
 void observe(ValueModel valueModel)
          Observes value changes in the given ValueModel.
 void reset()
          Resets this tracker's changed state to false.
 void retractInterestFor(java.lang.Object bean, java.lang.String propertyName)
          Retracts interest for the specified readable bound bean property in the given bean.
 void retractInterestFor(ValueModel valueModel)
          Retracts interest for value changes in the given ValueModel.
 
Methods inherited from class com.jgoodies.binding.beans.Model
addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, addVetoableChangeListener, equals, fireIndexedPropertyChange, fireIndexedPropertyChange, fireIndexedPropertyChange, fireMultiplePropertiesChanged, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, fireVetoableChange, fireVetoableChange, fireVetoableChange, fireVetoableChange, fireVetoableChange, fireVetoableChange, getPropertyChangeListeners, getPropertyChangeListeners, getVetoableChangeListeners, getVetoableChangeListeners, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, removeVetoableChangeListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PROPERTYNAME_CHANGED

public static final java.lang.String PROPERTYNAME_CHANGED
The name of the read-only bound bean property that indicates whether one of the observed properties has changed.

See Also:
isChanged(), Constant Field Values
Constructor Detail

ChangeTracker

public ChangeTracker()
Constructs a change tracker with change state set to false.

Method Detail

isChanged

public boolean isChanged()
Answers whether one of the registered ValueModels has changed since this tracker has been reset last time.

Returns:
true if an observed property has changed since the last reset

reset

public void reset()
Resets this tracker's changed state to false.


observe

public void observe(java.lang.Object bean,
                    java.lang.String propertyName)
Observes the specified readable bound bean property in the given bean.

Parameters:
bean - the bean to be observed
propertyName - the name of the readable bound bean property
Throws:
java.lang.NullPointerException - if the bean or propertyName is null
PropertyNotBindableException - if this tracker can't add the PropertyChangeListener from the bean
See Also:
retractInterestFor(Object, String)

observe

public void observe(ValueModel valueModel)
Observes value changes in the given ValueModel.

Parameters:
valueModel - the ValueModel to observe
Throws:
java.lang.NullPointerException - if the valueModel is null
See Also:
retractInterestFor(ValueModel)

retractInterestFor

public void retractInterestFor(java.lang.Object bean,
                               java.lang.String propertyName)
Retracts interest for the specified readable bound bean property in the given bean.

Parameters:
bean - the bean to be observed
propertyName - the name of the readable bound bean property
Throws:
java.lang.NullPointerException - if the bean or propertyName is null
PropertyNotBindableException - if this tracker can't remove the PropertyChangeListener from the bean
See Also:
observe(Object, String)

retractInterestFor

public void retractInterestFor(ValueModel valueModel)
Retracts interest for value changes in the given ValueModel.

Parameters:
valueModel - the ValueModel to observe
Throws:
java.lang.NullPointerException - if the valueModel is null
See Also:
retractInterestFor(ValueModel)


Copyright © 2002-2008 JGoodies Karsten Lentzsch. All Rights Reserved.