1 /* 2 * Copyright (c) 1998, 1999, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.tools.example.debug.gui; 27 28 import java.io.*; 29 import java.util.*; 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.tools.example.debug.event.*; 39 import com.sun.tools.example.debug.bdi.*; 40 41 public class ClassTreeTool extends JPanel { 42 43 private Environment env; 44 45 private ExecutionManager runtime; 46 private SourceManager sourceManager; 47 private ClassManager classManager; 48 49 private JTree tree; 50 private DefaultTreeModel treeModel; 51 private ClassTreeNode root; 52 private SearchPath sourcePath; 53 54 private CommandInterpreter interpreter; 55 56 private static String HEADING = "CLASSES"; 57 58 public ClassTreeTool(Environment env) { 59 60 super(new BorderLayout()); 61 62 this.env = env; 63 this.runtime = env.getExecutionManager(); 64 this.sourceManager = env.getSourceManager(); 65 66 this.interpreter = new CommandInterpreter(env); 67 68 root = createClassTree(HEADING); 69 treeModel = new DefaultTreeModel(root); 70 71 // Create a tree that allows one selection at a time. 72 73 tree = new JTree(treeModel); 74 tree.setSelectionModel(new SingleLeafTreeSelectionModel()); 75 76 /****** 77 // Listen for when the selection changes. 78 tree.addTreeSelectionListener(new TreeSelectionListener() { 79 public void valueChanged(TreeSelectionEvent e) { 80 ClassTreeNode node = (ClassTreeNode) 81 (e.getPath().getLastPathComponent()); 82 if (node != null) { 83 interpreter.executeCommand("view " + node.getReferenceTypeName()); 84 } 85 } 86 }); 87 ******/ 88 89 MouseListener ml = new MouseAdapter() { 90 public void mouseClicked(MouseEvent e) { 91 int selRow = tree.getRowForLocation(e.getX(), e.getY()); 92 TreePath selPath = tree.getPathForLocation(e.getX(), e.getY()); 93 if(selRow != -1) { 94 if(e.getClickCount() == 1) { 95 ClassTreeNode node = 96 (ClassTreeNode)selPath.getLastPathComponent(); 97 // If user clicks on leaf, select it, and issue 'view' command. 98 if (node.isLeaf()) { 99 tree.setSelectionPath(selPath); 100 interpreter.executeCommand("view " + node.getReferenceTypeName()); 101 } 102 } 103 } 104 } 105 }; 106 tree.addMouseListener(ml); 107 108 JScrollPane treeView = new JScrollPane(tree); 109 add(treeView); 110 111 // Create listener. 112 ClassTreeToolListener listener = new ClassTreeToolListener(); 113 runtime.addJDIListener(listener); 114 runtime.addSessionListener(listener); 115 116 //### remove listeners on exit! 117 } 118 119 private class ClassTreeToolListener extends JDIAdapter 120 implements JDIListener, SessionListener { 121 122 // SessionListener 123 124 public void sessionStart(EventObject e) { 125 // Get system classes and any others loaded before attaching. 126 try { 127 for (ReferenceType type : runtime.allClasses()) { 128 root.addClass(type); 129 } 130 } catch (VMDisconnectedException ee) { 131 // VM terminated unexpectedly. 132 } catch (NoSessionException ee) { 133 // Ignore. Should not happen. 134 } 135 } 136 137 public void sessionInterrupt(EventObject e) {} 138 public void sessionContinue(EventObject e) {} 139 140 // JDIListener 141 142 public void classPrepare(ClassPrepareEventSet e) { 143 root.addClass(e.getReferenceType()); 144 } 145 146 public void classUnload(ClassUnloadEventSet e) { 147 root.removeClass(e.getClassName()); 148 } 149 150 public void vmDisconnect(VMDisconnectEventSet e) { 151 // Clear contents of this view. 152 root = createClassTree(HEADING); 153 treeModel = new DefaultTreeModel(root); 154 tree.setModel(treeModel); 155 } 156 } 157 158 ClassTreeNode createClassTree(String label) { 159 return new ClassTreeNode(label, null); 160 } 161 162 class ClassTreeNode extends DefaultMutableTreeNode { 163 164 private String name; 165 private ReferenceType refTy; // null for package 166 167 ClassTreeNode(String name, ReferenceType refTy) { 168 this.name = name; 169 this.refTy = refTy; 170 } 171 172 public String toString() { 173 return name; 174 } 175 176 public ReferenceType getReferenceType() { 177 return refTy; 178 } 179 180 public String getReferenceTypeName() { 181 return refTy.name(); 182 } 183 184 private boolean isPackage() { 185 return (refTy == null); 186 } 187 188 public boolean isLeaf() { 189 return !isPackage(); 190 } 191 192 public void addClass(ReferenceType refTy) { 193 addClass(refTy.name(), refTy); 194 } 195 196 private void addClass(String className, ReferenceType refTy) { 197 if (className.equals("")) { 198 return; 199 } 200 int pos = className.indexOf('.'); 201 if (pos < 0) { 202 insertNode(className, refTy); 203 } else { 204 String head = className.substring(0, pos); 205 String tail = className.substring(pos + 1); 206 ClassTreeNode child = insertNode(head, null); 207 child.addClass(tail, refTy); 208 } 209 } 210 211 private ClassTreeNode insertNode(String name, ReferenceType refTy) { 212 for (int i = 0; i < getChildCount(); i++) { 213 ClassTreeNode child = (ClassTreeNode)getChildAt(i); 214 int cmp = name.compareTo(child.toString()); 215 if (cmp == 0) { 216 // like-named node already exists 217 return child; 218 } else if (cmp < 0) { 219 // insert new node before the child 220 ClassTreeNode newChild = new ClassTreeNode(name, refTy); 221 treeModel.insertNodeInto(newChild, this, i); 222 return newChild; 223 } 224 } 225 // insert new node after last child 226 ClassTreeNode newChild = new ClassTreeNode(name, refTy); 227 treeModel.insertNodeInto(newChild, this, getChildCount()); 228 return newChild; 229 } 230 231 public void removeClass(String className) { 232 if (className.equals("")) { 233 return; 234 } 235 int pos = className.indexOf('.'); 236 if (pos < 0) { 237 ClassTreeNode child = findNode(className); 238 if (!isPackage()) { 239 treeModel.removeNodeFromParent(child); 240 } 241 } else { 242 String head = className.substring(0, pos); 243 String tail = className.substring(pos + 1); 244 ClassTreeNode child = findNode(head); 245 child.removeClass(tail); 246 if (isPackage() && child.getChildCount() < 1) { 247 // Prune non-leaf nodes with no children. 248 treeModel.removeNodeFromParent(child); 249 } 250 } 251 } 252 253 private ClassTreeNode findNode(String name) { 254 for (int i = 0; i < getChildCount(); i++) { 255 ClassTreeNode child = (ClassTreeNode)getChildAt(i); 256 int cmp = name.compareTo(child.toString()); 257 if (cmp == 0) { 258 return child; 259 } else if (cmp > 0) { 260 // not found, since children are sorted 261 return null; 262 } 263 } 264 return null; 265 } 266 267 } 268 269 }