Prev Class | Next Class | Frames | No Frames |
Summary: Nested | Field | Method | Constr | Detail: Nested | Field | Method | Constr |
java.lang.Object
com.jgoodies.binding.beans.Model
com.jgoodies.binding.beans.BeanAdapter
public class BeanAdapter
extends Model
getValueModel(String)
or for a triple of (property name, getter
name, setter name) using getValueModel(String,String,String)
.
If you just specify the property name, the adapter uses the standard
Java Bean introspection to lookup the available properties and how to
read and write the property value. In case of custom readers and writers
you may specify a custom BeanInfo class, or as a shortcut use the method
that accepts the optional getter and setter name. If these are specified,
introspection will be bypassed and a PropertyDescriptor will be
created for the given property name, getter and setter name.
Note: For each property name subsequent calls
to these methods must use the same getter and setter names. Attempts
to violate this constraint are rejected with an IllegalArgumentException.
Property values for a given property name can be read using
getValue(String)
. To set a value for a for a property name
invoke setValue(String,Object)
.
Optionally the BeanAdapter can observe changes in bound
properties as described in section 7.4 of the Bean specification.
The bean then must provide support for listening on properties as described
in section 7.4 of this specification.
You can enable this feature by setting the constructor parameter
observeChanges
to true
.
If the adapter observes changes, the ValueModels returned by
#getValueModel
will fire value change events,
i.e. PropertyChangeEvents for the property "value"
.
Even if you ignore property changes, you can access the adapted
property value via #getValue()
.
It's just that you won't be notified about changes.
In addition you can observe the bean's bound properties
by registering PropertyChangeListeners with the bean using
#addBeanPropertyChangeListener
. These listeners will be removed
from the old bean before the bean changes and will be re-added after
the new bean has been set. Therefore these listeners will be notified
about changes only if the current bean changes a property. They won't be
notified if the bean changes - and in turn the property value. If you want
to observes property changes caused by bean changes too, register with the
adapting ValueModel as returned by #getValueModel(String)
.
The BeanAdapter provides two access styles to the target bean
that holds the adapted 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 adapted properties.
If the adapted bean is null
the BeanAdapter can
neither read nor set a value. In this case #getValue
returns null
and #setValue
will silently
ignore the new value.
This adapter throws three PropertyChangeEvents if the bean changes:
beforeBean, bean and afterBean. This is useful
when sharing a bean channel and you must perform an operation before
or after other listeners handle a bean change. Since you cannot rely
on the order listeners will be notified, only the beforeBean
and afterBean events are guaranteed to be fired before and
after the bean change is fired.
Note that #getBean()
returns the new bean before
any of these three PropertyChangeEvents is fired. Therefore listeners
that handle these events must use the event's old and new value
to determine the old and new bean.
The order of events fired during a bean change is:setBean(null)
or set the
bean channel's value to null.
As an alternative you can use event listener lists in your beans
that implement references with WeakReference
.
Setting the bean to null has side-effects, for example the adapter
fires a change event for the bound property bean and other properties.
And the value of ValueModel's vended by this adapter may change.
However, typically this is fine and setting the bean to null
is the first choice for removing the reference from the bean to the adapter.
Another way to clear the reference from the target bean is
to call #release
. It has no side-effects, but the adapter
must not be used anymore once #release has been called.
Constraints: If property changes shall be observed,
the bean class must support bound properties, i. e. it 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);PropertyAdapter vs. BeanAdapter vs. PresentationModel
PropertyAdapter
does for a
single bean property.
If you adapt multiple properties of the same bean, you better use
the BeanAdapter. It registers a single PropertyChangeListener with the bean,
where multiple PropertyAdapters would register multiple listeners.
If you adapt bean properties for an editor, you will typically use the
PresentationModel
. The PresentationModel is
more powerful than the BeanAdapter. It adds support for buffered models,
and provides an extensible mechanism for observing the change state
of the bean and related objects.
Basic Examples:
// Direct access, ignores changes Address address = new Address() BeanAdapter adapter = new BeanAdapter(address); adapter.setValue("street", "Broadway"); System.out.println(address.getStreet()); // Prints "Broadway" address.setStreet("Franz-Josef-Str."); System.out.println(adapter.getValue("street")); // Prints "Franz-Josef-Str." //Direct access, observes changes BeanAdapter adapter = new BeanAdapter(address, true); // Indirect access, ignores changes ValueHolder addressHolder = new ValueHolder(address1); BeanAdapter adapter = new BeanAdapter(addressHolder); adapter.setValue("street", "Broadway"); // Sets the street in address1 System.out.println(address1.getStreet()); // Prints "Broadway" adapter.setBean(address2); adapter.setValue("street", "Robert-Koch-Str."); // Sets the street in address2 System.out.println(address2.getStreet()); // Prints "Robert-Koch-Str." // Indirect access, observes changes ValueHolder addressHolder = new ValueHolder(); BeanAdapter adapter = new BeanAdapter(addressHolder, true); addressHolder.setValue(address1); address1.setStreet("Broadway"); System.out.println(adapter.getValue("street")); // Prints "Broadway" // Access through ValueModels Address address = new Address(); BeanAdapter adapter = new BeanAdapter(address); ValueModel streetModel = adapter.getValueModel("street"); ValueModel cityModel = adapter.getValueModel("city"); streetModel.setValue("Broadway"); System.out.println(address.getStreet()); // Prints "Broadway" address.setCity("Hamburg"); System.out.println(cityModel.getValue()); // Prints "Hamburg"Adapter Chain Example:
Country country = new Country(); country.setName("Germany"); country.setEuMember(true); BeanAdapter countryAdapter = new BeanAdapter(country, true); JTextField nameField = new JTextField(); nameField.setDocument(new DocumentAdapter( countryAdapter.getValueModel("name"))); JCheckBox euMemberBox = new JCheckBox("Is EU Member"); euMemberBox.setModel(new ToggleButtonAdapter( countryAdapter.getValueModel("euMember"))); // Using factory methods JTextField nameField = Factory.createTextField(country, "name"); JCheckBox euMemberBox = Factory.createCheckBox (country, "euMember"); euMemberBox.setText("Is EU Member");As of version 1.3 this class is no longer marked as final, but lacks the documentation for subclass constraints. I plan to introduce an interface that shall describe the semantics required by the PresentationModel class. Until then, this BeanAdapter implementation describes the semantics and all constraints. TODO: Improve the class comment and focus on the main features. TODO: Consider adding a feature to ensure that update notifications are performed in the event dispatch thread. In case the adapted bean is changed in a thread other than the event dispatch thread, such a feature would help complying with Swing's single thread rule. The feature could be implemented by an extended PropertyChangeSupport. TODO: I plan to improve the support for adapting beans that do not fire PropertyChangeEvents. This affects the classes PropertyAdapter, BeanAdapter, and PresentationModel. Basically the PropertyAdapter and the BeanAdapter's internal SimplePropertyAdapter's shall be able to optionally self-fire a PropertyChangeEvent in case the bean does not. There are several downsides with self-firing events compared to bound bean properties. See Issue 49 for more information about the downsides. The observeChanges constructor parameter shall be replaced by a more fine-grained choice to not observe (former observeChanges=false), to observe bound properties (former observeChanges=true), and a new setting for self-firing PropertyChangeEvents if a value is set. The latter case may be further splitted up to specify how the self-fired PropertyChangeEvent is created:
PropertyAdapter
, ValueModel
, ValueModel.getValue()
, ValueModel.setValue(Object)
, PropertyChangeEvent
, PropertyChangeListener
, java.beans.Introspector
, java.beans.BeanInfo
, PropertyDescriptor
Nested Class Summary | |
class |
|
Field Summary | |
static String |
|
static String |
|
static String |
|
static String |
|
Constructor Summary | |
| |
| |
| |
|
Method Summary | |
void |
|
void |
|
protected BeanAdapter.SimplePropertyAdapter |
|
Object |
|
ValueModel |
|
PropertyChangeListener[] |
|
PropertyChangeListener[] |
|
boolean |
|
Object |
|
BeanAdapter.SimplePropertyAdapter |
|
BeanAdapter.SimplePropertyAdapter |
|
boolean |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
public static final String PROPERTYNAME_AFTER_BEAN
The property name used in the PropertyChangeEvent that is fired after the bean property fires its PropertyChangeEvent. Useful to perform an operation after listeners that handle the bean change are notified. See also the class comment.
public static final String PROPERTYNAME_BEAN
The name of the read-write bound property that holds the target bean.
- See Also:
getBean()
,setBean(Object)
public static final String PROPERTYNAME_BEFORE_BEAN
The property name used in the PropertyChangeEvent that is fired before the bean property fires its PropertyChangeEvent. Useful to perform an operation before listeners that handle the bean change are notified. See also the class comment.
public static final String PROPERTYNAME_CHANGED
The name of the read-only bound bean property that indicates whether one of the observed properties has changed.
- See Also:
isChanged()
public BeanAdapter(Object bean)
Constructs a BeanAdapter for the given bean; does not observe changes. Installs a default bean channel that checks the identity not equity to ensure that listeners are reregistered properly if the old and new bean are equal but not the same.
- Parameters:
bean
- the bean that owns the properties to adapt
public BeanAdapter(Object bean, boolean observeChanges)
Constructs a BeanAdapter for the given bean; observes changes if specified. Installs a default bean channel that checks the identity not equity to ensure that listeners are reregistered properly if the old and new bean are equal but not the same.
- Parameters:
bean
- the bean that owns the properties to adaptobserveChanges
-true
to observe changes of bound or constrained properties,false
to ignore changes
public BeanAdapter(ValueModel beanChannel)
Constructs a BeanAdapter for the given bean channel; does not observe changes. It is strongly recommended that the bean channel checks the identity not equity. This ensures that listeners are reregistered properly if the old and new bean are equal but not the same.
- Parameters:
beanChannel
- the ValueModel that holds the bean
public BeanAdapter(ValueModel beanChannel, boolean observeChanges)
Constructs a BeanAdapter for the given bean channel; observes changes if specified. It is strongly recommended that the bean channel checks the identity not equity. This ensures that listeners are reregistered properly if the old and new bean are equal but not the same.
- Parameters:
beanChannel
- the ValueModel that holds the beanobserveChanges
-true
to observe changes of bound or constrained properties,false
to ignore changes
public void addBeanPropertyChangeListener(PropertyChangeListener listener)
Adds a PropertyChangeListener to the list of bean listeners. The listener is registered for all bound properties of the target bean. The listener will be notified if and only if this BeanAdapter's current bean changes a property. It'll not be notified if the bean changes. If listener isnull
, no exception is thrown and no action is performed.
- Parameters:
listener
- the PropertyChangeListener to be added
public void addBeanPropertyChangeListener(String propertyName, PropertyChangeListener listener)
Adds a PropertyChangeListener to the list of bean listeners for a specific property. The specified property may be user-defined. The listener will be notified if and only if this BeanAdapter's current bean changes the specified property. It'll not be notified if the bean changes. If you want to observe property changes and bean changes, you may observe the ValueModel that adapts this property - as returned by#getValueModel(String)
. 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 isnull
, no exception is thrown and no action is performed.
- Parameters:
propertyName
- one of the property names listed abovelistener
- the PropertyChangeListener to be added
protected BeanAdapter.SimplePropertyAdapter createPropertyAdapter(String propertyName, String getterName, String setterName)
Creates and returns a SimplePropertyAdapter that adapts the bound property with the specified name.
- Parameters:
propertyName
- the name of the property to adaptgetterName
- the name of the method that reads the valuesetterName
- the name of the method that sets the value
- Returns:
- a SimplePropertyAdapter that adapts the property with the specified name
- Since:
- 1.4
public Object getBean()
Returns the Java Bean that holds the adapted properties.
- Returns:
- the Bean that holds the adapted properties
- See Also:
setBean(Object)
public ValueModel getBeanChannel()
Returns the ValueModel that holds the bean that in turn holds the adapted properties. This bean channel is shared by the PropertyAdapters created by the factory method#getValueModel
.
- Returns:
- the ValueModel that holds the bean that in turn holds the adapted properties
- Since:
- 1.3
- See Also:
getBean()
,setBean(Object)
public PropertyChangeListener[] getBeanPropertyChangeListeners()
Returns an array of all the property change listeners registered on this component.
- Returns:
- all of this component's
PropertyChangeListener
s or an empty array if no property change listeners are currently registered
- See Also:
addBeanPropertyChangeListener(PropertyChangeListener)
,removeBeanPropertyChangeListener(PropertyChangeListener)
,getBeanPropertyChangeListeners(String)
,java.beans.PropertyChangeSupport.getPropertyChangeListeners()
public PropertyChangeListener[] getBeanPropertyChangeListeners(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
public boolean getObserveChanges()
Answers whether this adapter observes changes in the adapted Bean properties.
- Returns:
- true if this adapter observes changes, false if not
public Object getValue(String propertyName)
Returns the value of specified bean property,null
if the current bean isnull
. This operation is supported only for readable bean properties.
- Parameters:
propertyName
- the name of the property to be read
- Returns:
- the value of the adapted bean property, null if the bean is null
public BeanAdapter.SimplePropertyAdapter getValueModel(String propertyName)
Looks up and lazily creates a ValueModel that adapts the bound property with the specified name. Uses the Bean introspection to look up the getter and setter names. Subsequent calls to this method with the same property name return the same ValueModel. To prevent potential runtime errors this method eagerly looks up the associated PropertyDescriptor if the target bean is not null. For each property name all calls to this method and to#getValueModel(String, String, String)
must use the same getter and setter names. Attempts to violate this constraint will be rejected with an IllegalArgumentException. Especially once you've called this method you must not call#getValueModel(String, String, String)
with a non-null getter or setter name. And vice versa, once you've called the latter method with a non-null getter or setter name, you must not call this method. This method uses a return type of AbstractValueModel, not a ValueModel. This makessetVetoableValue(String,Object)
visible. It also makes the AbstractValueModel convenience type converters available, which can significantly shrink the source code necessary to read and write values from/to these models.
- Parameters:
propertyName
- the name of the property to adapt
- Returns:
- a ValueModel that adapts the property with the specified name
public BeanAdapter.SimplePropertyAdapter getValueModel(String propertyName, String getterName, String setterName)
Looks up and lazily creates a ValueModel that adapts the bound property with the specified name. Unlike#getValueModel(String)
this method bypasses the Bean Introspection and uses the given getter and setter names to setup the access to the adapted Bean property. Subsequent calls to this method with the same parameters will return the same ValueModel. To prevent potential runtime errors this method eagerly looks up the associated PropertyDescriptor if the target bean is not null. For each property name all calls to this method and to#getValueModel(String)
must use the same getter and setter names. Attempts to violate this constraint will be rejected with an IllegalArgumentException. Especially once you've called this method with a non-null getter or setter name, you must not call#getValueModel(String)
. And vice versa, once you've called the latter method you must not call this method with a non-null getter or setter name. This method uses a return type of AbstractValueModel, not a ValueModel. This makessetVetoableValue(String,Object)
visible. It also makes the AbstractValueModel convenience type converters available, which can significantly shrink the source code necessary to read and write values from/to these models.
- Parameters:
propertyName
- the name of the property to adaptgetterName
- the name of the method that reads the valuesetterName
- the name of the method that sets the value
- Returns:
- a ValueModel that adapts the property with the specified name
public boolean isChanged()
Answers whether a bean property has changed since the changed state has been reset. The changed state is implicitly reset everytime the target bean changes.
- Returns:
- true if a property of the current target bean has changed since the last reset
public void release()
Removes the PropertyChangeHandler from the observed bean, if the bean is notnull
and if bean property changes are observed. Also removes all listeners from the bean that have been registered with#addBeanPropertyChangeListener
before. BeanAdapters that observe changes have a PropertyChangeListener registered with the target bean. Hence, a bean has a reference to all BeanAdapters that observe it. To avoid memory leaks it is recommended to remove this listener if the bean lives much longer than the BeanAdapter, enabling the garbage collector to remove the adapter. To do so, you can callsetBean(null)
or set the bean channel's value to null. As an alternative you can use event listener lists in your beans that implement references withWeakReference
. Setting the bean to null has side-effects, for example the adapter fires a change event for the bound property bean and other properties. And the value of ValueModel's vended by this adapter may change. However, typically this is fine and setting the bean to null is the first choice for removing the reference from the bean to the adapter. Another way to clear the reference from the target bean is to call#release
. It has no side-effects, but the adapter must not be used anymore once #release has been called.
- See Also:
setBean(Object)
,java.lang.ref.WeakReference
public void removeBeanPropertyChangeListener(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 isnull
, no exception is thrown and no action is performed.
- Parameters:
listener
- the PropertyChangeListener to be removed
public void removeBeanPropertyChangeListener(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 isnull
, no exception is thrown and no action is performed.
- Parameters:
propertyName
- a valid property namelistener
- the PropertyChangeListener to be removed
public void resetChanged()
Resets this tracker's changed state tofalse
.
public void setBean(Object newBean)
Sets a new Java Bean as holder of the adapted properties. Notifies any registered value listeners that are registered with the adapting ValueModels created in#getValueModel
. Also notifies listeners that have been registered with this adapter to observe the bound property bean. Resets the changed state tofalse
. If this adapter observes bean changes, the bean change handler will be removed from the former bean and will be added to the new bean. Hence, if the new bean isnull
, this adapter has no listener registered with a bean. And so,setBean(null)
can be used as a clean release method that allows to use this adapter later again.
- Parameters:
newBean
- the new holder of the adapted properties
- See Also:
getBean()
,isChanged()
,resetChanged()
,release()
public void setValue(String propertyName, Object newValue)
Sets the given new value for the specified bean property. Does nothing if this adapter's bean isnull
. If the setter associated with the propertyName throws a PropertyVetoException, it is silently ignored. Notifies the associated value change listeners if the bean reports a property change. Note that a bean may suppress PropertyChangeEvents if the old and new value are the same, or if the old and new value are equal. This operation is supported only for writable bean properties.
- Parameters:
propertyName
- the name of the property to setnewValue
- the value to set
public void setVetoableValue(String propertyName, Object newValue) throws PropertyVetoException
Sets a new value for the specified bean property. Does nothing if the bean isnull
. If the setter associated with the propertyName throws a PropertyVetoException, this methods throws the same exception. Notifies the associated value change listeners if the bean reports a property change. Note that a bean may suppress PropertyChangeEvents if the old and new value are the same, or if the old and new value are equal. This operation is supported only for writable bean properties.
- Parameters:
propertyName
- the name of the property to setnewValue
- the value to set
- Since:
- 1.1