KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > beanbrowser > RawBeanPropKids


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 package org.netbeans.modules.apisupport.beanbrowser;
21
22 import java.beans.BeanInfo JavaDoc;
23 import java.beans.IndexedPropertyDescriptor JavaDoc;
24 import java.beans.Introspector JavaDoc;
25 import java.beans.PropertyDescriptor JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import java.util.Collections JavaDoc;
28 import org.openide.nodes.Children;
29 import org.openide.nodes.Node;
30
31 /** Children representing bean-like properties, unfiltered by type
32  * or by known BeanInfo information.
33  * <P>Keys may be PropertyDescriptor (a property)
34  * or Exception (error during introspection).
35  * @author Jesse Glick
36  */

37 public class RawBeanPropKids extends Children.Keys {
38     
39     protected Object JavaDoc thing;
40     
41     public RawBeanPropKids(Object JavaDoc thing) {
42         this.thing = thing;
43     }
44     
45     protected void addNotify() {
46         try {
47             BeanInfo JavaDoc bi = Introspector.getBeanInfo(thing.getClass(), Introspector.IGNORE_ALL_BEANINFO);
48             setKeys(bi.getPropertyDescriptors());
49             // XXX listen to changes in thing if it has a property change listener event set
50
} catch (Exception JavaDoc e) {
51             setKeys(Collections.singleton(e));
52         }
53     }
54     
55     protected void removeNotify() {
56         setKeys(Collections.EMPTY_SET);
57     }
58     
59     protected Node[] createNodes(Object JavaDoc key) {
60         if (key instanceof PropertyDescriptor JavaDoc) {
61             PropertyDescriptor JavaDoc pd = (PropertyDescriptor JavaDoc) key;
62             if (pd instanceof IndexedPropertyDescriptor JavaDoc) {
63                 // We can't show it, don't try.
64
return null;
65             }
66             Method JavaDoc meth = pd.getReadMethod();
67             if (meth == null) {
68                 return new Node[] {PropSetKids.makePlainNode("[unreadable: " + pd.getName() + "]")};
69             }
70             try {
71                 Object JavaDoc val;
72                 // Be brutal: we are showing *raw* bean properties here,
73
// after all, and it is frequently useful to inspect e.g.
74
// package-private stuff too.
75
meth.setAccessible(true);
76                 try {
77                     val = meth.invoke(thing, new Object JavaDoc[] { });
78                 } finally {
79                     meth.setAccessible(false);
80                 }
81                 Node n = PropSetKids.makeObjectNode(val);
82                 n.setDisplayName(pd.getName() + " = " + n.getDisplayName());
83                 return new Node[] { n };
84             } catch (Exception JavaDoc e) {
85                 Node n = PropSetKids.makeErrorNode(e);
86                 n.setDisplayName("[property: " + pd.getName() + "] " + n.getDisplayName());
87                 return new Node[] { n };
88             }
89         } else {
90             Node n = PropSetKids.makeErrorNode((Exception JavaDoc) key);
91             n.setDisplayName("[during introspection] " + n.getDisplayName());
92             return new Node[] { n };
93         }
94     }
95     
96 }
97
Popular Tags