KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > propertysheet > DefaultPropertyModel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.openide.explorer.propertysheet;
20
21
22 import java.beans.*;
23
24 import java.lang.reflect.*;
25 import java.util.logging.Level JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27
28
29 /** The default implementation of PropertyModel interface.
30 * It takes the bean instance and the property name which should
31 * be accessed through PropertyModel methods. We now implement
32 * the new ExPropertyModel interface (which extends PropertyModel).
33 *
34 * @deprecated Use org.openide.nodes.PropertySupport.Reflection instead
35 * @author Jaroslav Tulach, Petr Hamernik
36 */

37 public @Deprecated JavaDoc class DefaultPropertyModel extends Object JavaDoc implements ExPropertyModel, PropertyChangeListener {
38     /** The Java Bean */
39     Object JavaDoc bean;
40
41     /** Name of the property of the bean. */
42     String JavaDoc propertyName; //package private so error handling code can pick up
43

44     //the property name if the user enters an invalid value
45

46     /** support for the properties changes. */
47     private PropertyChangeSupport support;
48
49     /** Property descriptor for the bean. */
50     private PropertyDescriptor prop;
51
52     /** Read method if exists one. */
53     private Method readMethod;
54
55     /** Write method if exists one. */
56     private Method writeMethod;
57
58     /** Type of the property. */
59     private Class JavaDoc propertyTypeClass;
60
61     /** When we call setValue we want to disable refiring of property change*/
62     private boolean donotfire = false;
63
64     /** Creates new DefaultPropertyModel.
65     * @param bean the java bean to be introspected
66     * @param propertyName name of the property
67     *
68     * @exception IllegalArgumentException if there is any problem
69     * with the parameters (introspection of bean,...)
70     */

71     public DefaultPropertyModel(Object JavaDoc bean, String JavaDoc propertyName)
72     throws IllegalArgumentException JavaDoc {
73         this(bean, findInfo(bean, propertyName));
74     }
75
76     /** Creates new DefaultPropertyModel with provided specific
77      * <code>PropertyDescriptor</code>. This can be useful if one needs to
78      * set to provide specific attributes to the property editor.
79      * <PRE>
80      * PropertyDescriptor pd = new PropertyDescriptor ("myProperty", bean.getClass ());
81      * pd.setPropertyEditorClass (PropertyEditorManager.findEditor (Object.class));
82      *
83      * // special attributes to the property editor
84      * pb.setValue ("superClass", MyProperty.class);
85      *
86      *
87      * model = new DefaultPropertyModel (bean, pd);
88      * panel = new PropertyPanel (model);
89      * </PRE>
90      * This constructor replaces the default use of BeanInfo and that is why
91      * simplifies the use of <link>ExPropertyEditor</link>s.
92      *
93      * @param bean the java bean to be introspected
94      * @param descr the property descriptor of the property to use
95      *
96      * @since 2.4
97      */

98     public DefaultPropertyModel(Object JavaDoc bean, PropertyDescriptor descr) {
99         this.bean = bean;
100         this.propertyName = descr.getName();
101         support = new PropertyChangeSupport(this);
102
103         this.prop = descr;
104         this.propertyTypeClass = descr.getPropertyType();
105         this.readMethod = descr.getReadMethod();
106         this.writeMethod = descr.getWriteMethod();
107
108         try {
109             try {
110                 // bugfix #16703 addPropertyChangeListener(String, PropertyChangeListener) is looked first
111
// is not then addPropertyChangeListener(PropertyChangeListener) is looked
112
Method addList = bean.getClass().getMethod(
113                         "addPropertyChangeListener", new Class JavaDoc[] { String JavaDoc.class, PropertyChangeListener.class }
114                     ); // NOI18N
115

116                 addList.invoke(
117                     bean, new Object JavaDoc[] { propertyName, org.openide.util.WeakListeners.propertyChange(this, bean) }
118                 );
119             } catch (NoSuchMethodException JavaDoc nsme) {
120                 try {
121                     Method addList = bean.getClass().getMethod(
122                             "addPropertyChangeListener", new Class JavaDoc[] { PropertyChangeListener.class }
123                         ); // NOI18N
124

125                     addList.invoke(bean, new Object JavaDoc[] { org.openide.util.WeakListeners.propertyChange(this, bean) });
126                 } catch (NoSuchMethodException JavaDoc nosme) {
127                     // be quiet
128
}
129             }
130         } catch (Exception JavaDoc e) {
131             Logger.getLogger(DefaultPropertyModel.class.getName()).log(Level.WARNING, null, e);
132         }
133     }
134
135     /** Finds property descriptor for a bean.
136      * @param bean the bean
137      * @param name name of the property to find
138      * @return the descriptor
139      * @exception IllegalArgumentException if the method is not found
140      */

141     private static PropertyDescriptor findInfo(Object JavaDoc bean, String JavaDoc name)
142     throws IllegalArgumentException JavaDoc {
143         try {
144             BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
145             PropertyDescriptor[] descr = beanInfo.getPropertyDescriptors();
146
147             for (int i = 0; i < descr.length; i++) {
148                 if (descr[i].getName().equals(name)) {
149                     return descr[i];
150                 }
151             }
152
153             throw new IllegalArgumentException JavaDoc("No property named " + name + " in class " + bean.getClass() // NOI18N
154
);
155         } catch (IntrospectionException e) {
156             throw (IllegalArgumentException JavaDoc) new IllegalArgumentException JavaDoc(e.toString()).initCause(e);
157         }
158     }
159
160     /**
161      * @return the class of the property.
162      */

163     public Class JavaDoc getPropertyType() {
164         return propertyTypeClass;
165     }
166
167     /** Getter for current value of a property.
168      */

169     public Object JavaDoc getValue() throws InvocationTargetException {
170         try {
171             return (readMethod == null) ? null : readMethod.invoke(bean, new Object JavaDoc[] { });
172         } catch (IllegalAccessException JavaDoc e) {
173             Logger.getLogger(DefaultPropertyModel.class.getName()).log(Level.WARNING, null, e);
174             throw new InvocationTargetException(e);
175         }
176     }
177
178     /** Setter for a value of a property.
179      * @param v the value
180      * @exception InvocationTargetException
181      */

182     public void setValue(Object JavaDoc v) throws InvocationTargetException {
183         try {
184             if (writeMethod != null) {
185                 donotfire = true;
186                 writeMethod.invoke(bean, new Object JavaDoc[] { v });
187                 donotfire = false;
188             }
189         } catch (IllegalAccessException JavaDoc e) {
190             Logger.getLogger(DefaultPropertyModel.class.getName()).log(Level.WARNING, null, e);
191             throw new InvocationTargetException(e);
192         }
193     }
194
195     /** Adds listener to change of the value.
196      */

197     public void addPropertyChangeListener(PropertyChangeListener l) {
198         support.addPropertyChangeListener(l);
199     }
200
201     /** Removes listener to change of the value.
202      */

203     public void removePropertyChangeListener(PropertyChangeListener l) {
204         support.removePropertyChangeListener(l);
205     }
206
207     /** Implementation of PropertyChangeListener method */
208     public void propertyChange(PropertyChangeEvent evt) {
209         if (propertyName.equals(evt.getPropertyName()) && (!donotfire)) {
210             support.firePropertyChange(PROP_VALUE, evt.getOldValue(), evt.getNewValue());
211         }
212     }
213
214     /** The class of the property editor or <CODE>null</CODE>
215      * if default property editor should be used.
216      */

217     public Class JavaDoc getPropertyEditorClass() {
218         return prop.getPropertyEditorClass();
219     }
220
221     /**
222      * Returns an array of beans/nodes that this property belongs
223      * to. Implements the method from ExPropertyModel interface.
224      */

225     public Object JavaDoc[] getBeans() {
226         return new Object JavaDoc[] { bean };
227     }
228
229     /**
230      * Returns descriptor describing the property.
231      * Implements the method from ExPropertyModel interface.
232      */

233     public FeatureDescriptor getFeatureDescriptor() {
234         return prop;
235     }
236 }
237
Popular Tags