KickJava   Java API By Example, From Geeks To Geeks.

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


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 /*
20  * ModelProperty.java
21  *
22  * Created on 05 October 2003, 20:40
23  */

24 package org.openide.explorer.propertysheet;
25
26 import org.openide.nodes.Node;
27 import org.openide.nodes.Node.Property;
28 import org.openide.nodes.Node.PropertySet;
29 import org.openide.nodes.PropertySupport;
30
31 import java.beans.BeanDescriptor JavaDoc;
32 import java.beans.BeanInfo JavaDoc;
33 import java.beans.FeatureDescriptor JavaDoc;
34 import java.beans.Introspector JavaDoc;
35 import java.beans.PropertyDescriptor JavaDoc;
36 import java.beans.PropertyEditor JavaDoc;
37
38 import java.lang.reflect.Constructor JavaDoc;
39 import java.lang.reflect.InvocationTargetException JavaDoc;
40 import java.util.logging.Level JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42 import org.openide.util.Exceptions;
43
44
45 /** Wraps a legacy PropertyModel object in an instance of Node.Property.
46  *
47  * @author Tim Boudreau
48  */

49 class ModelProperty extends Property {
50     PropertyModel mdl;
51     private boolean valueSet = false;
52
53     /** Creates a new instance of ModelProperty */
54     private ModelProperty(PropertyModel pm) {
55         super(pm.getPropertyType());
56         this.mdl = pm;
57
58         if (mdl instanceof ExPropertyModel) {
59             FeatureDescriptor JavaDoc fd = ((ExPropertyModel) mdl).getFeatureDescriptor();
60             Boolean JavaDoc result = (Boolean JavaDoc) fd.getValue("canEditAsText"); // NOI18N
61

62             if (result != null) {
63                 this.setValue("canEditAsText", result);
64             }
65         }
66
67         //System.err.println(
68
//"Created ModelProperty wrapper for mystery PropertyModel " + pm);
69
}
70
71     Object JavaDoc[] getBeans() {
72         if (mdl instanceof ExPropertyModel) {
73             return ((ExPropertyModel) mdl).getBeans();
74         }
75
76         return null;
77     }
78
79     public PropertyEditor JavaDoc getPropertyEditor() {
80         if (mdl.getPropertyEditorClass() != null) {
81             try {
82                 //System.err.println("ModelProperty creating a "
83
//+ mdl.getPropertyEditorClass());
84
Constructor JavaDoc c = mdl.getPropertyEditorClass().getConstructor();
85                 c.setAccessible(true);
86
87                 return (PropertyEditor JavaDoc) c.newInstance(new Object JavaDoc[0]);
88             } catch (Exception JavaDoc e) {
89                 Exceptions.printStackTrace(e);
90
91                 return new PropUtils.NoPropertyEditorEditor();
92             }
93         }
94
95         return super.getPropertyEditor();
96     }
97
98     static Property toProperty(PropertyModel mdl) {
99         if (mdl instanceof NodePropertyModel) {
100             //System.err.println("Extracting prop from NodePropertyModel - " + ((NodePropertyModel) mdl).getProperty().getDisplayName());
101
return ((NodePropertyModel) mdl).getProperty();
102         } else if (mdl instanceof DefaultPropertyModel) {
103             return new DPMWrapper((DefaultPropertyModel) mdl);
104         } else if (
105             mdl instanceof ExPropertyModel &&
106                 ((ExPropertyModel) mdl).getFeatureDescriptor() instanceof PropertyDescriptor JavaDoc
107         ) {
108             Object JavaDoc[] beans = ((ExPropertyModel) mdl).getBeans();
109
110             if (beans.length == 1) {
111                 return new DPMWrapper(
112                     (PropertyDescriptor JavaDoc) ((ExPropertyModel) mdl).getFeatureDescriptor(),
113                     ((ExPropertyModel) mdl).getBeans()
114                 );
115             } else {
116                 return new ModelProperty(mdl);
117             }
118         } else if (mdl instanceof ExPropertyModel &&
119                 ((ExPropertyModel) mdl).getFeatureDescriptor() instanceof Property) {
120             UnsupportedOperationException JavaDoc uoe = new UnsupportedOperationException JavaDoc(
121                     "PropertyPanel now supports direct" + " use of Node.Property objects. Please do not use " +
122                     "ExPropertyModel if you only need to wrap a Node.Property " +
123                     "object. PropertyModel will be deprecated soon."
124                 ); //NOI18N
125
Logger.getLogger(ModelProperty.class.getName()).log(Level.WARNING, null, uoe);
126
127             return (Property) ((ExPropertyModel) mdl).getFeatureDescriptor();
128         } else if (mdl != null) {
129             return new ModelProperty(mdl);
130         } else {
131             //Model is null - formerly solved by PropertyPanel.EMPTY empty model
132
return new EmptyProperty();
133         }
134     }
135
136     /** Creates a ProxyProperty for a set of nodes given a property name */
137     static Property toProperty(Node[] n, String JavaDoc name) throws ClassCastException JavaDoc, NullPointerException JavaDoc {
138         Class JavaDoc clazz = null; //the class to look for
139

140         if (n.length == 0) {
141             //Give enough info for someone to figure out what's wrong
142
throw new NullPointerException JavaDoc(
143                 "Cannot find a property in an array" + " of 0 properties. Looking for " + name
144             );
145         }
146
147         for (int i = 0; i < n.length; i++) {
148             Property p = findProperty(n[i], name);
149
150             if (p == null) {
151                 throw new NullPointerException JavaDoc(
152                     "Node " + n[i].getDisplayName() + " does not contain a property " + name
153                 );
154             } else if (clazz == null) {
155                 clazz = p.getValueType();
156             } else {
157                 if (p.getValueType() != clazz) {
158                     throw new ClassCastException JavaDoc(
159                         "Found a property named " + n + " but it is of class " + p.getValueType().getName() + " not " +
160                         clazz.getName()
161                     );
162                 }
163             }
164         }
165
166         ProxyNode pn = new ProxyNode(n);
167         Property p = findProperty(pn, name);
168
169         if (p != null) {
170             return p;
171         }
172
173         //should never reach this point
174
throw new NullPointerException JavaDoc(
175             "Found properties named " + name + " but ProxyNode did not contain one with this name. This should " +
176             "be impossible; probably someone has modified ProxyNode"
177         );
178     }
179
180     /** Used in case of 1 element array */
181     static Property findProperty(Node n, String JavaDoc name) throws NullPointerException JavaDoc {
182         PropertySet[] ps = n.getPropertySets();
183
184         for (int j = 0; j < ps.length; j++) {
185             Property p = findProperty(ps[j], name);
186
187             if (p != null) {
188                 return p;
189             }
190         }
191
192         return null;
193     }
194
195     /** Searches for a named property in a property set */
196     private static Property findProperty(PropertySet set, String JavaDoc name) {
197         Property[] p = set.getProperties();
198
199         for (int i = 0; i < p.length; i++) {
200             if (p[i].getName().equals(name)) {
201                 return p[i];
202             }
203         }
204
205         return null;
206     }
207
208     public boolean canRead() {
209         return true; //who really knows?
210
}
211
212     public boolean canWrite() {
213         return true;
214     }
215
216     public Object JavaDoc getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
217         return mdl.getValue();
218     }
219
220     public void setValue(Object JavaDoc val) throws IllegalAccessException JavaDoc, IllegalArgumentException JavaDoc, InvocationTargetException JavaDoc {
221         //System.err.println("Setting value on ModelProperty to " + val + " forwarding to " + mdl);
222
mdl.setValue(val);
223         valueSet = true;
224     }
225
226     /** Used by EditablePropertyDisplayer to provide access to the real
227      * feature descriptor. Some property editors will cast the result of
228      * env.getFeatureDescriptor() as Property or PropertyDescriptor, so we
229      * need to return the original */

230     FeatureDescriptor JavaDoc getFeatureDescriptor() {
231         if (mdl instanceof ExPropertyModel) {
232             return ((ExPropertyModel) mdl).getFeatureDescriptor();
233         } else {
234             return this;
235         }
236     }
237
238     private static String JavaDoc findDisplayNameFor(Object JavaDoc o) {
239         try {
240             if (o == null) {
241                 return null;
242             }
243
244             if (o instanceof Node.Property) {
245                 return ((Node.Property) o).getDisplayName();
246             }
247
248             BeanInfo JavaDoc bi = Introspector.getBeanInfo(o.getClass());
249
250             if (bi != null) {
251                 BeanDescriptor JavaDoc bd = bi.getBeanDescriptor();
252
253                 if (bd != null) {
254                     return bd.getDisplayName();
255                 }
256             }
257         } catch (Exception JavaDoc e) {
258             //okay, we did our best
259
}
260
261         return null;
262     }
263
264     /** A wrapper for PropertyModels that just use DefaultPropertyModel - converts
265      * them to PropertySupport.Reflection */

266     static class DPMWrapper extends PropertySupport.Reflection {
267         PropertyDescriptor JavaDoc descriptor;
268         PropertyModel mdl;
269         private String JavaDoc beanName = null;
270
271         public DPMWrapper(DefaultPropertyModel mdl) {
272             super(
273                 mdl.bean, mdl.getPropertyType(), ((PropertyDescriptor JavaDoc) mdl.getFeatureDescriptor()).getReadMethod(),
274                 ((PropertyDescriptor JavaDoc) mdl.getFeatureDescriptor()).getWriteMethod()
275             );
276             descriptor = ((PropertyDescriptor JavaDoc) mdl.getFeatureDescriptor());
277
278             // System.err.println("Created DPMWrapper for DefaultPropertyModel " + descriptor.getName() + " - " + descriptor.getDisplayName());
279
// System.err.println("Value class for dpm: " + mdl.getPropertyType().getName() + " for created property: " + this.getValueType());
280
// System.err.println("SuppressCustomEditor: " + descriptor.getValue("suppressCustomEditor"));
281
this.mdl = mdl;
282             beanName = findDisplayNameFor(mdl.bean);
283         }
284
285         public DPMWrapper(PropertyDescriptor JavaDoc desc, Object JavaDoc[] instances) {
286             super(instances[0], desc.getPropertyType(), desc.getReadMethod(), desc.getWriteMethod());
287             descriptor = desc;
288
289             if ((instances != null) && (instances.length == 1)) {
290                 beanName = findDisplayNameFor(instances[0]);
291             }
292
293             // System.err.println("Created DPMWrapper for PropertyDescriptor " + descriptor.getName() + " - " + descriptor.getDisplayName() + " with array of instances");
294
}
295
296         public String JavaDoc getBeanName() {
297             return beanName;
298         }
299
300         Object JavaDoc[] getBeans() {
301             if (mdl instanceof DefaultPropertyModel) {
302                 return ((DefaultPropertyModel) mdl).getBeans();
303             }
304
305             return null;
306         }
307
308         /** Used by EditablePropertyDisplayer to provide access to the real
309          * feature descriptor. Some property editors will cast the result of
310          * env.getFeatureDescriptor() as Property or PropertyDescriptor, so we
311          * need to return the original */

312         FeatureDescriptor JavaDoc getFeatureDescriptor() {
313             return descriptor;
314         }
315
316         public String JavaDoc getDisplayName() {
317             return descriptor.getDisplayName();
318         }
319
320         public String JavaDoc getShortDescription() {
321             return descriptor.getShortDescription();
322         }
323
324         public Object JavaDoc getValue(String JavaDoc key) {
325             Object JavaDoc result = descriptor.getValue(key);
326
327             if (result == null) {
328                 result = super.getValue(key);
329             }
330
331             return result;
332         }
333
334         public void setValue(String JavaDoc key, Object JavaDoc val) {
335             descriptor.setValue(key, val);
336         }
337
338         public PropertyEditor JavaDoc getPropertyEditor() {
339             Class JavaDoc edClass;
340
341             if (mdl != null) {
342                 edClass = mdl.getPropertyEditorClass();
343             } else {
344                 edClass = descriptor.getPropertyEditorClass();
345             }
346
347             if (edClass != null) {
348                 //Handle case of e.g. java wizard class customizer which
349
//overrides getPropertyEditorClass()
350
try {
351                     //System.err.println(getDisplayName() + "Returning editor class specified property editor - " + edClass);
352
return (PropertyEditor JavaDoc) edClass.newInstance();
353                 } catch (Exception JavaDoc e) {
354                     //fall through
355
}
356             }
357
358             return super.getPropertyEditor();
359         }
360     }
361
362     /** A property implementation for cases where the property panel would
363      * be using the empty model */

364     private static class EmptyProperty extends Property {
365         public EmptyProperty() {
366             super(Object JavaDoc.class);
367         }
368
369         public boolean canRead() {
370             return true;
371         }
372
373         public boolean canWrite() {
374             return false;
375         }
376
377         public Object JavaDoc getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
378             return "";
379         }
380
381         public void setValue(Object JavaDoc val)
382         throws IllegalAccessException JavaDoc, IllegalArgumentException JavaDoc, InvocationTargetException JavaDoc {
383             //do nothing
384
}
385
386         public PropertyEditor JavaDoc getPropertyEditor() {
387             return new PropUtils.NoPropertyEditorEditor();
388         }
389     }
390 }
391
Popular Tags