KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ThreadPanel.java
3  *
4  * Copyright (C) 2002-2003 Peter Graves
5  * $Id: ThreadPanel.java,v 1.5 2003/06/09 16:33:39 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.IncompatibleThreadStateException;
25 import com.sun.jdi.Location;
26 import com.sun.jdi.Method;
27 import com.sun.jdi.StackFrame;
28 import com.sun.jdi.ThreadReference;
29 import com.sun.jdi.VirtualMachine;
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 ThreadPanel 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 threads;
56
57     public ThreadPanel(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         final Vector JavaDoc v = new Vector JavaDoc();
76         int index = -1;
77         VirtualMachine vm = jdb.getVM();
78         if (vm != null) {
79             threads = vm.allThreads();
80             for (Iterator JavaDoc iter = threads.iterator(); iter.hasNext();) {
81                 ThreadReference threadRef = (ThreadReference) iter.next();
82                 FastStringBuffer sb = new FastStringBuffer();
83                 sb.append(threadRef.name());
84                 sb.append(" (id=");
85                 sb.append(threadRef.uniqueID());
86                 sb.append(") ");
87                 switch (threadRef.status()) {
88                     case ThreadReference.THREAD_STATUS_UNKNOWN:
89                         sb.append("UNKNOWN");
90                         break;
91                     case ThreadReference.THREAD_STATUS_ZOMBIE:
92                         sb.append("ZOMBIE");
93                         break;
94                     case ThreadReference.THREAD_STATUS_RUNNING:
95                         sb.append("RUNNING");
96                         break;
97                     case ThreadReference.THREAD_STATUS_SLEEPING:
98                         sb.append("SLEEPING");
99                         break;
100                     case ThreadReference.THREAD_STATUS_MONITOR:
101                         sb.append("MONITOR");
102                         break;
103                     case ThreadReference.THREAD_STATUS_WAIT:
104                         sb.append("WAIT");
105                         break;
106                     case ThreadReference.THREAD_STATUS_NOT_STARTED:
107                         sb.append("NOT_STARTED");
108                         break;
109                 }
110                 if (threadRef == jdb.getCurrentThread())
111                     index = v.size();
112                 v.add(sb.toString());
113             }
114         }
115         // Update UI in event dispatch thread.
116
final int finalIndex = index;
117         Runnable JavaDoc r = new Runnable JavaDoc() {
118             public void run()
119             {
120                 list.setListData(v);
121                 list.setSelectedIndex(finalIndex);
122             }
123         };
124         SwingUtilities.invokeLater(r);
125     }
126
127     public void mousePressed(MouseEvent JavaDoc e)
128     {
129         if (!jdb.isSuspended())
130             return;
131         // Mask off the bits we don't care about (Java 1.4).
132
int modifiers = e.getModifiers() & 0x1f;
133         if (modifiers == InputEvent.BUTTON1_MASK ||
134             modifiers == InputEvent.BUTTON2_MASK) {
135             if (modifiers == InputEvent.BUTTON2_MASK)
136                 list.setSelectedIndex(list.locationToIndex(e.getPoint()));
137             list.paintImmediately(0, 0, list.getWidth(), list.getHeight());
138             int index = list.getSelectedIndex();
139             if (threads != null && index >= 0 && index < threads.size()) {
140                 ThreadReference threadRef = (ThreadReference) threads.get(index);
141                 jdb.setCurrentThread(threadRef);
142                 try {
143                     List JavaDoc frames = threadRef.frames();
144                     if (frames.size() > 0) {
145                         StackFrame stackFrame = threadRef.frame(0);
146                         Location location = stackFrame.location();
147                         Method method = location.method();
148                         if (method != null && !method.isNative()) {
149                             String JavaDoc className = location.declaringType().name();
150                             final int lineNumber = location.lineNumber();
151                             Log.debug(className.concat(":").concat(String.valueOf(lineNumber)));
152                             File file =
153                                 JavaSource.findSource(className, jdb.getSourcePath());
154                             if (file != null) {
155                                 Buffer buffer = Editor.getBuffer(file);
156                                 if (buffer != null) {
157                                     Editor editor = null;
158                                     for (EditorIterator it = new EditorIterator(); it.hasNext();) {
159                                         Editor ed = it.nextEditor();
160                                         if (ed.getBuffer() instanceof Jdb) {
161                                             editor = ed;
162                                             break;
163                                         }
164                                     }
165                                     if (editor != null) {
166                                         editor.makeNext(buffer);
167                                         editor = editor.activateInOtherWindow(buffer);
168                                     } else {
169                                         editor = Editor.currentEditor();
170                                         editor.makeNext(buffer);
171                                         editor.activate(buffer);
172                                     }
173                                     if (lineNumber > 0) {
174                                         editor.jumpToLine(lineNumber - 1);
175                                         editor.updateDisplay();
176                                     }
177                                 }
178                             }
179                         }
180                     }
181                     // Update stack panel.
182
jdb.fireContextChanged();
183                 }
184                 catch (IncompatibleThreadStateException ex) {
185                     Log.error(ex);
186                 }
187             }
188         }
189         dialog.requestDefaultFocus();
190     }
191
192     public void mouseReleased(MouseEvent JavaDoc e) {}
193
194     public void mouseClicked(MouseEvent JavaDoc e) {}
195
196     public void mouseEntered(MouseEvent JavaDoc e) {}
197
198     public void mouseExited(MouseEvent JavaDoc e) {}
199 }
200
Popular Tags