com.itmill.toolkit.data.util
Class MethodProperty

java.lang.Object
  extended by com.itmill.toolkit.data.util.MethodProperty
All Implemented Interfaces:
Property, Property.ReadOnlyStatusChangeNotifier, Property.ValueChangeNotifier

public class MethodProperty
extends Object
implements Property, Property.ValueChangeNotifier, Property.ReadOnlyStatusChangeNotifier

Proxy class for creating Properties from pairs of getter and setter methods of a Bean property. An instance of this class can be thought as having been attached to a field of an object. Accessing the object through the Property interface directly manipulates the underlying field.

It's assumed that the return value returned by the getter method is assignable to the type of the property, and the setter method parameter is assignable to that value.

A valid getter method must always be available, but instance of this class can be constructed with a null setter method in which case the resulting MethodProperty is read-only.

MethodProperty implements Property.ValueChangeNotifier, but does not automatically know whether or not the getter method will actually return a new value - value change listeners are always notified when setValue is called, without verifying what the getter returns.

Since:
3.0
Version:
5.3.0
Author:
IT Mill Ltd.

Nested Class Summary
 class MethodProperty.MethodException
          Exception object that signals that there were problems calling or finding the specified getter or setter methods of the property.
 
Nested classes/interfaces inherited from interface com.itmill.toolkit.data.Property
Property.ConversionException, Property.Editor, Property.ReadOnlyException, Property.ReadOnlyStatusChangeListener, Property.ReadOnlyStatusChangeNotifier, Property.ValueChangeListener, Property.ValueChangeNotifier, Property.Viewer
 
Constructor Summary
MethodProperty(Class type, Object instance, Method getMethod, Method setMethod)
           Creates a new instance of MethodProperty with the getter and setter methods.
MethodProperty(Class type, Object instance, Method getMethod, Method setMethod, Object[] getArgs, Object[] setArgs, int setArgumentIndex)
           Creates a new instance of MethodProperty from the getter and setter methods, and argument lists.
MethodProperty(Class type, Object instance, String getMethodName, String setMethodName)
           Creates a new instance of MethodProperty from named getter and setter methods.
MethodProperty(Class type, Object instance, String getMethodName, String setMethodName, Object[] getArgs, Object[] setArgs, int setArgumentIndex)
           Creates a new instance of MethodProperty from named getter and setter methods and argument lists.
MethodProperty(Object instance, String beanPropertyName)
           Creates a new instance of MethodProperty from a named bean property.
 
Method Summary
 void addListener(Property.ReadOnlyStatusChangeListener listener)
          Registers a new read-only status change listener for this Property.
 void addListener(Property.ValueChangeListener listener)
          Registers a new value change listener for this Property.
 void fireValueChange()
          Sends a value change event to all registered listeners.
 Class getType()
          Returns the type of the Property.
 Object getValue()
          Gets the value stored in the Property.
 boolean isReadOnly()
          Tests if the object is in read-only mode.
 void removeListener(Property.ReadOnlyStatusChangeListener listener)
          Removes a previously registered read-only status change listener.
 void removeListener(Property.ValueChangeListener listener)
          Removes a previously registered value change listener.
 void setArguments(Object[] getArgs, Object[] setArgs, int setArgumentIndex)
           Sets the setter method and getter method argument lists.
 void setReadOnly(boolean newStatus)
          Sets the Property's read-only mode to the specified status.
 void setValue(Object newValue)
          Sets the value of the property.
 String toString()
          Returns the value of the MethodProperty in human readable textual format.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MethodProperty

public MethodProperty(Object instance,
                      String beanPropertyName)

Creates a new instance of MethodProperty from a named bean property. This constructor takes an object and the name of a bean property and initializes itself with the accessor methods for the property.

The getter method of a MethodProperty instantiated with this constructor will be called with no arguments, and the setter method with only the new value as the sole argument.

If the setter method is unavailable, the resulting MethodProperty will be read-only, otherwise it will be read-write.

Method names are constucted from the bean property by adding get/is/are/set prefix and capitalising the first character in the name of the given bean property.

Parameters:
instance - the object that includes the property.
beanPropertyName - the name of the property to bind to.

MethodProperty

public MethodProperty(Class type,
                      Object instance,
                      String getMethodName,
                      String setMethodName)

Creates a new instance of MethodProperty from named getter and setter methods. The getter method of a MethodProperty instantiated with this constructor will be called with no arguments, and the setter method with only the new value as the sole argument.

If the setter method is null, the resulting MethodProperty will be read-only, otherwise it will be read-write.

Parameters:
type - the type of the property.
instance - the object that includes the property.
getMethodName - the name of the getter method.
setMethodName - the name of the setter method.

MethodProperty

public MethodProperty(Class type,
                      Object instance,
                      Method getMethod,
                      Method setMethod)

Creates a new instance of MethodProperty with the getter and setter methods. The getter method of a MethodProperty instantiated with this constructor will be called with no arguments, and the setter method with only the new value as the sole argument.

If the setter method is null, the resulting MethodProperty will be read-only, otherwise it will be read-write.

Parameters:
type - the type of the property.
instance - the object that includes the property.
getMethod - the getter method.
setMethod - the setter method.

MethodProperty

public MethodProperty(Class type,
                      Object instance,
                      String getMethodName,
                      String setMethodName,
                      Object[] getArgs,
                      Object[] setArgs,
                      int setArgumentIndex)

Creates a new instance of MethodProperty from named getter and setter methods and argument lists. The getter method of a MethodProperty instantiated with this constructor will be called with the getArgs as arguments. The setArgs will be used as the arguments for the setter method, though the argument indexed by the setArgumentIndex will be replaced with the argument passed to the setValue(Object newValue) method.

For example, if the setArgs contains A, B and C, and setArgumentIndex = 1, the call methodProperty.setValue(X) would result in the setter method to be called with the parameter set of {A, X, C}

Parameters:
type - the type of the property.
instance - the object that includes the property.
getMethodName - the name of the getter method.
setMethodName - the name of the setter method.
getArgs - the fixed argument list to be passed to the getter method.
setArgs - the fixed argument list to be passed to the setter method.
setArgumentIndex - the index of the argument in setArgs to be replaced with newValue when setValue(Object newValue) is called.

MethodProperty

public MethodProperty(Class type,
                      Object instance,
                      Method getMethod,
                      Method setMethod,
                      Object[] getArgs,
                      Object[] setArgs,
                      int setArgumentIndex)

Creates a new instance of MethodProperty from the getter and setter methods, and argument lists.

This constructor behaves exactly like MethodProperty(Class type, Object instance, String getMethodName, String setMethodName, Object [] getArgs, Object [] setArgs, int setArgumentIndex) except that instead of names of the getter and setter methods this constructor is given the actual methods themselves.

Parameters:
type - the type of the property.
instance - the object that includes the property.
getMethod - the getter method.
setMethod - the setter method.
getArgs - the fixed argument list to be passed to the getter method.
setArgs - the fixed argument list to be passed to the setter method.
setArgumentIndex - the index of the argument in setArgs to be replaced with newValue when setValue(Object newValue) is called.
Method Detail

getType

public final Class getType()
Returns the type of the Property. The methods getValue and setValue must be compatible with this type: one must be able to safely cast the value returned from getValue to the given type and pass any variable assignable to this type as an argument to setValue.

Specified by:
getType in interface Property
Returns:
type of the Property

isReadOnly

public boolean isReadOnly()
Tests if the object is in read-only mode. In read-only mode calls to setValue will throw ReadOnlyException and will not modify the value of the Property.

Specified by:
isReadOnly in interface Property
Returns:
true if the object is in read-only mode, false if it's not

getValue

public Object getValue()
Gets the value stored in the Property. The value is resolved by calling the specified getter method with the argument specified at instantiation.

Specified by:
getValue in interface Property
Returns:
the value of the Property

toString

public String toString()
Returns the value of the MethodProperty in human readable textual format. The return value should be assignable to the setValue method if the Property is not in read-only mode.

Specified by:
toString in interface Property
Overrides:
toString in class Object
Returns:
String representation of the value stored in the Property

setArguments

public void setArguments(Object[] getArgs,
                         Object[] setArgs,
                         int setArgumentIndex)

Sets the setter method and getter method argument lists.

Parameters:
getArgs - the fixed argument list to be passed to the getter method.
setArgs - the fixed argument list to be passed to the setter method.
setArgumentIndex - the index of the argument in setArgs to be replaced with newValue when setValue(Object newValue) is called.

setValue

public void setValue(Object newValue)
              throws Property.ReadOnlyException,
                     Property.ConversionException
Sets the value of the property. This method supports setting from Strings if either String is directly assignable to property type, or the type class contains a string constructor.

Specified by:
setValue in interface Property
Parameters:
newValue - the New value of the property.
Throws:
Property.ReadOnlyException - if the object is in read-only mode.
Property.ConversionException - if newValue can't be converted into the Property's native type directly or through String.
Property.ReadOnlyException - if the object is in read-only mode
Property.ConversionException - if newValue can't be converted into the Property's native type directly or through String
See Also:
invokeSetMethod(Object)

setReadOnly

public void setReadOnly(boolean newStatus)
Sets the Property's read-only mode to the specified status.

Specified by:
setReadOnly in interface Property
Parameters:
newStatus - the new read-only status of the Property.

addListener

public void addListener(Property.ReadOnlyStatusChangeListener listener)
Registers a new read-only status change listener for this Property.

Specified by:
addListener in interface Property.ReadOnlyStatusChangeNotifier
Parameters:
listener - the new Listener to be registered.

removeListener

public void removeListener(Property.ReadOnlyStatusChangeListener listener)
Removes a previously registered read-only status change listener.

Specified by:
removeListener in interface Property.ReadOnlyStatusChangeNotifier
Parameters:
listener - the listener to be removed.

addListener

public void addListener(Property.ValueChangeListener listener)
Description copied from interface: Property.ValueChangeNotifier
Registers a new value change listener for this Property.

Specified by:
addListener in interface Property.ValueChangeNotifier
Parameters:
listener - the new Listener to be registered

removeListener

public void removeListener(Property.ValueChangeListener listener)
Description copied from interface: Property.ValueChangeNotifier
Removes a previously registered value change listener.

Specified by:
removeListener in interface Property.ValueChangeNotifier
Parameters:
listener - listener to be removed

fireValueChange

public void fireValueChange()
Sends a value change event to all registered listeners.



Copyright © 2000-2009 IT Mill Ltd. All Rights Reserved.