public class VetoableChangeMulticaster
extends java.lang.Object
implements java.io.Serializable
Sample usage.
class Thing { protected Color myColor = Color.red; // an example property protected boolean changePending; // track whether in midst of change // vetoable listeners: protected VetoableChangeMulticaster vetoers = new VetoableChangeMulticaster(this); // Possibly also some ordinary listeners: protected PropertyChangeMulticaster listeners = new PropertyChangeMulticaster(this); // registration methods, including: void addVetoer(VetoableChangeListener l) { // Use the `ifAbsent' version to avoid duplicate notifications vetoers.addVetoableChangeListenerIfAbsent(l); } public synchronized Color getColor() { // accessor return myColor; } // Simple transactional control for vetos public void setColor(int newColor) throws PropertyVetoException { Color oldColor = prepareSetColor(newColor); try { vetoers.fireVetoableChange("color", oldColor, newColor); commitColor(newColor); listeners.firePropertyChange("color", oldColor, newColor); } catch(PropertyVetoException ex) { abortSetColor(); throw ex; } } // Called on entry to proposed vetoable change from setColor. // Throws exception if there is already another change in progress. // Returns current color synchronized int prepareSetColor(Color c) throws PropertyVetoException { // only support one transaction at a time if (changePending) throw new PropertyVetoException("Concurrent modification"); // (Could alternatively wait out other transactions via // a wait/notify construction based on changePending.) // perhaps some other screenings, like: else if (c == null) throw new PropertyVetoException("Cannot change color to Null"); else { changePending = true; return myColor; } } synchronized void commitColor(Color newColor) { myColor = newColor; changePending = false; } synchronized void abortSetColor() { changePending = false; } }
Modifier and Type | Field and Description |
---|---|
protected java.util.HashMap |
children
HashMap for managing listeners for specific properties.
|
protected java.beans.VetoableChangeListener[] |
listeners
The array of listeners.
|
protected java.lang.Object |
source
The object to be provided as the "source" for any generated events.
|
Constructor and Description |
---|
VetoableChangeMulticaster(java.lang.Object sourceBean)
Constructs a
VetoableChangeMulticaster object. |
Modifier and Type | Method and Description |
---|---|
void |
addVetoableChangeListener(java.lang.String propertyName,
java.beans.VetoableChangeListener listener)
Add a VetoableChangeListener for a specific property.
|
void |
addVetoableChangeListener(java.beans.VetoableChangeListener listener)
Add a VetoableChangeListener to the listener list.
|
void |
addVetoableChangeListenerIfAbsent(java.lang.String propertyName,
java.beans.VetoableChangeListener listener)
Add a VetoableChangeListener for a specific property, if it is not
already registered.
|
void |
addVetoableChangeListenerIfAbsent(java.beans.VetoableChangeListener listener)
Add a PropertyChangeListener to the listener list if it is
not already present.
|
void |
fireVetoableChange(java.beans.PropertyChangeEvent evt)
Report a vetoable property update to any registered listeners.
|
void |
fireVetoableChange(java.lang.String propertyName,
boolean oldValue,
boolean newValue)
Report a vetoable property update to any registered listeners.
|
void |
fireVetoableChange(java.lang.String propertyName,
int oldValue,
int newValue)
Report a vetoable property update to any registered listeners.
|
void |
fireVetoableChange(java.lang.String propertyName,
java.lang.Object oldValue,
java.lang.Object newValue)
Report a vetoable property update to any registered listeners.
|
protected VetoableChangeMulticaster |
getChild(java.lang.String propertyName)
Return the child associated with property, or null if no such
|
boolean |
hasListeners(java.lang.String propertyName)
Check if there are any listeners for a specific property.
|
protected void |
multicast(java.beans.PropertyChangeEvent evt)
Helper method to relay evt to all listeners.
|
void |
removeVetoableChangeListener(java.lang.String propertyName,
java.beans.VetoableChangeListener listener)
Remove a VetoableChangeListener for a specific property.
|
void |
removeVetoableChangeListener(java.beans.VetoableChangeListener listener)
Remove an occurrence of a VetoableChangeListener from the listener list.
|
protected transient java.beans.VetoableChangeListener[] listeners
protected final java.lang.Object source
protected java.util.HashMap children
public VetoableChangeMulticaster(java.lang.Object sourceBean)
VetoableChangeMulticaster
object.sourceBean
- The bean to be given as the source for any events.java.lang.NullPointerException
- if sourceBean is nullprotected VetoableChangeMulticaster getChild(java.lang.String propertyName)
public void addVetoableChangeListener(java.beans.VetoableChangeListener listener)
listener
- The VetoableChangeListener to be addedpublic void addVetoableChangeListenerIfAbsent(java.beans.VetoableChangeListener listener)
listener
- The PropertyChangeListener to be addedjava.lang.NullPointerException
- If listener is nullpublic void removeVetoableChangeListener(java.beans.VetoableChangeListener listener)
listener
- The VetoableChangeListener to be removedpublic void addVetoableChangeListener(java.lang.String propertyName, java.beans.VetoableChangeListener listener)
propertyName
- The name of the property to listen on.listener
- The VetoableChangeListener to be addedjava.lang.NullPointerException
- If listener is nullpublic void addVetoableChangeListenerIfAbsent(java.lang.String propertyName, java.beans.VetoableChangeListener listener)
propertyName
- The name of the property to listen on.listener
- The VetoableChangeListener to be addedjava.lang.NullPointerException
- If listener is nullpublic void removeVetoableChangeListener(java.lang.String propertyName, java.beans.VetoableChangeListener listener)
propertyName
- The name of the property that was listened on.listener
- The VetoableChangeListener to be removedprotected void multicast(java.beans.PropertyChangeEvent evt) throws java.beans.PropertyVetoException
java.beans.PropertyVetoException
public void fireVetoableChange(java.lang.String propertyName, java.lang.Object oldValue, java.lang.Object newValue) throws java.beans.PropertyVetoException
No event is fired if old and new are equal non-null.
propertyName
- The programmatic name of the property
that was changed.oldValue
- The old value of the property.newValue
- The new value of the property.java.beans.PropertyVetoException
- if a recipient wishes the property
change to be rolled back.public void fireVetoableChange(java.lang.String propertyName, int oldValue, int newValue) throws java.beans.PropertyVetoException
No event is fired if old and new are equal.
This is merely a convenience wrapper around the more general fireVetoableChange method that takes Object values.
propertyName
- The programmatic name of the property
that was changed.oldValue
- The old value of the property.newValue
- The new value of the property.java.beans.PropertyVetoException
- if the recipient wishes the property
change to be rolled back.public void fireVetoableChange(java.lang.String propertyName, boolean oldValue, boolean newValue) throws java.beans.PropertyVetoException
No event is fired if old and new are equal.
This is merely a convenience wrapper around the more general fireVetoableChange method that takes Object values.
propertyName
- The programmatic name of the property
that was changed.oldValue
- The old value of the property.newValue
- The new value of the property.java.beans.PropertyVetoException
- if the recipient wishes the property
change to be rolled back.public void fireVetoableChange(java.beans.PropertyChangeEvent evt) throws java.beans.PropertyVetoException
No event is fired if old and new are equal and non-null. equal and non-null.
evt
- The PropertyChangeEvent object.java.beans.PropertyVetoException
- if the recipient wishes the property
change to be rolled back.public boolean hasListeners(java.lang.String propertyName)
propertyName
- the property name.