public final class ChangeTracker extends Model
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.
ValueModel
Modifier and Type | Field and Description |
---|---|
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 and Description |
---|
ChangeTracker()
Constructs a change tracker with change state set to
false . |
Modifier and Type | Method and Description |
---|---|
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.
|
createPropertyChangeSupport, firePropertyChange
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
addPropertyChangeListener, removePropertyChangeListener
public static final java.lang.String PROPERTYNAME_CHANGED
isChanged()
,
Constant Field Valuespublic ChangeTracker()
false
.public boolean isChanged()
public void reset()
false
.public void observe(java.lang.Object bean, java.lang.String propertyName)
bean
- the bean to be observedpropertyName
- the name of the readable bound bean propertyjava.lang.NullPointerException
- if the bean or propertyName is nullPropertyNotBindableException
- if this tracker can't add
the PropertyChangeListener from the beanretractInterestFor(Object, String)
public void observe(ValueModel valueModel)
valueModel
- the ValueModel to observejava.lang.NullPointerException
- if the valueModel is nullretractInterestFor(ValueModel)
public void retractInterestFor(java.lang.Object bean, java.lang.String propertyName)
bean
- the bean to be observedpropertyName
- the name of the readable bound bean propertyjava.lang.NullPointerException
- if the bean or propertyName is nullPropertyNotBindableException
- if this tracker can't remove
the PropertyChangeListener from the beanobserve(Object, String)
public void retractInterestFor(ValueModel valueModel)
valueModel
- the ValueModel to observejava.lang.NullPointerException
- if the valueModel is nullretractInterestFor(ValueModel)
Copyright © 2002-2010 JGoodies Karsten Lentzsch. All Rights Reserved.