KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.aspectj.debugger.gui;
23
24 import com.sun.jdi.*;
25 import com.sun.jdi.event.*;
26 import org.aspectj.debugger.base.*;
27 import org.aspectj.debugger.request.*;
28 import org.aspectj.tools.ide.*;
29 import java.awt.*;
30 import java.awt.event.*;
31 import java.io.*;
32 import java.util.*;
33 import java.util.List JavaDoc;
34 import javax.swing.*;
35
36 /**
37  * SourcePane.java
38  *
39  *
40  * Created: Tue Sep 05 14:08:06 2000
41  *
42  * @author <a HREF="mailto:palm@parc.xerox.com"Jeffrey Palm</a>
43  */

44
45 public class SourcePane
46     extends AbstractSourcePane implements ClassListener
47 {
48
49     private HashMap models = new HashMap();
50     private final Font font = new Font("monospaced", Font.PLAIN, 10);
51     private JScrollPane scrollPane;
52     private JList jlist;
53     private TitlePanel titlePanel = new TitlePanel();
54
55     public SourcePane(GUIDebugger guid) {
56         super(guid);
57         jlist = new JList(new DefaultListModel());
58         jlist.setCellRenderer(new SourcePaneCellRenderer());
59         jlist.addMouseListener(new SourcePaneMouseListener());
60         jlist.setFont(font);
61         add(scrollPane = new JScrollPane(jlist));
62         add(titlePanel, BorderLayout.SOUTH);
63         guid.getDebugger().addClassListener(this);
64     }
65
66     /************************ AbstractSourcePane ***************************/
67
68     public boolean showSource(String JavaDoc relativePath) {
69         SourceListModel model = getModel(relativePath);
70         if (model == null) return false;
71         setModel(model);
72         titlePanel.setTitle(relativePath);
73         return true;
74     }
75
76     public void showLineForCurrentModel(int line, boolean isAtBreakpoint) {
77         if (line > 0 && line <= jlist.getModel().getSize()) {
78             jlist.ensureIndexIsVisible(line-1);
79             jlist.ensureIndexIsVisible(line-1-4);
80             jlist.ensureIndexIsVisible(line-1+4);
81             jlist.setSelectedIndex(line-1);
82         }
83     }
84
85     public String JavaDoc getSourceName() {
86         return ((SourceListModel) jlist.getModel()).getSourceName();
87     }
88
89     private void setModel(SourceListModel model) {
90         if (model != null) {
91             markLines(model);
92             jlist.setModel(model);
93         } else {
94             //TODO
95
System.out.println("Model is null");
96         }
97     }
98
99     private SourceListModel getModel(ReferenceType refType) {
100         SourceListModel model = null;
101         if ((model = (SourceListModel) models.get(refType)) != null) {
102             return model;
103         }
104         try {
105             String JavaDoc sourceName = debugger().sourceName(refType); //refType.sourceName();
106
if (false) throw new AbsentInformationException();
107             if ((model = getModel(sourceName)) != null) {
108                 models.put(refType, model);
109                 return model;
110             }
111             String JavaDoc strippedSourceName = debugger().strip(sourceName);
112             if ((model = getModel(strippedSourceName)) != null) {
113                 models.put(sourceName, model);
114                 models.put(refType, model);
115                 return model;
116             }
117         } catch (AbsentInformationException aie) {
118         }
119         return null;
120     }
121
122     private SourceListModel getModel(Request request) {
123         if (request instanceof ClassMethodBreakpointRequestAction) {
124             ClassMethodBreakpointRequestAction ba =
125                 (ClassMethodBreakpointRequestAction) request;
126             return getModel(ba.getSourceName());
127         } else if (request instanceof ClassBreakpointRequestAction) {
128             ClassBreakpointRequestAction ba =
129                 (ClassBreakpointRequestAction) request;
130             String JavaDoc className = ba.getClassName();
131             SourceListModel model = (SourceListModel) models.get(className);
132             if (model != null) {
133                 return model;
134             }
135             try {
136                 Iterator iter = debugger().getVM().allClasses().iterator();
137                 while (iter.hasNext()) {
138                     ReferenceType refType = (ReferenceType) iter.next();
139                     if (className.equals(refType.name()) &&
140                         (model = getModel(refType)) != null) {
141                         return model;
142                     }
143                 }
144             } catch (NoVMException nvme) {
145             }
146         } else if (request instanceof BreakpointRequestAction) {
147             SourceLine sl = debugger.getSourceLine((BreakpointRequestAction) request);
148             return getModel(sl.filename);
149         }
150         return null;
151     }
152
153     private SourceListModel getModel(String JavaDoc relativePath) {
154         if (relativePath == null || relativePath.equals("<not-available>")) {
155             return null;
156         }
157         String JavaDoc oldRelativePath = relativePath;
158         relativePath = guid().getDebugger().removeSourcePath(relativePath);
159         SourceListModel model = (SourceListModel) models.get(relativePath);
160         if (model != null) {
161             return model;
162         }
163         List JavaDoc lines = debugger.getSourceManager().getSourceLines(relativePath);
164         if (lines != null) {
165             model = new SourceListModel(lines, relativePath);
166             models.put(relativePath, model);
167             models.put(oldRelativePath, model);
168             String JavaDoc fullPath = AJLineMapper.getFullFileNameFromFile(relativePath, false).toLowerCase();
169             models.put(fullPath, model);
170         } else {
171         }
172         return model;
173     }
174
175     private void markLines(SourceListModel model) {
176         try {
177             Iterator iter = debugger().getVM().allClasses().iterator();
178             while (iter.hasNext()) {
179                 ReferenceType refType = (ReferenceType) iter.next();
180                 try {
181 // String sourceName = refType.sourceName();
182
String JavaDoc sourceName = debugger().sourceName(refType);
183                     if (sourceName.equals(model.getSourceName())) {
184                         model.markLines(refType);
185                     }
186                 } catch (Exception JavaDoc e) { //AbsentInformationException aie) {
187

188                 }
189             }
190         } catch (NoVMException nvme) {
191         } catch (VMDisconnectedException vmde) {
192         }
193     }
194
195     /************************** DebuggerListener **************************/
196
197     public void requestSet(String JavaDoc filename, int line, BreakpointRequestAction ba) {
198         SourceListModel model = getModel(filename);
199         if (model != null) {
200             model.verifyBreakpoint(line, ba);
201         }
202     }
203
204     public void requestClear(String JavaDoc filename, int line, BreakpointRequestAction ba) {
205         SourceListModel model = getModel(ba);
206         if (model != null) {
207             model.clearBreakpoint(line);
208         }
209     }
210
211     public void requestDeferred(String JavaDoc filename, int line, BreakpointRequestAction ba) {
212         SourceListModel model = getModel(ba);
213         if (model != null) {
214             model.unverifyBreakpoint(line, ba);
215         }
216     }
217
218     /****************************** VMListener ******************************/
219
220     public void vmStartEvent(VMStartEvent e) {
221         try {
222             Iterator iter = debugger.getVM().allClasses().iterator();
223             while (iter.hasNext()) {
224                 ReferenceType refType = (ReferenceType) iter.next();
225                 SourceListModel model = getModel(refType);
226                 if (model != null) {
227                     model.markLines(refType);
228                 }
229             }
230         } catch (NoVMException nvme) {
231         }
232     }
233
234     /**************************** Inner Classes ****************************/
235
236    class TitlePanel extends JPanel {
237         private JLabel label = new JLabel();
238         public TitlePanel() {
239             super();
240             setLayout(new FlowLayout());
241             add(new JLabel("Source: "));
242             add(label);
243         }
244         public void setTitle(String JavaDoc str) {
245             set(str);
246         }
247         public void loading(String JavaDoc str) {
248             set("Loading..." + str);
249         }
250         private void set(String JavaDoc str) {
251             String JavaDoc relativePath = str;
252             int i = relativePath.length() - 1;
253             for (; i >= 0; i--) {
254                 char c = relativePath.charAt(i);
255                 if (c == File.pathSeparatorChar ||
256                     c == '\\' ||
257                     c == '/') break;
258             }
259             if (i > -1 && i < relativePath.length()-1) {
260                 relativePath = relativePath.substring(i+1);
261             }
262             label.setText(relativePath);
263         }
264     }
265
266     class SourcePaneMouseListener extends MouseAdapter {
267         public void mousePressed(MouseEvent e) {
268             popup(e);
269         }
270
271         private void popup(MouseEvent e) {
272             JList jlist = (JList) e.getSource();
273             int x = e.getX();
274             int y = e.getY();
275             SourceManager.SourceLine sl = (SourceManager.SourceLine) jlist.getSelectedValue();
276             if (SwingUtilities.isMiddleMouseButton(e) ||
277                 SwingUtilities.isRightMouseButton(e)) {
278                 show(jlist, sl, x, y);
279             }
280         }
281
282         private void show(JList jlist, SourceManager.SourceLine sl, int x, int y) {
283             if (sl == null) { System.out.println("sl = null");
284                 return;
285             }
286             BreakpointRequestAction req = sl.getRequest();
287             if (req != null) {
288                 System.out.println("request=" + req.getProto());
289             }
290             final String JavaDoc sourceName = sl.getSourceName();
291             final int line = jlist.getSelectedIndex() + 1;
292             JPopupMenu popup = new JPopupMenu();
293             JMenuItem item;
294             switch (sl.getStatus()) {
295                 case SourceManager.SourceLine.NONE:
296                     popup.add(item("stop at " + sourceName + ":" + line,
297                                    "stop at line " + line));
298                     break;
299                 case SourceManager.SourceLine.VERIFIED:
300                 case SourceManager.SourceLine.UNVERIFIED:
301                     popup.add(item("clear " + req.getProto(),
302                                    "clear " + req.getProto()));
303             }
304             popup.show(jlist, x + popup.getWidth()/2,
305                               y + popup.getHeight()/2);
306         }
307
308         private JMenuItem item(final String JavaDoc setString, String JavaDoc titleString) {
309             JMenuItem item = new JMenuItem(titleString);
310             item.addActionListener(new ActionListener() {
311                     public void actionPerformed(ActionEvent e) {
312                         debugger.execute(setString);
313                     }
314                 });
315             return item;
316         }
317     }
318
319     class SourcePaneCellRenderer extends DefaultListCellRenderer {
320
321         private final Font font = new Font("monospaced", Font.PLAIN, 10);
322         private final Color BLUE = Color.blue.darker();
323         private final Color RED = Color.red.darker();
324
325         public Component getListCellRendererComponent(JList list,
326                                                       Object JavaDoc value,
327                                                       int index,
328                                                       boolean isSelected,
329                                                       boolean cellHasFocus) {
330             super.getListCellRendererComponent(list, value, index,
331                                                isSelected, cellHasFocus);
332             SourceManager.SourceLine sl = (SourceManager.SourceLine) value;
333             if (sl.getStatus() == SourceManager.SourceLine.NONE && sl.isExecutable()) {
334                 setIcon(AJIcons.getIcon(AJIcons.EXECUTABLE_ICON));
335             } else {
336                 switch (sl.getStatus()) {
337                 case SourceManager.SourceLine.NONE:
338                     setIcon(AJIcons.getIcon(AJIcons.BLANK_ICON));
339                     break;
340                 case SourceManager.SourceLine.UNVERIFIED:
341                     setIcon(AJIcons.getIcon(AJIcons.UNVERIFIED_BREAKPOINT_ICON));
342                     break;
343                 case SourceManager.SourceLine.VERIFIED:
344                     setIcon(AJIcons.getIcon(AJIcons.VERIFIED_BREAKPOINT_ICON));
345                 }
346             }
347             String JavaDoc str = shrink(SourcePane.this.guid().getDebugger().sourceLine(sl));
348             setText(str);
349 // if (debugger.isStopped(sl)) {
350
// setBackground(RED);
351
// } else {
352
// setBackground(DEFAULT);
353
// }
354
return this;
355         }
356
357         public Font getFont() {
358             return font;
359         }
360
361         private String JavaDoc shrink(String JavaDoc str) {
362             return str.trim();
363         }
364     }
365
366     class SourceListModel
367         extends AbstractListModel
368         implements SourceManager.LineUpdatable {
369
370         private List JavaDoc lines;
371         private String JavaDoc sourceName;
372
373         public SourceListModel(List JavaDoc lines, String JavaDoc sourceName) {
374             this.lines = lines;
375             this.sourceName = sourceName;
376             setUpdater(lines);
377         }
378
379         public void update(int line) {
380             fireContentsChanged(this, line, line);
381         }
382
383         public int getSize() {
384             return lines.size();
385         }
386
387         public void setExecutable(int line, String JavaDoc className) {
388             line(line).ex(className);
389             update(line);
390         }
391
392         public void verifyBreakpoint(int line, BreakpointRequestAction request) {
393             line(line).verify(request);
394             update(line);
395         }
396
397         public void unverifyBreakpoint(int line, BreakpointRequestAction request) {
398             line(line).unverify(request);
399             update(line);
400         }
401
402         public void clearBreakpoint(int line) {
403             line(line).clear();
404             update(line);
405         }
406
407         SourceManager.SourceLine line(int line) {
408             return (SourceManager.SourceLine) getElementAt(line-1);
409         }
410
411         public String JavaDoc getSourceName() {
412             return sourceName;
413         }
414
415         public Object JavaDoc getElementAt(int index) {
416             SourceManager.SourceLine line = SourceManager.SourceLine.emptyLine;
417             try {
418                 line = (SourceManager.SourceLine) lines.get(index);
419             } catch (ArrayIndexOutOfBoundsException JavaDoc aioobe) {
420             }
421             return line;
422         }
423
424         public boolean equals(Object JavaDoc other) {
425             if (!(other instanceof SourceListModel)) {
426                 return super.equals(other);
427             }
428             SourceListModel model = (SourceListModel) other;
429             return getSourceName().equals(model.getSourceName());
430         }
431
432         /**
433          * FIXES: removed
434          */

435         public void markLines(ReferenceType refType) {
436 // String className = refType.name();
437
// try {
438
// Iterator iter = refType.allLineLocations().iterator();
439
// while (iter.hasNext()) {
440
// SourceLine sl = debugger().sourceLine((Location) iter.next());
441
// if (sl.line > 0) {
442
// setExecutable(sl.line, className);
443
// }
444
// }
445
// } catch (AbsentInformationException aie) {
446
// } catch (ClassNotPreparedException cnpe) {
447
// }
448
}
449
450         private void setUpdater(List JavaDoc lines) {
451             Iterator iter = lines.iterator();
452             while (iter.hasNext()) {
453                 ((SourceManager.SourceLine) iter.next()).setUpdater(this);
454             }
455         }
456
457         public String JavaDoc toString() {
458             return sourceName + ":" + (lines != null ? lines.size() : -1);
459         }
460     }
461
462     /* ClassListener */
463     public void classPrepareEvent(ClassPrepareEvent e) {
464         SourceListModel model = getModel(e.referenceType());
465         if (model != null) {
466             model.markLines(e.referenceType());
467         }
468     }
469     public void classUnloadEvent(ClassUnloadEvent e) {
470         //!!! unmarklines?
471
}
472
473 }
474
Popular Tags