Showing the changes to java/pkg/com/sun/tools/example/debug/gui/ClassTreeTool.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,278 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 1998, 2008, 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.util.*; | |
| 29 | + | |
| 30 | +import javax.swing.*; | |
| 31 | +import javax.swing.tree.*; | |
| 32 | +import java.awt.*; | |
| 33 | +import java.awt.event.*; | |
| 34 | + | |
| 35 | +import com.sun.jdi.*; | |
| 36 | +import com.sun.tools.example.debug.event.*; | |
| 37 | +import com.sun.tools.example.debug.bdi.*; | |
| 38 | + | |
| 39 | +public class ClassTreeTool extends JPanel { | |
| 40 | + | |
| 41 | + private static final long serialVersionUID = 526178912591739259L; | |
| 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 | + @Override | |
| 91 | + public void mouseClicked(MouseEvent e) { | |
| 92 | + int selRow = tree.getRowForLocation(e.getX(), e.getY()); | |
| 93 | + TreePath selPath = tree.getPathForLocation(e.getX(), e.getY()); | |
| 94 | + if(selRow != -1) { | |
| 95 | + if(e.getClickCount() == 1) { | |
| 96 | + ClassTreeNode node = | |
| 97 | + (ClassTreeNode)selPath.getLastPathComponent(); | |
| 98 | + // If user clicks on leaf, select it, and issue 'view' command. | |
| 99 | + if (node.isLeaf()) { | |
| 100 | + tree.setSelectionPath(selPath); | |
| 101 | + interpreter.executeCommand("view " + node.getReferenceTypeName()); | |
| 102 | + } | |
| 103 | + } | |
| 104 | + } | |
| 105 | + } | |
| 106 | + }; | |
| 107 | + tree.addMouseListener(ml); | |
| 108 | + | |
| 109 | + JScrollPane treeView = new JScrollPane(tree); | |
| 110 | + add(treeView); | |
| 111 | + | |
| 112 | + // Create listener. | |
| 113 | + ClassTreeToolListener listener = new ClassTreeToolListener(); | |
| 114 | + runtime.addJDIListener(listener); | |
| 115 | + runtime.addSessionListener(listener); | |
| 116 | + | |
| 117 | + //### remove listeners on exit! | |
| 118 | + } | |
| 119 | + | |
| 120 | + private class ClassTreeToolListener extends JDIAdapter | |
| 121 | + implements JDIListener, SessionListener { | |
| 122 | + | |
| 123 | + // SessionListener | |
| 124 | + | |
| 125 | + @Override | |
| 126 | + public void sessionStart(EventObject e) { | |
| 127 | + // Get system classes and any others loaded before attaching. | |
| 128 | + try { | |
| 129 | + for (ReferenceType type : runtime.allClasses()) { | |
| 130 | + root.addClass(type); | |
| 131 | + } | |
| 132 | + } catch (VMDisconnectedException ee) { | |
| 133 | + // VM terminated unexpectedly. | |
| 134 | + } catch (NoSessionException ee) { | |
| 135 | + // Ignore. Should not happen. | |
| 136 | + } | |
| 137 | + } | |
| 138 | + | |
| 139 | + @Override | |
| 140 | + public void sessionInterrupt(EventObject e) {} | |
| 141 | + @Override | |
| 142 | + public void sessionContinue(EventObject e) {} | |
| 143 | + | |
| 144 | + // JDIListener | |
| 145 | + | |
| 146 | + @Override | |
| 147 | + public void classPrepare(ClassPrepareEventSet e) { | |
| 148 | + root.addClass(e.getReferenceType()); | |
| 149 | + } | |
| 150 | + | |
| 151 | + @Override | |
| 152 | + public void classUnload(ClassUnloadEventSet e) { | |
| 153 | + root.removeClass(e.getClassName()); | |
| 154 | + } | |
| 155 | + | |
| 156 | + @Override | |
| 157 | + public void vmDisconnect(VMDisconnectEventSet e) { | |
| 158 | + // Clear contents of this view. | |
| 159 | + root = createClassTree(HEADING); | |
| 160 | + treeModel = new DefaultTreeModel(root); | |
| 161 | + tree.setModel(treeModel); | |
| 162 | + } | |
| 163 | + } | |
| 164 | + | |
| 165 | + ClassTreeNode createClassTree(String label) { | |
| 166 | + return new ClassTreeNode(label, null); | |
| 167 | + } | |
| 168 | + | |
| 169 | + class ClassTreeNode extends DefaultMutableTreeNode { | |
| 170 | + | |
| 171 | + private String name; | |
| 172 | + private ReferenceType refTy; // null for package | |
| 173 | + | |
| 174 | + ClassTreeNode(String name, ReferenceType refTy) { | |
| 175 | + this.name = name; | |
| 176 | + this.refTy = refTy; | |
| 177 | + } | |
| 178 | + | |
| 179 | + @Override | |
| 180 | + public String toString() { | |
| 181 | + return name; | |
| 182 | + } | |
| 183 | + | |
| 184 | + public ReferenceType getReferenceType() { | |
| 185 | + return refTy; | |
| 186 | + } | |
| 187 | + | |
| 188 | + public String getReferenceTypeName() { | |
| 189 | + return refTy.name(); | |
| 190 | + } | |
| 191 | + | |
| 192 | + private boolean isPackage() { | |
| 193 | + return (refTy == null); | |
| 194 | + } | |
| 195 | + | |
| 196 | + @Override | |
| 197 | + public boolean isLeaf() { | |
| 198 | + return !isPackage(); | |
| 199 | + } | |
| 200 | + | |
| 201 | + public void addClass(ReferenceType refTy) { | |
| 202 | + addClass(refTy.name(), refTy); | |
| 203 | + } | |
| 204 | + | |
| 205 | + private void addClass(String className, ReferenceType refTy) { | |
| 206 | + if (className.equals("")) { | |
| 207 | + return; | |
| 208 | + } | |
| 209 | + int pos = className.indexOf('.'); | |
| 210 | + if (pos < 0) { | |
| 211 | + insertNode(className, refTy); | |
| 212 | + } else { | |
| 213 | + String head = className.substring(0, pos); | |
| 214 | + String tail = className.substring(pos + 1); | |
| 215 | + ClassTreeNode child = insertNode(head, null); | |
| 216 | + child.addClass(tail, refTy); | |
| 217 | + } | |
| 218 | + } | |
| 219 | + | |
| 220 | + private ClassTreeNode insertNode(String name, ReferenceType refTy) { | |
| 221 | + for (int i = 0; i < getChildCount(); i++) { | |
| 222 | + ClassTreeNode child = (ClassTreeNode)getChildAt(i); | |
| 223 | + int cmp = name.compareTo(child.toString()); | |
| 224 | + if (cmp == 0) { | |
| 225 | + // like-named node already exists | |
| 226 | + return child; | |
| 227 | + } else if (cmp < 0) { | |
| 228 | + // insert new node before the child | |
| 229 | + ClassTreeNode newChild = new ClassTreeNode(name, refTy); | |
| 230 | + treeModel.insertNodeInto(newChild, this, i); | |
| 231 | + return newChild; | |
| 232 | + } | |
| 233 | + } | |
| 234 | + // insert new node after last child | |
| 235 | + ClassTreeNode newChild = new ClassTreeNode(name, refTy); | |
| 236 | + treeModel.insertNodeInto(newChild, this, getChildCount()); | |
| 237 | + return newChild; | |
| 238 | + } | |
| 239 | + | |
| 240 | + public void removeClass(String className) { | |
| 241 | + if (className.equals("")) { | |
| 242 | + return; | |
| 243 | + } | |
| 244 | + int pos = className.indexOf('.'); | |
| 245 | + if (pos < 0) { | |
| 246 | + ClassTreeNode child = findNode(className); | |
| 247 | + if (!isPackage()) { | |
| 248 | + treeModel.removeNodeFromParent(child); | |
| 249 | + } | |
| 250 | + } else { | |
| 251 | + String head = className.substring(0, pos); | |
| 252 | + String tail = className.substring(pos + 1); | |
| 253 | + ClassTreeNode child = findNode(head); | |
| 254 | + child.removeClass(tail); | |
| 255 | + if (isPackage() && child.getChildCount() < 1) { | |
| 256 | + // Prune non-leaf nodes with no children. | |
| 257 | + treeModel.removeNodeFromParent(child); | |
| 258 | + } | |
| 259 | + } | |
| 260 | + } | |
| 261 | + | |
| 262 | + private ClassTreeNode findNode(String name) { | |
| 263 | + for (int i = 0; i < getChildCount(); i++) { | |
| 264 | + ClassTreeNode child = (ClassTreeNode)getChildAt(i); | |
| 265 | + int cmp = name.compareTo(child.toString()); | |
| 266 | + if (cmp == 0) { | |
| 267 | + return child; | |
| 268 | + } else if (cmp > 0) { | |
| 269 | + // not found, since children are sorted | |
| 270 | + return null; | |
| 271 | + } | |
| 272 | + } | |
| 273 | + return null; | |
| 274 | + } | |
| 275 | + | |
| 276 | + } | |
| 277 | + | |
| 278 | +} | |