net.sf.sdedit.ui.components.configuration
Class Bean<T extends DataObject>

java.lang.Object
  extended by net.sf.sdedit.ui.components.configuration.Bean<T>
Type Parameters:
T - the interface type of the data object
All Implemented Interfaces:
InvocationHandler

public class Bean<T extends DataObject>
extends Object
implements InvocationHandler

A Bean provides a single instance of a "data object" that implements the T interface which should only define get-, set-, and is-methods like a Java bean. The instance is returned by getDataObject(). For all manipulations of the data object's state (invocations of set-methods) the Bean immediately sends notifications to all interested PropertyChangeListeners. The state of the data object can be loaded and stored, using XML documents (see load(Document, String), store(Document, String, String)). The values of the data object can also be accessed by passing their corresponding properties as arguments (see setValue(PropertyDescriptor, Object), getValue(String)).

The values returned by the data object managed by a Bean are always, provided the bean has been set up/loaded properly, not null (this can be enforced by the permitNullValues property set to false), and legal, i. e. string properties for which there is a set of alternative values are always assigned to a legal value.

Author:
Markus Strauch

Constructor Summary
Bean(Class<T> dataClass, StringSelectionProvider ssp)
          Creates a new bean that provides a single data object.
 
Method Summary
 void addPropertyChangeListener(PropertyChangeListener listener)
          Adds a listener that is notified when a property is modified via setValue(PropertyDescriptor, Object).
 Bean<T> copy()
          Returns a shallow copy of this bean.
 boolean equals(Object o)
          Returns true if and only if o is a reference to a bean with the same properties that have the same values as this bean's properties.
 T getDataObject()
          Returns the synthetic data object implementing the data interface belonging to this Bean.
 Collection<PropertyDescriptor> getProperties()
          Returns the properties of this Bean that are annotated with an Adjustable annotation.
 PropertyDescriptor getProperty(String name)
          Returns the PropertyDescriptor for the property with the given name.
 Set<String> getStringsForProperty(PropertyDescriptor property)
          Returns a set of strings representing all alternative values for the given String property.
 Object getValue(PropertyDescriptor pd)
           
 Object getValue(String property)
          Returns the current value of the given property, represented by its name.
 int hashCode()
           
 Object invoke(Object proxy, Method method, Object[] args)
           
 boolean isPermitNullValues()
          Returns a flag denoting if null values can be used as parameters of the data object's set methods.
 void load(Document document, String pathToElement)
          Changes this bean's properties' values such that they reflect the values found in the given document.
 void removePropertyChangeListener(PropertyChangeListener listener)
          Removes a property change listener.
 void setPermitNullValues(boolean permitNullValues)
          Sets a flag denoting if null values can be used as parameters of the data object's set methods.
 void setValue(PropertyDescriptor property, Object value)
          Sets a new value for a property and informs all PropertyChangeListeners about that.
 void store(Document document, String pathToParent, String elementName)
          Stores all properties' current values in a newly created subtree of the given document.
 void takeValuesFrom(Bean<T> bean)
          Changes this bean's properties' values such that they are equal to the given bean's properties' values.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Bean

public Bean(Class<T> dataClass,
            StringSelectionProvider ssp)
Creates a new bean that provides a single data object. It is not permitted to set null values for this data object, as long as setPermitNullValues(boolean) is not called.

Parameters:
dataClass - the interface type of the data object
ssp - a StringSelectionProvider that provides an array of strings for methods of the data object which are annotated Adjustable.stringSelectionProvided()
Method Detail

getDataObject

public T getDataObject()
Returns the synthetic data object implementing the data interface belonging to this Bean.

Returns:
the synthetic data object implementing the data interface belonging to this Bean

invoke

public final Object invoke(Object proxy,
                           Method method,
                           Object[] args)
                    throws Throwable
Specified by:
invoke in interface InvocationHandler
Throws:
Throwable
See Also:
InvocationHandler.invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])

getProperties

public Collection<PropertyDescriptor> getProperties()
Returns the properties of this Bean that are annotated with an Adjustable annotation.

Returns:
the properties of this Bean that are annotated with an Adjustable annotation

getProperty

public PropertyDescriptor getProperty(String name)
Returns the PropertyDescriptor for the property with the given name.

Parameters:
name - the name of a property
Returns:
the corresponding PropertyDescriptor or null if there is no property with the name

addPropertyChangeListener

public void addPropertyChangeListener(PropertyChangeListener listener)
Adds a listener that is notified when a property is modified via setValue(PropertyDescriptor, Object).

Parameters:
listener - a listener that is notified when a property is modified via setValue(PropertyDescriptor, Object)

removePropertyChangeListener

public void removePropertyChangeListener(PropertyChangeListener listener)
Removes a property change listener.

Parameters:
listener - the listener to be removed

takeValuesFrom

public void takeValuesFrom(Bean<T> bean)
Changes this bean's properties' values such that they are equal to the given bean's properties' values.

Parameters:
bean - another bean

copy

public Bean<T> copy()
Returns a shallow copy of this bean.

Returns:
a shallow copy of this bean

getValue

public final Object getValue(String property)
Returns the current value of the given property, represented by its name.

Parameters:
property - the name of a property
Returns:
the current value of the property

getValue

public final Object getValue(PropertyDescriptor pd)

load

public void load(Document document,
                 String pathToElement)
          throws DocUtil.XMLException
Changes this bean's properties' values such that they reflect the values found in the given document. It remains unchanged if the document does not contain a subtree corresponding to pathToElement.

Parameters:
document - a document
pathToElement - XPath to the subtree where the properties' values are described
Throws:
DocUtil.XMLException

store

public void store(Document document,
                  String pathToParent,
                  String elementName)
           throws DocUtil.XMLException
Stores all properties' current values in a newly created subtree of the given document.

Parameters:
document - the document
pathToParent - XPath to the parent of the root of the subtree
elementName - the name of the root of the subtree
Throws:
DocUtil.XMLException

setValue

public final void setValue(PropertyDescriptor property,
                           Object value)
Sets a new value for a property and informs all PropertyChangeListeners about that. If the admitNullValues property is false, a new value of null will be ignored and not set. Furthermore, it is not permitted to set a string value for a property that has a set of alternative values, if none of these matches. The illegal value will be silently ignored.

Parameters:
property - the descriptor of the property
value - the new value of the property

equals

public boolean equals(Object o)
Returns true if and only if o is a reference to a bean with the same properties that have the same values as this bean's properties.

Overrides:
equals in class Object
Returns:
true if and only if o is a reference to a bean with the same properties that have the same values as this bean's properties

hashCode

public int hashCode()
Overrides:
hashCode in class Object
See Also:
Object.hashCode()

getStringsForProperty

public Set<String> getStringsForProperty(PropertyDescriptor property)
Returns a set of strings representing all alternative values for the given String property. If there is no alternative, i. e. all values are possible, the set is empty.

Parameters:
property - a String property
Returns:
an array of strings representing all alternative values for the given String property

toString

public String toString()
Overrides:
toString in class Object

isPermitNullValues

public boolean isPermitNullValues()
Returns a flag denoting if null values can be used as parameters of the data object's set methods.

Returns:
a flag denoting if null values can be used as parameters of the data object's set methods

setPermitNullValues

public void setPermitNullValues(boolean permitNullValues)
Sets a flag denoting if null values can be used as parameters of the data object's set methods.

Parameters:
permitNullValues - a flag denoting if null values can be used as parameters of the data object's set methods


Copyright © 2011. All Rights Reserved.