001: /*
002: * StackPanel.java
003: *
004: * Copyright (C) 2002-2003 Peter Graves
005: * $Id: StackPanel.java,v 1.4 2003/06/09 16:33:04 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j.jdb;
023:
024: import com.sun.jdi.AbsentInformationException;
025: import com.sun.jdi.Location;
026: import com.sun.jdi.Method;
027: import com.sun.jdi.ReferenceType;
028: import com.sun.jdi.StackFrame;
029: import com.sun.jdi.ThreadReference;
030: import java.awt.Component;
031: import java.awt.event.InputEvent;
032: import java.awt.event.MouseEvent;
033: import java.awt.event.MouseListener;
034: import java.util.Iterator;
035: import java.util.List;
036: import java.util.Vector;
037: import javax.swing.JList;
038: import javax.swing.JScrollPane;
039: import javax.swing.SwingUtilities;
040: import org.armedbear.j.Buffer;
041: import org.armedbear.j.Editor;
042: import org.armedbear.j.EditorIterator;
043: import org.armedbear.j.File;
044: import org.armedbear.j.FastStringBuffer;
045: import org.armedbear.j.JavaSource;
046: import org.armedbear.j.Log;
047:
048: public final class StackPanel implements ContextListener, MouseListener {
049: private final Jdb jdb;
050: private final JdbControlDialog dialog;
051: private final JList list;
052: private final JScrollPane scrollPane;
053:
054: private List frames;
055:
056: public StackPanel(Jdb jdb, JdbControlDialog dialog) {
057: this .jdb = jdb;
058: this .dialog = dialog;
059: Vector v = new Vector();
060: list = new JList(v);
061: scrollPane = new JScrollPane(list);
062: jdb.addContextListener(this );
063: list.addMouseListener(this );
064: }
065:
066: public Component getComponent() {
067: return scrollPane;
068: }
069:
070: public void contextChanged() {
071: ThreadReference threadRef = jdb.getCurrentThread();
072: if (threadRef != null) {
073: try {
074: frames = threadRef.frames();
075: final Vector v = new Vector();
076: int selectedIndex = -1;
077: if (frames.size() > 0) {
078: StackFrame currentStackFrame = jdb
079: .getCurrentStackFrame();
080: if (currentStackFrame == null) {
081: currentStackFrame = (StackFrame) frames.get(0);
082: jdb.setCurrentStackFrame(currentStackFrame);
083: }
084: int index = 1;
085: Iterator iter = frames.iterator();
086: while (iter.hasNext()) {
087: StackFrame frame = (StackFrame) iter.next();
088: if (frame != null
089: && frame.equals(currentStackFrame))
090: selectedIndex = index - 1;
091: Location location = frame.location();
092: Method method = location.method();
093: FastStringBuffer sb = new FastStringBuffer();
094: if (index < 10)
095: sb.append(' ');
096: sb.append(index++);
097: sb.append(' ');
098: sb
099: .append(getSimpleName(method
100: .declaringType()));
101: sb.append('.');
102: sb.append(method.name());
103: if (method.isNative()) {
104: sb.append(" (native method)");
105: } else {
106: String sourceName = null;
107: try {
108: sourceName = location.sourceName();
109: } catch (AbsentInformationException ignored) {
110: }
111: int lineNumber = location.lineNumber();
112: if (sourceName != null
113: && sourceName.length() > 0) {
114: sb.append(" (");
115: sb.append(sourceName);
116: if (lineNumber > 0) {
117: sb.append(':');
118: sb.append(lineNumber);
119: }
120: sb.append(')');
121: }
122: }
123: v.add(sb.toString());
124: }
125: }
126: final int finalSelectedIndex = selectedIndex;
127: // Update UI in event dispatch thread.
128: Runnable r = new Runnable() {
129: public void run() {
130: list.setListData(v);
131: list.setSelectedIndex(finalSelectedIndex);
132: }
133: };
134: SwingUtilities.invokeLater(r);
135: } catch (Exception e) {
136: Log.error(e);
137: }
138: }
139: }
140:
141: private static String getSimpleName(ReferenceType refType) {
142: String name = refType.name();
143: int index = name.lastIndexOf('.');
144: if (index >= 0)
145: return name.substring(index + 1);
146: else
147: return name;
148: }
149:
150: public void mousePressed(MouseEvent e) {
151: if (!jdb.isSuspended())
152: return;
153:
154: // Mask off the bits we don't care about (Java 1.4).
155: int modifiers = e.getModifiers() & 0x1f;
156:
157: if (modifiers == InputEvent.BUTTON1_MASK
158: || modifiers == InputEvent.BUTTON2_MASK) {
159: if (modifiers == InputEvent.BUTTON2_MASK)
160: list.setSelectedIndex(list
161: .locationToIndex(e.getPoint()));
162: list.paintImmediately(0, 0, list.getWidth(), list
163: .getHeight());
164: int index = list.getSelectedIndex();
165: if (frames != null && index >= 0 && index < frames.size()) {
166: StackFrame stackFrame = (StackFrame) frames.get(index);
167: jdb.setCurrentStackFrame(stackFrame);
168: Location location = stackFrame.location();
169: Method method = location.method();
170: if (method != null && !method.isNative()) {
171: String className = location.declaringType().name();
172: int i = className.indexOf('$');
173: if (i > 0)
174: className = className.substring(0, i);
175: File file = JavaSource.findSource(className, jdb
176: .getSourcePath());
177: if (file != null) {
178: Buffer buffer = Editor.getBuffer(file);
179: if (buffer != null) {
180: Editor editor = null;
181: for (EditorIterator it = new EditorIterator(); it
182: .hasNext();) {
183: Editor ed = it.nextEditor();
184: if (ed.getBuffer() instanceof Jdb) {
185: editor = ed;
186: break;
187: }
188: }
189: if (editor != null) {
190: editor.makeNext(buffer);
191: editor = editor
192: .activateInOtherWindow(buffer);
193: } else {
194: editor = Editor.currentEditor();
195: editor.makeNext(buffer);
196: editor.activate(buffer);
197: }
198: int lineNumber = location.lineNumber();
199: if (lineNumber > 0) {
200: editor.jumpToLine(lineNumber - 1);
201: editor.updateDisplay();
202: }
203: }
204: }
205: }
206: }
207: }
208: dialog.requestDefaultFocus();
209: }
210:
211: public void mouseReleased(MouseEvent e) {
212: }
213:
214: public void mouseClicked(MouseEvent e) {
215: }
216:
217: public void mouseEntered(MouseEvent e) {
218: }
219:
220: public void mouseExited(MouseEvent e) {
221: }
222: }
|