com.jgoodies.binding.util

Class ChangeTracker

Implemented Interfaces:
Observable, 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

Field Summary

static 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(Object bean, 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(Object bean, 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

Field Details

PROPERTYNAME_CHANGED

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

Constructor Details

ChangeTracker

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

Method Details

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

observe

public void observe(Object bean,
                    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

observe

public void observe(ValueModel valueModel)
Observes value changes in the given ValueModel.
Parameters:
valueModel - the ValueModel to observe

reset

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

retractInterestFor

public void retractInterestFor(Object bean,
                               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

retractInterestFor

public void retractInterestFor(ValueModel valueModel)
Retracts interest for value changes in the given ValueModel.
Parameters:
valueModel - the ValueModel to observe

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