KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > debugger > gui > ClassTreePane


1 /* -*- Mode: JDE; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the debugger and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  */

22
23 package org.aspectj.debugger.gui;
24
25 import org.aspectj.debugger.base.*;
26
27 import java.io.*;
28 import java.util.*;
29 import java.util.List JavaDoc;
30
31 import javax.swing.*;
32 import javax.swing.tree.*;
33 import javax.swing.event.*;
34 import java.awt.*;
35 import java.awt.event.*;
36
37 import com.sun.jdi.*;
38 import com.sun.jdi.event.*;
39
40 import org.aspectj.tools.ide.*;
41
42 public class ClassTreePane extends JPanel
43                            implements VMListener,
44                                       ClassListener
45 {
46
47     private JTree tree;
48     private DefaultTreeModel treeModel;
49     private RootNode root;
50     private GUIDebugger guiDebugger;
51
52     public ClassTreePane(GUIDebugger guiDebugger) {
53
54         super(new BorderLayout());
55
56         this.guiDebugger = guiDebugger;
57         guiDebugger.getDebugger().addVMListener(this);
58         guiDebugger.getDebugger().addClassListener(this);
59
60         root = new RootNode();
61         treeModel = new DefaultTreeModel(root);
62
63         tree = new AJTree(treeModel) {
64                 public void clear() {}
65                 public int getVisibleRowCount() { return getRowCount(); }
66             };
67         tree.getSelectionModel().setSelectionMode
68             (TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
69         tree.setCellRenderer(new AJTreeCellRenderer());
70         tree.addTreeSelectionListener(new ClassNodeSelectionListener(guiDebugger));
71         tree.addMouseListener(new ClassNodeMouseListener(guiDebugger, tree));
72         tree.addTreeWillExpandListener(new NoCollapsingRootExpansionListener(tree));
73         //ToolTipManager.sharedInstance().registerComponent(tree);
74

75         JScrollPane treeView = new JScrollPane(tree);
76         add(treeView);
77     }
78
79     public JTree getTree() {
80         return tree;
81     }
82
83     public DefaultTreeModel getTreeModel() {
84         return treeModel;
85     }
86
87     final static String JavaDoc stop = "stop in ";
88     void showMenu(int x, int y,
89                   final String JavaDoc cmd,
90                   final Vector advice,
91                   final Vector advisedMethods,
92                   final Vector methods,
93                   final boolean isAdvice) {
94         JPopupMenu menu = new JPopupMenu();
95         if (cmd != null && !cmd.equals("")) {
96             final String JavaDoc adviceOrMethod = isAdvice ? "advice" : "method";
97             menu.add(new AbstractAction("Break in " + adviceOrMethod) {
98                 public void actionPerformed(ActionEvent e) {
99                     ComponentRepository.getCommandLine().executeCommand(stop + cmd);
100             }});
101         }
102         if (advice != null && advice.size() > 0) {
103             menu.add(new AbstractAction("Break in all advice") {
104                 public void actionPerformed(ActionEvent e) {
105                     for (int i = 0; i < advice.size(); i++) {
106                         ComponentRepository.getCommandLine().executeCommand
107                             (stop + advice.get(i));
108                     }
109                 }});
110         }
111         if (methods != null && methods.size() > 0) {
112             menu.add(new AbstractAction("Break in all methods") {
113                 public void actionPerformed(ActionEvent e) {
114                     for (int i = 0; i < methods.size(); i++) {
115                         ComponentRepository.getCommandLine().executeCommand
116                             (stop + methods.get(i));
117                     }
118                 }});
119         }
120         if (advisedMethods != null && advisedMethods.size() > 0) {
121             menu.add(new AbstractAction("Break in all advised methods") {
122                 public void actionPerformed(ActionEvent e) {
123                     for (int i = 0; i < advisedMethods.size(); i++) {
124                         ComponentRepository.getCommandLine().executeCommand
125                             (stop + advisedMethods.get(i));
126                     }
127                 }});
128         }
129         if (methods != null && methods.size() > 0 &&
130             advice != null && advice.size() > 0) {
131             menu.add(new AbstractAction("Break in all advice & methods") {
132                 public void actionPerformed(ActionEvent e) {
133                     for (int i = 0; i < methods.size(); i++) {
134                         ComponentRepository.getCommandLine().executeCommand
135                             (stop + methods.get(i));
136                     }
137                     for (int i = 0; i < advice.size(); i++) {
138                         ComponentRepository.getCommandLine().executeCommand
139                             (stop + advice.get(i));
140                     }
141                 }});
142         }
143         if (methods != null && methods.size() > 0 &&
144             advice != null && advice.size() > 0 &&
145             advisedMethods != null && advisedMethods.size() > 0) {
146             menu.add(new AbstractAction
147                 ("Break in all advice, advised methods, and methods") {
148                 public void actionPerformed(ActionEvent e) {
149                     for (int i = 0; i < methods.size(); i++) {
150                         ComponentRepository.getCommandLine().executeCommand
151                             (stop + methods.get(i));
152                     }
153                     for (int i = 0; i < advice.size(); i++) {
154                         ComponentRepository.getCommandLine().executeCommand
155                             (stop + advice.get(i));
156                     }
157                     for (int i = 0; i < methods.size(); i++) {
158                         ComponentRepository.getCommandLine().executeCommand
159                             (stop + advisedMethods.get(i));
160                     }
161                 }});
162         }
163         menu.show(tree, x, y);
164     }
165
166     void showMultipleMenu(int x, int y, final Vector cmds) {
167         JPopupMenu menu = new JPopupMenu();
168         menu.add(new AbstractAction("Break in " + cmds.size() + " points") {
169             public void actionPerformed(ActionEvent e) {
170                 for (int i = 0; i < cmds.size(); i++) {
171                     ComponentRepository.getCommandLine().executeCommand
172                         (stop + cmds.get(i));
173                 }
174         }});
175         menu.show(tree, x, y);
176     }
177
178     public void loadAJCClasses(String JavaDoc newSourcePath) {
179         new AJCClassLoader().load(newSourcePath);
180     }
181
182     private static class AJCClassLoader {
183         public void load(String JavaDoc sourcePath) {
184             if (sourcePath == null || "".equals(sourcePath)) return;
185             SymbolManager sm = SymbolManager.getSymbolManager();
186             File file = new File(sourcePath);
187             if (file == null || !file.exists()) return;
188             if (file.isDirectory()) loadAJCClasses(file);
189             else loadAJCClasses(file.getParentFile());
190         }
191
192         private void loadAJCClasses(File dir) {
193             if (dir == null || !dir.exists() || !dir.isDirectory()) return;
194             File[] javaFiles = dir.listFiles(javaFilter());
195             for (int i = 0; i < javaFiles.length; i++) {
196                 loadAJCClass(javaFiles[i]);
197             }
198             File[] dirs = dir.listFiles(dirFilter());
199             for (int i = 0; i < dirs.length; i++) {
200                 loadAJCClasses(dirs[i]);
201             }
202         }
203
204         private void loadAJCClass(File file) {
205 // String outputFile = file.getAbsolutePath();
206
// System.out.println("outputFile=" + outputFile);
207
}
208
209         private static FileFilter dirFilter() {
210             return new FileFilter() {
211                 public boolean accept(File file) {
212                     return file.isDirectory();
213                 }
214             };
215         }
216
217
218         private static FileFilter javaFilter() {
219             return new FileFilter() {
220                 public boolean accept(File file) {
221                     return file.getName().indexOf("ajworkingdir") == -1 &&
222                            file.getName().endsWith(".java");
223                 }
224             };
225         }
226     }
227
228
229     protected void reset() {
230         root = new RootNode();
231         treeModel = new DefaultTreeModel(root);
232         tree.setModel(treeModel);
233     }
234
235     public void addClass(ReferenceType refType) {
236         boolean expand = root.getChildCount() == 0;
237         root.addClass(refType);
238         if (expand) tree.expandRow(0);
239     }
240
241     public void removeClass(String JavaDoc className) {
242         root.removeClass(className);
243     }
244
245     /* VMListener */
246     public void vmDeathEvent(VMDeathEvent e) {
247         reset();
248     }
249     public void vmDisconnectEvent(VMDisconnectEvent e) {
250         reset();
251     }
252     public void vmStartEvent(VMStartEvent e) {
253         reset();
254     }
255
256     /* ClassListener */
257     public void classPrepareEvent(ClassPrepareEvent e) {
258         addClass(e.referenceType());
259     }
260     public void classUnloadEvent(ClassUnloadEvent e) {
261         removeClass(e.className());
262     }
263     
264     MutableTreeNode insertNode(String JavaDoc name,
265                                MutableTreeNode parent,
266                                MutableTreeNode newChild) {
267         int N = parent.getChildCount();
268         for (int i = 0; i < N; i++) {
269             MutableTreeNode child = (MutableTreeNode) parent.getChildAt(i);
270             //XXX Hack until
271
String JavaDoc childString = child.toString();
272             int ispace = childString.indexOf(" ");
273             if (ispace != -1) {
274                 childString = childString.substring(0, ispace);
275             }
276             int cmp = name.compareTo(childString);
277             if (cmp == 0) {
278                 return child;
279             } else if (cmp < 0) {
280                 treeModel.insertNodeInto(newChild, parent, i);
281                 return newChild;
282             }
283         }
284         treeModel.insertNodeInto(newChild, parent, N);
285         if (parent == root) {
286             //System.out.println("expanding");
287
tree.expandPath(new TreePath(((DefaultMutableTreeNode)newChild).getPath()));
288         }
289         return newChild;
290     }
291
292     class RootNode extends AJTreeNode {
293         private int classCount = 0;
294
295         public RootNode() {
296             super(AJIcons.CLASSES_ICON);
297             setUserObject("Classes");
298         }
299
300         public boolean isOKToExpand() {
301             return true;
302         }
303
304         public ClassNode addClass(ReferenceType refType) {
305             return addClass(refType.name(), refType);
306         }
307
308         public ClassNode addClass(String JavaDoc name) {
309             return addClass(name, null);
310         }
311
312         public ClassNode removeClass(ReferenceType refType) {
313             return null;
314         }
315
316         public ClassNode removeClass(String JavaDoc className) {
317             return null;
318         }
319
320         protected ClassNode addClass(String JavaDoc name, ReferenceType refType) {
321             incClassCount();
322             int idot = name.indexOf(".");
323             if (idot == -1) {
324                 ClassNode classNode = ClassNodeFactory.classNode(name, refType);
325                 classNode.setAlwaysExpand(true);
326                 ClassTreePane.this.insertNode(name, this, classNode);
327                 return classNode;
328             } else {
329                 String JavaDoc head = name.substring(0, idot);
330                 String JavaDoc tail = name.substring(idot+1);
331                 return ((RootNode)ClassTreePane.this.insertNode
332                         (head, this, new PackageNode(head))).addClass(tail, refType);
333             }
334         }
335
336         public int getClassCount() {
337             return classCount;
338         }
339
340         public void incClassCount() {
341             changeClassCount(1);
342         }
343
344         public void decClassCount() {
345             changeClassCount(-1);
346         }
347
348         private void changeClassCount(int delta) {
349             classCount += delta;
350             try {
351                 getTreeModel().nodeChanged(this);
352             } catch (ArrayIndexOutOfBoundsException JavaDoc aioobe) {
353             }
354         }
355
356     }
357
358     class PackageNode extends RootNode {
359         private String JavaDoc name;
360
361         public PackageNode(String JavaDoc name) {
362             super();
363             setType(AJIcons.PACKAGE_ICON);
364             setUserObject(this.name = name);
365         }
366     }
367
368     public String JavaDoc toString() { return d(); }
369     public static String JavaDoc d() {
370         return "Class Tree Pane";
371     }
372 }
373
Popular Tags