Showing the changes to java/pkg/com/sun/tools/example/debug/gui/StackTraceTool.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,198 @@ | ||
| 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 javax.swing.*; | |
| 29 | +import javax.swing.event.*; | |
| 30 | +import java.awt.*; | |
| 31 | +import com.sun.jdi.*; | |
| 32 | +import com.sun.tools.example.debug.bdi.*; | |
| 33 | + | |
| 34 | +public class StackTraceTool extends JPanel { | |
| 35 | + | |
| 36 | + private static final long serialVersionUID = 9140041989427965718L; | |
| 37 | + | |
| 38 | + private Environment env; | |
| 39 | + | |
| 40 | + private ExecutionManager runtime; | |
| 41 | + private ContextManager context; | |
| 42 | + | |
| 43 | + private ThreadInfo tinfo; | |
| 44 | + | |
| 45 | + private JList list; | |
| 46 | + private ListModel stackModel; | |
| 47 | + | |
| 48 | + public StackTraceTool(Environment env) { | |
| 49 | + | |
| 50 | + super(new BorderLayout()); | |
| 51 | + | |
| 52 | + this.env = env; | |
| 53 | + this.runtime = env.getExecutionManager(); | |
| 54 | + this.context = env.getContextManager(); | |
| 55 | + | |
| 56 | + stackModel = new DefaultListModel(); // empty | |
| 57 | + | |
| 58 | + list = new JList(stackModel); | |
| 59 | + list.setCellRenderer(new StackFrameRenderer()); | |
| 60 | + | |
| 61 | + JScrollPane listView = new JScrollPane(list); | |
| 62 | + add(listView); | |
| 63 | + | |
| 64 | + // Create listener. | |
| 65 | + StackTraceToolListener listener = new StackTraceToolListener(); | |
| 66 | + context.addContextListener(listener); | |
| 67 | + list.addListSelectionListener(listener); | |
| 68 | + | |
| 69 | + //### remove listeners on exit! | |
| 70 | + } | |
| 71 | + | |
| 72 | + private class StackTraceToolListener | |
| 73 | + implements ContextListener, ListSelectionListener | |
| 74 | + { | |
| 75 | + | |
| 76 | + // ContextListener | |
| 77 | + | |
| 78 | + // If the user selects a new current frame, display it in | |
| 79 | + // this view. | |
| 80 | + | |
| 81 | + //### I suspect we handle the case badly that the VM is not interrupted. | |
| 82 | + | |
| 83 | + @Override | |
| 84 | + public void currentFrameChanged(CurrentFrameChangedEvent e) { | |
| 85 | + // If the current frame of the thread appearing in this | |
| 86 | + // view is changed, move the selection to track it. | |
| 87 | + int frameIndex = e.getIndex(); | |
| 88 | + ThreadInfo ti = e.getThreadInfo(); | |
| 89 | + if (e.getInvalidate() || tinfo != ti) { | |
| 90 | + tinfo = ti; | |
| 91 | + showStack(ti, frameIndex); | |
| 92 | + } else { | |
| 93 | + if (frameIndex < stackModel.getSize()) { | |
| 94 | + list.setSelectedIndex(frameIndex); | |
| 95 | + list.ensureIndexIsVisible(frameIndex); | |
| 96 | + } | |
| 97 | + } | |
| 98 | + } | |
| 99 | + | |
| 100 | + // ListSelectionListener | |
| 101 | + | |
| 102 | + @Override | |
| 103 | + public void valueChanged(ListSelectionEvent e) { | |
| 104 | + int index = list.getSelectedIndex(); | |
| 105 | + if (index != -1) { | |
| 106 | + //### should use listener? | |
| 107 | + try { | |
| 108 | + context.setCurrentFrameIndex(index); | |
| 109 | + } catch (VMNotInterruptedException exc) { | |
| 110 | + } | |
| 111 | + } | |
| 112 | + } | |
| 113 | + } | |
| 114 | + | |
| 115 | + private class StackFrameRenderer extends DefaultListCellRenderer { | |
| 116 | + | |
| 117 | + @Override | |
| 118 | + public Component getListCellRendererComponent(JList list, | |
| 119 | + Object value, | |
| 120 | + int index, | |
| 121 | + boolean isSelected, | |
| 122 | + boolean cellHasFocus) { | |
| 123 | + | |
| 124 | + //### We should indicate the current thread independently of the | |
| 125 | + //### selection, e.g., with an icon, because the user may change | |
| 126 | + //### the selection graphically without affecting the current | |
| 127 | + //### thread. | |
| 128 | + | |
| 129 | + super.getListCellRendererComponent(list, value, index, | |
| 130 | + isSelected, cellHasFocus); | |
| 131 | + if (value == null) { | |
| 132 | + this.setText("<unavailable>"); | |
| 133 | + } else { | |
| 134 | + StackFrame frame = (StackFrame)value; | |
| 135 | + Location loc = frame.location(); | |
| 136 | + Method meth = loc.method(); | |
| 137 | + String methName = | |
| 138 | + meth.declaringType().name() + '.' + meth.name(); | |
| 139 | + String position = ""; | |
| 140 | + if (meth.isNative()) { | |
| 141 | + position = " (native method)"; | |
| 142 | + } else if (loc.lineNumber() != -1) { | |
| 143 | + position = ":" + loc.lineNumber(); | |
| 144 | + } else { | |
| 145 | + long pc = loc.codeIndex(); | |
| 146 | + if (pc != -1) { | |
| 147 | + position = ", pc = " + pc; | |
| 148 | + } | |
| 149 | + } | |
| 150 | + // Indices are presented to the user starting from 1, not 0. | |
| 151 | + this.setText("[" + (index+1) +"] " + methName + position); | |
| 152 | + } | |
| 153 | + return this; | |
| 154 | + } | |
| 155 | + } | |
| 156 | + | |
| 157 | + // Point this view at the given thread and frame. | |
| 158 | + | |
| 159 | + private void showStack(ThreadInfo tinfo, int selectFrame) { | |
| 160 | + StackTraceListModel model = new StackTraceListModel(tinfo); | |
| 161 | + stackModel = model; | |
| 162 | + list.setModel(stackModel); | |
| 163 | + list.setSelectedIndex(selectFrame); | |
| 164 | + list.ensureIndexIsVisible(selectFrame); | |
| 165 | + } | |
| 166 | + | |
| 167 | + private static class StackTraceListModel extends AbstractListModel { | |
| 168 | + | |
| 169 | + private final ThreadInfo tinfo; | |
| 170 | + | |
| 171 | + public StackTraceListModel(ThreadInfo tinfo) { | |
| 172 | + this.tinfo = tinfo; | |
| 173 | + } | |
| 174 | + | |
| 175 | + @Override | |
| 176 | + public Object getElementAt(int index) { | |
| 177 | + try { | |
| 178 | + return tinfo == null? null : tinfo.getFrame(index); | |
| 179 | + } catch (VMNotInterruptedException e) { | |
| 180 | + //### Is this the right way to handle this? | |
| 181 | + //### Would happen if user scrolled stack trace | |
| 182 | + //### while not interrupted -- should probably | |
| 183 | + //### block user interaction in this case. | |
| 184 | + return null; | |
| 185 | + } | |
| 186 | + } | |
| 187 | + | |
| 188 | + @Override | |
| 189 | + public int getSize() { | |
| 190 | + try { | |
| 191 | + return tinfo == null? 1 : tinfo.getFrameCount(); | |
| 192 | + } catch (VMNotInterruptedException e) { | |
| 193 | + //### Is this the right way to handle this? | |
| 194 | + return 0; | |
| 195 | + } | |
| 196 | + } | |
| 197 | + } | |
| 198 | +} | |