org.chwf.filter
Class BeanWrapper

java.lang.Object
  |
  +--org.chwf.filter.BeanWrapper

public class BeanWrapper
extends java.lang.Object

The BeanWrapper class is a stateful business object wrapper that manages string-to-object value conversions and property change events for a single business object. It simplifies BeanFilter methods by eliminating the object parameter, but it is not thread-safe. The BeanWrapper is appropriate for non-multi-threaded environments like Java GUIs:

    BeanWrapper wrapper = new BeanWrapper(account);
    wrapper.set("name", "John Smith");
        // account.setName("John Smith");
    wrapper.set("balance", "1234.56");
        // account.setBalance(1234.56);
    wrapper.set("startDate", "10/11/2001");
        // account.setStartDate(<10/11/2001>);
    wrapper.invoke("save");
        // account.save();
The BeanWrapper class also allows registration of PropertyChangeListener objects to handle PropertyChangeEvents. This means that the business objects themselves do not need to be encumbered with such GUI specific code. These property change events are only generated when the property is changed through the BeanWrapper, using its set() and setAll() methods.
    BeanWrapper wrapper = new BeanWrapper(account);
    PropertyChangeListener bcl = new BalanceChangeListener();
    wrapper.addPropertyChangeListener("balance", bcl);
    // listener is notified when balance is changed via the wrapper

Author:
Paul Strack

Constructor Summary
BeanWrapper(java.lang.Object object)
          Wraps the given object in a BeanWrapper.
 
Method Summary
 void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
          Add a PropertyChangeListener to the listener list.
 void addPropertyChangeListener(java.lang.String propertyName, java.beans.PropertyChangeListener listener)
          Add a PropertyChangeListener for a specific property.
 java.lang.String get(java.lang.String property)
          Get the specified property of the wrapped object, as a string.
 java.lang.Object getObject()
          Retrieves the underlying object.
 void removePropertyChangeListener(java.beans.PropertyChangeListener listener)
          Remove a PropertyChangeListener from the listener list.
 void removePropertyChangeListener(java.lang.String propertyName, java.beans.PropertyChangeListener listener)
          Remove a PropertyChangeListener for a specific property.
 void set(java.lang.String property, java.lang.String value)
          Set the specified property of the wrapped object, using a string.
 void setAll(java.util.Map properties)
          Set all properties of the wrapped object using a map of name/value pairs.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BeanWrapper

public BeanWrapper(java.lang.Object object)
            throws InitializationException
Wraps the given object in a BeanWrapper.

Parameters:
object - The target object.
Throws:
InitializationException - If the filter cannot be initialized.
Method Detail

getObject

public java.lang.Object getObject()
Retrieves the underlying object.

Returns:
The original object.

get

public java.lang.String get(java.lang.String property)
                     throws FilterException
Get the specified property of the wrapped object, as a string.

Parameters:
property - The property name.
Returns:
The property value.
Throws:
FilterException - For read failure.

set

public void set(java.lang.String property,
                java.lang.String value)
         throws ConversionException,
                FilterException
Set the specified property of the wrapped object, using a string.

Parameters:
property - The property name.
value - The property value.
Throws:
ConversionException - If the value cannot be converted to the correct type.
FilterException - For write failure.

setAll

public void setAll(java.util.Map properties)
            throws ConversionException,
                   OperationException
Set all properties of the wrapped object using a map of name/value pairs. Note that if an exception is thrown, it is possible that the object has been partially updated, with some property values updated and others unchanged. Only writeable properties are updated.

Parameters:
properties - A map of property values keyed by property name.
Throws:
ConversionException - If a value cannot be converted to the correct type.
OperationException - One of the underlying set operations throws an exception.

addPropertyChangeListener

public void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
Add a PropertyChangeListener to the listener list. The listener is registered for all properties.

Parameters:
listener - The PropertyChangeListener to be added.

addPropertyChangeListener

public void addPropertyChangeListener(java.lang.String propertyName,
                                      java.beans.PropertyChangeListener listener)
Add a PropertyChangeListener for a specific property. The listener will be invoked only when that property is changed.

Parameters:
propertyName - The name of the property to listen for.
listener - The PropertyChangeListener to be added.

removePropertyChangeListener

public void removePropertyChangeListener(java.beans.PropertyChangeListener listener)
Remove a PropertyChangeListener from the listener list. This removes a PropertyChangeListener that was registered for all properties.

Parameters:
listener - The PropertyChangeListener to be removed.

removePropertyChangeListener

public void removePropertyChangeListener(java.lang.String propertyName,
                                         java.beans.PropertyChangeListener listener)
Remove a PropertyChangeListener for a specific property.

Parameters:
propertyName - The name of the property that was listened for.
listener - The PropertyChangeListener to be removed.


Copyright © 2002-2004, Paul Strack. All Rights Reserved.