1 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 root) { 55 tree.setModel(new DefaultTreeModel(new RootNode(new File(root)))); 57 } 58 59 private void showRunMenu(int x, int y, final String 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 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 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 name) { 121 return new File(dir, name).isDirectory(); 122 }}); 123 File[] javaFiles = file.listFiles(new FilenameFilter() { 124 public boolean accept(File dir, String 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 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 t) { 158 new ErrorLogger(ComponentRepository.getAJDebugger()).log(t); 159 } 160 } 161 162 public void leftMouseButton(MouseEvent e) { 163 String fullName; 164 try { 165 fullName = file.getCanonicalPath(); 166 } catch (IOException ee) { 167 fullName = file.getAbsolutePath(); 168 } 169 String relativePath = 170 ComponentRepository.getAJDebugger().removeSourcePath(fullName); 171 showSourceFile(relativePath); 172 } 173 174 public void rightMouseButton(MouseEvent e) { 175 String name = file.getName(); 176 int ijava = name.indexOf(".java"); 177 if (ijava != -1) { 178 name = name.substring(0, ijava); 179 } 180 String title = "run class " + name; 181 final String runCommand = "run " + name; 182 189 } 190 191 public String toString() { 192 return file.getName(); 193 } 194 } 195 196 class SourceTreeModel implements TreeModel { 197 198 private String root; 199 200 public SourceTreeModel(String 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 newValue) { 209 } 210 211 public Object getChild(Object parent, int index) { 212 if (parent instanceof File) return getChildren((File) parent)[index]; 213 return null; 214 } 215 216 public int getChildCount(Object parent) { 217 if (parent instanceof File) { 218 return getChildren((File) parent).length; 219 } 220 return -1; 221 } 222 223 public int getIndexOfChild(Object parent, Object 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 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 getRoot() { 240 return sourcePane.getSourceManager().getFile(root); 241 } 242 243 public boolean isLeaf(Object 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 name) { 259 return new File(dir, name).isDirectory(); 260 }}); 261 File[] javaFiles = file.listFiles(new FilenameFilter() { 262 public boolean accept(File dir, String 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 322 public static String d() { 323 return "Source Tree Pane"; 324 } 325 326 public String toString() { return d(); } 327 } 328 | Popular Tags |