KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.aspectj.debugger.gui;
23
24 import org.aspectj.debugger.base.*;
25 import java.io.*;
26 import java.util.*;
27 import javax.swing.*;
28 import javax.swing.tree.*;
29 import javax.swing.event.*;
30 import java.awt.*;
31 import java.awt.event.*;
32 import com.sun.jdi.*;
33
34 public class SourceTreePane extends JPanel {
35
36     private AbstractSourcePane sourcePane;
37     private JTree tree;
38     protected GUIDebugger guiDebugger;
39
40     public SourceTreePane(GUIDebugger guiDebugger, AbstractSourcePane sourcePane) {
41         super(new BorderLayout());
42         this.sourcePane = sourcePane;
43         this.guiDebugger = guiDebugger;
44         tree = new JTree();
45         setRoot(sourcePane.getSourcePath());
46         tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
47         tree.addMouseListener(new SourceTreePaneMouseListener(tree));
48         tree.addTreeSelectionListener(new ClassNodeSelectionListener(guiDebugger));
49         tree.setCellRenderer(new AJTreeCellRenderer());
50         JScrollPane treeView = new JScrollPane(tree);
51         add(treeView);
52     }
53
54     public void setRoot(String JavaDoc root) {
55 // tree.setModel(new SourceTreeModel(root));
56
tree.setModel(new DefaultTreeModel(new RootNode(new File(root))));
57     }
58
59     private void showRunMenu(int x, int y, final String JavaDoc pathString) {
60         showMenu(x, y, new AbstractAction("Debug class " + pathString) {
61             public void actionPerformed(ActionEvent e) {
62                 ComponentRepository.getGUIDebugger().executeCommand("run " + pathString);
63         }});
64     }
65
66     private void showUseMenu(int x, int y) {
67         showMenu(x, y, new AbstractAction("Change the source path") {
68             public void actionPerformed(ActionEvent e) {
69                 Dialogs.showUseDialog();
70         }});
71     }
72
73     private void showMenu(int x, int y, AbstractAction a) {
74         JPopupMenu menu = new JPopupMenu();
75         menu.add(a);
76         menu.show(tree, x, y);
77     }
78
79     void showSourceFile(String JavaDoc relativePath) {
80       sourcePane.showSource(relativePath);
81     }
82
83     class RootNode extends FileNode {
84         public RootNode(File file) {
85             super(file);
86         }
87
88         public String JavaDoc toString() {
89             return file + "";
90         }
91
92         public void rightMouseButton(MouseEvent e) {
93             showUseMenu(e.getX(), e.getY());
94         }
95     }
96
97     class FileNode extends AJTreeNode {
98
99         protected File file;
100
101         public FileNode(File file) {
102             super();
103             setUserObject(this.file = file);
104             if (file != null) {
105                 if (file.isDirectory()) {
106                     addFiles();
107                 } else if (!guiDebugger.getDebugger().getOptions().isSet("noajcclasses")){
108                     addClasses();
109                 }
110             }
111         }
112
113         public int getType() {
114             if (file.isDirectory()) return AJIcons.FOLDER_ICON;
115             return AJIcons.DOCUMENT_ICON;
116         }
117
118         private void addFiles() {
119             File[] dirs = file.listFiles(new FilenameFilter() {
120                     public boolean accept(File dir, String JavaDoc name) {
121                         return new File(dir, name).isDirectory();
122                     }});
123             File[] javaFiles = file.listFiles(new FilenameFilter() {
124                     public boolean accept(File dir, String JavaDoc name) {
125                         return name.endsWith(".java");
126                     }});
127             File[] files;
128             if (javaFiles == null || dirs == null) {
129                 files = new File[0];
130             } else {
131                 files = new File[dirs.length + javaFiles.length];
132                 System.arraycopy(dirs, 0, files, 0, dirs.length);
133                 System.arraycopy(javaFiles, 0, files, dirs.length, javaFiles.length);
134             }
135             addFiles(files);
136         }
137
138         private void addFiles(File[] files) {
139             for (int i = 0; i < files.length; i++) {
140                 add(new FileNode(files[i]));
141             }
142         }
143
144         private void addClasses() {
145             try {
146             String JavaDoc fullName;
147             try {
148                 fullName = file.getCanonicalPath();
149             } catch (IOException e) {
150                 fullName = file.getAbsolutePath();
151             }
152             if (fullName == null) return;
153             AJCClassNode[] nodes = AJCClassNode.makeNodes(fullName);
154             for (int i = 0; i < nodes.length; i++) {
155                 add(nodes[i]);
156             }
157             } catch (Throwable JavaDoc t) {
158                 new ErrorLogger(ComponentRepository.getAJDebugger()).log(t);
159             }
160         }
161
162         public void leftMouseButton(MouseEvent e) {
163             String JavaDoc fullName;
164             try {
165                 fullName = file.getCanonicalPath();
166             } catch (IOException ee) {
167                 fullName = file.getAbsolutePath();
168             }
169             String JavaDoc relativePath =
170                 ComponentRepository.getAJDebugger().removeSourcePath(fullName);
171             showSourceFile(relativePath);
172         }
173
174         public void rightMouseButton(MouseEvent e) {
175             String JavaDoc name = file.getName();
176             int ijava = name.indexOf(".java");
177             if (ijava != -1) {
178                 name = name.substring(0, ijava);
179             }
180             String JavaDoc title = "run class " + name;
181             final String JavaDoc runCommand = "run " + name;
182             //!!! Do it later
183
// showMenu(e.getX(), e.getY(), new AbstractAction(title) {
184
// public void actionPerformed(ActionEvent ae) {
185
// sourcePane.debugger.execute(runCommand);
186
// }
187
// });
188

189         }
190
191         public String JavaDoc toString() {
192             return file.getName();
193         }
194     }
195
196     class SourceTreeModel implements TreeModel {
197
198         private String JavaDoc root;
199
200         public SourceTreeModel(String JavaDoc root) {
201             this.root = root;
202         }
203
204         public void addTreeModelListener(TreeModelListener l) {
205         }
206         public void removeTreeModelListener(TreeModelListener l) {
207         }
208         public void valueForPathChanged(TreePath path, Object JavaDoc newValue) {
209         }
210
211         public Object JavaDoc getChild(Object JavaDoc parent, int index) {
212             if (parent instanceof File) return getChildren((File) parent)[index];
213             return null;
214         }
215
216         public int getChildCount(Object JavaDoc parent) {
217             if (parent instanceof File) {
218                 return getChildren((File) parent).length;
219             }
220             return -1;
221         }
222
223         public int getIndexOfChild(Object JavaDoc parent, Object JavaDoc child) {
224             if (parent instanceof File) {
225                 return getIndexOfChild(getChildren((File) parent), child);
226             }
227             return -1;
228         }
229
230         private int getIndexOfChild(File[] kids, Object JavaDoc child) {
231             for (int i = 0; i < kids.length; i++) {
232                 if (kids[i].equals(child)) {
233                     return i;
234                 }
235             }
236             return -1;
237         }
238
239         public Object JavaDoc getRoot() {
240             return sourcePane.getSourceManager().getFile(root);
241         }
242
243         public boolean isLeaf(Object JavaDoc node) {
244             return !((File) node).isDirectory();
245
246         }
247
248         private ClassNode[] getClassNodes(File file) {
249             try {
250                 return AJCClassNode.makeNodes(file.getCanonicalPath());
251             } catch (IOException ioe) {
252             }
253             return new ClassNode[0];
254         }
255
256         private File[] getChildren(File file) {
257             File[] dirs = file.listFiles(new FilenameFilter() {
258                     public boolean accept(File dir, String JavaDoc name) {
259                         return new File(dir, name).isDirectory();
260                     }});
261             File[] javaFiles = file.listFiles(new FilenameFilter() {
262                     public boolean accept(File dir, String JavaDoc name) {
263                         return name.endsWith(".java");
264                     }});
265             File[] files;
266             if (javaFiles == null || dirs == null) {
267                 files = new File[0];
268             } else {
269                 files = new File[dirs.length + javaFiles.length];
270                 System.arraycopy(dirs, 0, files, 0, dirs.length);
271                 System.arraycopy(javaFiles, 0, files, dirs.length, javaFiles.length);
272             }
273             return files;
274         }
275     }
276
277     class SourceTreePaneMouseListener extends ClassNodeMouseListener {
278
279         public SourceTreePaneMouseListener(JTree tree) {
280             super(SourceTreePane.this.guiDebugger, tree);
281         }
282
283         public void mouseClicked(MouseEvent e) {
284
285             int x = e.getX();
286             int y = e.getY();
287             int selRow = tree().getRowForLocation(x, y);
288             TreePath selPath = tree().getPathForLocation(x, y);
289             if (selPath == null) return;
290             AJTreeNode node = (AJTreeNode) selPath.getLastPathComponent();
291             if (node instanceof FileNode) {
292                 node.mouseEvent(e);
293                 return;
294             }
295             super.mouseClicked(e);
296         }
297     }
298
299 // class SourceTreePaneTreeCellRenderer extends DefaultTreeCellRenderer {
300
// public Component getTreeCellRendererComponent(JTree tree,
301
// Object value,
302
// boolean sel,
303
// boolean expanded,
304
// boolean leaf,
305
// int row,
306
// boolean hasFocus) {
307
// super.getTreeCellRendererComponent(tree, value, sel, expanded,
308
// leaf, row, hasFocus);
309
// File file = (File) value;
310
// if (row != 0) {
311
// setText(file.getName());
312
// }
313
// if (file.isDirectory()) {
314
// setIcon(AJIcons.getIcon(AJIcons.FOLDER_ICON));
315
// } else {
316
// setIcon(AJIcons.getIcon(AJIcons.DOCUMENT_ICON));
317
// }
318
// return this;
319
// }
320
// }
321

322     public static String JavaDoc d() {
323         return "Source Tree Pane";
324     }
325
326     public String JavaDoc toString() { return d(); }
327 }
328
Popular Tags