Class AbstractBean
- java.lang.Object
-
- org.jdesktop.beans.AbstractBean
-
- Direct Known Subclasses:
AbstractFilter
,AbstractPainter
,AbstractSerializableBean
,JXGraph.Plot
,LoginService
,UserNameStore
public abstract class AbstractBean extends java.lang.Object
A convenience class from which to extend all non-visual AbstractBeans. It manages the PropertyChange notification system, making it relatively trivial to add support for property change events in getters/setters.
A non-visual java bean is a Java class that conforms to the AbstractBean patterns to allow visual manipulation of the bean's properties and event handlers at design-time.
Here is a simple example bean that contains one property, foo, and the proper pattern for implementing property change notification:
public class ABean extends AbstractBean { private String foo; public void setFoo(String newFoo) { String old = getFoo(); this.foo = newFoo; firePropertyChange("foo", old, getFoo()); } public String getFoo() { return foo; } }
You will notice that "getFoo()" is used in the setFoo method rather than accessing "foo" directly for the gets. This is done intentionally so that if a subclass overrides getFoo() to return, for instance, a constant value the property change notification system will continue to work properly.
The firePropertyChange method takes into account the old value and the new value. Only if the two differ will it fire a property change event. So you can be assured from the above code fragment that a property change event will only occur if old is indeed different from getFoo()
AbstractBean
also supports vetoablePropertyChangeEvent
events. These events are similar toPropertyChange
events, except a special exception can be used to veto changing the property. For example, perhaps the property is changing from "fred" to "red", but a listener deems that "red" is unexceptable. In this case, the listener can fire a veto exception and the property must remain "fred". For example:public class ABean extends AbstractBean { private String foo; public void setFoo(String newFoo) throws PropertyVetoException { String old = getFoo(); this.foo = newFoo; fireVetoableChange("foo", old, getFoo()); } public String getFoo() { return foo; } } public class Tester { public static void main(String... args) { try { ABean a = new ABean(); a.setFoo("fred"); a.addVetoableChangeListener(new VetoableChangeListener() { public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException { if ("red".equals(evt.getNewValue()) { throw new PropertyVetoException("Cannot be red!", evt); } } } a.setFoo("red"); } catch (Exception e) { e.printStackTrace(); // this will be executed } } }
AbstractBean
is notSerializable
. Special care must be taken when creatingSerializable
subclasses, as theSerializable
listeners will not be saved. Subclasses will need to manually save the serializable listeners. TheAbstractSerializableBean
isSerializable
and already handles the listeners correctly. If possible, it is recommended thatSerializable
beans should extendAbstractSerializableBean
. If it is not possible, theAbstractSerializableBean
bean implementation provides details on how to correctly serialize anAbstractBean
subclass.- See Also:
AbstractSerializableBean
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractBean()
Creates a new instance of AbstractBeanprotected
AbstractBean(java.beans.PropertyChangeSupport pcs, java.beans.VetoableChangeSupport vcs)
Creates a new instance of AbstractBean, using the supplied PropertyChangeSupport and VetoableChangeSupport delegates.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addPropertyChangeListener(java.beans.PropertyChangeListener listener)
Add a PropertyChangeListener to the listener list.void
addPropertyChangeListener(java.lang.String propertyName, java.beans.PropertyChangeListener listener)
Add a PropertyChangeListener for a specific property.void
addVetoableChangeListener(java.beans.VetoableChangeListener listener)
Add a VetoableListener to the listener list.void
addVetoableChangeListener(java.lang.String propertyName, java.beans.VetoableChangeListener listener)
Add a VetoableChangeListener for a specific property.java.lang.Object
clone()
protected void
fireIndexedPropertyChange(java.lang.String propertyName, int index, java.lang.Object oldValue, java.lang.Object newValue)
Report a bound indexed property update to any registered listeners.protected void
firePropertyChange(java.beans.PropertyChangeEvent evt)
Fire an existing PropertyChangeEvent to any registered listeners.protected void
firePropertyChange(java.lang.String propertyName, java.lang.Object oldValue, java.lang.Object newValue)
Report a bound property update to any registered listeners.protected void
fireVetoableChange(java.beans.PropertyChangeEvent evt)
Fire a vetoable property update to any registered listeners.protected void
fireVetoableChange(java.lang.String propertyName, java.lang.Object oldValue, java.lang.Object newValue)
Report a vetoable property update to any registered listeners.java.beans.PropertyChangeListener[]
getPropertyChangeListeners()
Returns an array of all the listeners that were added to the PropertyChangeSupport object with addPropertyChangeListener().java.beans.PropertyChangeListener[]
getPropertyChangeListeners(java.lang.String propertyName)
Returns an array of all the listeners which have been associated with the named property.java.beans.VetoableChangeListener[]
getVetoableChangeListeners()
Returns the list of VetoableChangeListeners.java.beans.VetoableChangeListener[]
getVetoableChangeListeners(java.lang.String propertyName)
Returns an array of all the listeners which have been associated with the named property.protected boolean
hasPropertyChangeListeners(java.lang.String propertyName)
Check if there are any listeners for a specific property, including those registered on all properties.protected boolean
hasVetoableChangeListeners(java.lang.String propertyName)
Check if there are any listeners for a specific property, including those registered on all properties.void
removePropertyChangeListener(java.beans.PropertyChangeListener listener)
Remove a PropertyChangeListener from the listener list.void
removePropertyChangeListener(java.lang.String propertyName, java.beans.PropertyChangeListener listener)
Remove a PropertyChangeListener for a specific property.void
removeVetoableChangeListener(java.beans.VetoableChangeListener listener)
Remove a VetoableChangeListener from the listener list.void
removeVetoableChangeListener(java.lang.String propertyName, java.beans.VetoableChangeListener listener)
Remove a VetoableChangeListener for a specific property.
-
-
-
Constructor Detail
-
AbstractBean
protected AbstractBean()
Creates a new instance of AbstractBean
-
AbstractBean
protected AbstractBean(java.beans.PropertyChangeSupport pcs, java.beans.VetoableChangeSupport vcs)
Creates a new instance of AbstractBean, using the supplied PropertyChangeSupport and VetoableChangeSupport delegates. Neither of these may be null.
-
-
Method Detail
-
addPropertyChangeListener
public final void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
Add a PropertyChangeListener to the listener list. The listener is registered for all properties. The same listener object may be added more than once, and will be called as many times as it is added. Iflistener
is null, no exception is thrown and no action is taken.- Parameters:
listener
- The PropertyChangeListener to be added
-
removePropertyChangeListener
public final void removePropertyChangeListener(java.beans.PropertyChangeListener listener)
Remove a PropertyChangeListener from the listener list. This removes a PropertyChangeListener that was registered for all properties. Iflistener
was added more than once to the same event source, it will be notified one less time after being removed. Iflistener
is null, or was never added, no exception is thrown and no action is taken.- Parameters:
listener
- The PropertyChangeListener to be removed
-
getPropertyChangeListeners
public final java.beans.PropertyChangeListener[] getPropertyChangeListeners()
Returns an array of all the listeners that were added to the PropertyChangeSupport object with addPropertyChangeListener().If some listeners have been added with a named property, then the returned array will be a mixture of PropertyChangeListeners and
PropertyChangeListenerProxy
s. If the calling method is interested in distinguishing the listeners then it must test each element to see if it's aPropertyChangeListenerProxy
, perform the cast, and examine the parameter.PropertyChangeListener[] listeners = bean.getPropertyChangeListeners(); for (int i = 0; i < listeners.length; i++) { if (listeners[i] instanceof PropertyChangeListenerProxy) { PropertyChangeListenerProxy proxy = (PropertyChangeListenerProxy)listeners[i]; if (proxy.getPropertyName().equals("foo")) { // proxy is a PropertyChangeListener which was associated // with the property named "foo" } } }
- Returns:
- all of the
PropertyChangeListeners
added or an empty array if no listeners have been added - See Also:
PropertyChangeListenerProxy
-
addPropertyChangeListener
public final void addPropertyChangeListener(java.lang.String propertyName, java.beans.PropertyChangeListener listener)
Add a PropertyChangeListener for a specific property. The listener will be invoked only when a call on firePropertyChange names that specific property. The same listener object may be added more than once. For each property, the listener will be invoked the number of times it was added for that property. IfpropertyName
orlistener
is null, no exception is thrown and no action is taken.- Parameters:
propertyName
- The name of the property to listen on.listener
- The PropertyChangeListener to be added
-
removePropertyChangeListener
public final void removePropertyChangeListener(java.lang.String propertyName, java.beans.PropertyChangeListener listener)
Remove a PropertyChangeListener for a specific property. Iflistener
was added more than once to the same event source for the specified property, it will be notified one less time after being removed. IfpropertyName
is null, no exception is thrown and no action is taken. Iflistener
is null, or was never added for the specified property, no exception is thrown and no action is taken.- Parameters:
propertyName
- The name of the property that was listened on.listener
- The PropertyChangeListener to be removed
-
getPropertyChangeListeners
public final java.beans.PropertyChangeListener[] getPropertyChangeListeners(java.lang.String propertyName)
Returns an array of all the listeners which have been associated with the named property.- Parameters:
propertyName
- The name of the property being listened to- Returns:
- all of the
PropertyChangeListeners
associated with the named property. If no such listeners have been added, or ifpropertyName
is null, an empty array is returned.
-
firePropertyChange
protected final void firePropertyChange(java.lang.String propertyName, java.lang.Object oldValue, java.lang.Object newValue)
Report a bound property update to any registered listeners. No event is fired if old and new are equal and non-null.This is merely a convenience wrapper around the more general firePropertyChange method that takes
PropertyChangeEvent
value.- Parameters:
propertyName
- The programmatic name of the property that was changed.oldValue
- The old value of the property.newValue
- The new value of the property.
-
firePropertyChange
protected final void firePropertyChange(java.beans.PropertyChangeEvent evt)
Fire an existing PropertyChangeEvent to any registered listeners. No event is fired if the given event's old and new values are equal and non-null.- Parameters:
evt
- The PropertyChangeEvent object.
-
fireIndexedPropertyChange
protected final void fireIndexedPropertyChange(java.lang.String propertyName, int index, java.lang.Object oldValue, java.lang.Object newValue)
Report a bound indexed property update to any registered listeners.No event is fired if old and new values are equal and non-null.
This is merely a convenience wrapper around the more general firePropertyChange method that takes
PropertyChangeEvent
value.- Parameters:
propertyName
- The programmatic name of the property that was changed.index
- index of the property element that was changed.oldValue
- The old value of the property.newValue
- The new value of the property.
-
hasPropertyChangeListeners
protected final boolean hasPropertyChangeListeners(java.lang.String propertyName)
Check if there are any listeners for a specific property, including those registered on all properties. IfpropertyName
is null, only check for listeners registered on all properties.- Parameters:
propertyName
- the property name.- Returns:
- true if there are one or more listeners for the given property
-
hasVetoableChangeListeners
protected final boolean hasVetoableChangeListeners(java.lang.String propertyName)
Check if there are any listeners for a specific property, including those registered on all properties. IfpropertyName
is null, only check for listeners registered on all properties.- Parameters:
propertyName
- the property name.- Returns:
- true if there are one or more listeners for the given property
-
addVetoableChangeListener
public final void addVetoableChangeListener(java.beans.VetoableChangeListener listener)
Add a VetoableListener to the listener list. The listener is registered for all properties. The same listener object may be added more than once, and will be called as many times as it is added. Iflistener
is null, no exception is thrown and no action is taken.- Parameters:
listener
- The VetoableChangeListener to be added
-
removeVetoableChangeListener
public final void removeVetoableChangeListener(java.beans.VetoableChangeListener listener)
Remove a VetoableChangeListener from the listener list. This removes a VetoableChangeListener that was registered for all properties. Iflistener
was added more than once to the same event source, it will be notified one less time after being removed. Iflistener
is null, or was never added, no exception is thrown and no action is taken.- Parameters:
listener
- The VetoableChangeListener to be removed
-
getVetoableChangeListeners
public final java.beans.VetoableChangeListener[] getVetoableChangeListeners()
Returns the list of VetoableChangeListeners. If named vetoable change listeners were added, then VetoableChangeListenerProxy wrappers will returned- Returns:
- List of VetoableChangeListeners and VetoableChangeListenerProxys if named property change listeners were added.
-
addVetoableChangeListener
public final void addVetoableChangeListener(java.lang.String propertyName, java.beans.VetoableChangeListener listener)
Add a VetoableChangeListener for a specific property. The listener will be invoked only when a call on fireVetoableChange names that specific property. The same listener object may be added more than once. For each property, the listener will be invoked the number of times it was added for that property. IfpropertyName
orlistener
is null, no exception is thrown and no action is taken.- Parameters:
propertyName
- The name of the property to listen on.listener
- The VetoableChangeListener to be added
-
removeVetoableChangeListener
public final void removeVetoableChangeListener(java.lang.String propertyName, java.beans.VetoableChangeListener listener)
Remove a VetoableChangeListener for a specific property. Iflistener
was added more than once to the same event source for the specified property, it will be notified one less time after being removed. IfpropertyName
is null, no exception is thrown and no action is taken. Iflistener
is null, or was never added for the specified property, no exception is thrown and no action is taken.- Parameters:
propertyName
- The name of the property that was listened on.listener
- The VetoableChangeListener to be removed
-
getVetoableChangeListeners
public final java.beans.VetoableChangeListener[] getVetoableChangeListeners(java.lang.String propertyName)
Returns an array of all the listeners which have been associated with the named property.- Parameters:
propertyName
- The name of the property being listened to- Returns:
- all the
VetoableChangeListeners
associated with the named property. If no such listeners have been added, or ifpropertyName
is null, an empty array is returned.
-
fireVetoableChange
protected final void fireVetoableChange(java.lang.String propertyName, java.lang.Object oldValue, java.lang.Object newValue) throws java.beans.PropertyVetoException
Report a vetoable property update to any registered listeners. If anyone vetos the change, then fire a new event reverting everyone to the old value and then rethrow the PropertyVetoException.No event is fired if old and new are equal and non-null.
- Parameters:
propertyName
- The programmatic name of the property that is about to change..oldValue
- The old value of the property.newValue
- The new value of the property.- Throws:
java.beans.PropertyVetoException
- if the recipient wishes the property change to be rolled back.
-
fireVetoableChange
protected final void fireVetoableChange(java.beans.PropertyChangeEvent evt) throws java.beans.PropertyVetoException
Fire a vetoable property update to any registered listeners. If anyone vetos the change, then fire a new event reverting everyone to the old value and then rethrow the PropertyVetoException.No event is fired if old and new are equal and non-null.
- Parameters:
evt
- The PropertyChangeEvent to be fired.- Throws:
java.beans.PropertyVetoException
- if the recipient wishes the property change to be rolled back.
-
clone
public java.lang.Object clone() throws java.lang.CloneNotSupportedException
- Overrides:
clone
in classjava.lang.Object
- Throws:
java.lang.CloneNotSupportedException
-
-