KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > ui > models > VariablesTreeModelFilterSI


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.debugger.jpda.ui.models;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Set JavaDoc;
27 import javax.swing.Action JavaDoc;
28
29 import org.netbeans.spi.debugger.ContextProvider;
30 import org.netbeans.api.debugger.jpda.Field;
31 import org.netbeans.api.debugger.jpda.ObjectVariable;
32 import org.netbeans.spi.viewmodel.NodeActionsProvider;
33 import org.netbeans.spi.viewmodel.NodeModel;
34 import org.netbeans.spi.viewmodel.TableModel;
35 import org.netbeans.spi.viewmodel.TreeModel;
36 import org.netbeans.spi.viewmodel.TreeModelFilter;
37 import org.netbeans.spi.viewmodel.ModelListener;
38 import org.netbeans.spi.viewmodel.UnknownTypeException;
39 //import org.netbeans.modules.debugger.jpda.ui.FixedWatch;
40
import org.openide.util.NbBundle;
41
42
43 /**
44  * Filters some original tree of nodes (represented by {@link TreeModel}).
45  *
46  * @author Jan Jancura
47  */

48 public class VariablesTreeModelFilterSI implements TreeModelFilter,
49 NodeModel, TableModel, NodeActionsProvider {
50
51     public static final String JavaDoc INHERITED =
52         "org/netbeans/modules/debugger/resources/watchesView/SuperVariable";
53     public static final String JavaDoc STATIC =
54         "org/netbeans/modules/debugger/resources/watchesView/SuperVariable";
55     private static final Set JavaDoc ignore = new HashSet JavaDoc (Arrays.asList (new String JavaDoc[] {
56         "java.lang.String",
57         "java.lang.StringBuffer",
58         "java.lang.Character",
59         "java.lang.Integer",
60         "java.lang.Float",
61         "java.lang.Byte",
62         "java.lang.Boolean",
63         "java.lang.Double",
64         "java.lang.Long",
65         "java.lang.Short",
66
67         "java.lang.ref.WeakReference",
68         
69         "java.util.ArrayList",
70         "java.util.HashSet",
71         "java.util.LinkedHashSet",
72         "java.util.LinkedList",
73         "java.util.Stack",
74         "java.util.TreeSet",
75         "java.util.Vector",
76         "java.util.Hashtable",
77         "java.util.Hashtable$Entry",
78         "java.util.HashMap",
79         "java.util.HashMap$Entry",
80         "java.util.IdentityHashMap",
81         "java.util.AbstractMap$SimpleEntry",
82         "java.util.TreeMap",
83         "java.util.TreeMap$Entry",
84         "java.util.WeakHashMap",
85         "java.util.LinkedHashMap",
86         "java.util.LinkedHashMap$Entry",
87         "java.beans.PropertyChangeSupport"
88     }));
89     private ContextProvider lookupProvider;
90     
91     
92     public VariablesTreeModelFilterSI (ContextProvider lookupProvider) {
93         this.lookupProvider = lookupProvider;
94     }
95
96     /**
97      * Returns filtered root of hierarchy.
98      *
99      * @param original the original tree model
100      * @return filtered root of hierarchy
101      */

102     public Object JavaDoc getRoot (TreeModel original) {
103         return original.getRoot ();
104     }
105     
106     /**
107      * Returns filtered children for given parent on given indexes.
108      *
109      * @param original the original tree model
110      * @param parent a parent of returned nodes
111      * @throws NoInformationException if the set of children can not be
112      * resolved
113      * @throws ComputingException if the children resolving process
114      * is time consuming, and will be performed off-line
115      * @throws UnknownTypeException if this TreeModelFilter implementation is not
116      * able to resolve dchildren for given node type
117      *
118      * @return children for given parent on given indexes
119      */

120     public Object JavaDoc[] getChildren (
121         TreeModel original,
122         Object JavaDoc parent,
123         int from,
124         int to
125     ) throws UnknownTypeException {
126         //parent = switchParentIfFixedWatch(parent);
127
if (parent instanceof ObjectVariable) {
128             ObjectVariable variable = (ObjectVariable) parent;
129             if (ignore.contains (variable.getType ()))
130                 return original.getChildren (parent, from, to);
131             int tto = Math.min (to, original.getChildrenCount (parent));
132             List JavaDoc l = new ArrayList JavaDoc (Arrays.asList (
133                 original.getChildren (parent, from, tto)
134             ));
135             if (l.size() < to && variable.getAllStaticFields(0, 0).length > 0)
136                 l.add (new StaticNode(variable));
137             if (l.size() < to && variable.getInheritedFields(0, 0).length > 0)
138                 l.add (new InheritedNode(variable));
139             return l.toArray ();
140         } else if (parent instanceof SpecialNode) {
141             return ((SpecialNode) parent).getChildren(0, 0);
142         }
143         return original.getChildren (parent, from, to);
144     }
145
146     /**
147      * Returns number of filtered children for given variable.
148      *
149      * @param original the original tree model
150      * @param parent a variable of returned fields
151      *
152      * @throws NoInformationException if the set of children can not be
153      * resolved
154      * @throws ComputingException if the children resolving process
155      * is time consuming, and will be performed off-line
156      * @throws UnknownTypeException if this TreeModelFilter implementation is not
157      * able to resolve dchildren for given node type
158      *
159      * @return number of filtered children for given variable
160      */

161     public int getChildrenCount (
162         TreeModel original,
163         Object JavaDoc parent
164     ) throws UnknownTypeException {
165         //parent = switchParentIfFixedWatch(parent);
166
if (parent instanceof ObjectVariable) {
167             ObjectVariable variable = (ObjectVariable) parent;
168             if (ignore.contains (variable.getType ()))
169                 return original.getChildrenCount (parent);
170             int i = original.getChildrenCount (parent);
171             if (i == Integer.MAX_VALUE) {
172                 return i;
173             }
174             if (variable.getAllStaticFields (0, 0).length > 0) i++;
175             if (variable.getInheritedFields (0, 0).length > 0) i++;
176             return i;
177         } else if (parent instanceof SpecialNode) {
178             return ((SpecialNode) parent).getChildren(0, 0).length;
179         }
180         return original.getChildrenCount (parent);
181     }
182
183 // private Object switchParentIfFixedWatch (Object parent) {
184
// if (parent instanceof FixedWatch) {
185
// FixedWatch fw = (FixedWatch) parent;
186
// if (fw.getVariable() instanceof ObjectVariable) {
187
// return fw.getVariable();
188
// }
189
// }
190
// return parent;
191
// }
192

193     /**
194      * Returns true if node is leaf.
195      *
196      * @param original the original tree model
197      * @throws UnknownTypeException if this TreeModel implementation is not
198      * able to resolve dchildren for given node type
199      * @return true if node is leaf
200      */

201     public boolean isLeaf (
202         TreeModel original,
203         Object JavaDoc node
204     ) throws UnknownTypeException {
205         return (node instanceof SpecialNode) ? false : original.isLeaf(node);
206     }
207
208     public void addModelListener (ModelListener l) {
209     }
210
211     public void removeModelListener (ModelListener l) {
212     }
213     
214     
215     // NodeModelFilter
216

217     public String JavaDoc getDisplayName (Object JavaDoc node)
218     throws UnknownTypeException {
219         if (node instanceof SpecialNode) return ((SpecialNode) node).getDisplayName();
220         throw new UnknownTypeException (node);
221     }
222     
223     public String JavaDoc getIconBase (Object JavaDoc node)
224     throws UnknownTypeException {
225         if (node instanceof SpecialNode) return ((SpecialNode) node).getIconBase();
226         throw new UnknownTypeException (node);
227     }
228     
229     public String JavaDoc getShortDescription (Object JavaDoc node)
230     throws UnknownTypeException {
231         if (node instanceof SpecialNode) return null;
232         throw new UnknownTypeException (node);
233     }
234     
235     
236     // NodeActionsProviderFilter
237

238     public Action JavaDoc[] getActions (
239         Object JavaDoc node
240     ) throws UnknownTypeException {
241         if (node instanceof SpecialNode) return new Action JavaDoc [0];
242         throw new UnknownTypeException (node);
243     }
244     
245     public void performDefaultAction (
246         Object JavaDoc node
247     ) throws UnknownTypeException {
248         if (node instanceof SpecialNode) return;
249         throw new UnknownTypeException (node);
250     }
251     
252     
253     // TableModelFilter
254

255     public Object JavaDoc getValueAt (
256         Object JavaDoc row,
257         String JavaDoc columnID
258     ) throws UnknownTypeException {
259         if (row instanceof SpecialNode) return "";
260         throw new UnknownTypeException (row);
261     }
262     
263     public boolean isReadOnly (
264         Object JavaDoc row,
265         String JavaDoc columnID
266     ) throws UnknownTypeException {
267         if (row instanceof SpecialNode) return true;
268         throw new UnknownTypeException (row);
269     }
270     
271     public void setValueAt (
272         Object JavaDoc row,
273         String JavaDoc columnID,
274         Object JavaDoc value
275     ) throws UnknownTypeException {
276         if (row instanceof SpecialNode) return;
277         throw new UnknownTypeException (row);
278     }
279     
280     private static class StaticNode extends SpecialNode {
281
282         StaticNode(ObjectVariable parent) {
283             super(parent);
284         }
285
286         Field [] getChildren(int from, int to) {
287             return object.getAllStaticFields(0, 0);
288         }
289
290         public boolean equals(Object JavaDoc o) {
291             if (this == o) return true;
292             if (!(o instanceof StaticNode)) return false;
293             return object.equals(((StaticNode) o).object);
294         }
295
296         String JavaDoc getDisplayName() {
297             return NbBundle.getBundle(VariablesTreeModelFilterSI.class).getString("MSG_VariablesFilter_StaticNode"); // NOI18N
298
}
299
300         String JavaDoc getIconBase() {
301             return STATIC;
302         }
303     }
304
305     private static class InheritedNode extends SpecialNode {
306
307         InheritedNode(ObjectVariable object) {
308             super(object);
309         }
310
311         Field [] getChildren(int from, int to) {
312             return object.getInheritedFields(0, 0);
313         }
314
315         public boolean equals(Object JavaDoc o) {
316             if (this == o) return true;
317             if (!(o instanceof InheritedNode)) return false;
318             return object.equals(((InheritedNode) o).object);
319         }
320
321         String JavaDoc getDisplayName() {
322             return NbBundle.getBundle(VariablesTreeModelFilterSI.class).getString("MSG_VariablesFilter_InheritedNode"); // NOI18N
323
}
324
325         String JavaDoc getIconBase() {
326             return INHERITED;
327         }
328     }
329
330     private static abstract class SpecialNode {
331         protected ObjectVariable object;
332
333         protected SpecialNode(ObjectVariable parent) {
334             this.object = parent;
335         }
336
337         public int hashCode() {
338             return object.hashCode();
339         }
340
341         abstract Field [] getChildren(int from, int to);
342         abstract String JavaDoc getDisplayName();
343         abstract String JavaDoc getIconBase();
344     }
345
346     // helper methods ..........................................................
347
}
348
Popular Tags