com.jgoodies.binding.beans

Class IndirectPropertyChangeSupport


public final class IndirectPropertyChangeSupport
extends java.lang.Object

A helper class for observing changes in bound bean properties where the target bean changes. Provides two access styles to the target bean that holds the observed property: you can specify a bean directly, or you can use a bean channel to access the bean indirectly. In the latter case you specify a ValueModel that holds the bean that in turn holds the observed properties.

If the target bean is null, it won't report any changes.

It is recommended to remove all listener by invoking #removeAll if the observed bean lives much longer than this change support instance. As an alternative you may use event listener lists that are based on WeakReferences.

Constraints: All target bean classes must support bound properties, i. e. must provide the following pair of methods for registration of multicast property change event listeners:

 public void addPropertyChangeListener(PropertyChangeListener x);
 public void removePropertyChangeListener(PropertyChangeListener x);
 
and the following methods for listening on named properties:
 public void addPropertyChangeListener(String, PropertyChangeListener x);
 public void removePropertyChangeListener(String, PropertyChangeListener x);
 
Version:
$Revision: 1.5 $
Author:
Karsten Lentzsch
See Also:
PropertyChangeEvent, PropertyChangeListener, PropertyChangeSupport, BeanAdapter

Constructor Summary

IndirectPropertyChangeSupport()
Constructs an IndirectPropertyChangeSupport that has no bean set.
IndirectPropertyChangeSupport(Object bean)
Constructs an IndirectPropertyChangeSupport with the given initial bean.
IndirectPropertyChangeSupport(ValueModel beanChannel)
Constructs an IndirectPropertyChangeSupport using the given bean channel.

Method Summary

void
addPropertyChangeListener(PropertyChangeListener listener)
Adds a PropertyChangeListener to the list of bean listeners.
void
addPropertyChangeListener(String propertyName, PropertyChangeListener listener)
Adds a PropertyChangeListener to the list of bean listeners for a specific property.
Object
getBean()
Returns the Java Bean that holds the observed properties.
PropertyChangeListener[]
getPropertyChangeListeners()
Returns an array of all the property change listeners registered on this component.
PropertyChangeListener[]
getPropertyChangeListeners(String propertyName)
Returns an array of all the listeners which have been associated with the named property.
void
removeAll()
Removes all registered PropertyChangeListeners from the current target bean - if any.
void
removePropertyChangeListener(PropertyChangeListener listener)
Removes a PropertyChangeListener from the list of bean listeners.
void
removePropertyChangeListener(String propertyName, PropertyChangeListener listener)
Removes a PropertyChangeListener from the listener list for a specific property.
void
setBean(Object newBean)
Sets a new Java Bean as holder of the observed properties.

Constructor Details

IndirectPropertyChangeSupport

public IndirectPropertyChangeSupport()
Constructs an IndirectPropertyChangeSupport that has no bean set.

IndirectPropertyChangeSupport

public IndirectPropertyChangeSupport(Object bean)
Constructs an IndirectPropertyChangeSupport with the given initial bean.
Parameters:
bean - the initial bean

IndirectPropertyChangeSupport

public IndirectPropertyChangeSupport(ValueModel beanChannel)
Constructs an IndirectPropertyChangeSupport using the given bean channel.
Parameters:
beanChannel - the ValueModel that holds the bean

Method Details

addPropertyChangeListener

public void addPropertyChangeListener(PropertyChangeListener listener)
Adds a PropertyChangeListener to the list of bean listeners. The listener is registered for all bound properties of the target bean.

If listener is null, no exception is thrown and no action is performed.

Parameters:
listener - the PropertyChangeListener to be added

addPropertyChangeListener

public void addPropertyChangeListener(String propertyName,
                                      PropertyChangeListener listener)
Adds a PropertyChangeListener to the list of bean listeners for a specific property. The specified property may be user-defined.

Note that if the bean is inheriting a bound property, then no event will be fired in response to a change in the inherited property.

If listener is null, no exception is thrown and no action is performed.

Parameters:
propertyName - one of the property names listed above
listener - the PropertyChangeListener to be added

getBean

public Object getBean()
Returns the Java Bean that holds the observed properties.
Returns:
the Bean that holds the observed properties

getPropertyChangeListeners

public PropertyChangeListener[] getPropertyChangeListeners()
Returns an array of all the property change listeners registered on this component.
Returns:
all of this component's PropertyChangeListeners or an empty array if no property change listeners are currently registered

getPropertyChangeListeners

public PropertyChangeListener[] getPropertyChangeListeners(String propertyName)
Returns an array of all the listeners which have been associated with the named property.
Parameters:
propertyName - the name of the property to lookup listeners
Returns:
all of the PropertyChangeListeners associated with the named property or an empty array if no listeners have been added

removeAll

public void removeAll()
Removes all registered PropertyChangeListeners from the current target bean - if any.

removePropertyChangeListener

public void removePropertyChangeListener(PropertyChangeListener listener)
Removes a PropertyChangeListener from the list of bean listeners. This method should be used to remove PropertyChangeListeners that were registered for all bound properties of the target bean.

If listener is null, no exception is thrown and no action is performed.

Parameters:
listener - the PropertyChangeListener to be removed

removePropertyChangeListener

public void removePropertyChangeListener(String propertyName,
                                         PropertyChangeListener listener)
Removes a PropertyChangeListener from the listener list for a specific property. This method should be used to remove PropertyChangeListeners that were registered for a specific bound property.

If listener is null, no exception is thrown and no action is performed.

Parameters:
propertyName - a valid property name
listener - the PropertyChangeListener to be removed

setBean

public void setBean(Object newBean)
Sets a new Java Bean as holder of the observed properties. Removes all registered listeners from the old bean and adds them to the new bean.
Parameters:
newBean - the new holder of the observed properties

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