Class BeanProperty<S,V>
- java.lang.Object
-
- org.jdesktop.beansbinding.Property<S,V>
-
- org.jdesktop.beansbinding.PropertyHelper<S,V>
-
- org.jdesktop.beansbinding.BeanProperty<S,V>
-
- Type Parameters:
S- the type of source object that thisBeanPropertyoperates onV- the type of value that thisBeanPropertyrepresents
public final class BeanProperty<S,V> extends PropertyHelper<S,V>
An implementation ofPropertythat uses a simple dot-separated path syntax to address Java Beans properties of source objects. For example, to create a property representing thefirstNameproperty of an obect:BeanProperty.create("firstName");Or to create a property representing the
firstNameproperty of an object'smotherproperty:BeanProperty.create("mother.firstName");An instance of
BeanPropertyis immutable and can be used with different source objects. When aPropertyStateListeneris added to aBeanPropertyfor a given source object, theBeanPropertystarts listening to all objects along the path (based on that source object) for change notification, and reflects any changes by notifying the listener associated with the property for that source object. So, in the second example above, if aPropertyStateListeneris added to the property for an objectDuke, thePropertyStateListeneris notified when eitherDuke'smother changes (if the new mother's name is different), orDuke's mother's firstNamechanges.It is very important that any bean properties addressed via a
BeanPropertyfollow the Java Beans specification, including firing property change notification; otherwise,BeanPropertycannot respond to change. As some beans outside of your control may not follow the Java Beans specification,BeanPropertyalways checks theBeanAdapterFactoryto see if a delegate provider has been registered to provide a delegate bean to take the place of an object for a given property. See the ext package level documentation for more details.When there are no
PropertyStateListenersinstalled on aBeanPropertyfor a given source, allPropertymethods act by traversing the entire path from the source to the end point, thereby always providing "live" information. On the contrary, when there arePropertyStateListenersinstalled, the beans along the path (including the final value) are cached, and only updated upon notification of change from a bean. Again, this makes it very important that any bean property that could change along the path fires property change notification.Readability of a
BeanPropertyfor a given source is defined as follows: ABeanPropertyis readable for a given source if and only if a) each bean in the path, starting with the source, defines a Java Beans getter method for the the property to be read on it AND b) each bean in the path, starting with the source and ending with the bean on which we read the final property, isnon-null. The final value beingnulldoes not affect the readability.So, in the second example given earlier, the
BeanPropertyis readable for (@code Duke} when all of the following are true:Dukedefines a Java Beans getter formother,Duke's motherdefines a Java Beans getter forfirstName,Dukeisnon-null,Duke's motherisnon-null. TheBeanPropertyis therefore unreadable when any of the following is true:Dukedoes not define a Java Beans getter formother,Duke's motherdoes not define a Java Beans getter forfirstName,Dukeisnull,Duke's motherisnull.Writeability of a
BeanPropertyfor a given source is defined as follows: ABeanPropertyis writeable for a given source if and only if a) each bean in the path, starting with the source and ending with the bean on which we set the final property, defines a Java Beans getter method for the property to be read on it AND b) the bean on which we set the final property defines a Java Beans setter for the property to be set on it AND c) each bean in the path, starting with the source and ending with the bean on which we set the final property, isnon-null. The final value beingnulldoes not affect the writeability.So, in the second example given earlier, the
BeanPropertyis writeable forDukewhen all of the following are true:Dukedefines a Java Beans getter formother,Duke's motherdefines a Java Beans setter forfirstName,Dukeisnon-null,Duke's motherisnon-null. TheBeanPropertyis therefore unreadable when any of the following is true:Dukedoes not define a Java Beans getter formother,Duke's motherdoes not define a Java Beans setter forfirstName,Dukeisnull,Duke's motherisnull.In addition to working on Java Beans properties, any object in the path can be an instance of
Map. In this case, theMap's getmethod is used with the property name as the getter, and theMap's putmethod is used with the property name as the setter.BeanPropertycan only respond to changes inMapsif they are instances ofObservableMap.Some methods in this class document that they can throw
PropertyResolutionExceptionif an exception occurs while trying to resolve the path. The throwing of this exception represents an abnormal condition and if listeners are installed for the given source object, leaves theBeanPropertyin an inconsistent state for that source object. ABeanPropertyshould not be used again for that same source object after such an exception without first removing all listeners associated with theBeanPropertyfor that source object.- Author:
- Shannon Hickey, Scott Violet
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <S,V>
BeanProperty<S,V>create(String path)Creates an instance ofBeanPropertyfor the given path.static <S,V>
BeanProperty<S,V>create(Property<S,?> baseProperty, String path)Creates an instance ofBeanPropertyfor the given base property and path.VgetValue(S source)Returns the value of thisPropertyfor the given source.Class<? extends V>getWriteType(S source)Returns the type of object that is suitable for setting as the value of thisPropertyby calls tosetValue.booleanisReadable(S source)Returns whether or not thePropertyis readable for the given source.booleanisWriteable(S source)Returns whether or not thePropertyis writeable for the given source.protected voidlisteningStarted(S source)Called when thisPropertyHelperchanges from having no listeners installed for the given source object to having listeners installed for the given source object.protected voidlisteningStopped(S source)Called when thisPropertyHelperchanges from having listeners installed for the given source object to having no listeners installed for the given source object.voidsetValue(S source, V value)Sets the value of thisPropertyfor the given source.StringtoString()Returns a string representation of theBeanProperty.-
Methods inherited from class org.jdesktop.beansbinding.PropertyHelper
addPropertyStateListener, firePropertyStateChange, getPropertyStateListeners, isListening, removePropertyStateListener
-
-
-
-
Method Detail
-
create
public static final <S,V> BeanProperty<S,V> create(String path)
Creates an instance ofBeanPropertyfor the given path.- Parameters:
path- the path- Returns:
- an instance of
BeanPropertyfor the given path - Throws:
IllegalArgumentException- if the path is null, or contains no property names
-
create
public static final <S,V> BeanProperty<S,V> create(Property<S,?> baseProperty, String path)
Creates an instance ofBeanPropertyfor the given base property and path. The path is relative to the value of the base property.- Parameters:
baseProperty- the base propertypath- the path- Returns:
- an instance of
BeanPropertyfor the given base property and path - Throws:
IllegalArgumentException- if the path is null, or contains no property names
-
getWriteType
public Class<? extends V> getWriteType(S source)
Returns the type of object that is suitable for setting as the value of thisPropertyby calls tosetValue.See the class level documentation for the definition of writeability.
- Specified by:
getWriteTypein classPropertyHelper<S,V>- Parameters:
source- the source object on which to operate- Returns:
- the type of object suitable for setting as the value
- Throws:
UnsupportedOperationException- if thePropertyis not writeable for the given sourcePropertyResolutionException- if an exception occurs while resolving the path- See Also:
setValue(S, V),isWriteable(S)
-
getValue
public V getValue(S source)
Returns the value of thisPropertyfor the given source.See the class level documentation for the definition of readability.
- Specified by:
getValuein classPropertyHelper<S,V>- Parameters:
source- the source object on which to operate- Returns:
- the value of this
Propertyfor the given source - Throws:
UnsupportedOperationException- if thePropertyis not readable for the given sourcePropertyResolutionException- if an exception occurs while resolving the path- See Also:
isReadable(S)
-
setValue
public void setValue(S source, V value)
Sets the value of thisPropertyfor the given source.See the class level documentation for the definition of writeability.
- Specified by:
setValuein classPropertyHelper<S,V>- Parameters:
source- the source object on which to operatevalue- the new value for theProperty- Throws:
UnsupportedOperationException- if thePropertyis not writeable for the given sourcePropertyResolutionException- if an exception occurs while resolving the path- See Also:
isWriteable(S),getWriteType(S)
-
isReadable
public boolean isReadable(S source)
Returns whether or not thePropertyis readable for the given source.See the class level documentation for the definition of readability.
- Specified by:
isReadablein classPropertyHelper<S,V>- Parameters:
source- the source object on which to operate- Returns:
- whether or not the
Propertyis readable for the given source. - Throws:
UnsupportedOperationExceptionPropertyResolutionException- if an exception occurs while resolving the path- See Also:
isWriteable(S)
-
isWriteable
public boolean isWriteable(S source)
Returns whether or not thePropertyis writeable for the given source.See the class level documentation for the definition of writeability.
- Specified by:
isWriteablein classPropertyHelper<S,V>- Parameters:
source- the source object on which to operate- Returns:
- whether or not the
Propertyis writeable for the given source. - Throws:
UnsupportedOperationExceptionPropertyResolutionException- if an exception occurs while resolving the path- See Also:
isReadable(S)
-
listeningStarted
protected final void listeningStarted(S source)
Description copied from class:PropertyHelperCalled when thisPropertyHelperchanges from having no listeners installed for the given source object to having listeners installed for the given source object. This is the ideal time for subclasses to install any listeners needed to track change on the source object.- Overrides:
listeningStartedin classPropertyHelper<S,V>- See Also:
PropertyHelper.listeningStopped(S)
-
listeningStopped
protected final void listeningStopped(S source)
Description copied from class:PropertyHelperCalled when thisPropertyHelperchanges from having listeners installed for the given source object to having no listeners installed for the given source object. This is the ideal time for subclasses to remove any listeners that they've installed to track changes on the source object.- Overrides:
listeningStoppedin classPropertyHelper<S,V>- See Also:
PropertyHelper.listeningStopped(S)
-
toString
public String toString()
Returns a string representation of theBeanProperty. This method is intended to be used for debugging purposes only, and the content and format of the returned string may vary between implementations. The returned string may be empty but may not benull.
-
-