Coverage details for org.chwf.filter.BeanWrapper

LineHitsSource
1 /*
2 Chrysalis Web Framework [http://chrysalis.sourceforge.net]
3 Copyright (c) 2002, 2003, 2004, Paul Strack
4  
5 All rights reserved.
6  
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9  
10 1. Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12  
13 2. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16  
17 3. Neither the name of the copyright holder nor the names of its contributors
18 may be used to endorse or promote products derived from this software without
19 specific prior written permission.
20  
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
25 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32  
33 package org.chwf.filter;
34  
35 import java.beans.PropertyChangeListener;
36 import java.beans.PropertyChangeSupport;
37 import java.util.Map;
38  
39 import org.chwf.converter.ConversionException;
40  
41 /**
42  * The <code>BeanWrapper</code> class is a stateful business object
43  * wrapper that manages string-to-object value conversions and property
44  * change events for a single business object. It simplifies
45  * <code>BeanFilter</code> methods by eliminating the object parameter,
46  * but it is not thread-safe. The <code>BeanWrapper</code> is
47  * appropriate for non-multi-threaded environments like Java GUIs:
48  *
49  * <pre>
50  * BeanWrapper wrapper = new BeanWrapper(account);
51  * wrapper.set("name", "John Smith");
52  * // account.setName("John Smith");
53  * wrapper.set("balance", "1234.56");
54  * // account.setBalance(1234.56);
55  * wrapper.set("startDate", "10/11/2001");
56  * // account.setStartDate(&lt;<i>10/11/2001</i>&gt;);
57  * wrapper.invoke("save");
58  * // account.save();</pre>
59  *
60  * The <code>BeanWrapper</code> class also allows registration of
61  * <code>PropertyChangeListener</code> objects to handle
62  * <code>PropertyChangeEvent</code>s. This means that the business
63  * objects themselves do not need to be encumbered with such GUI
64  * specific code. These property change events are only generated
65  * when the property is changed through the <code>BeanWrapper</code>,
66  * using its <code>set()</code> and <code>setAll()</code> methods.
67  *
68  * <pre>
69  * BeanWrapper wrapper = new BeanWrapper(account);
70  * PropertyChangeListener bcl = new BalanceChangeListener();
71  * wrapper.addPropertyChangeListener("balance", bcl);
72  * // listener is notified when balance is changed via the wrapper</pre>
73  *
74  * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a>
75  */
76 public class BeanWrapper {
77  
78   /** The bean filter. */
79   private final BeanFilter filter;
80  
81   /** The wrapped object. */
82   private final Object object;
83  
84   /** For property changed events. */
85   private final PropertyChangeSupport pcs;
86  
87   /**
88    * Wraps the given object in a <code>BeanWrapper</code>. <p>
89    *
90    * @param object The target object.
91    * @throws InitializationException If the filter cannot be initialized.
92    */
9310  public BeanWrapper(Object object) throws InitializationException {
9410    this.object = object;
9510    this.filter = BeanFilter.findFilter(object);
9610    this.pcs = new PropertyChangeSupport(object);
9710  }
98  
99   /**
100    * Retrieves the underlying object.<p>
101    *
102    * @return The original object.
103    */
104   public Object getObject() {
1051    return this.object;
106   }
107  
108   /**
109    * Get the specified property of the wrapped object, as a string.<p>
110    *
111    * @param property The property name.
112    * @return The property value.
113    * @throws FilterException For read failure.
114    */
115   public String get(String property) throws FilterException {
11617    return filter.get(object, property);
117   }
118  
119   /**
120    * Set the specified property of the wrapped object, using a string.<p>
121    *
122    * @param property The property name.
123    * @param value The property value.
124    * @throws ConversionException If the value cannot be converted to
125    * the correct type.
126    * @throws FilterException For write failure.
127    */
128   public void set(String property, String value)
129     throws ConversionException, FilterException {
13013    String oldValue = null;
131     try {
13213      oldValue = get(property);
13312    } catch (PropertyNotReadableException ex) {
134       // Cannot determine old value
1351      oldValue = null;
136     }
13713    filter.set(object, property, value);
13813    pcs.firePropertyChange(property, oldValue, value);
13913  }
140  
141   /**
142    * Set all properties of the wrapped object using a map of name/value pairs.
143    * Note that if an exception is thrown, it is possible that the object has
144    * been partially updated, with some property values updated and others
145    * unchanged. Only writeable properties are updated.<p>
146    *
147    * @param properties A map of property values keyed by property name.
148    * @throws ConversionException If a value cannot be converted to
149    * the correct type.
150    * @throws OperationException One of the underlying set operations
151    * throws an exception.
152    */
153   public void setAll(Map properties)
154     throws ConversionException, OperationException {
1551    PropertyFilter[] filters = filter.getPropertyFilters();
1569    for (int i = 0; i < filters.length; i++) {
1578      String property = filters[i].getName();
1588      Object value = properties.get(property);
1598      if ((value != null) && filters[i].isWriteable()) {
1605        set(property, value.toString());
161       }
162     }
1631  }
164  
165   /**
166    * Add a PropertyChangeListener to the listener list. The listener is
167    * registered for all properties.<p>
168    *
169    * @param listener The PropertyChangeListener to be added.
170    */
171   public void addPropertyChangeListener(PropertyChangeListener listener) {
1722    pcs.addPropertyChangeListener(listener);
1732  }
174  
175   /**
176    * Add a PropertyChangeListener for a specific property. The listener will
177    * be invoked only when that property is changed.<p>
178    *
179    * @param propertyName The name of the property to listen for.
180    * @param listener The PropertyChangeListener to be added.
181    */
182   public void addPropertyChangeListener(
183     String propertyName,
184     PropertyChangeListener listener) {
1852    pcs.addPropertyChangeListener(propertyName, listener);
1862  }
187  
188   /**
189    * Remove a PropertyChangeListener from the listener list. This removes a
190    * PropertyChangeListener that was registered for all properties.<p>
191    *
192    * @param listener The PropertyChangeListener to be removed.
193    */
194   public void removePropertyChangeListener(PropertyChangeListener listener) {
1951    pcs.removePropertyChangeListener(listener);
1961  }
197  
198   /**
199    * Remove a PropertyChangeListener for a specific property.<p>
200    *
201    * @param propertyName The name of the property that was listened for.
202    * @param listener The PropertyChangeListener to be removed.
203    */
204   public void removePropertyChangeListener(
205     String propertyName,
206     PropertyChangeListener listener) {
2071    pcs.removePropertyChangeListener(propertyName, listener);
2081  }
209 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.