KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > tools > example > debug > gui > ClassTreeTool


1 /*
2  * @(#)ClassTreeTool.java 1.12 05/11/17
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 /*
8  * Copyright (c) 1997-1999 by Sun Microsystems, Inc. All Rights Reserved.
9  *
10  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
11  * modify and redistribute this software in source and binary code form,
12  * provided that i) this copyright notice and license appear on all copies of
13  * the software; and ii) Licensee does not utilize the software in a manner
14  * which is disparaging to Sun.
15  *
16  * This software is provided "AS IS," without a warranty of any kind. ALL
17  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
18  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
19  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
20  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
21  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
22  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
23  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
24  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
25  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGES.
27  *
28  * This software is not designed or intended for use in on-line control of
29  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
30  * the design, construction, operation or maintenance of any nuclear
31  * facility. Licensee represents and warrants that it will not use or
32  * redistribute the Software for such purposes.
33  */

34
35 package com.sun.tools.example.debug.gui;
36
37 import java.io.*;
38 import java.util.*;
39
40 import javax.swing.*;
41 import javax.swing.tree.*;
42 import javax.swing.event.*;
43 import java.awt.*;
44 import java.awt.event.*;
45
46 import com.sun.jdi.*;
47 import com.sun.tools.example.debug.event.*;
48 import com.sun.tools.example.debug.bdi.*;
49
50 public class ClassTreeTool extends JPanel {
51
52     private Environment env;
53
54     private ExecutionManager runtime;
55     private SourceManager sourceManager;
56     private ClassManager classManager;
57
58     private JTree tree;
59     private DefaultTreeModel treeModel;
60     private ClassTreeNode root;
61     private SearchPath sourcePath;
62
63     private CommandInterpreter interpreter;
64
65     private static String JavaDoc HEADING = "CLASSES";
66
67     public ClassTreeTool(Environment env) {
68
69     super(new BorderLayout());
70
71     this.env = env;
72     this.runtime = env.getExecutionManager();
73     this.sourceManager = env.getSourceManager();
74
75     this.interpreter = new CommandInterpreter(env);
76
77     root = createClassTree(HEADING);
78     treeModel = new DefaultTreeModel(root);
79     
80         // Create a tree that allows one selection at a time.
81

82         tree = new JTree(treeModel);
83     tree.setSelectionModel(new SingleLeafTreeSelectionModel());
84
85     /******
86         // Listen for when the selection changes.
87         tree.addTreeSelectionListener(new TreeSelectionListener() {
88             public void valueChanged(TreeSelectionEvent e) {
89                 ClassTreeNode node = (ClassTreeNode)
90             (e.getPath().getLastPathComponent());
91         if (node != null) {
92             interpreter.executeCommand("view " + node.getReferenceTypeName());
93         }
94         }
95     });
96     ******/

97
98     MouseListener ml = new MouseAdapter() {
99         public void mouseClicked(MouseEvent e) {
100         int selRow = tree.getRowForLocation(e.getX(), e.getY());
101         TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
102         if(selRow != -1) {
103             if(e.getClickCount() == 1) {
104             ClassTreeNode node =
105                 (ClassTreeNode)selPath.getLastPathComponent();
106             // If user clicks on leaf, select it, and issue 'view' command.
107
if (node.isLeaf()) {
108                 tree.setSelectionPath(selPath);
109                 interpreter.executeCommand("view " + node.getReferenceTypeName());
110             }
111             }
112         }
113         }
114     };
115     tree.addMouseListener(ml);
116
117     JScrollPane treeView = new JScrollPane(tree);
118     add(treeView);
119
120     // Create listener.
121
ClassTreeToolListener listener = new ClassTreeToolListener();
122     runtime.addJDIListener(listener);
123     runtime.addSessionListener(listener);
124
125         //### remove listeners on exit!
126
}
127
128     private class ClassTreeToolListener extends JDIAdapter
129                        implements JDIListener, SessionListener {
130
131     // SessionListener
132

133     public void sessionStart(EventObject e) {
134         // Get system classes and any others loaded before attaching.
135
try {
136         Iterator iter = runtime.allClasses().iterator();
137         while (iter.hasNext()) {
138             ReferenceType type = ((ReferenceType)iter.next());
139             root.addClass(type);
140         }
141         } catch (VMDisconnectedException ee) {
142         // VM terminated unexpectedly.
143
} catch (NoSessionException ee) {
144         // Ignore. Should not happen.
145
}
146     }
147
148     public void sessionInterrupt(EventObject e) {}
149     public void sessionContinue(EventObject e) {}
150
151     // JDIListener
152

153         public void classPrepare(ClassPrepareEventSet e) {
154         root.addClass(e.getReferenceType());
155     }
156
157     public void classUnload(ClassUnloadEventSet e) {
158         root.removeClass(e.getClassName());
159     }
160
161         public void vmDisconnect(VMDisconnectEventSet e) {
162         // Clear contents of this view.
163
root = createClassTree(HEADING);
164         treeModel = new DefaultTreeModel(root);
165         tree.setModel(treeModel);
166     }
167     }
168
169     ClassTreeNode createClassTree(String JavaDoc label) {
170     return new ClassTreeNode(label, null);
171     }
172
173     class ClassTreeNode extends DefaultMutableTreeNode {
174
175     private String JavaDoc name;
176     private ReferenceType refTy; // null for package
177

178     ClassTreeNode(String JavaDoc name, ReferenceType refTy) {
179         this.name = name;
180         this.refTy = refTy;
181     }
182
183     public String JavaDoc toString() {
184         return name;
185     }
186
187     public ReferenceType getReferenceType() {
188         return refTy;
189     }
190     
191     public String JavaDoc getReferenceTypeName() {
192         return refTy.name();
193     }
194
195     private boolean isPackage() {
196         return (refTy == null);
197     }
198
199     public boolean isLeaf() {
200         return !isPackage();
201     }
202
203     public void addClass(ReferenceType refTy) {
204         addClass(refTy.name(), refTy);
205     }
206
207     private void addClass(String JavaDoc className, ReferenceType refTy) {
208         if (className.equals("")) {
209         return;
210         }
211         int pos = className.indexOf('.');
212         if (pos < 0) {
213         insertNode(className, refTy);
214         } else {
215         String JavaDoc head = className.substring(0, pos);
216         String JavaDoc tail = className.substring(pos + 1);
217         ClassTreeNode child = insertNode(head, null);
218         child.addClass(tail, refTy);
219         }
220     }
221
222     private ClassTreeNode insertNode(String JavaDoc name, ReferenceType refTy) {
223         for (int i = 0; i < getChildCount(); i++) {
224         ClassTreeNode child = (ClassTreeNode)getChildAt(i);
225         int cmp = name.compareTo(child.toString());
226         if (cmp == 0) {
227             // like-named node already exists
228
return child;
229         } else if (cmp < 0) {
230             // insert new node before the child
231
ClassTreeNode newChild = new ClassTreeNode(name, refTy);
232             treeModel.insertNodeInto(newChild, this, i);
233             return newChild;
234         }
235         }
236         // insert new node after last child
237
ClassTreeNode newChild = new ClassTreeNode(name, refTy);
238         treeModel.insertNodeInto(newChild, this, getChildCount());
239         return newChild;
240     }
241
242     public void removeClass(String JavaDoc className) {
243         if (className.equals("")) {
244         return;
245         }
246         int pos = className.indexOf('.');
247         if (pos < 0) {
248         ClassTreeNode child = findNode(className);
249         if (!isPackage()) {
250             treeModel.removeNodeFromParent(child);
251         }
252         } else {
253         String JavaDoc head = className.substring(0, pos);
254         String JavaDoc tail = className.substring(pos + 1);
255         ClassTreeNode child = findNode(head);
256         child.removeClass(tail);
257         if (isPackage() && child.getChildCount() < 1) {
258             // Prune non-leaf nodes with no children.
259
treeModel.removeNodeFromParent(child);
260         }
261         }
262     }
263
264     private ClassTreeNode findNode(String JavaDoc name) {
265         for (int i = 0; i < getChildCount(); i++) {
266         ClassTreeNode child = (ClassTreeNode)getChildAt(i);
267         int cmp = name.compareTo(child.toString());
268         if (cmp == 0) {
269             return child;
270         } else if (cmp > 0) {
271             // not found, since children are sorted
272
return null;
273         }
274         }
275         return null;
276     }
277     
278     }
279
280 }
281
Popular Tags