Showing the changes to java/pkg/com/sun/tools/example/debug/gui/SourceTool.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,383 @@ | ||
| 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.awt.*; | |
| 30 | +import java.awt.event.*; | |
| 31 | +import javax.swing.*; | |
| 32 | + | |
| 33 | +import com.sun.jdi.*; | |
| 34 | +import com.sun.jdi.request.*; | |
| 35 | + | |
| 36 | +import com.sun.tools.example.debug.bdi.*; | |
| 37 | + | |
| 38 | +public class SourceTool extends JPanel { | |
| 39 | + | |
| 40 | + private static final long serialVersionUID = -5461299294186395257L; | |
| 41 | + | |
| 42 | + private Environment env; | |
| 43 | + | |
| 44 | + private ExecutionManager runtime; | |
| 45 | + private ContextManager context; | |
| 46 | + private SourceManager sourceManager; | |
| 47 | + | |
| 48 | + private JList list; | |
| 49 | + private ListModel sourceModel; | |
| 50 | + | |
| 51 | + // Information on source file that is on display, or failed to be | |
| 52 | + // displayed due to inaccessible source. Used to update display | |
| 53 | + // when sourcepath is changed. | |
| 54 | + | |
| 55 | + private String sourceName; // relative path name, if showSourceFile | |
| 56 | + private Location sourceLocn; // location, if showSourceForLocation | |
| 57 | + private CommandInterpreter interpreter; | |
| 58 | + | |
| 59 | + public SourceTool(Environment env) { | |
| 60 | + | |
| 61 | + super(new BorderLayout()); | |
| 62 | + | |
| 63 | + this.env = env; | |
| 64 | + | |
| 65 | + runtime = env.getExecutionManager(); | |
| 66 | + sourceManager = env.getSourceManager(); | |
| 67 | + this.context = env.getContextManager(); | |
| 68 | + this.interpreter = new CommandInterpreter(env, true); | |
| 69 | + | |
| 70 | + sourceModel = new DefaultListModel(); // empty | |
| 71 | + | |
| 72 | + list = new JList(sourceModel); | |
| 73 | + list.setCellRenderer(new SourceLineRenderer()); | |
| 74 | + | |
| 75 | + list.setPrototypeCellValue(SourceModel.prototypeCellValue); | |
| 76 | + | |
| 77 | + SourceToolListener listener = new SourceToolListener(); | |
| 78 | + context.addContextListener(listener); | |
| 79 | + runtime.addSpecListener(listener); | |
| 80 | + sourceManager.addSourceListener(listener); | |
| 81 | + | |
| 82 | + MouseListener squeek = new STMouseListener(); | |
| 83 | + list.addMouseListener(squeek); | |
| 84 | + | |
| 85 | + add(new JScrollPane(list)); | |
| 86 | + } | |
| 87 | + | |
| 88 | + public void setTextFont(Font f) { | |
| 89 | + list.setFont(f); | |
| 90 | + list.setPrototypeCellValue(SourceModel.prototypeCellValue); | |
| 91 | + } | |
| 92 | + | |
| 93 | + private class SourceToolListener | |
| 94 | + implements ContextListener, SourceListener, SpecListener | |
| 95 | + { | |
| 96 | + | |
| 97 | + // ContextListener | |
| 98 | + | |
| 99 | + @Override | |
| 100 | + public void currentFrameChanged(CurrentFrameChangedEvent e) { | |
| 101 | + showSourceContext(e.getThread(), e.getIndex()); | |
| 102 | + } | |
| 103 | + | |
| 104 | + // Clear source view. | |
| 105 | + // sourceModel = new DefaultListModel(); // empty | |
| 106 | + | |
| 107 | + // SourceListener | |
| 108 | + | |
| 109 | + @Override | |
| 110 | + public void sourcepathChanged(SourcepathChangedEvent e) { | |
| 111 | + // Reload source view if its contents depend | |
| 112 | + // on the source path. | |
| 113 | + if (sourceName != null) { | |
| 114 | + showSourceFile(sourceName); | |
| 115 | + } else if (sourceLocn != null) { | |
| 116 | + showSourceForLocation(sourceLocn); | |
| 117 | + } | |
| 118 | + } | |
| 119 | + | |
| 120 | + // SpecListener | |
| 121 | + | |
| 122 | + @Override | |
| 123 | + public void breakpointSet(SpecEvent e) { | |
| 124 | + breakpointResolved(e); | |
| 125 | + } | |
| 126 | + | |
| 127 | + @Override | |
| 128 | + public void breakpointDeferred(SpecEvent e) { } | |
| 129 | + | |
| 130 | + @Override | |
| 131 | + public void breakpointDeleted(SpecEvent e) { | |
| 132 | + BreakpointRequest req = (BreakpointRequest)e.getEventRequest(); | |
| 133 | + Location loc = req.location(); | |
| 134 | + if (loc != null) { | |
| 135 | + try { | |
| 136 | + SourceModel sm = sourceManager.sourceForLocation(loc); | |
| 137 | + sm.showBreakpoint(loc.lineNumber(), false); | |
| 138 | + showSourceForLocation(loc); | |
| 139 | + } catch (Exception exc) { | |
| 140 | + } | |
| 141 | + } | |
| 142 | + } | |
| 143 | + | |
| 144 | + @Override | |
| 145 | + public void breakpointResolved(SpecEvent e) { | |
| 146 | + BreakpointRequest req = (BreakpointRequest)e.getEventRequest(); | |
| 147 | + Location loc = req.location(); | |
| 148 | + try { | |
| 149 | + SourceModel sm = sourceManager.sourceForLocation(loc); | |
| 150 | + sm.showBreakpoint(loc.lineNumber(), true); | |
| 151 | + showSourceForLocation(loc); | |
| 152 | + } catch (Exception exc) { | |
| 153 | + } | |
| 154 | + } | |
| 155 | + | |
| 156 | + @Override | |
| 157 | + public void breakpointError(SpecErrorEvent e) { | |
| 158 | + breakpointDeleted(e); | |
| 159 | + } | |
| 160 | + | |
| 161 | + @Override | |
| 162 | + public void watchpointSet(SpecEvent e) { | |
| 163 | + } | |
| 164 | + @Override | |
| 165 | + public void watchpointDeferred(SpecEvent e) { | |
| 166 | + } | |
| 167 | + @Override | |
| 168 | + public void watchpointDeleted(SpecEvent e) { | |
| 169 | + } | |
| 170 | + @Override | |
| 171 | + public void watchpointResolved(SpecEvent e) { | |
| 172 | + } | |
| 173 | + @Override | |
| 174 | + public void watchpointError(SpecErrorEvent e) { | |
| 175 | + } | |
| 176 | + | |
| 177 | + @Override | |
| 178 | + public void exceptionInterceptSet(SpecEvent e) { | |
| 179 | + } | |
| 180 | + @Override | |
| 181 | + public void exceptionInterceptDeferred(SpecEvent e) { | |
| 182 | + } | |
| 183 | + @Override | |
| 184 | + public void exceptionInterceptDeleted(SpecEvent e) { | |
| 185 | + } | |
| 186 | + @Override | |
| 187 | + public void exceptionInterceptResolved(SpecEvent e) { | |
| 188 | + } | |
| 189 | + @Override | |
| 190 | + public void exceptionInterceptError(SpecErrorEvent e) { | |
| 191 | + } | |
| 192 | + } | |
| 193 | + | |
| 194 | + private void showSourceContext(ThreadReference thread, int index) { | |
| 195 | + //### Should use ThreadInfo here. | |
| 196 | + StackFrame frame = null; | |
| 197 | + if (thread != null) { | |
| 198 | + try { | |
| 199 | + frame = thread.frame(index); | |
| 200 | + } catch (IncompatibleThreadStateException e) {} | |
| 201 | + } | |
| 202 | + if (frame == null) { | |
| 203 | + return; | |
| 204 | + } | |
| 205 | + Location locn = frame.location(); | |
| 206 | + /***** | |
| 207 | + if (!showSourceForLocation(locn)) { | |
| 208 | + env.notice("Could not display source for " | |
| 209 | + + Utils.locationString(locn)); | |
| 210 | + } | |
| 211 | + *****/ | |
| 212 | + showSourceForLocation(locn); | |
| 213 | + } | |
| 214 | + | |
| 215 | + public boolean showSourceForLocation(Location locn) { | |
| 216 | + sourceName = null; | |
| 217 | + sourceLocn = locn; | |
| 218 | + int lineNo = locn.lineNumber(); | |
| 219 | + if (lineNo != -1) { | |
| 220 | + SourceModel source = sourceManager.sourceForLocation(locn); | |
| 221 | + if (source != null) { | |
| 222 | + showSourceAtLine(source, lineNo-1); | |
| 223 | + return true; | |
| 224 | + } | |
| 225 | + } | |
| 226 | + // Here if we could not display source. | |
| 227 | + showSourceUnavailable(); | |
| 228 | + return false; | |
| 229 | + } | |
| 230 | + | |
| 231 | + public boolean showSourceFile(String fileName) { | |
| 232 | + sourceLocn = null; | |
| 233 | + File file; | |
| 234 | + if (!fileName.startsWith(File.separator)) { | |
| 235 | + sourceName = fileName; | |
| 236 | + SearchPath sourcePath = sourceManager.getSourcePath(); | |
| 237 | + file = sourcePath.resolve(fileName); | |
| 238 | + if (file == null) { | |
| 239 | + //env.failure("Source not found on current source path."); | |
| 240 | + showSourceUnavailable(); | |
| 241 | + return false; | |
| 242 | + } | |
| 243 | + } else { | |
| 244 | + sourceName = null; // Absolute pathname does not depend on sourcepath. | |
| 245 | + file = new File(fileName); | |
| 246 | + } | |
| 247 | + SourceModel source = sourceManager.sourceForFile(file); | |
| 248 | + if (source != null) { | |
| 249 | + showSource(source); | |
| 250 | + return true; | |
| 251 | + } | |
| 252 | + showSourceUnavailable(); | |
| 253 | + return false; | |
| 254 | + } | |
| 255 | + | |
| 256 | + private void showSource(SourceModel model) { | |
| 257 | + setViewModel(model); | |
| 258 | + } | |
| 259 | + | |
| 260 | + private void showSourceAtLine(SourceModel model, int lineNo) { | |
| 261 | + setViewModel(model); | |
| 262 | + if (model.isActuallySource && (lineNo < model.getSize())) { | |
| 263 | + list.setSelectedIndex(lineNo); | |
| 264 | + if (lineNo+4 < model.getSize()) { | |
| 265 | + list.ensureIndexIsVisible(lineNo+4); // give some context | |
| 266 | + } | |
| 267 | + list.ensureIndexIsVisible(lineNo); | |
| 268 | + } | |
| 269 | + } | |
| 270 | + | |
| 271 | + private void showSourceUnavailable() { | |
| 272 | + SourceModel model = new SourceModel("[Source code is not available]"); | |
| 273 | + setViewModel(model); | |
| 274 | + } | |
| 275 | + | |
| 276 | + private void setViewModel(SourceModel model) { | |
| 277 | + if (model != sourceModel) { | |
| 278 | + // install new model | |
| 279 | + list.setModel(model); | |
| 280 | + sourceModel = model; | |
| 281 | + } | |
| 282 | + } | |
| 283 | + | |
| 284 | + private class SourceLineRenderer extends DefaultListCellRenderer { | |
| 285 | + | |
| 286 | + @Override | |
| 287 | + public Component getListCellRendererComponent(JList list, | |
| 288 | + Object value, | |
| 289 | + int index, | |
| 290 | + boolean isSelected, | |
| 291 | + boolean cellHasFocus) { | |
| 292 | + | |
| 293 | + //### Should set background highlight and/or icon if breakpoint on this line. | |
| 294 | + // Configures "this" | |
| 295 | + super.getListCellRendererComponent(list, value, index, | |
| 296 | + isSelected, cellHasFocus); | |
| 297 | + | |
| 298 | + SourceModel.Line line = (SourceModel.Line)value; | |
| 299 | + | |
| 300 | + //### Tab expansion is now done when source file is read in, | |
| 301 | + //### to speed up display. This costs a lot of space, slows | |
| 302 | + //### down source file loading, and has not been demonstrated | |
| 303 | + //### to yield an observable improvement in display performance. | |
| 304 | + //### Measurements may be appropriate here. | |
| 305 | + //String sourceLine = expandTabs((String)value); | |
| 306 | + setText(line.text); | |
| 307 | + if (line.hasBreakpoint) { | |
| 308 | + setIcon(Icons.stopSignIcon); | |
| 309 | + } else if (line.isExecutable()) { | |
| 310 | + setIcon(Icons.execIcon); | |
| 311 | + } else { | |
| 312 | + setIcon(Icons.blankIcon); | |
| 313 | + } | |
| 314 | + | |
| 315 | + | |
| 316 | + return this; | |
| 317 | + } | |
| 318 | + | |
| 319 | + @Override | |
| 320 | + public Dimension getPreferredSize() { | |
| 321 | + Dimension dim = super.getPreferredSize(); | |
| 322 | + return new Dimension(dim.width, dim.height-5); | |
| 323 | + } | |
| 324 | + | |
| 325 | + } | |
| 326 | + | |
| 327 | + private class STMouseListener extends MouseAdapter implements MouseListener { | |
| 328 | + @Override | |
| 329 | + public void mousePressed(MouseEvent e) { | |
| 330 | + if (e.isPopupTrigger()) { | |
| 331 | + showPopupMenu((Component)e.getSource(), | |
| 332 | + e.getX(), e.getY()); | |
| 333 | + } | |
| 334 | + } | |
| 335 | + | |
| 336 | + @Override | |
| 337 | + public void mouseReleased(MouseEvent e) { | |
| 338 | + if (e.isPopupTrigger()) { | |
| 339 | + showPopupMenu((Component)e.getSource(), | |
| 340 | + e.getX(), e.getY()); | |
| 341 | + } | |
| 342 | + } | |
| 343 | + | |
| 344 | + private void showPopupMenu(Component invoker, int x, int y) { | |
| 345 | + JList list = (JList)invoker; | |
| 346 | + int ln = list.getSelectedIndex() + 1; | |
| 347 | + SourceModel.Line line = | |
| 348 | + (SourceModel.Line)list.getSelectedValue(); | |
| 349 | + JPopupMenu popup = new JPopupMenu(); | |
| 350 | + | |
| 351 | + if (line == null) { | |
| 352 | + popup.add(new JMenuItem("please select a line")); | |
| 353 | + } else if (line.isExecutable()) { | |
| 354 | + String className = line.refType.name(); | |
| 355 | + if (line.hasBreakpoint()) { | |
| 356 | + popup.add(commandItem("Clear Breakpoint", | |
| 357 | + "clear " + className + | |
| 358 | + ":" + ln)); | |
| 359 | + } else { | |
| 360 | + popup.add(commandItem("Set Breakpoint", | |
| 361 | + "stop at " + className + | |
| 362 | + ":" + ln)); | |
| 363 | + } | |
| 364 | + } else { | |
| 365 | + popup.add(new JMenuItem("not an executable line")); | |
| 366 | + } | |
| 367 | + | |
| 368 | + popup.show(invoker, | |
| 369 | + x + popup.getWidth()/2, y + popup.getHeight()/2); | |
| 370 | + } | |
| 371 | + | |
| 372 | + private JMenuItem commandItem(String label, final String cmd) { | |
| 373 | + JMenuItem item = new JMenuItem(label); | |
| 374 | + item.addActionListener(new ActionListener() { | |
| 375 | + @Override | |
| 376 | + public void actionPerformed(ActionEvent e) { | |
| 377 | + interpreter.executeCommand(cmd); | |
| 378 | + } | |
| 379 | + }); | |
| 380 | + return item; | |
| 381 | + } | |
| 382 | + } | |
| 383 | +} | |