KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.aspectj.debugger.ide.*;
27
28 import java.awt.*;
29 import java.awt.event.*;
30 import java.util.*;
31 import java.util.List JavaDoc;
32 import javax.swing.*;
33 import com.sun.jdi.*;
34 import com.sun.jdi.event.*;
35
36
37 public class AJToolBar
38     extends JToolBar
39     implements AJActionsConsts,
40                ThreadListener,
41                VMListener {
42
43     JComboBox comboBox = comboBox();
44     JToolBar toolBar = new JToolBar();
45     final static String JavaDoc ALL_THREADS = "All threads";
46     final static int FONT_SIZE = 10;
47     final static int SC_FONT_SIZE = 8;
48     final static double WIDTH_FACTOR = .7;
49     final static double HEIGHT_FACTOR = .4;
50     private AJDebugger debugger;
51
52     public AJToolBar(GUIDebugger guid) {
53
54         this.debugger = guid.getDebugger();
55         debugger.addThreadListener(this);
56         debugger.addVMListener(this);
57
58         String JavaDoc mode = Modes.getMode();
59         if (mode.equals(Modes.GUI)) {
60             createGuiToolbar(guid);
61 // }
62
// else if (mode.equals(Modes.JBUILDER)
63
// || mode.equals(Modes.JBUILDER3)
64
// || mode.equals(Modes.JBUILDER4)) {
65
// createJBuilderToolbar((JBuilderGUIDebugger)guid);
66
} else {
67             createForteToolbar(guid);
68         }
69     }
70
71     private void createForteToolbar(GUIDebugger guid) {
72         badd(new ForteRunAction(guid));
73         badd(new ContAction(guid));
74         //!!! attach
75
badd(new QuitAction(guid));
76         badd(new SuspendAction(guid, threadGetter()));
77         badd(new ForteResumeAction(guid, threadGetter()));
78         badd(new StepAction(guid));
79         badd(new StepIAction(guid));
80         badd(new NextAction(guid));
81         badd(new StepUpAction(guid));
82         //!!! add/remove
83
//badd(new BreakpointAction(guid));
84
badd(new IDELineBreakpointAction(guid), "Add Breakpoint");
85         badd(new WatchAction(guid));
86     }
87
88 // private void createJBuilderToolbar(JBuilderGUIDebugger guid) {
89
// badd(new JBuilderQuitAction(guid), "Reset Program");
90
// badd(new JBuilderRunAction(guid), "Restart Program", "Cont", guid);
91
// badd(new SuspendAction(guid, threadGetter()), "Pause Program");
92
// jbSeparator();
93
// //smart step: only in professional edition
94
// badd(new NextAction(guid), "Step Over");
95
// badd(new StepAction(guid), "Step Into");
96
// badd(new StepUpAction(guid), "Step Out");
97
// badd(new IDELineBreakpointAction(guid), "Add Breakpoint");
98
// badd(new WatchAction(guid), "Add Watch");
99
// badd(new WhereAction(guid, threadGetter()), "Show Current Frame");
100
// add(new CommandLinePanel(guid));
101
// }
102

103     private void jbSeparator() {
104         addSeparator();
105     }
106
107      private void createGuiToolbar(GUIDebugger guid) {
108
109          // Main
110
badd(new QuitAction(guid));
111          badd(new UseAction(guid));
112          badd(new DisconnectAction(guid));
113          badd(new RunAction(guid));
114          addSeparator();
115
116          // Breakpoints
117
badd(new BreakpointAction(guid));
118          badd(new CatchAction(guid));
119          badd(new ClearAction(guid));
120          badd(new ContAction(guid));
121          badd(new NextAction(guid));
122          badd(new StepAction(guid));
123          badd(new StepUpAction(guid));
124          badd(new StepIAction(guid));
125          addSeparator();
126
127         // Threads
128
badd(new SuspendAction(guid, threadGetter()));
129         badd(new ResumeAction(guid, threadGetter()));
130         badd(new WhereAction(guid, threadGetter()));
131         comboBox = comboBox();
132         add(comboBox);
133         addSeparator();
134
135         // Objects
136
badd(new DumpAction(guid));
137         badd(new EvalAction(guid));
138         badd(new FieldsAction(guid));
139         badd(new LocalsAction(guid));
140         badd(new PrintAction(guid));
141
142         add(Box.createHorizontalGlue());
143     }
144
145     public AJDebugger getDebugger() {
146         return debugger;
147     }
148
149     /****************************** ThreadListener ******************************/
150
151     public void threadDeathEvent(ThreadDeathEvent e) {
152         removeThread(e.thread());
153     }
154
155     public void threadStartEvent(ThreadStartEvent e) {
156         addThread(e.thread());
157     }
158
159     /****************************** VMListener ******************************/
160
161     public void vmDeathEvent(VMDeathEvent e) {
162         clearThreads();
163     }
164
165     public void vmDisconnectEvent(VMDisconnectEvent e) {
166         clearThreads();
167     }
168
169     public void vmStartEvent(VMStartEvent e) {
170         clearThreads();
171         try {
172             Iterator iter = getDebugger().vm().allThreads().iterator();
173             while (iter.hasNext()) {
174                 addThread((ThreadReference) iter.next());
175             }
176         } catch (Exception JavaDoc ee) {}
177     }
178
179     JComboBox comboBox() {
180         if (comboBox == null) {
181             comboBox = new JComboBox();
182         }
183         comboBox.addItem(new ThreadPair(ALL_THREADS, -1));
184         comboBox.setPreferredSize(new Dimension(100, 30));
185         return comboBox;
186     }
187
188     ThreadGetter threadGetter() {
189         return new ThreadGetter() {
190             public String JavaDoc getThread() {
191                 AJToolBar.ThreadPair pair =
192                     (AJToolBar.ThreadPair) comboBox.getSelectedItem();
193                 if (pair.id != -1) {
194                     return pair.id + "";
195                 }
196                 return "";
197             }
198         };
199     }
200
201     private void addToT(JComponent c) {
202         toolBar.add(c);
203     }
204
205     private void addT(String JavaDoc title) {
206         toolBar.setToolTipText(title);
207         toolBar.setName(title);
208         add(toolBar);
209         toolBar = new JToolBar();
210     }
211
212     private void badd(AbstractActionStruct a) {
213         badd(a, null);
214     }
215
216
217     private void badd(AbstractActionStruct a, String JavaDoc toolTipText) {
218         badd(a, toolTipText, null, null);
219     }
220
221     private void badd(AbstractActionStruct a, String JavaDoc toolTipText, final String JavaDoc toolTipText2,
222                       final GUIDebugger guid) {
223         Icon icon = (Icon) a.getValue(ICON);
224         JButton b;
225         if (guid == null) {
226             b = new JButton(icon);
227         } else {
228             b = new JButton(icon) {
229                     public String JavaDoc getToolTipText() {
230                         return guid.getDebugger().isRunning() ? toolTipText2 : super.getToolTipText();
231                     }
232                 };
233         }
234         String JavaDoc name = (String JavaDoc) a.getValue(LABEL);
235         JLabel label = new JLabel(name);
236         String JavaDoc shortCutStr = KeyEvent.getKeyModifiersText(a.mask()) + "-" +
237                              KeyEvent.getKeyText(a.key());
238         JLabel shortCutLabel = new JLabel(shortCutStr);
239         Font f = label.getFont();
240         label.setFont(new Font(f.getName(), f.getStyle(), SC_FONT_SIZE));
241         shortCutLabel.setFont(new Font(f.getName(), f.getStyle(), FONT_SIZE));
242         b.addActionListener(a);
243         if (toolTipText == null) {
244             b.setToolTipText(name + " [" + shortCutStr + "]");
245         } else {
246             b.setToolTipText(toolTipText);
247         }
248         forward(b);
249         JPanel p = new JPanel();
250         p.setLayout(new BorderLayout());
251         p.add(b, BorderLayout.CENTER);
252         forward(p);
253         changeDim(p);
254         add(p);
255     }
256
257     void forward(JComponent c) {
258         c.setNextFocusableComponent(ComponentRepository.getCommandLine());
259     }
260
261     void changeDim(JComponent b) {
262         Dimension d = b.getPreferredSize();
263         double w = d.getWidth();
264         double h = d.getHeight();
265         double wp = WIDTH_FACTOR * w;
266         double hp = HEIGHT_FACTOR * h;
267         Dimension newD = new Dimension((int) wp,(int) h);
268         b.setMaximumSize(newD);
269         b.setMinimumSize(newD);
270         b.setPreferredSize(newD);
271     }
272
273     private Action action(final String JavaDoc str, int icon) {
274         return new AbstractAction(str, AJIcons.getIcon(icon)) {
275             public void actionPerformed(ActionEvent e) {
276                 execute(str);
277             }
278         };
279     }
280
281     void execute(String JavaDoc str) {
282         CommandLine cl = ComponentRepository.getCommandLine();
283         if (cl != null) {
284             cl.executeCommand(str);
285         }
286     }
287
288
289     void clearThreads() {
290         if (comboBox != null) {
291             comboBox.removeAllItems();
292         }
293         comboBox = comboBox();
294     }
295
296     void addThread(ThreadReference threadRef) {
297         addThread(threadRef.name(), threadRef.uniqueID());
298     }
299
300     void addThread(String JavaDoc name, long id) {
301         if (comboBox == null) return;
302         ThreadPair newPair = new ThreadPair(name, id);
303         int i = 1;
304         int size = comboBox.getModel().getSize();
305         long pid = -1;
306         ThreadPair pair = null;
307         while (i < size &&
308                (pid = ((ThreadPair) comboBox.getModel().getElementAt(i)).id) < id) {
309             i++;
310         }
311         if (pid != id ) {
312             comboBox.insertItemAt(newPair, i);
313         }
314     }
315
316     void removeThread(ThreadReference threadRef) {
317         removeThread(threadRef.name(), threadRef.uniqueID());
318     }
319
320     void removeThread(String JavaDoc name, long id) {
321         if (comboBox == null) return;
322         for (int i = 0; i < comboBox.getModel().getSize(); i++) {
323             ThreadPair pair = (ThreadPair) comboBox.getModel().getElementAt(i);
324             if (pair.name.equals(name) && pair.id == id) {
325                 comboBox.removeItemAt(i);
326             }
327         }
328     }
329
330     static class ThreadPair {
331         String JavaDoc name;
332         long id;
333         ThreadPair(String JavaDoc name, long id) {
334             this.name = name;
335             this.id = id;
336         }
337         public String JavaDoc toString() {
338             return name + (id == -1 ? "" : " (" + id + ")");
339         }
340         public boolean equals(Object JavaDoc o) {
341             if (!(o instanceof ThreadPair)) {
342                 return super.equals(o);
343             }
344             return ( ((ThreadPair) o).name.equals(name) &&
345                      ((ThreadPair) o).id == id );
346         }
347     }
348
349     public static String JavaDoc d() { return "Toolbar"; }
350     public String JavaDoc toString() { return d(); }
351 }
352
Popular Tags