KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > examples > debugger > jpda > callstackviewfilterring > CallStackFilter


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.examples.debugger.jpda.callstackviewfilterring;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import org.netbeans.api.debugger.jpda.CallStackFrame;
25 import org.netbeans.spi.viewmodel.ComputingException;
26 import org.netbeans.spi.viewmodel.NoInformationException;
27 import org.netbeans.spi.viewmodel.NodeModel;
28 import org.netbeans.spi.viewmodel.TreeModel;
29 import org.netbeans.spi.viewmodel.TreeModelFilter;
30 import org.netbeans.spi.viewmodel.TreeModelListener;
31 import org.netbeans.spi.viewmodel.UnknownTypeException;
32
33
34
35
36 public class CallStackFilter implements TreeModelFilter, NodeModel {
37     
38     
39     /**
40      * Returns filtered root of hierarchy.
41      *
42      * @param original the original tree model
43      * @return filtered root of hierarchy
44      */

45     public Object JavaDoc getRoot (TreeModel original) {
46         return original.getRoot ();
47     }
48     
49     /**
50      * Returns number of filterred children for given node.
51      *
52      * @param original the original tree model
53      * @param node the parent node
54      * @throws NoInformationException if the set of children can not be
55      * resolved
56      * @throws ComputingException if the children resolving process
57      * is time consuming, and will be performed off-line
58      * @throws UnknownTypeException if this TreeModel implementation is not
59      * able to resolve children for given node type
60      *
61      * @return true if node is leaf
62      */

63     public int getChildrenCount (
64         TreeModel original,
65         Object JavaDoc node
66     ) throws NoInformationException, ComputingException, UnknownTypeException {
67         if (node.equals (original.getRoot ())) {
68             Object JavaDoc[] originalCh = original.getChildren (
69                 node,
70                 0,
71                 original.getChildrenCount (node)
72             );
73             int i, k = originalCh.length, j = 0;
74             boolean in = false;
75             for (i = 0; i < k; i++) {
76                 if (! (originalCh [i] instanceof CallStackFrame)) {
77                     j++;
78                     continue;
79                 }
80                 CallStackFrame f = (CallStackFrame) originalCh [i];
81                 String JavaDoc className = f.getClassName ();
82                 if (className.startsWith ("java")) {
83                     if (!in) {
84                         j++;
85                         in = true;
86                     }
87                 } else {
88                     in = false;
89                     j++;
90                 }
91             }
92             return j;
93         }
94         if (node instanceof JavaFrames)
95             return ((JavaFrames) node).getStack ().size ();
96         return original.getChildrenCount (node);
97     }
98     
99     /**
100      * Returns filtered children for given parent on given indexes.
101      * Typically you should get original nodes
102      * (<code>original.getChildren (...)</code>), and modify them, or return
103      * it without modifications. You should not throw UnknownTypeException
104      * directly from this method!
105      *
106      * @param original the original tree model
107      * @param parent a parent of returned nodes
108      * @throws NoInformationException if the set of children can not be
109      * resolved
110      * @throws ComputingException if the children resolving process
111      * is time consuming, and will be performed off-line
112      * @throws UnknownTypeException this exception can be thrown from
113      * <code>original.getChildren (...)</code> method call only!
114      *
115      * @return children for given parent on given indexes
116      */

117     public Object JavaDoc[] getChildren (
118         TreeModel original,
119         Object JavaDoc parent,
120         int from,
121         int to
122     ) throws NoInformationException, ComputingException, UnknownTypeException {
123         if (parent.equals (original.getRoot ())) {
124             Object JavaDoc[] originalCh = original.getChildren (
125                 parent,
126                 0,
127                 original.getChildrenCount (parent)
128             );
129             int i, k = originalCh.length;
130             ArrayList JavaDoc newCh = new ArrayList JavaDoc ();
131             JavaFrames javaFrames = null;
132             for (i = 0; i < k; i++) {
133                 if (! (originalCh [i] instanceof CallStackFrame)) {
134                     newCh.add (originalCh [i]);
135                     continue;
136                 }
137                 CallStackFrame f = (CallStackFrame) originalCh [i];
138                 String JavaDoc className = f.getClassName ();
139                 if (className.startsWith ("java")) {
140                     if (javaFrames == null) {
141                         javaFrames = new JavaFrames ();
142                         newCh.add (javaFrames);
143                     }
144                     javaFrames.addFrame (f);
145                 } else {
146                     javaFrames = null;
147                     newCh.add (f);
148                 }
149             }
150             return newCh.subList (from, to).toArray ();
151         }
152         if (parent instanceof JavaFrames)
153             return ((JavaFrames) parent).getStack ().toArray ();
154         return original.getChildren (parent, from, to);
155     }
156     
157     /**
158      * Returns true if node is leaf. You should not throw UnknownTypeException
159      * directly from this method!
160      *
161      * @param original the original tree model
162      * @throws UnknownTypeException this exception can be thrown from
163      * <code>original.isLeaf (...)</code> method call only!
164      * @return true if node is leaf
165      */

166     public boolean isLeaf (TreeModel original, Object JavaDoc node)
167     throws UnknownTypeException {
168         if (node instanceof JavaFrames) return false;
169         return original.isLeaf (node);
170     }
171     
172     public void addTreeModelListener (TreeModelListener l) {
173     }
174     public void removeTreeModelListener (TreeModelListener l) {
175     }
176     
177     public String JavaDoc getDisplayName (Object JavaDoc node) throws UnknownTypeException {
178         if (node instanceof JavaFrames)
179             return "Java Callstack Frames";
180         throw new UnknownTypeException (node);
181     }
182     
183     public String JavaDoc getIconBase (Object JavaDoc node) throws UnknownTypeException {
184         if (node instanceof JavaFrames)
185             return "org/netbeans/examples/debugger/jpda/callstackviewfilterring/NonCurrentFrame";
186         throw new UnknownTypeException (node);
187     }
188     
189     public String JavaDoc getShortDescription (Object JavaDoc node) throws UnknownTypeException {
190         if (node instanceof JavaFrames)
191             return "Unimportant hidden callstack frames";
192         throw new UnknownTypeException (node);
193     }
194     
195     
196     // innerclasses ............................................................
197

198     private static class JavaFrames {
199         private List JavaDoc frames = new ArrayList JavaDoc ();
200         
201         void addFrame (CallStackFrame frame) {
202             frames.add (frame);
203         }
204         
205         List JavaDoc getStack () {
206             return frames;
207         }
208         
209         public boolean equals (Object JavaDoc o) {
210             if (!(o instanceof JavaFrames)) return false;
211             if (frames.size () != ((JavaFrames) o).frames.size ()) return false;
212             if (frames.size () == 0) return o == this;
213             return frames.get (0).equals (
214                 ((JavaFrames) o).frames.get (0)
215             );
216         }
217         
218         public int hashCode () {
219             if (frames.size () == 0) return super.hashCode ();
220             return frames.get (0).hashCode ();
221         }
222     }
223 }
224
Popular Tags