KickJava   Java API By Example, From Geeks To Geeks.

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


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.IntrospectionException JavaDoc;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import org.openide.DialogDisplayer;
32 import org.openide.NotifyDescriptor;
33 import org.openide.nodes.AbstractNode;
34 import org.openide.nodes.Children;
35 import org.openide.nodes.Node;
36 import org.openide.util.HelpCtx;
37 import org.openide.util.Lookup;
38 import org.openide.util.Utilities;
39
40 /** A list of all properties in a property set.
41  * The keys are of type Node.Property.
42  */

43 public class PropSetKids extends Children.Keys {
44     
45     private Node original;
46     private Node.PropertySet ps;
47     private PropertyChangeListener JavaDoc pcListener = null;
48     
49     public PropSetKids(Node original, Node.PropertySet ps) {
50         this.ps = ps;
51         this.original = original;
52     }
53     
54     /** Update the key list.
55      * Looks for all properties which are readable, and not primitive or String or Class.
56      */

57     private void updateKeys() {
58         Collection JavaDoc newKeys = new ArrayList JavaDoc();
59         Node.Property[] props = ps.getProperties();
60         for (int j = 0; j < props.length; j++) {
61             Node.Property prop = props[j];
62             if (prop.canRead()) {
63                 Class JavaDoc type = prop.getValueType();
64                 if (! (type.isPrimitive() || type == String JavaDoc.class || type == Class JavaDoc.class)) {
65                     newKeys.add(prop);
66                 }
67             }
68         }
69         setKeys(newKeys);
70     }
71     
72     /** Set the keys.
73      * Also attach a listener to the original node so that if one of its
74      * properties (node properties, not meta-properties of the node itself)
75      * changes, the children can be recalculated.
76      */

77     protected void addNotify() {
78         updateKeys();
79         if (pcListener == null) {
80             pcListener = new PropertyChangeListener JavaDoc() {
81                 public void propertyChange(PropertyChangeEvent JavaDoc ev) {
82                     String JavaDoc prop = ev.getPropertyName();
83                     Node.Property[] props = ps.getProperties();
84                     for (int j = 0; j < props.length; j++) {
85                         if (props[j].getName().equals(prop)) {
86                             refreshKey(props[j]);
87                             break;
88                         }
89                     }
90                 }
91             };
92             original.addPropertyChangeListener(pcListener);
93         }
94     }
95     
96     protected void removeNotify() {
97         if (pcListener != null) {
98             original.removePropertyChangeListener(pcListener);
99             pcListener = null;
100         }
101         setKeys(Collections.EMPTY_SET);
102     }
103     
104     /** Create the node for this property.
105      * @param key the property
106      * @return the (one) node to represent it
107      */

108     protected Node[] createNodes(Object JavaDoc key) {
109         return new Node[] { makePropertyNode((Node.Property) key) };
110     }
111     
112     /** Make a node for a property and its value.
113      * @param prop the property to represent
114      * @return a node to represent it
115      */

116     private static Node makePropertyNode(Node.Property prop) {
117         Class JavaDoc type = prop.getValueType();
118         Node node;
119         try {
120             node = makeObjectNode(prop.getValue());
121         } catch (Throwable JavaDoc t) {
122             node = makeErrorNode(t);
123         }
124         node.setDisplayName(Utilities.getClassName(type) + " " + prop.getDisplayName() + " = " + node.getDisplayName());
125         return node;
126     }
127     
128     /** Make a node to meaningfully represent some object.
129      * Special treatment for null; arrays or generalized collections; String and Class objects.
130      * All else gets a RefinedBeanNode.
131      * The name and tooltip are set to something helpful.
132      * @param val the object to represent
133      * @return a node displaying it
134      */

135     public static Node makeObjectNode(Object JavaDoc val) {
136         if (val == null) {
137             return makePlainNode("null");
138         } else if (val instanceof Object JavaDoc[]) {
139             return makeCollectionNode(Collections.enumeration(Arrays.asList((Object JavaDoc[]) val)));
140         } else if (val.getClass().isArray()) {
141             return makeCollectionNode(Collections.enumeration(Arrays.asList(Utilities.toObjectArray(val))));
142         } else if (val instanceof Lookup) {
143             Node n = LookupNode.localLookupNode((Lookup) val);
144             n.setShortDescription("String value: `" + val + "'");
145             n.setDisplayName(n.getDisplayName() + " (class " + val.getClass().getName() + ")");
146             return n;
147         } else if (val instanceof Enumeration JavaDoc) {
148             return makeCollectionNode((Enumeration JavaDoc) val);
149         } else if (val instanceof Collection JavaDoc) {
150             return makeCollectionNode(Collections.enumeration((Collection JavaDoc) val));
151         } else if (val instanceof String JavaDoc) {
152             return makePlainNode("\"" + (String JavaDoc) val + "\"");
153         } else if (val instanceof Class JavaDoc) {
154             return makePlainNode("class " + ((Class JavaDoc) val).getName());
155         } else if ((val instanceof Boolean JavaDoc) || (val instanceof Number JavaDoc)) {
156             return makePlainNode(val.toString());
157         } else if (val instanceof Character JavaDoc) {
158             return makePlainNode("(char) '" + val.toString() + "'");
159         } else {
160             Node objnode;
161             try {
162                 objnode = new RefinedBeanNode(val);
163             } catch (IntrospectionException JavaDoc e) {
164                 objnode = makeErrorNode(e);
165             }
166             String JavaDoc stringval;
167             try {
168                 stringval = "\"" + val + "\"";
169             } catch (RuntimeException JavaDoc e) {
170                 stringval = e.toString();
171             }
172             objnode.setShortDescription(stringval + ": " + objnode.getShortDescription());
173             objnode.setDisplayName(objnode.getDisplayName() + " (class " + val.getClass().getName() + ")");
174             return Wrapper.make(objnode);
175         }
176     }
177     
178     /** Make a leaf node just displaying some text.
179      * @param name the text
180      * @return the node
181      */

182     static Node makePlainNode(String JavaDoc name) {
183         AbstractNode toret = new AbstractNode(Children.LEAF) {
184             public HelpCtx getHelpCtx() {
185                 return new HelpCtx("org.netbeans.modules.apisupport.beanbrowser");
186             }
187         };
188         toret.setName(name);
189         toret.setIconBaseWithExtension("org/netbeans/modules/apisupport/beanbrowser/BeanBrowserIcon.gif");
190         return toret;
191     }
192     
193     /** Make a node representing an error condition and describing the error.
194      * @param t the error
195      * @return a node displaying it (as a Bean)
196      */

197     static Node makeErrorNode(Throwable JavaDoc t) {
198         Node node = makeObjectNode(t);
199         node.setDisplayName("[thrown] " + node.getDisplayName());
200         return node;
201     }
202     
203     /** Make a node representing an array or list or somesuch.
204      * Safety valve warns the user before creating a huge array.
205      * @param val an Enuemration of Object's
206      * @return a node displaying the objects as children
207      */

208     private static Node makeCollectionNode(final Enumeration JavaDoc val) {
209         final Node[] _base = new Node[] { null };
210         final String JavaDoc defaultName = "<list of objects>";
211         Children kids = new Children.Array() {
212             protected void addNotify() {
213                 new Thread JavaDoc(new Runnable JavaDoc() {
214                     public void run() {
215                         int count = 0;
216                         while (val.hasMoreElements()) {
217                             Node n = makeObjectNode(val.nextElement());
218                             n.setDisplayName("[" + count + "] " + n.getDisplayName());
219                             add(new Node[] { n });
220                             if (count++ == 50) {
221                                 if (! NotifyDescriptor.OK_OPTION.equals(
222                                         DialogDisplayer.getDefault().notify(new NotifyDescriptor.Confirmation(new String JavaDoc[] {
223                                     "There were over 50 elements in this array.",
224                                     "Actually show all of them?"
225                                 })))) {
226                                     break;
227                                 }
228                             }
229                         }
230                         if (defaultName.equals(_base[0].getDisplayName())) {
231                             _base[0].setDisplayName("A list of " + count + " children...");
232                             _base[0].setShortDescription(_base[0].getDisplayName());
233                         } else {
234                             _base[0].setShortDescription("[" + count + " children] " + _base[0].getShortDescription());
235                         }
236                     }
237                 }, "making collection node").start();
238             }
239         };
240         AbstractNode base = new AbstractNode(kids) {
241             public HelpCtx getHelpCtx() {
242                 return new HelpCtx("org.netbeans.modules.apisupport.beanbrowser");
243             }
244         };
245         _base[0] = base;
246         base.setName("collection");
247         base.setDisplayName(defaultName);
248         base.setIconBaseWithExtension("org/netbeans/modules/apisupport/beanbrowser/BeanBrowserIcon.gif");
249         return base;
250     }
251 }
252
Popular Tags