java.beans

Class PropertyChangeSupport

public class PropertyChangeSupport extends Object implements Serializable

PropertyChangeSupport makes it easy to fire property change events and handle listeners. It allows chaining of listeners, as well as filtering by property name. In addition, it will serialize only those listeners which are serializable, ignoring the others without problem. This class is thread-safe.

Since: 1.1

UNKNOWN: updated to 1.4

Constructor Summary
PropertyChangeSupport(Object source)
Create a PropertyChangeSupport to work with a specific source bean.
Method Summary
voidaddPropertyChangeListener(PropertyChangeListener l)
Adds a PropertyChangeListener to the list of global listeners.
voidaddPropertyChangeListener(String propertyName, PropertyChangeListener l)
Adds a PropertyChangeListener listening on the specified property.
voidfireIndexedPropertyChange(String name, int index, Object oldValue, Object newValue)
Fire an indexed property change event.
voidfireIndexedPropertyChange(String name, int index, int oldValue, int newValue)
Fire an indexed property change event.
voidfireIndexedPropertyChange(String name, int index, boolean oldValue, boolean newValue)
Fire an indexed property change event.
voidfirePropertyChange(String propertyName, Object oldVal, Object newVal)
Fire a PropertyChangeEvent containing the old and new values of the property to all the global listeners, and to all the listeners for the specified property name.
voidfirePropertyChange(String propertyName, int oldVal, int newVal)
Fire a PropertyChangeEvent containing the old and new values of the property to all the global listeners, and to all the listeners for the specified property name.
voidfirePropertyChange(String propertyName, boolean oldVal, boolean newVal)
Fire a PropertyChangeEvent containing the old and new values of the property to all the global listeners, and to all the listeners for the specified property name.
voidfirePropertyChange(PropertyChangeEvent event)
Fire a PropertyChangeEvent to all the global listeners, and to all the listeners for the specified property name.
PropertyChangeListener[]getPropertyChangeListeners()
Returns an array of all registered property change listeners.
PropertyChangeListener[]getPropertyChangeListeners(String propertyName)
Returns an array of all property change listeners registered under the given property name.
booleanhasListeners(String propertyName)
Tell whether the specified property is being listened on or not.
voidremovePropertyChangeListener(PropertyChangeListener l)
Removes a PropertyChangeListener from the list of global listeners.
voidremovePropertyChangeListener(String propertyName, PropertyChangeListener l)
Removes a PropertyChangeListener from listening to a specific property.

Constructor Detail

PropertyChangeSupport

public PropertyChangeSupport(Object source)
Create a PropertyChangeSupport to work with a specific source bean.

Parameters: source the source bean to use

Throws: NullPointerException if source is null

Method Detail

addPropertyChangeListener

public void addPropertyChangeListener(PropertyChangeListener l)
Adds a PropertyChangeListener to the list of global listeners. All property change events will be sent to this listener. The listener add is not unique: that is, n adds with the same listener will result in n events being sent to that listener for every property change. Adding a null listener is silently ignored. This method will unwrap a PropertyChangeListenerProxy, registering the underlying delegate to the named property list.

Parameters: l the listener to add

addPropertyChangeListener

public void addPropertyChangeListener(String propertyName, PropertyChangeListener l)
Adds a PropertyChangeListener listening on the specified property. Events will be sent to the listener only if the property name matches. The listener add is not unique; that is, n adds on a particular property for a particular listener will result in n events being sent to that listener when that property is changed. The effect is cumulative, too; if you are registered to listen to receive events on all property changes, and then you register on a particular property, you will receive change events for that property twice. Adding a null listener is silently ignored. This method will unwrap a PropertyChangeListenerProxy, registering the underlying delegate to the named property list if the names match, and discarding it otherwise.

Parameters: propertyName the name of the property to listen on l the listener to add

Throws: NullPointerException if propertyName is null

fireIndexedPropertyChange

public void fireIndexedPropertyChange(String name, int index, Object oldValue, Object newValue)
Fire an indexed property change event. This will only fire an event if the old and new values are not equal and not null.

Parameters: name the name of the property which changed index the index of the property which changed oldValue the old value of the property newValue the new value of the property

Since: 1.5

fireIndexedPropertyChange

public void fireIndexedPropertyChange(String name, int index, int oldValue, int newValue)
Fire an indexed property change event. This will only fire an event if the old and new values are not equal.

Parameters: name the name of the property which changed index the index of the property which changed oldValue the old value of the property newValue the new value of the property

Since: 1.5

fireIndexedPropertyChange

public void fireIndexedPropertyChange(String name, int index, boolean oldValue, boolean newValue)
Fire an indexed property change event. This will only fire an event if the old and new values are not equal.

Parameters: name the name of the property which changed index the index of the property which changed oldValue the old value of the property newValue the new value of the property

Since: 1.5

firePropertyChange

public void firePropertyChange(String propertyName, Object oldVal, Object newVal)
Fire a PropertyChangeEvent containing the old and new values of the property to all the global listeners, and to all the listeners for the specified property name. This does nothing if old and new are non-null and equal.

Parameters: propertyName the name of the property that changed oldVal the old value newVal the new value

firePropertyChange

public void firePropertyChange(String propertyName, int oldVal, int newVal)
Fire a PropertyChangeEvent containing the old and new values of the property to all the global listeners, and to all the listeners for the specified property name. This does nothing if old and new are equal.

Parameters: propertyName the name of the property that changed oldVal the old value newVal the new value

firePropertyChange

public void firePropertyChange(String propertyName, boolean oldVal, boolean newVal)
Fire a PropertyChangeEvent containing the old and new values of the property to all the global listeners, and to all the listeners for the specified property name. This does nothing if old and new are equal.

Parameters: propertyName the name of the property that changed oldVal the old value newVal the new value

firePropertyChange

public void firePropertyChange(PropertyChangeEvent event)
Fire a PropertyChangeEvent to all the global listeners, and to all the listeners for the specified property name. This does nothing if old and new values of the event are equal.

Parameters: event the event to fire

Throws: NullPointerException if event is null

getPropertyChangeListeners

public PropertyChangeListener[] getPropertyChangeListeners()
Returns an array of all registered property change listeners. Those that were registered under a name will be wrapped in a PropertyChangeListenerProxy, so you must check whether the listener is an instance of the proxy class in order to see what name the real listener is registered under. If there are no registered listeners, this returns an empty array.

Returns: the array of registered listeners

Since: 1.4

See Also: PropertyChangeListenerProxy

getPropertyChangeListeners

public PropertyChangeListener[] getPropertyChangeListeners(String propertyName)
Returns an array of all property change listeners registered under the given property name. If there are no registered listeners, or propertyName is null, this returns an empty array.

Returns: the array of registered listeners

Since: 1.4

hasListeners

public boolean hasListeners(String propertyName)
Tell whether the specified property is being listened on or not. This will only return true if there are listeners on all properties or if there is a listener specifically on this property.

Parameters: propertyName the property that may be listened on

Returns: whether the property is being listened on

removePropertyChangeListener

public void removePropertyChangeListener(PropertyChangeListener l)
Removes a PropertyChangeListener from the list of global listeners. If any specific properties are being listened on, they must be deregistered by themselves; this will only remove the general listener to all properties. If add() has been called multiple times for a particular listener, remove() will have to be called the same number of times to deregister it. This method will unwrap a PropertyChangeListenerProxy, removing the underlying delegate from the named property list.

Parameters: l the listener to remove

removePropertyChangeListener

public void removePropertyChangeListener(String propertyName, PropertyChangeListener l)
Removes a PropertyChangeListener from listening to a specific property. If add() has been called multiple times for a particular listener on a property, remove() will have to be called the same number of times to deregister it. This method will unwrap a PropertyChangeListenerProxy, removing the underlying delegate from the named property list if the names match.

Parameters: propertyName the property to stop listening on l the listener to remove

Throws: NullPointerException if propertyName is null