KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > property > FieldBoundPropertyListener


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util.property;
23
24 import org.jboss.util.FieldInstance;
25 import org.jboss.util.NullArgumentException;
26 import org.jboss.util.Classes;
27 import org.jboss.util.ThrowableHandler;
28 import org.jboss.util.propertyeditor.PropertyEditors;
29
30 import java.beans.PropertyEditor JavaDoc;
31
32 /**
33  * Binds property values to class fields.
34  *
35  * @version <tt>$Revision: 1958 $</tt>
36  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
37  */

38 public class FieldBoundPropertyListener
39    extends BoundPropertyAdapter
40 {
41    /** Property name which we are bound to */
42    protected final String JavaDoc propertyName;
43
44    /** Field instance */
45    protected final FieldInstance fieldInstance;
46
47    /**
48     * Constructs a <tt>FieldBoundPropertyListener</tt>.
49     *
50     * @param instance Instance object.
51     * @param fieldName Field name.
52     * @param propertyName Property to bind to.
53     *
54     * @throws NullArgumentException Property name is <tt>null</tt>.
55     */

56    public FieldBoundPropertyListener(final Object JavaDoc instance,
57                                      final String JavaDoc fieldName,
58                                      final String JavaDoc propertyName)
59    {
60       if (propertyName == null)
61          throw new NullArgumentException("propertyName");
62       // FieldInstance checks instance & fieldName
63

64       this.propertyName = propertyName;
65
66       try {
67          // construct field instance
68
fieldInstance = new FieldInstance(instance, fieldName);
69          try {
70             fieldInstance.getField().setAccessible(true);
71          }
72          catch (SecurityException JavaDoc e) {
73             ThrowableHandler.add(e);
74          }
75
76          // force the given class to load, so that any CoersionHelpers
77
// that are nested in it are loaded properly
78
Classes.forceLoad(fieldInstance.getField().getType());
79       }
80       catch (NoSuchFieldException JavaDoc e) {
81          throw new PropertyException(e);
82       }
83    }
84
85    /**
86     * Constructs a <tt>FieldBoundPropertyListener</tt>.
87     *
88     * <p>Field name is used for property name.
89     *
90     * @param instance Instance object.
91     * @param fieldName Field name.
92     */

93    public FieldBoundPropertyListener(final Object JavaDoc instance,
94                                      final String JavaDoc fieldName)
95    {
96       this(instance, fieldName, fieldName);
97    }
98
99    /**
100     * Get the property name which this listener is bound to.
101     *
102     * @return Property name.
103     */

104    public final String JavaDoc getPropertyName() {
105       return propertyName;
106    }
107
108    /**
109     * Filter the property value prior to coercing and binding to field.
110     *
111     * <p>Allows instance to filter values prior to object coercion and
112     * field binding.
113     *
114     * @param value Property value.
115     */

116    public String JavaDoc filterValue(String JavaDoc value) {
117       return value;
118    }
119
120    /**
121     * Coerce and set specified value to field.
122     *
123     * @param value Field value.
124     *
125     * @throws PropertyException Failed to set field value.
126     */

127    protected void setFieldValue(String JavaDoc value) {
128       try {
129          // filter property value
130
value = filterValue(value);
131
132          // coerce value to field type
133
Class JavaDoc type = fieldInstance.getField().getType();
134          PropertyEditor JavaDoc editor = PropertyEditors.findEditor(type);
135          editor.setAsText(value);
136          Object JavaDoc coerced = editor.getValue();
137
138          // bind value to field
139
fieldInstance.set(coerced);
140       }
141       catch (IllegalAccessException JavaDoc e) {
142          throw new PropertyException(e);
143       }
144    }
145
146    /**
147     * Notifies that a property has been added.
148     *
149     * @param event Property event.
150     */

151    public void propertyAdded(final PropertyEvent event) {
152       setFieldValue(event.getPropertyValue());
153    }
154
155    /**
156     * Notifies that a property has changed
157     *
158     * @param event Property event
159     */

160    public void propertyChanged(final PropertyEvent event) {
161       setFieldValue(event.getPropertyValue());
162    }
163
164    /**
165     * Notifies that this listener was bound to a property.
166     *
167     * @param map PropertyMap which contains property bound to.
168     */

169    public void propertyBound(final PropertyMap map) {
170       // only set the field if the map contains the property already
171
if (map.containsProperty(propertyName)) {
172          setFieldValue(map.getProperty(propertyName));
173       }
174    }
175 }
176
Popular Tags