1 7 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 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 tree = new JTree(new DefaultTreeModel(root)); 79 tree.setSelectionModel(new SingleLeafTreeSelectionModel()); 80 81 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 (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 116 SourceTreeToolListener listener = new SourceTreeToolListener(); 117 sourceManager.addSourceListener(listener); 118 119 } 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 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 label) { 142 try { 143 return new SourceTreeNode(label, null, "", true); 144 } catch (SecurityException 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 name; 154 private boolean isDirectory; 155 private SourceTreeNode parent; 156 private SourceTreeNode[] children; 157 private String relativePath; 158 private boolean isExpanded; 159 160 private SourceTreeNode(String label, 161 SourceTreeNode parent, 162 String 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 toString() { 171 return name; 172 } 173 174 public String getRelativePath() { 175 return relativePath; 176 } 177 178 private void expandIfNeeded() { 179 try { 180 if (!isExpanded && isDirectory) { 181 String [] files = sourcePath.children(relativePath, filter); 182 children = new SourceTreeNode[files.length]; 183 for (int i = 0; i < files.length; i++) { 184 String 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 e) { 196 children = null; 197 env.failure("Cannot access source file or directory"); 198 } 199 } 200 201 203 207 public TreeNode getChildAt(int childIndex) { 208 expandIfNeeded(); 209 return children[childIndex]; 210 } 211 212 216 public int getChildCount() { 217 expandIfNeeded(); 218 return children.length; 219 } 220 221 224 public TreeNode getParent() { 225 return parent; 226 } 227 228 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 245 public boolean getAllowsChildren() { 246 return isDirectory; 247 } 248 249 252 public boolean isLeaf() { 253 expandIfNeeded(); 254 return !isDirectory; 255 } 256 257 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 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 |