KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > debugger > gui > StackTablePane


1 /* -*- Mode: JDE; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the debugger and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  */

22
23 package org.aspectj.debugger.gui;
24
25 import org.aspectj.debugger.base.*;
26
27 import com.sun.jdi.*;
28 import com.sun.jdi.event.*;
29 import java.awt.*;
30 import java.awt.event.*;
31 import javax.swing.*;
32 import javax.swing.event.*;
33 import javax.swing.table.*;
34 import javax.swing.plaf.basic.*;
35 import java.util.*;
36 import java.util.List JavaDoc;
37
38 public class StackTablePane
39     extends AJPanel
40     implements StopListener {
41
42     private JTable table = null;
43     private JScrollPane scrollPane = null;
44
45     public StackTablePane(GUIDebugger debugger) {
46         super(debugger);
47         debugger.getDebugger().addStopListener(this);
48         setLayout(new BorderLayout());
49         ThreadReference threadRef = null;
50         try {
51             threadRef = guid().getDebugger().getDefaultThread();
52         } catch (DebuggerException de) {
53         }
54         table = new StackTable(new StackTableModel(threadRef));
55         table.setDefaultRenderer(Integer JavaDoc.class, new IntegerTableCellRenderer());
56         scrollPane = new JScrollPane(table);
57         add(scrollPane, BorderLayout.CENTER);
58     }
59
60     protected JTable getTable() {
61         return table;
62     }
63
64     /* StopListener */
65     public void accessWatchpointEvent(AccessWatchpointEvent e) {
66         showForThread(e.thread());
67     }
68     public void breakpointEvent(BreakpointEvent e) {
69         showForThread(e.thread());
70     }
71     public void exceptionEvent(ExceptionEvent e) {
72         showForThread(e.thread());
73     }
74     public void modificationWatchpointEvent(ModificationWatchpointEvent e) {
75         showForThread(e.thread());
76     }
77     public void stepEvent(StepEvent e) {
78         showForThread(e.thread());
79     }
80
81     public void showForThread(ThreadReference threadRef) {
82         table.setModel(new StackTableModel(threadRef));
83         try {
84             table.setRowSelectionInterval(0, 0);
85         } catch (IllegalArgumentException JavaDoc iae) {
86         } catch (IndexOutOfBoundsException JavaDoc ioobe) {
87         }
88     }
89
90     public static String JavaDoc d() { return "Stack Table Pane"; }
91     public String JavaDoc toString() { return d(); }
92 }
93
94 class StackTable extends JTable {
95     public StackTable(TableModel model) {
96         super(model);
97         setUI(new StackTableUI());
98     }
99
100     public void setModel(TableModel model) {
101         super.setModel(model);
102         TableColumn col = getColumnModel().getColumn(0);
103         int charWidth = 10;
104         col.setWidth(4 * charWidth);
105         col.setMinWidth(3 * charWidth);
106         col.setMaxWidth(5 * charWidth);
107     }
108
109     class StackTableUI extends BasicTableUI {
110         protected MouseInputListener createMouseInputListener() {
111             return new BasicTableUI.MouseInputHandler() {
112                 public void mouseClicked(MouseEvent e) {
113                     int row = StackTable.this.getSelectedRow();
114                     int col = StackTable.this.getSelectedColumn();
115                     int x = e.getX();
116                     int y = e.getY();
117                     AJStackFrameFormatter.MethodAndSource
118                         ms = (AJStackFrameFormatter.MethodAndSource)
119                                 StackTable.this.getModel().getValueAt
120                                                                 (row, 1);
121                         if (!ms.isValid()) {
122                             return;
123                         }
124                         String JavaDoc s = ms.getFullPath();
125                         final int l = ms.lineNumber;
126                     if (SwingUtilities.isRightMouseButton(e) ||
127                         SwingUtilities.isMiddleMouseButton(e)) {
128                         JPopupMenu popup = new JPopupMenu();
129                         final String JavaDoc source = AJLineMapper.removeRoot(s);
130                         JMenuItem item = new JMenuItem("Set breakpoint");
131                         popup.add(item);
132                         final String JavaDoc cmd = "stop on " + source + ":" + l;
133
134                         //XXX Can't get the popup right, now
135
item.addActionListener(new ActionListener() {
136                             public void actionPerformed(ActionEvent _) {
137                                 //XXX Total hack
138
// ComponentRepository.getCommandLine().executeCommand(cmd);
139
Integer JavaDoc ing = new Integer JavaDoc(l);
140                             }
141                         });
142                         popup.show(StackTable.this,
143                                    x + popup.getWidth()/2,
144                                    y + popup.getHeight()/2);
145
146
147                     } else if (e.getClickCount() == 2) {
148                         ComponentRepository.getSourcePane().showSourceForFileAndLine(s, l);
149                     }
150                 }
151             };
152         }
153     }
154 }
155
156 class StackTableModel extends DefaultTableModel {
157
158     private final String JavaDoc[] colNames
159         = {"#", "Location"};
160     //private final Class[] colClasses = {Integer.class, String.class};
161
private final Class JavaDoc[] colClasses
162         = {Integer JavaDoc.class, AJStackFrameFormatter.MethodAndSource.class};
163
164     public StackTableModel(ThreadReference threadRef) {
165         if (threadRef != null) {
166             colNames[1] += ": " + AJThreadFormatter.format(threadRef);
167         }
168         setColumnIdentifiers(colNames);
169         if (threadRef != null) {
170             try {
171 // List stack = threadRef.frames();
172
List JavaDoc stack = ComponentRepository.getAJDebugger().frames(threadRef);
173                 for (int i = 0; i < stack.size(); i++) {
174                     Object JavaDoc[] os = { new Integer JavaDoc(i+1),
175                                     AJStackFrameFormatter.format((StackFrame) stack.get(i)) };
176                     addRow(os);
177                 }
178             } catch (IncompatibleThreadStateException e) {
179             }
180         }
181     }
182
183     public Class JavaDoc getColumnClass(int col) {
184         return colClasses[col];
185     }
186
187     public boolean isCellEditable(int row, int col) {
188         return false;
189     }
190
191     String JavaDoc format(StackFrame frame) {
192         if (frame == null) {
193             return "<BAD FRAME>";
194         }
195         Location loc = frame.location();
196         Method meth = loc.method();
197         String JavaDoc str = meth.declaringType().name() + "." + meth.name() + " (";
198         if (meth instanceof Method && ((Method)meth).isNative()) {
199             str += "native method";
200         } else if (loc.lineNumber() > -1) {
201             try {
202                 str += loc.sourceName();
203             } catch (AbsentInformationException e) {
204                 str += "<unknown>";
205             }
206             str += ":";
207             str += loc.lineNumber();
208         } else {
209             str += "<unknown>";
210         }
211         str += ")";
212         return str;
213     }
214
215     public String JavaDoc toString() {
216         return "Stack Table Pane";
217     }
218 }
219
220 class IntegerTableCellRenderer extends DefaultTableCellRenderer {
221     public Component getTableCellRendererComponent(JTable table,
222                                                    Object JavaDoc value,
223                                                    boolean isSelected,
224                                                    boolean hasFocus,
225                                                    int row,
226                                                    int col)
227     {
228         super.getTableCellRendererComponent
229             (table, value, isSelected, hasFocus, row, col);
230
231         if (value != null) {
232             setHorizontalAlignment(SwingConstants.CENTER);
233             setText("[" + value + "]");
234         }
235         return this;
236     }
237 }
238
Popular Tags