com.jgoodies.binding.beans
public final class PropertyConnector extends java.lang.Object
If one of the bean properties fires a property change, this connector will set the other to the same value. If a bean property is read-only, the PropertyConnector will not listen to the other bean's property and so won't update the read-only property. And if a bean does not provide support for bound properties, it won't be observed. The properties must be single value bean properties as described by the Java Bean Secification.
Constraints: the properties must be type compatible, i. e. values returned by one reader must be accepted by the other's writer, and vice versa.
Examples:
Note that the following examples are for demonstration purposes.
The classes BasicComponentFactory
and Bindings
provide predefined connections for formatted text fields and combo boxes.
// Connects a ValueModel and a JFormattedTextField JFormattedTextField textField = new JFormattedTextField(); textField.setEditable(editable); PropertyConnector connector = PropertyConnector.connectAndUpdate(valueModel, "value", textField, "value"); // Connects the boolean property "selectable" with a component enablement JComboBox comboBox = new JComboBox(); ... PropertyConnector.connect(mainModel, "selectable", comboBox, "enabled");
PropertyChangeEvent
,
PropertyChangeListener
,
PropertyDescriptor
Modifier and Type | Method and Description |
---|---|
static PropertyConnector |
connect(java.lang.Object bean1,
java.lang.String property1Name,
java.lang.Object bean2,
java.lang.String property2Name)
Synchronizes the two bound bean properties as specified
by the given pairs of bean and associated property name.
|
static void |
connectAndUpdate(ValueModel valueModel,
java.lang.Object bean2,
java.lang.String property2Name)
Synchronizes the ValueModel with the specified bound bean property,
and updates the bean immediately.
|
java.lang.Object |
getBean1()
Returns the Java Bean that holds the first property.
|
java.lang.Object |
getBean2()
Returns the Java Bean that holds the first property.
|
java.lang.String |
getProperty1Name()
Returns the name of the first Java Bean property.
|
java.lang.String |
getProperty2Name()
Returns the name of the second Java Bean property.
|
void |
release()
Removes the PropertyChangeHandler from the observed bean,
if the bean is not null and if property changes are not observed.
|
void |
updateProperty1()
Reads the value of the second bean property and sets it as new
value of the first bean property.
|
void |
updateProperty2()
Reads the value of the first bean property and sets it as new
value of the second bean property.
|
public static PropertyConnector connect(java.lang.Object bean1, java.lang.String property1Name, java.lang.Object bean2, java.lang.String property2Name)
Bean1#property1Name
changes it updates
Bean2#property2Name
and vice versa.
If a bean does not provide support for bound properties,
changes will not be observed.
If a bean property is read-only, this connector won't listen to
the other bean's property and so won't update the read-only property.Returns the PropertyConnector that is required if one or the other property shall be updated.
bean1
- the bean that owns the first propertyproperty1Name
- the name of the first propertybean2
- the bean that owns the second propertyproperty2Name
- the name of the second propertyjava.lang.NullPointerException
- if a bean or property name is null
java.lang.IllegalArgumentException
- if the beans are identical and
the property name are equalpublic static void connectAndUpdate(ValueModel valueModel, java.lang.Object bean2, java.lang.String property2Name)
Bean2#property2Name
and vice versa. If the bean doesn't provide support for bound properties,
changes will not be observed.
If the bean property is read-only, this connector will not listen
to the ValueModel and so won't update the read-only property.valueModel
- the ValueModel that provides a bound valuebean2
- the bean that owns the second propertyproperty2Name
- the name of the second propertyjava.lang.NullPointerException
- if the ValueModel, bean or property name is null
java.lang.IllegalArgumentException
- if the bean is the ValueModel
and the property name is "value"
public java.lang.Object getBean1()
public java.lang.Object getBean2()
public java.lang.String getProperty1Name()
public java.lang.String getProperty2Name()
public void updateProperty1()
updateProperty2()
public void updateProperty2()
updateProperty1()
public void release()
#release
.To avoid memory leaks it is recommended to invoke this method, if the connected beans live much longer than this connector.
As an alternative you may use event listener lists in the connected
beans that are implemented using WeakReference
.
WeakReference
Copyright © 2002-2010 JGoodies Karsten Lentzsch. All Rights Reserved.