2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
41
package org.openide.explorer.propertysheet;
46
import java.lang.reflect.*;
47
import java.util.logging.Level;
48
import java.util.logging.Logger;
51
/** The default implementation of PropertyModel interface.
52
* It takes the bean instance and the property name which should
53
* be accessed through PropertyModel methods. We now implement
54
* the new ExPropertyModel interface (which extends PropertyModel).
56
* @deprecated Use org.openide.nodes.PropertySupport.Reflection instead
57
* @author Jaroslav Tulach, Petr Hamernik
59
public @Deprecated class DefaultPropertyModel extends Object implements ExPropertyModel, PropertyChangeListener {
63
/** Name of the property of the bean. */
64
String propertyName; //package private so error handling code can pick up
66
//the property name if the user enters an invalid value
68
/** support for the properties changes. */
69
private PropertyChangeSupport support;
71
/** Property descriptor for the bean. */
72
private PropertyDescriptor prop;
74
/** Read method if exists one. */
75
private Method readMethod;
77
/** Write method if exists one. */
78
private Method writeMethod;
80
/** Type of the property. */
81
private Class propertyTypeClass;
83
/** When we call setValue we want to disable refiring of property change*/
84
private boolean donotfire = false;
86
/** Creates new DefaultPropertyModel.
87
* @param bean the java bean to be introspected
88
* @param propertyName name of the property
90
* @exception IllegalArgumentException if there is any problem
91
* with the parameters (introspection of bean,...)
93
public DefaultPropertyModel(Object bean, String propertyName)
94
throws IllegalArgumentException {
95
this(bean, findInfo(bean, propertyName));
98
/** Creates new DefaultPropertyModel with provided specific
99
* <code>PropertyDescriptor</code>. This can be useful if one needs to
100
* set to provide specific attributes to the property editor.
102
* PropertyDescriptor pd = new PropertyDescriptor ("myProperty", bean.getClass ());
103
* pd.setPropertyEditorClass (PropertyEditorManager.findEditor (Object.class));
105
* // special attributes to the property editor
106
* pb.setValue ("superClass", MyProperty.class);
109
* model = new DefaultPropertyModel (bean, pd);
110
* panel = new PropertyPanel (model);
112
* This constructor replaces the default use of BeanInfo and that is why
113
* simplifies the use of <link>ExPropertyEditor</link>s.
115
* @param bean the java bean to be introspected
116
* @param descr the property descriptor of the property to use
120
public DefaultPropertyModel(Object bean, PropertyDescriptor descr) {
122
this.propertyName = descr.getName();
123
support = new PropertyChangeSupport(this);
126
this.propertyTypeClass = descr.getPropertyType();
127
this.readMethod = descr.getReadMethod();
128
this.writeMethod = descr.getWriteMethod();
132
// bugfix #16703 addPropertyChangeListener(String, PropertyChangeListener) is looked first
133
// is not then addPropertyChangeListener(PropertyChangeListener) is looked
134
Method addList = bean.getClass().getMethod(
135
"addPropertyChangeListener", new Class[] { String.class, PropertyChangeListener.class }
139
bean, new Object[] { propertyName, org.openide.util.WeakListeners.propertyChange(this, bean) }
141
} catch (NoSuchMethodException nsme) {
143
Method addList = bean.getClass().getMethod(
144
"addPropertyChangeListener", new Class[] { PropertyChangeListener.class }
147
addList.invoke(bean, new Object[] { org.openide.util.WeakListeners.propertyChange(this, bean) });
148
} catch (NoSuchMethodException nosme) {
152
} catch (Exception e) {
153
Logger.getLogger(DefaultPropertyModel.class.getName()).log(Level.WARNING, null, e);
157
/** Finds property descriptor for a bean.
158
* @param bean the bean
159
* @param name name of the property to find
160
* @return the descriptor
161
* @exception IllegalArgumentException if the method is not found
163
private static PropertyDescriptor findInfo(Object bean, String name)
164
throws IllegalArgumentException {
166
BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
167
PropertyDescriptor[] descr = beanInfo.getPropertyDescriptors();
169
for (int i = 0; i < descr.length; i++) {
170
if (descr[i].getName().equals(name)) {
175
throw new IllegalArgumentException("No property named " + name + " in class " + bean.getClass() // NOI18N
177
} catch (IntrospectionException e) {
178
throw (IllegalArgumentException) new IllegalArgumentException(e.toString()).initCause(e);
183
* @return the class of the property.
185
public Class getPropertyType() {
186
return propertyTypeClass;
189
/** Getter for current value of a property.
191
public Object getValue() throws InvocationTargetException {
193
return (readMethod == null) ? null : readMethod.invoke(bean, new Object[] { });
194
} catch (IllegalAccessException e) {
195
Logger.getLogger(DefaultPropertyModel.class.getName()).log(Level.WARNING, null, e);
196
throw new InvocationTargetException(e);
200
/** Setter for a value of a property.
202
* @exception InvocationTargetException
204
public void setValue(Object v) throws InvocationTargetException {
206
if (writeMethod != null) {
208
writeMethod.invoke(bean, new Object[] { v });
211
} catch (IllegalAccessException e) {
212
Logger.getLogger(DefaultPropertyModel.class.getName()).log(Level.WARNING, null, e);
213
throw new InvocationTargetException(e);
217
/** Adds listener to change of the value.
219
public void addPropertyChangeListener(PropertyChangeListener l) {
220
support.addPropertyChangeListener(l);
223
/** Removes listener to change of the value.
225
public void removePropertyChangeListener(PropertyChangeListener l) {
226
support.removePropertyChangeListener(l);
229
/** Implementation of PropertyChangeListener method */
230
public void propertyChange(PropertyChangeEvent evt) {
231
if (propertyName.equals(evt.getPropertyName()) && (!donotfire)) {
232
support.firePropertyChange(PROP_VALUE, evt.getOldValue(), evt.getNewValue());
236
/** The class of the property editor or <CODE>null</CODE>
237
* if default property editor should be used.
239
public Class getPropertyEditorClass() {
240
return prop.getPropertyEditorClass();
244
* Returns an array of beans/nodes that this property belongs
245
* to. Implements the method from ExPropertyModel interface.
247
public Object[] getBeans() {
248
return new Object[] { bean };
252
* Returns descriptor describing the property.
253
* Implements the method from ExPropertyModel interface.
255
public FeatureDescriptor getFeatureDescriptor() {