KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > debugger > panels > DBControlPanel


1 package org.antlr.works.debugger.panels;
2
3 import org.antlr.works.debugger.Debugger;
4 import org.antlr.works.debugger.events.DBEvent;
5 import org.antlr.works.debugger.tivo.DBPlayerContextInfo;
6 import org.antlr.works.debugger.tivo.DBRecorder;
7 import org.antlr.works.prefs.AWPrefs;
8 import org.antlr.works.stats.StatisticsAW;
9 import org.antlr.works.swing.Toolbar;
10 import org.antlr.works.utils.IconManager;
11 import org.antlr.works.utils.NumberSet;
12
13 import javax.swing.*;
14 import java.awt.*;
15 import java.awt.event.ActionEvent JavaDoc;
16 import java.awt.event.ActionListener JavaDoc;
17 import java.util.Set JavaDoc;
18 /*
19
20 [The "BSD licence"]
21 Copyright (c) 2005-2006 Jean Bovet
22 All rights reserved.
23
24 Redistribution and use in source and binary forms, with or without
25 modification, are permitted provided that the following conditions
26 are met:
27
28 1. Redistributions of source code must retain the above copyright
29 notice, this list of conditions and the following disclaimer.
30 2. Redistributions in binary form must reproduce the above copyright
31 notice, this list of conditions and the following disclaimer in the
32 documentation and/or other materials provided with the distribution.
33 3. The name of the author may not be used to endorse or promote products
34 derived from this software without specific prior written permission.
35
36 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
37 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
39 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
40 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
45 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46
47 */

48
49 public class DBControlPanel extends JPanel {
50
51     protected JButton stopButton;
52     protected JButton goToStartButton;
53     protected JButton goToEndButton;
54     protected JButton fastForwardButton;
55     protected JButton backButton;
56     protected JButton forwardButton;
57     protected JButton stepOverButton;
58
59     protected JCheckBox breakAllButton;
60     protected JCheckBox breakLocationButton;
61     protected JCheckBox breakConsumeButton;
62     protected JCheckBox breakLTButton;
63     protected JCheckBox breakExceptionButton;
64
65     protected JLabel infoLabel;
66
67     protected Debugger debugger;
68
69     public DBControlPanel(Debugger debugger) {
70         super(new BorderLayout());
71
72         this.debugger = debugger;
73
74         Toolbar box = Toolbar.createHorizontalToolbar();
75         box.addElement(stopButton = createStopButton());
76         box.addGroupSeparator();
77         box.addElement(goToStartButton = createGoToStartButton());
78         box.addElement(backButton = createStepBackButton());
79         box.addElement(forwardButton = createStepForwardButton());
80         box.addElement(stepOverButton = createStepOverButton());
81         box.addElement(fastForwardButton = createFastForwardButton());
82         box.addElement(goToEndButton = createGoToEndButton());
83         box.addGroupSeparator();
84         createBreakEvents(box);
85         box.addElement(Box.createHorizontalGlue());
86         box.addElement(createInfoLabelPanel());
87
88         add(box, BorderLayout.CENTER);
89     }
90
91     public JButton createStopButton() {
92         JButton button = new JButton(IconManager.shared().getIconStop());
93         button.setToolTipText("Stop");
94         button.setFocusable(false);
95         button.addActionListener(new ActionListener JavaDoc() {
96             public void actionPerformed(ActionEvent JavaDoc event) {
97                 debugger.debuggerStop(false);
98                 StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_DEBUGGER_STOP);
99             }
100         });
101         return button;
102     }
103
104     public JButton createStepBackButton() {
105         JButton button = new JButton(IconManager.shared().getIconStepBackward());
106         button.setToolTipText("Step Back");
107         button.addActionListener(new ActionListener JavaDoc() {
108             public void actionPerformed(ActionEvent JavaDoc event) {
109                 debugger.getRecorder().stepBackward(getBreakEvent());
110                 updateInterfaceLater();
111                 StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_DEBUGGER_STEP_BACK);
112             }
113         });
114         return button;
115     }
116
117     public JButton createStepForwardButton() {
118         JButton button = new JButton(IconManager.shared().getIconStepForward());
119         button.setToolTipText("Step Forward");
120         button.addActionListener(new ActionListener JavaDoc() {
121             public void actionPerformed(ActionEvent JavaDoc event) {
122                 debugger.getRecorder().stepForward(getBreakEvent());
123                 updateInterfaceLater();
124                 StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_DEBUGGER_STEP_FORWARD);
125             }
126         });
127         return button;
128     }
129
130     public JButton createStepOverButton() {
131         JButton button = new JButton(IconManager.shared().getIconStepOver());
132         button.setToolTipText("Step Over");
133         button.addActionListener(new ActionListener JavaDoc() {
134             public void actionPerformed(ActionEvent JavaDoc event) {
135                 debugger.getRecorder().stepOver();
136                 updateInterfaceLater();
137                 StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_DEBUGGER_STEP_OVER);
138             }
139         });
140         return button;
141     }
142
143     public JButton createGoToStartButton() {
144         JButton button = new JButton(IconManager.shared().getIconGoToStart());
145         button.setToolTipText("Go To Start");
146         button.setFocusable(false);
147         button.addActionListener(new ActionListener JavaDoc() {
148             public void actionPerformed(ActionEvent JavaDoc event) {
149                 debugger.resetMarkLocationInGrammar();
150                 debugger.getRecorder().goToStart();
151                 updateInterfaceLater();
152                 StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_DEBUGGER_GOTO_START);
153             }
154         });
155         return button;
156     }
157
158     public JButton createGoToEndButton() {
159         JButton button = new JButton(IconManager.shared().getIconGoToEnd());
160         button.setToolTipText("Go To End");
161         button.setFocusable(false);
162         button.addActionListener(new ActionListener JavaDoc() {
163             public void actionPerformed(ActionEvent JavaDoc event) {
164                 debugger.getRecorder().goToEnd();
165                 updateInterfaceLater();
166                 StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_DEBUGGER_GOTO_END);
167             }
168         });
169         return button;
170     }
171
172     public JButton createFastForwardButton() {
173         JButton button = new JButton(IconManager.shared().getIconFastForward());
174         button.setToolTipText("Fast forward");
175         button.setFocusable(false);
176         button.addActionListener(new ActionListener JavaDoc() {
177             public void actionPerformed(ActionEvent JavaDoc event) {
178                 debugger.getRecorder().fastForward();
179                 updateInterfaceLater();
180                 StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_DEBUGGER_FAST_FORWARD);
181             }
182         });
183         return button;
184     }
185
186     public void createBreakEvents(Toolbar box) {
187         box.addElement(new JLabel("Break on:"));
188         box.addElement(breakAllButton = createBreakButton("All"));
189         box.addElement(breakLocationButton = createBreakButton("Location"));
190         box.addElement(breakConsumeButton = createBreakButton("Consume"));
191         box.addElement(breakLTButton = createBreakButton("LT"));
192         box.addElement(breakExceptionButton = createBreakButton("Exception"));
193
194         AWPrefs.getPreferences().bindToPreferences(breakAllButton, AWPrefs.PREF_DEBUG_BREAK_ALL, false);
195         AWPrefs.getPreferences().bindToPreferences(breakLocationButton, AWPrefs.PREF_DEBUG_BREAK_LOCATION, false);
196         AWPrefs.getPreferences().bindToPreferences(breakConsumeButton, AWPrefs.PREF_DEBUG_BREAK_CONSUME, true);
197         AWPrefs.getPreferences().bindToPreferences(breakLTButton, AWPrefs.PREF_DEBUG_BREAK_LT, false);
198         AWPrefs.getPreferences().bindToPreferences(breakExceptionButton, AWPrefs.PREF_DEBUG_BREAK_EXCEPTION, false);
199     }
200
201     public JCheckBox createBreakButton(String JavaDoc title) {
202         JCheckBox button = new JCheckBox(title);
203         button.setFocusable(false);
204         button.addActionListener(new ActionListener JavaDoc() {
205             public void actionPerformed(ActionEvent JavaDoc e) {
206                 /** Select 'All' if no events are selected */
207                 if(getBreakEvent().isEmpty()) {
208                     breakAllButton.setSelected(true);
209                     AWPrefs.getPreferences().setBoolean(AWPrefs.PREF_DEBUG_BREAK_ALL, true);
210                 }
211             }
212         });
213         return button;
214     }
215
216     public JComponent createInfoLabelPanel() {
217         infoLabel = new JLabel();
218         return infoLabel;
219     }
220
221     public Set getBreakEvent() {
222         NumberSet set = new NumberSet();
223
224         if(breakAllButton.isSelected())
225             set.add(DBEvent.ALL);
226
227         if(breakLocationButton.isSelected())
228             set.add(DBEvent.LOCATION);
229
230         if(breakConsumeButton.isSelected())
231             set.add(DBEvent.CONSUME_TOKEN);
232
233         if(breakLTButton.isSelected())
234             set.add(DBEvent.LT);
235
236         if(breakExceptionButton.isSelected())
237             set.add(DBEvent.RECOGNITION_EXCEPTION);
238
239         return set;
240     }
241
242     public void updateStatusInfo() {
243         StringBuffer JavaDoc info = new StringBuffer JavaDoc();
244
245         String JavaDoc status = "-";
246         switch(debugger.getRecorder().getStatus()) {
247             case DBRecorder.STATUS_STOPPED: status = "Stopped"; break;
248             case DBRecorder.STATUS_STOPPING: status = "Stopping"; break;
249             case DBRecorder.STATUS_LAUNCHING: status = "Launching"; break;
250             case DBRecorder.STATUS_RUNNING: status = "Running"; break;
251             case DBRecorder.STATUS_BREAK: status = "Break on "+DBEvent.getEventName(debugger.getRecorder().getStoppedOnEvent()); break;
252         }
253
254         info.append(status);
255
256         DBPlayerContextInfo context = debugger.getPlayer().getContextInfo();
257         if(context.isBacktracking()) {
258             info.append(" (backtrack ");
259             info.append(context.getBacktrack());
260             info.append(")");
261         }
262
263         infoLabel.setText(info.toString());
264         updateInterface();
265     }
266
267     public void updateInterface() {
268         stopButton.setEnabled(debugger.getRecorder().getStatus() != DBRecorder.STATUS_STOPPED);
269
270         boolean enabled = debugger.getRecorder().isAlive();
271         boolean atBeginning = debugger.getRecorder().isAtBeginning();
272         boolean atEnd = debugger.getRecorder().isAtEnd();
273
274         backButton.setEnabled(enabled && !atBeginning);
275         forwardButton.setEnabled(enabled && !atEnd);
276         stepOverButton.setEnabled(enabled && !atEnd);
277         fastForwardButton.setEnabled(enabled && !atEnd);
278         goToStartButton.setEnabled(enabled && !atBeginning);
279         goToEndButton.setEnabled(enabled && !atEnd);
280     }
281
282     public void updateInterfaceLater() {
283         SwingUtilities.invokeLater(new Runnable JavaDoc() {
284             public void run() {
285                 updateInterface();
286             }
287         });
288     }
289
290 }
291
Popular Tags