KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > jdb > StackPanel


1 /*
2  * StackPanel.java
3  *
4  * Copyright (C) 2002-2003 Peter Graves
5  * $Id: StackPanel.java,v 1.4 2003/06/09 16:33:04 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j.jdb;
23
24 import com.sun.jdi.AbsentInformationException;
25 import com.sun.jdi.Location;
26 import com.sun.jdi.Method;
27 import com.sun.jdi.ReferenceType;
28 import com.sun.jdi.StackFrame;
29 import com.sun.jdi.ThreadReference;
30 import java.awt.Component JavaDoc;
31 import java.awt.event.InputEvent JavaDoc;
32 import java.awt.event.MouseEvent JavaDoc;
33 import java.awt.event.MouseListener JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Vector JavaDoc;
37 import javax.swing.JList JavaDoc;
38 import javax.swing.JScrollPane JavaDoc;
39 import javax.swing.SwingUtilities JavaDoc;
40 import org.armedbear.j.Buffer;
41 import org.armedbear.j.Editor;
42 import org.armedbear.j.EditorIterator;
43 import org.armedbear.j.File;
44 import org.armedbear.j.FastStringBuffer;
45 import org.armedbear.j.JavaSource;
46 import org.armedbear.j.Log;
47
48 public final class StackPanel implements ContextListener, MouseListener JavaDoc
49 {
50     private final Jdb jdb;
51     private final JdbControlDialog dialog;
52     private final JList JavaDoc list;
53     private final JScrollPane JavaDoc scrollPane;
54
55     private List JavaDoc frames;
56
57     public StackPanel(Jdb jdb, JdbControlDialog dialog)
58     {
59         this.jdb = jdb;
60         this.dialog = dialog;
61         Vector JavaDoc v = new Vector JavaDoc();
62         list = new JList JavaDoc(v);
63         scrollPane = new JScrollPane JavaDoc(list);
64         jdb.addContextListener(this);
65         list.addMouseListener(this);
66     }
67
68     public Component JavaDoc getComponent()
69     {
70         return scrollPane;
71     }
72
73     public void contextChanged()
74     {
75         ThreadReference threadRef = jdb.getCurrentThread();
76         if (threadRef != null) {
77             try {
78                 frames = threadRef.frames();
79                 final Vector JavaDoc v = new Vector JavaDoc();
80                 int selectedIndex = -1;
81                 if (frames.size() > 0) {
82                     StackFrame currentStackFrame = jdb.getCurrentStackFrame();
83                     if (currentStackFrame == null) {
84                         currentStackFrame = (StackFrame) frames.get(0);
85                         jdb.setCurrentStackFrame(currentStackFrame);
86                     }
87                     int index = 1;
88                     Iterator JavaDoc iter = frames.iterator();
89                     while (iter.hasNext()) {
90                         StackFrame frame = (StackFrame) iter.next();
91                         if (frame != null && frame.equals(currentStackFrame))
92                             selectedIndex = index - 1;
93                         Location location = frame.location();
94                         Method method = location.method();
95                         FastStringBuffer sb = new FastStringBuffer();
96                         if (index < 10)
97                             sb.append(' ');
98                         sb.append(index++);
99                         sb.append(' ');
100                         sb.append(getSimpleName(method.declaringType()));
101                         sb.append('.');
102                         sb.append(method.name());
103                         if (method.isNative()) {
104                             sb.append(" (native method)");
105                         } else {
106                             String JavaDoc sourceName = null;
107                             try {
108                                 sourceName = location.sourceName();
109                             }
110                             catch (AbsentInformationException ignored) {}
111                             int lineNumber = location.lineNumber();
112                             if (sourceName != null && sourceName.length() > 0) {
113                                 sb.append(" (");
114                                 sb.append(sourceName);
115                                 if (lineNumber > 0) {
116                                     sb.append(':');
117                                     sb.append(lineNumber);
118                                 }
119                                 sb.append(')');
120                             }
121                         }
122                         v.add(sb.toString());
123                     }
124                 }
125                 final int finalSelectedIndex = selectedIndex;
126                 // Update UI in event dispatch thread.
127
Runnable JavaDoc r = new Runnable JavaDoc() {
128                     public void run()
129                     {
130                         list.setListData(v);
131                         list.setSelectedIndex(finalSelectedIndex);
132                     }
133                 };
134                 SwingUtilities.invokeLater(r);
135             }
136             catch (Exception JavaDoc e) {
137                 Log.error(e);
138             }
139         }
140     }
141
142     private static String JavaDoc getSimpleName(ReferenceType refType)
143     {
144         String JavaDoc name = refType.name();
145         int index = name.lastIndexOf('.');
146         if (index >= 0)
147             return name.substring(index+1);
148         else
149             return name;
150     }
151
152     public void mousePressed(MouseEvent JavaDoc e)
153     {
154         if (!jdb.isSuspended())
155             return;
156
157         // Mask off the bits we don't care about (Java 1.4).
158
int modifiers = e.getModifiers() & 0x1f;
159
160         if (modifiers == InputEvent.BUTTON1_MASK ||
161             modifiers == InputEvent.BUTTON2_MASK) {
162             if (modifiers == InputEvent.BUTTON2_MASK)
163                 list.setSelectedIndex(list.locationToIndex(e.getPoint()));
164             list.paintImmediately(0, 0, list.getWidth(), list.getHeight());
165             int index = list.getSelectedIndex();
166             if (frames != null && index >= 0 && index < frames.size()) {
167                 StackFrame stackFrame = (StackFrame) frames.get(index);
168                 jdb.setCurrentStackFrame(stackFrame);
169                 Location location = stackFrame.location();
170                 Method method = location.method();
171                 if (method != null && !method.isNative()) {
172                     String JavaDoc className = location.declaringType().name();
173                     int i = className.indexOf('$');
174                     if (i > 0)
175                         className = className.substring(0, i);
176                     File file =
177                         JavaSource.findSource(className, jdb.getSourcePath());
178                     if (file != null) {
179                         Buffer buffer = Editor.getBuffer(file);
180                         if (buffer != null) {
181                             Editor editor = null;
182                             for (EditorIterator it = new EditorIterator(); it.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.activateInOtherWindow(buffer);
192                             } else {
193                                 editor = Editor.currentEditor();
194                                 editor.makeNext(buffer);
195                                 editor.activate(buffer);
196                             }
197                             int lineNumber = location.lineNumber();
198                             if (lineNumber > 0) {
199                                 editor.jumpToLine(lineNumber - 1);
200                                 editor.updateDisplay();
201                             }
202                         }
203                     }
204                 }
205             }
206         }
207         dialog.requestDefaultFocus();
208     }
209
210     public void mouseReleased(MouseEvent JavaDoc e) {}
211
212     public void mouseClicked(MouseEvent JavaDoc e) {}
213
214     public void mouseEntered(MouseEvent JavaDoc e) {}
215
216     public void mouseExited(MouseEvent JavaDoc e) {}
217 }
218
Popular Tags