|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjavax.beans.binding.Binding
public class Binding
Binding represents a binding between two properties of two
objects. Once bound, the two properties of the two objects are
kept in sync.
The following example illustrates binding the "name" property
of a Customer to the "text" property of a JTextField:
class Customer {
public void setName(String text);
public String getName();
}
JTextField textField = ...;
Customer customer = ...;
new Binding(customer, "${name}", textField, "text").bind();
The properties of the two objects are identified as a dot separated list of
property names resolved using reflection. For example, the path
"manager.firstName" is equivalent to
target.getManager().getFirstName().
Binding makes use of PropertyResolver to resolve
the value for a property; refer to it for information on the path syntax.
To keep the two properties of the two objects in sync, listeners are
installed on all objects along the paths. Any time an object along the path
changes, the other object is updated. For example, if the source path is
"manager.firstName", and either the "manager" or "firstName" property
changes, the the target property is updated. There is one exception to
this; when initially bound if the target path is incomplete, changes in the
target path only update the source if getValueForIncompleteTargetPath
returns non-null, otherwise, once the target path becomes complete
it's value is reset from that of the source.
Data flowing from the source to the target, and from target to
source, are passed to a BindingConverter. A BindingConverter is used to convert the value in some way. For
example, you might bind the background color of a JTextField to a
string property with a BindingConverter that can convert the
String to a Color.
If a BindingValidator is specified, all changes from the
target are passed to the BindingValidator. The BindingValidator is used for testing the validity of a change, as
well as for specifying if the change should be propagated back to
the source.
A Binding also specifies an update strategy for
the binding. The update strategy dictates how the
source and target properties are kept in sync.
The source and target are typically non-null values. An
exception to this is for children bindings, in which case the parent
binding supplies the two endpoints.
Once a Binding is bound it can not be
mutated. All setter methods of this class throw an IllegalStateException if invoked on a bound Binding.
Often times it is not possible to evaluate the path of the source
or target; this is referred to as an incomplete path. For example,
if source.getManager() returns null when evaluating the
path "manager.firstName", then the path is incomplete.
Binding provides the setValueForIncompleteSourcePath and
setValueForIncompleteTargetPath methods that allow you to specify the
value to use when the source or target path is incomplete. If a property
along the source or target path changes, and the corresponding value for
incomplete path property is non-null, then it is applied to the
opposite object, otherwise the opposite property is not updated. For example,
if the source path is "manager.firstName", the target path is "text", and
the "manager" property of the source is initially null, then if
setValueForIncompleteSourcePath is non-null, it is set on
the target, otherwise the target is not updated.
SwingBindingSupport| Nested Class Summary | |
|---|---|
class |
Binding.BindingController
BindingController is used by BindingTargets to control
the binding. |
static class |
Binding.Parameter<T>
Parameter is used to provide additional information to configure a
specific binding. |
static class |
Binding.UpdateStrategy
Enumeration of the possible ways the source and target properties can be kept in sync. |
static class |
Binding.ValueState
Enumeration of the possible states the source and target may be in. |
| Constructor Summary | |
|---|---|
Binding()
Creates a Binding. |
|
Binding(java.lang.Object source,
java.lang.String sourcePath,
java.lang.Object target,
java.lang.String targetPath,
java.lang.Object... args)
Creates a Binding. |
|
Binding(java.lang.String sourcePath,
java.lang.String targetPath,
java.lang.Object... args)
Creates a Binding. |
|
| Method Summary | ||
|---|---|---|
void |
addBinding(Binding binding)
Adds a Binding as a child of this Binding. |
|
Binding |
addBinding(java.lang.String sourcePath,
java.lang.String targetPath,
java.lang.Object... args)
Creates and adds a Binding as a child of this Binding. |
|
void |
addBindingListener(BindingListener listener)
Adds a BindingListener to this Binding. |
|
void |
bind()
Realizes this Binding. |
|
java.util.List<Binding> |
getBindings()
Returns a list of the child Bindings of this Binding. |
|
BindingContext |
getContext()
Returns the BindingContext this Binding is
contained in. |
|
BindingConverter |
getConverter()
Returns the BindingConverter used to convert values. |
|
ListCondenser |
getListCondenser()
Returns the ListCondenser that is used to condense a list into
a single value. |
|
java.lang.Object |
getNullSourceValue()
Returns the value to use if the value of the property of the source is null. |
|
java.lang.Object |
getNullTargetValue()
Returns the value to use if the value of the property of the target is null. |
|
java.lang.Object |
getSource()
Returns the source of the binding. |
|
java.lang.String |
getSourcePath()
Returns the path to the property of the source to bind to. |
|
BindingConverter |
getSourceToTargetConverter(java.lang.Class<?> sourceType,
java.lang.Object sourceValue,
java.lang.Class<?> targetType)
Returns the BindingConverter used to convert the source value to the
target value. |
|
Binding.ValueState |
getSourceValueState()
Returns the value state of the source. |
|
java.lang.Object |
getTarget()
Returns the target of the binding. |
|
java.lang.String |
getTargetPath()
Returns the path to the property of the target to bind to as a PropertyPath. |
|
BindingConverter |
getTargetToSourceConverter(java.lang.Class<?> targetType,
java.lang.Object targetValue,
java.lang.Class<?> sourceType)
Returns the BindingConverter used to convert the target value to the source value. |
|
Binding.ValueState |
getTargetValueState()
Returns the value state of the target. |
|
Binding.UpdateStrategy |
getUpdateStrategy()
Returns the update strategy. |
|
BindingValidator |
getValidator()
Returns the BindingValidator used to validate changes
originating from the target. |
|
|
getValue(Binding.Parameter<T> key,
T defaultValue)
Returns the value for the specified parameter. |
|
java.lang.Object |
getValueForIncompleteSourcePath()
Returns the value to use if the source path can not be completely evaluated. |
|
java.lang.Object |
getValueForIncompleteTargetPath()
Returns the value to use if the target path can not be completely evaluated. |
|
boolean |
isBound()
Returns true if currently bound. |
|
void |
removeBinding(Binding binding)
Removes a previously added Binding. |
|
void |
removeBindingListener(BindingListener listener)
Removes a BindingListener from this Binding. |
|
void |
setConverter(BindingConverter converter)
Sets the BindingConverter used to convert values. |
|
void |
setListCondenser(ListCondenser condenser)
Sets the ListCondenser that is used to condense a list into a
single value. |
|
void |
setNullSourceValue(java.lang.Object value)
Sets the value to use if the value of the property of the source is null. |
|
void |
setNullTargetValue(java.lang.Object value)
Sets the value to use if the value of the property of the target is null. |
|
void |
setSource(java.lang.Object source)
Sets the source of the binding. |
|
void |
setSourcePath(java.lang.String path)
Sets the path to the property of the source to bind to. |
|
void |
setSourceValueFromTargetValue()
Sets the property of the source from that of the target. |
|
void |
setTarget(java.lang.Object target)
Sets the target of the binding. |
|
void |
setTargetPath(java.lang.String path)
Sets the path to the property of the target to bind to. |
|
void |
setTargetValueFromSourceValue()
Sets the property of the target from that of the source. |
|
void |
setUpdateStrategy(Binding.UpdateStrategy strategy)
Sets the update strategy for the binding. |
|
void |
setValidator(BindingValidator validator)
Sets the BindingValidator used to validate changes originating
from the target. |
|
|
setValue(Binding.Parameter<T> key,
T value)
Sets a parameter for the binding. |
|
void |
setValueForIncompleteSourcePath(java.lang.Object value)
Sets the value to use if the source path can not be completely evaluated. |
|
void |
setValueForIncompleteTargetPath(java.lang.Object value)
Sets the value to use if the target path can not be completely evaluated. |
|
java.lang.String |
toString()
Returns a string representing the state of this binding. |
|
void |
unbind()
Unrealizes this binding. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
|---|
public Binding()
Binding.
public Binding(java.lang.String sourcePath,
java.lang.String targetPath,
java.lang.Object... args)
Binding. See
SwingBindingSupport for examples.
sourcePath - path to the property of the sourcetargetPath - path to the property of the targetargs - alternating set of key/value pairs where each even numbered
entry is of type Paramater, and the following
value is of a type specified by the Parameter
java.lang.NullPointerException - if one of the even entries in args
is null
java.lang.ClassCastException - if one of the even entries in args
is not a Parameter, or one of the odd entries is not
of a type identified by the preceeding Parameter entry
java.lang.IllegalArgumentException - if args is odd
public Binding(java.lang.Object source,
java.lang.String sourcePath,
java.lang.Object target,
java.lang.String targetPath,
java.lang.Object... args)
Binding. See
SwingBindingSupport for examples.
source - the source of the bindingsourcePath - path to the property of the sourcetarget - the target of the bindingtargetPath - path to the paroperty of the targetargs - alternating set of key/value pairs; each even numbered
entry is of type Parameter, and the following
value is of a type specified by the Parameter
java.lang.NullPointerException - if one of the even entries in args
is null
java.lang.ClassCastException - if one of the even entries in args
is not a Parameter, or one of the odd entries is not of a type
identified by the preceeding Parameter entry
java.lang.IllegalArgumentException - if args is odd| Method Detail |
|---|
public final void setSource(java.lang.Object source)
source - the source of the binding
java.lang.IllegalStateException - if boundpublic final java.lang.Object getSource()
public final void setSourcePath(java.lang.String path)
path - path to the property of the source to bind to
java.lang.IllegalStateException - if boundpublic final java.lang.String getSourcePath()
public final void setTarget(java.lang.Object target)
target - the target of the binding
java.lang.IllegalStateException - if boundpublic final java.lang.Object getTarget()
public final void setTargetPath(java.lang.String path)
path - path to the property of the target to bind to
java.lang.IllegalStateException - if bound
java.lang.IllegalArgumentException - if path is non-null and emptypublic final java.lang.String getTargetPath()
PropertyPath.
public final void setValidator(BindingValidator validator)
BindingValidator used to validate changes originating
from the target.
validator - the BindingValidator
java.lang.IllegalStateException - if boundpublic final BindingValidator getValidator()
BindingValidator used to validate changes
originating from the target.
public final void setConverter(BindingConverter converter)
BindingConverter used to convert values.
BindingConverter is only used to convert non-null
values.
converter - the BindingConverter
java.lang.IllegalStateException - if boundpublic final BindingConverter getConverter()
BindingConverter used to convert values.
BindingConverterpublic final void setValueForIncompleteSourcePath(java.lang.Object value)
value - the valuepublic final java.lang.Object getValueForIncompleteSourcePath()
public final void setValueForIncompleteTargetPath(java.lang.Object value)
value - the valuepublic final java.lang.Object getValueForIncompleteTargetPath()
public final void setNullSourceValue(java.lang.Object value)
null.
value - the value to use if the value of the property of the
source is null
java.lang.IllegalStateException - if boundpublic final java.lang.Object getNullSourceValue()
null.
nullpublic final void setNullTargetValue(java.lang.Object value)
null.
value - the value to use if the value of the property of the
target is null
java.lang.IllegalStateException - if boundpublic final java.lang.Object getNullTargetValue()
null.
nullpublic final void setUpdateStrategy(Binding.UpdateStrategy strategy)
READ_WRITE.
strategy - the update strategy
java.lang.IllegalArgumentException - if strategy is null
java.lang.IllegalStateException - if boundpublic final Binding.UpdateStrategy getUpdateStrategy()
public final void setListCondenser(ListCondenser condenser)
ListCondenser that is used to condense a list into a
single value.
condenser - the condenserpublic final ListCondenser getListCondenser()
ListCondenser that is used to condense a list into
a single value.
public final <T> void setValue(Binding.Parameter<T> key,
T value)
value is null, the entry is removed. This method is
used to specify target specific properties. For example, the following
specifies the "text" property of a JTextComponent should change
as you type:
binding.setValue(SwingBindingSupport.TextChangeStrategyParameter,
TextChangeStrategy.CHANGE_ON_TYPE);
key - the keyvalue - the value
java.lang.ClassCastException - if value is not of the type defined
by key
java.lang.NullPointerException - if key is null
java.lang.IllegalStateException - if bound
public final <T> T getValue(Binding.Parameter<T> key,
T defaultValue)
key - the key to obtain the value fordefaultValue - the value to return if setValue has not
been invoked with the specified key
java.lang.NullPointerException - if key is null
public BindingConverter getSourceToTargetConverter(java.lang.Class<?> sourceType,
java.lang.Object sourceValue,
java.lang.Class<?> targetType)
BindingConverter used to convert the source value to the
target value.
sourceType - the type of the source propertysourceValue - the value of the sourcetargetType - the type of the target property
public BindingConverter getTargetToSourceConverter(java.lang.Class<?> targetType,
java.lang.Object targetValue,
java.lang.Class<?> sourceType)
targetType - the type of the target propertytargetValue - the value of the targetsourceType - the type of the source propertypublic final BindingContext getContext()
BindingContext this Binding is
contained in.
BindingContext.public final boolean isBound()
true if currently bound.
true if currently boundpublic final void bind()
Binding.
java.lang.IllegalStateException - if already bound, or
the source or target is null
PropertyResolverException - if PropertyResolver throws an
exception; refer to PropertyResolver for the conditions
under which an exception is thrown
java.lang.IllegalArgumentException - if the child bindings do not contain
the expected information for the targetpublic final void unbind()
java.lang.IllegalStateException - if already bound
PropertyResolverException - if PropertyResolver throws an
exception; refer to PropertyResolver for the conditions
under which an exception is thrownpublic final void setSourceValueFromTargetValue()
PropertyResolverException - if PropertyResolver throws an
exception; refer to PropertyResolver for the conditions
under which an exception is thrown
java.lang.IllegalStateException - if not boundpublic final void setTargetValueFromSourceValue()
PropertyResolverException
java.lang.IllegalStateException - if not boundpublic void addBindingListener(BindingListener listener)
BindingListener to this Binding.
listener - the BindingListener to addpublic void removeBindingListener(BindingListener listener)
BindingListener from this Binding.
listener - the BindingListener to removepublic final Binding.ValueState getTargetValueState()
public final Binding.ValueState getSourceValueState()
public final Binding addBinding(java.lang.String sourcePath,
java.lang.String targetPath,
java.lang.Object... args)
Binding as a child of this Binding.
sourcePath - path to the property of the sourcetargetPath - path to the paroperty of the targetargs - alternating set of key/value pairs where each even numbered
entry is of type Parameter, and the following
value is of a type specified by the Parameter
java.lang.NullPointerException - if one of the even entries is null
java.lang.ClassCastException - if one of the even entries is not a
Parameter, or one of the odd entries is not of a type
identified by the preceeding Parameter entrypublic final void addBinding(Binding binding)
Binding as a child of this Binding.
binding - the Binding to add as a child
java.lang.IllegalArgumentException - if binding has already been
added to another Binding
java.lang.IllegalStateException - if bound
java.lang.NullPointerException - if binding is nullpublic final void removeBinding(Binding binding)
Binding.
binding - the Binding to remove
java.lang.NullPointerException - if binding is null
java.lang.IllegalArgumentException - if binding has not been added
to this Binding
java.lang.IllegalStateException - if boundpublic final java.util.List<Binding> getBindings()
Bindings of this Binding.
The returned list is unmodifiable.
Bindingspublic java.lang.String toString()
null.
toString in class java.lang.Object
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||