KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)SourceTreeTool.java 1.10 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.bdi.*;
48
49 public class SourceTreeTool extends JPanel {
50
51     private Environment env;
52
53     private ExecutionManager runtime;
54     private SourceManager sourceManager;
55     private ClassManager classManager;
56
57     private JTree tree;
58     private SourceTreeNode root;
59     private SearchPath sourcePath;
60     private CommandInterpreter interpreter;
61
62     private static String JavaDoc HEADING = "SOURCES";
63
64     public SourceTreeTool(Environment env) {
65
66     super(new BorderLayout());
67
68     this.env = env;
69     this.runtime = env.getExecutionManager();
70     this.sourceManager = env.getSourceManager();
71
72     this.interpreter = new CommandInterpreter(env);
73
74     sourcePath = sourceManager.getSourcePath();
75     root = createDirectoryTree(HEADING);
76     
77         // Create a tree that allows one selection at a time.
78
tree = new JTree(new DefaultTreeModel(root));
79     tree.setSelectionModel(new SingleLeafTreeSelectionModel());
80
81     /******
82         // Listen for when the selection changes.
83         tree.addTreeSelectionListener(new TreeSelectionListener() {
84             public void valueChanged(TreeSelectionEvent e) {
85                 SourceTreeNode node = (SourceTreeNode)
86             (e.getPath().getLastPathComponent());
87         interpreter.executeCommand("view " + node.getRelativePath());
88         }
89     });
90     ******/

91
92     MouseListener ml = new MouseAdapter() {
93         public void mouseClicked(MouseEvent e) {
94         int selRow = tree.getRowForLocation(e.getX(), e.getY());
95         TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
96         if(selRow != -1) {
97             if(e.getClickCount() == 1) {
98             SourceTreeNode node =
99                 (SourceTreeNode)selPath.getLastPathComponent();
100             // If user clicks on leaf, select it, and issue 'view' command.
101
if (node.isLeaf()) {
102                 tree.setSelectionPath(selPath);
103                 interpreter.executeCommand("view " + node.getRelativePath());
104             }
105             }
106         }
107         }
108     };
109     tree.addMouseListener(ml);
110
111     JScrollPane treeView = new JScrollPane(tree);
112     add(treeView);
113
114     // Create listener for source path changes.
115

116     SourceTreeToolListener listener = new SourceTreeToolListener();
117     sourceManager.addSourceListener(listener);
118
119         //### remove listeners on exit!
120
}
121
122     private class SourceTreeToolListener implements SourceListener {
123
124     public void sourcepathChanged(SourcepathChangedEvent e) {
125             sourcePath = sourceManager.getSourcePath();
126         root = createDirectoryTree(HEADING);
127         tree.setModel(new DefaultTreeModel(root));
128     }
129     
130     }
131
132     private static class SourceOrDirectoryFilter implements FilenameFilter {
133     public boolean accept(File dir, String JavaDoc name) {
134         return (name.endsWith(".java") ||
135             new File(dir, name).isDirectory());
136     }
137     }
138
139     private static FilenameFilter filter = new SourceOrDirectoryFilter();
140
141     SourceTreeNode createDirectoryTree(String JavaDoc label) {
142     try {
143         return new SourceTreeNode(label, null, "", true);
144     } catch (SecurityException JavaDoc e) {
145         env.failure("Cannot access source file or directory");
146         return null;
147     }
148     }
149
150
151     class SourceTreeNode implements TreeNode {
152     
153     private String JavaDoc name;
154     private boolean isDirectory;
155     private SourceTreeNode parent;
156     private SourceTreeNode[] children;
157     private String JavaDoc relativePath;
158     private boolean isExpanded;
159     
160     private SourceTreeNode(String JavaDoc label,
161                    SourceTreeNode parent,
162                    String JavaDoc relativePath,
163                    boolean isDirectory) {
164         this.name = label;
165         this.relativePath = relativePath;
166         this.parent = parent;
167         this.isDirectory = isDirectory;
168     }
169
170     public String JavaDoc toString() {
171         return name;
172     }
173     
174     public String JavaDoc getRelativePath() {
175         return relativePath;
176     }
177     
178     private void expandIfNeeded() {
179         try {
180         if (!isExpanded && isDirectory) {
181             String JavaDoc[] files = sourcePath.children(relativePath, filter);
182             children = new SourceTreeNode[files.length];
183             for (int i = 0; i < files.length; i++) {
184             String JavaDoc childName =
185                 (relativePath.equals(""))
186                 ? files[i]
187                 : relativePath + File.separator + files[i];
188             File file = sourcePath.resolve(childName);
189             boolean isDir = (file != null && file.isDirectory());
190             children[i] =
191                 new SourceTreeNode(files[i], this, childName, isDir);
192             }
193         }
194         isExpanded = true;
195         } catch (SecurityException JavaDoc e) {
196         children = null;
197         env.failure("Cannot access source file or directory");
198         }
199     }
200     
201     // -- interface TreeNode --
202

203     /*
204      * Returns the child <code>TreeNode</code> at index
205      * <code>childIndex</code>.
206      */

207     public TreeNode getChildAt(int childIndex) {
208         expandIfNeeded();
209         return children[childIndex];
210     }
211     
212     /**
213      * Returns the number of children <code>TreeNode</code>s the receiver
214      * contains.
215      */

216     public int getChildCount() {
217         expandIfNeeded();
218         return children.length;
219     }
220     
221     /**
222      * Returns the parent <code>TreeNode</code> of the receiver.
223      */

224     public TreeNode getParent() {
225         return parent;
226     }
227     
228     /**
229      * Returns the index of <code>node</code> in the receivers children.
230      * If the receiver does not contain <code>node</code>, -1 will be
231      * returned.
232      */

233     public int getIndex(TreeNode node) {
234         expandIfNeeded();
235         for (int i = 0; i < children.length; i++) {
236         if (children[i] == node)
237             return i;
238         }
239         return -1;
240     }
241     
242     /**
243      * Returns true if the receiver allows children.
244      */

245     public boolean getAllowsChildren() {
246         return isDirectory;
247     }
248     
249     /**
250      * Returns true if the receiver is a leaf.
251      */

252     public boolean isLeaf() {
253         expandIfNeeded();
254         return !isDirectory;
255     }
256     
257     /**
258      * Returns the children of the receiver as an Enumeration.
259      */

260     public Enumeration children() {
261         expandIfNeeded();
262         return new Enumeration() {
263         int i = 0;
264         public boolean hasMoreElements() {
265             return (i < children.length);
266         }
267         public Object JavaDoc nextElement() throws NoSuchElementException {
268             if (i >= children.length) {
269             throw new NoSuchElementException();
270             }
271             return children[i++];
272         }
273         };
274     }
275
276     }
277
278 }
279
Popular Tags