|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.jdesktop.beans.AbstractBean
public abstract class AbstractBean
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 vetoable
PropertyChangeEvent
events. These events are similar to
PropertyChange
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 not Serializable
. Special care must
be taken when creating Serializable
subclasses, as the
Serializable
listeners will not be saved. Subclasses will need to
manually save the serializable listeners. The AbstractSerializableBean
is Serializable
and already handles the listeners correctly. If
possible, it is recommended that Serializable
beans should extend
AbstractSerializableBean
. If it is not possible, the
AbstractSerializableBean
bean implementation provides details on
how to correctly serialize an AbstractBean
subclass.
AbstractSerializableBean
Constructor Summary | |
---|---|
protected |
AbstractBean()
Creates a new instance of AbstractBean |
protected |
AbstractBean(PropertyChangeSupport pcs,
VetoableChangeSupport vcs)
Creates a new instance of AbstractBean, using the supplied PropertyChangeSupport and VetoableChangeSupport delegates. |
Method Summary | |
---|---|
void |
addPropertyChangeListener(PropertyChangeListener listener)
Add a PropertyChangeListener to the listener list. |
void |
addPropertyChangeListener(String propertyName,
PropertyChangeListener listener)
Add a PropertyChangeListener for a specific property. |
void |
addVetoableChangeListener(String propertyName,
VetoableChangeListener listener)
Add a VetoableChangeListener for a specific property. |
void |
addVetoableChangeListener(VetoableChangeListener listener)
Add a VetoableListener to the listener list. |
Object |
clone()
Creates and returns a copy of this object. |
protected void |
fireIndexedPropertyChange(String propertyName,
int index,
Object oldValue,
Object newValue)
Report a bound indexed property update to any registered listeners. |
protected void |
firePropertyChange(PropertyChangeEvent evt)
Fire an existing PropertyChangeEvent to any registered listeners. |
protected void |
firePropertyChange(String propertyName,
Object oldValue,
Object newValue)
Report a bound property update to any registered listeners. |
protected void |
fireVetoableChange(PropertyChangeEvent evt)
Fire a vetoable property update to any registered listeners. |
protected void |
fireVetoableChange(String propertyName,
Object oldValue,
Object newValue)
Report a vetoable property update to any registered listeners. |
PropertyChangeListener[] |
getPropertyChangeListeners()
Returns an array of all the listeners that were added to the PropertyChangeSupport object with addPropertyChangeListener(). |
PropertyChangeListener[] |
getPropertyChangeListeners(String propertyName)
Returns an array of all the listeners which have been associated with the named property. |
VetoableChangeListener[] |
getVetoableChangeListeners()
Returns the list of VetoableChangeListeners. |
VetoableChangeListener[] |
getVetoableChangeListeners(String propertyName)
Returns an array of all the listeners which have been associated with the named property. |
protected boolean |
hasPropertyChangeListeners(String propertyName)
Check if there are any listeners for a specific property, including those registered on all properties. |
protected boolean |
hasVetoableChangeListeners(String propertyName)
Check if there are any listeners for a specific property, including those registered on all properties. |
void |
removePropertyChangeListener(PropertyChangeListener listener)
Remove a PropertyChangeListener from the listener list. |
void |
removePropertyChangeListener(String propertyName,
PropertyChangeListener listener)
Remove a PropertyChangeListener for a specific property. |
void |
removeVetoableChangeListener(String propertyName,
VetoableChangeListener listener)
Remove a VetoableChangeListener for a specific property. |
void |
removeVetoableChangeListener(VetoableChangeListener listener)
Remove a VetoableChangeListener from the listener list. |
Methods inherited from class java.lang.Object |
---|
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
protected AbstractBean()
protected AbstractBean(PropertyChangeSupport pcs, VetoableChangeSupport vcs)
Method Detail |
---|
public final void addPropertyChangeListener(PropertyChangeListener listener)
listener
is null, no exception is thrown and no action
is taken.
listener
- The PropertyChangeListener to be addedpublic final void removePropertyChangeListener(PropertyChangeListener listener)
listener
was added more than once to the same event
source, it will be notified one less time after being removed.
If listener
is null, or was never added, no exception is
thrown and no action is taken.
listener
- The PropertyChangeListener to be removedpublic final PropertyChangeListener[] getPropertyChangeListeners()
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 a
PropertyChangeListenerProxy
, 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" } } }
PropertyChangeListeners
added or an
empty array if no listeners have been addedPropertyChangeListenerProxy
public final void addPropertyChangeListener(String propertyName, PropertyChangeListener listener)
propertyName
or listener
is null, no
exception is thrown and no action is taken.
propertyName
- The name of the property to listen on.listener
- The PropertyChangeListener to be addedpublic final void removePropertyChangeListener(String propertyName, PropertyChangeListener listener)
listener
was added more than once to the same event
source for the specified property, it will be notified one less time
after being removed.
If propertyName
is null, no exception is thrown and no
action is taken.
If listener
is null, or was never added for the specified
property, no exception is thrown and no action is taken.
propertyName
- The name of the property that was listened on.listener
- The PropertyChangeListener to be removedpublic final PropertyChangeListener[] getPropertyChangeListeners(String propertyName)
propertyName
- The name of the property being listened to
PropertyChangeListeners
associated with
the named property. If no such listeners have been added,
or if propertyName
is null, an empty array is
returned.protected final void firePropertyChange(String propertyName, Object oldValue, Object newValue)
This is merely a convenience wrapper around the more general
firePropertyChange method that takes PropertyChangeEvent
value.
propertyName
- The programmatic name of the property
that was changed.oldValue
- The old value of the property.newValue
- The new value of the property.protected final void firePropertyChange(PropertyChangeEvent evt)
evt
- The PropertyChangeEvent object.protected final void fireIndexedPropertyChange(String propertyName, int index, Object oldValue, Object newValue)
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.
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.protected final boolean hasPropertyChangeListeners(String propertyName)
propertyName
is null, only check for listeners registered on all properties.
propertyName
- the property name.
protected final boolean hasVetoableChangeListeners(String propertyName)
propertyName
is null, only check for listeners registered on all properties.
propertyName
- the property name.
public final void addVetoableChangeListener(VetoableChangeListener listener)
listener
is null, no exception is thrown and no action
is taken.
listener
- The VetoableChangeListener to be addedpublic final void removeVetoableChangeListener(VetoableChangeListener listener)
listener
was added more than once to the same event
source, it will be notified one less time after being removed.
If listener
is null, or was never added, no exception is
thrown and no action is taken.
listener
- The VetoableChangeListener to be removedpublic final VetoableChangeListener[] getVetoableChangeListeners()
public final void addVetoableChangeListener(String propertyName, VetoableChangeListener listener)
propertyName
or listener
is null, no
exception is thrown and no action is taken.
propertyName
- The name of the property to listen on.listener
- The VetoableChangeListener to be addedpublic final void removeVetoableChangeListener(String propertyName, VetoableChangeListener listener)
listener
was added more than once to the same event
source for the specified property, it will be notified one less time
after being removed.
If propertyName
is null, no exception is thrown and no
action is taken.
If listener
is null, or was never added for the specified
property, no exception is thrown and no action is taken.
propertyName
- The name of the property that was listened on.listener
- The VetoableChangeListener to be removedpublic final VetoableChangeListener[] getVetoableChangeListeners(String propertyName)
propertyName
- The name of the property being listened to
VetoableChangeListeners
associated with
the named property. If no such listeners have been added,
or if propertyName
is null, an empty array is
returned.protected final void fireVetoableChange(String propertyName, Object oldValue, Object newValue) throws PropertyVetoException
No event is fired if old and new are equal and non-null.
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.
PropertyVetoException
- if the recipient wishes the property
change to be rolled back.protected final void fireVetoableChange(PropertyChangeEvent evt) throws PropertyVetoException
No event is fired if old and new are equal and non-null.
evt
- The PropertyChangeEvent to be fired.
PropertyVetoException
- if the recipient wishes the property
change to be rolled back.public Object clone() throws CloneNotSupportedException
will be true, and that the expression:x.clone() != x
will be true, but these are not absolute requirements. While it is typically the case that:x.clone().getClass() == x.getClass()
will be true, this is not an absolute requirement.x.clone().equals(x)
By convention, the returned object should be obtained by calling super.clone. If a class and all of its superclasses (except Object) obey this convention, it will be the case that x.clone().getClass() == x.getClass().
By convention, the object returned by this method should be independent of this object (which is being cloned). To achieve this independence, it may be necessary to modify one or more fields of the object returned by super.clone before returning it. Typically, this means copying any mutable objects that comprise the internal "deep structure" of the object being cloned and replacing the references to these objects with references to the copies. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned by super.clone need to be modified.
The method clone for class Object performs a specific cloning operation. First, if the class of this object does not implement the interface Cloneable, then a CloneNotSupportedException is thrown. Note that all arrays are considered to implement the interface Cloneable. Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation.
The class Object does not itself implement the interface Cloneable, so calling the clone method on an object whose class is Object will result in throwing an exception at run time.
clone
in class Object
CloneNotSupportedException
- if the object's class does not
support the Cloneable
interface. Subclasses
that override the clone
method can also
throw this exception to indicate that an instance cannot
be cloned.Cloneable
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |