Showing the changes to java/pkg/com/sun/tools/example/debug/gui/SourceTreeTool.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,283 @@ | |||
| 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 java.awt.*; | ||
| 34 | import java.awt.event.*; | ||
| 35 | |||
| 36 | import com.sun.tools.example.debug.bdi.*; | ||
| 37 | |||
| 38 | public class SourceTreeTool extends JPanel { | ||
| 39 | |||
| 40 | private static final long serialVersionUID = 3336680912107956419L; | ||
| 41 | |||
| 42 | private Environment env; | ||
| 43 | |||
| 44 | private ExecutionManager runtime; | ||
| 45 | private SourceManager sourceManager; | ||
| 46 | private ClassManager classManager; | ||
| 47 | |||
| 48 | private JTree tree; | ||
| 49 | private SourceTreeNode root; | ||
| 50 | private SearchPath sourcePath; | ||
| 51 | private CommandInterpreter interpreter; | ||
| 52 | |||
| 53 | private static String HEADING = "SOURCES"; | ||
| 54 | |||
| 55 | public SourceTreeTool(Environment env) { | ||
| 56 | |||
| 57 | super(new BorderLayout()); | ||
| 58 | |||
| 59 | this.env = env; | ||
| 60 | this.runtime = env.getExecutionManager(); | ||
| 61 | this.sourceManager = env.getSourceManager(); | ||
| 62 | |||
| 63 | this.interpreter = new CommandInterpreter(env); | ||
| 64 | |||
| 65 | sourcePath = sourceManager.getSourcePath(); | ||
| 66 | root = createDirectoryTree(HEADING); | ||
| 67 | |||
| 68 | // Create a tree that allows one selection at a time. | ||
| 69 | tree = new JTree(new DefaultTreeModel(root)); | ||
| 70 | tree.setSelectionModel(new SingleLeafTreeSelectionModel()); | ||
| 71 | |||
| 72 | /****** | ||
| 73 | // Listen for when the selection changes. | ||
| 74 | tree.addTreeSelectionListener(new TreeSelectionListener() { | ||
| 75 | public void valueChanged(TreeSelectionEvent e) { | ||
| 76 | SourceTreeNode node = (SourceTreeNode) | ||
| 77 | (e.getPath().getLastPathComponent()); | ||
| 78 | interpreter.executeCommand("view " + node.getRelativePath()); | ||
| 79 | } | ||
| 80 | }); | ||
| 81 | ******/ | ||
| 82 | |||
| 83 | MouseListener ml = new MouseAdapter() { | ||
| 84 | @Override | ||
| 85 | public void mouseClicked(MouseEvent e) { | ||
| 86 | int selRow = tree.getRowForLocation(e.getX(), e.getY()); | ||
| 87 | TreePath selPath = tree.getPathForLocation(e.getX(), e.getY()); | ||
| 88 | if(selRow != -1) { | ||
| 89 | if(e.getClickCount() == 1) { | ||
| 90 | SourceTreeNode node = | ||
| 91 | (SourceTreeNode)selPath.getLastPathComponent(); | ||
| 92 | // If user clicks on leaf, select it, and issue 'view' command. | ||
| 93 | if (node.isLeaf()) { | ||
| 94 | tree.setSelectionPath(selPath); | ||
| 95 | interpreter.executeCommand("view " + node.getRelativePath()); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | } | ||
| 99 | } | ||
| 100 | }; | ||
| 101 | tree.addMouseListener(ml); | ||
| 102 | |||
| 103 | JScrollPane treeView = new JScrollPane(tree); | ||
| 104 | add(treeView); | ||
| 105 | |||
| 106 | // Create listener for source path changes. | ||
| 107 | |||
| 108 | SourceTreeToolListener listener = new SourceTreeToolListener(); | ||
| 109 | sourceManager.addSourceListener(listener); | ||
| 110 | |||
| 111 | //### remove listeners on exit! | ||
| 112 | } | ||
| 113 | |||
| 114 | private class SourceTreeToolListener implements SourceListener { | ||
| 115 | |||
| 116 | @Override | ||
| 117 | public void sourcepathChanged(SourcepathChangedEvent e) { | ||
| 118 | sourcePath = sourceManager.getSourcePath(); | ||
| 119 | root = createDirectoryTree(HEADING); | ||
| 120 | tree.setModel(new DefaultTreeModel(root)); | ||
| 121 | } | ||
| 122 | |||
| 123 | } | ||
| 124 | |||
| 125 | private static class SourceOrDirectoryFilter implements FilenameFilter { | ||
| 126 | @Override | ||
| 127 | public boolean accept(File dir, String name) { | ||
| 128 | return (name.endsWith(".java") || | ||
| 129 | new File(dir, name).isDirectory()); | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | private static FilenameFilter filter = new SourceOrDirectoryFilter(); | ||
| 134 | |||
| 135 | SourceTreeNode createDirectoryTree(String label) { | ||
| 136 | try { | ||
| 137 | return new SourceTreeNode(label, null, "", true); | ||
| 138 | } catch (SecurityException e) { | ||
| 139 | env.failure("Cannot access source file or directory"); | ||
| 140 | return null; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | |||
| 145 | class SourceTreeNode implements TreeNode { | ||
| 146 | |||
| 147 | private String name; | ||
| 148 | private boolean isDirectory; | ||
| 149 | private SourceTreeNode parent; | ||
| 150 | private SourceTreeNode[] children; | ||
| 151 | private String relativePath; | ||
| 152 | private boolean isExpanded; | ||
| 153 | |||
| 154 | private SourceTreeNode(String label, | ||
| 155 | SourceTreeNode parent, | ||
| 156 | String relativePath, | ||
| 157 | boolean isDirectory) { | ||
| 158 | this.name = label; | ||
| 159 | this.relativePath = relativePath; | ||
| 160 | this.parent = parent; | ||
| 161 | this.isDirectory = isDirectory; | ||
| 162 | } | ||
| 163 | |||
| 164 | @Override | ||
| 165 | public String toString() { | ||
| 166 | return name; | ||
| 167 | } | ||
| 168 | |||
| 169 | public String getRelativePath() { | ||
| 170 | return relativePath; | ||
| 171 | } | ||
| 172 | |||
| 173 | private void expandIfNeeded() { | ||
| 174 | try { | ||
| 175 | if (!isExpanded && isDirectory) { | ||
| 176 | String[] files = sourcePath.children(relativePath, filter); | ||
| 177 | children = new SourceTreeNode[files.length]; | ||
| 178 | for (int i = 0; i < files.length; i++) { | ||
| 179 | String childName = | ||
| 180 | (relativePath.equals("")) | ||
| 181 | ? files[i] | ||
| 182 | : relativePath + File.separator + files[i]; | ||
| 183 | File file = sourcePath.resolve(childName); | ||
| 184 | boolean isDir = (file != null && file.isDirectory()); | ||
| 185 | children[i] = | ||
| 186 | new SourceTreeNode(files[i], this, childName, isDir); | ||
| 187 | } | ||
| 188 | } | ||
| 189 | isExpanded = true; | ||
| 190 | } catch (SecurityException e) { | ||
| 191 | children = null; | ||
| 192 | env.failure("Cannot access source file or directory"); | ||
| 193 | } | ||
| 194 | } | ||
| 195 | |||
| 196 | // -- interface TreeNode -- | ||
| 197 | |||
| 198 | /* | ||
| 199 | * Returns the child <code>TreeNode</code> at index | ||
| 200 | * <code>childIndex</code>. | ||
| 201 | */ | ||
| 202 | @Override | ||
| 203 | public TreeNode getChildAt(int childIndex) { | ||
| 204 | expandIfNeeded(); | ||
| 205 | return children[childIndex]; | ||
| 206 | } | ||
| 207 | |||
| 208 | /** | ||
| 209 | * Returns the number of children <code>TreeNode</code>s the receiver | ||
| 210 | * contains. | ||
| 211 | */ | ||
| 212 | @Override | ||
| 213 | public int getChildCount() { | ||
| 214 | expandIfNeeded(); | ||
| 215 | return children.length; | ||
| 216 | } | ||
| 217 | |||
| 218 | /** | ||
| 219 | * Returns the parent <code>TreeNode</code> of the receiver. | ||
| 220 | */ | ||
| 221 | @Override | ||
| 222 | public TreeNode getParent() { | ||
| 223 | return parent; | ||
| 224 | } | ||
| 225 | |||
| 226 | /** | ||
| 227 | * Returns the index of <code>node</code> in the receivers children. | ||
| 228 | * If the receiver does not contain <code>node</code>, -1 will be | ||
| 229 | * returned. | ||
| 230 | */ | ||
| 231 | @Override | ||
| 232 | public int getIndex(TreeNode node) { | ||
| 233 | expandIfNeeded(); | ||
| 234 | for (int i = 0; i < children.length; i++) { | ||
| 235 | if (children[i] == node) { | ||
| 236 | return i; | ||
| 237 | } | ||
| 238 | } | ||
| 239 | return -1; | ||
| 240 | } | ||
| 241 | |||
| 242 | /** | ||
| 243 | * Returns true if the receiver allows children. | ||
| 244 | */ | ||
| 245 | @Override | ||
| 246 | public boolean getAllowsChildren() { | ||
| 247 | return isDirectory; | ||
| 248 | } | ||
| 249 | |||
| 250 | /** | ||
| 251 | * Returns true if the receiver is a leaf. | ||
| 252 | */ | ||
| 253 | @Override | ||
| 254 | public boolean isLeaf() { | ||
| 255 | expandIfNeeded(); | ||
| 256 | return !isDirectory; | ||
| 257 | } | ||
| 258 | |||
| 259 | /** | ||
| 260 | * Returns the children of the receiver as an Enumeration. | ||
| 261 | */ | ||
| 262 | @Override | ||
| 263 | public Enumeration children() { | ||
| 264 | expandIfNeeded(); | ||
| 265 | return new Enumeration() { | ||
| 266 | int i = 0; | ||
| 267 | @Override | ||
| 268 | public boolean hasMoreElements() { | ||
| 269 | return (i < children.length); | ||
| 270 | } | ||
| 271 | @Override | ||
| 272 | public Object nextElement() throws NoSuchElementException { | ||
| 273 | if (i >= children.length) { | ||
| 274 | throw new NoSuchElementException(); | ||
| 275 | } | ||
| 276 | return children[i++]; | ||
| 277 | } | ||
| 278 | }; | ||
| 279 | } | ||
| 280 | |||
| 281 | } | ||
| 282 | |||
| 283 | } | ||