Debian Codegraph @

rhino/toolsrc/org/mozilla/javascript/tools/debugger/Main.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 package org.mozilla.javascript.tools.debugger;
8
9 import java.io.InputStream;
10 import java.io.PrintStream;
11
12 import javax.swing.JFrame;
13
14 import org.mozilla.javascript.*;
15 import org.mozilla.javascript.tools.shell.Global;
16
17 /**
18 * Rhino script debugger main class. This class links together a
19 * debugger object ({@link Dim}) and a debugger GUI object ({@link SwingGui}).
20 */
21 public class Main {
22
23 /**
24 * The debugger.
25 */
26 private Dim dim;
27
28 /**
29 * The debugger frame.
30 */
31 private SwingGui debugGui;
32
33 /**
34 * Creates a new Main.
35 */
36 public Main(String title) {
37 dim = new Dim();
38 debugGui = new SwingGui(dim, title);
39 }
40
41 /**
42 * Returns the debugger window {@link JFrame}.
43 */
44 public JFrame getDebugFrame() {
45 return debugGui;
46 }
47
48 /**
49 * Breaks execution of the script.
50 */
51 public void doBreak() {
52 dim.setBreak();
53 }
54
55 /**
56 * Sets whether execution should break when a script exception is thrown.
57 */
58 public void setBreakOnExceptions(boolean value) {
59 dim.setBreakOnExceptions(value);
60 debugGui.getMenubar().getBreakOnExceptions().setSelected(value);
61 }
62
63 /**
64 * Sets whether execution should break when a function is entered.
65 */
66 public void setBreakOnEnter(boolean value) {
67 dim.setBreakOnEnter(value);
68 debugGui.getMenubar().getBreakOnEnter().setSelected(value);
69 }
70
71 /**
72 * Sets whether execution should break when a function is left.
73 */
74 public void setBreakOnReturn(boolean value) {
75 dim.setBreakOnReturn(value);
76 debugGui.getMenubar().getBreakOnReturn().setSelected(value);
77 }
78
79 /**
80 * Removes all breakpoints.
81 */
82 public void clearAllBreakpoints() {
83 dim.clearAllBreakpoints();
84 }
85
86 /**
87 * Resumes execution of the script.
88 */
89 public void go() {
90 dim.go();
91 }
92
93 /**
94 * Sets the scope to be used for script evaluation.
95 */
96 public void setScope(Scriptable scope) {
97 setScopeProvider(IProxy.newScopeProvider(scope));
98 }
99
100 /**
101 * Sets the {@link ScopeProvider} that provides a scope to be used
102 * for script evaluation.
103 */
104 public void setScopeProvider(ScopeProvider p) {
105 dim.setScopeProvider(p);
106 }
107
108 /**
109 * Sets the {@link SourceProvider} that provides the source to be displayed
110 * for script evaluation.
111 */
112 public void setSourceProvider(final SourceProvider sourceProvider) {
113 dim.setSourceProvider(sourceProvider);
114 }
115
116 /**
117 * Assign a Runnable object that will be invoked when the user
118 * selects "Exit..." or closes the Debugger main window.
119 */
120 public void setExitAction(Runnable r) {
121 debugGui.setExitAction(r);
122 }
123
124 /**
125 * Returns an {@link InputStream} for stdin from the debugger's internal
126 * Console window.
127 */
128 public InputStream getIn() {
129 return debugGui.getConsole().getIn();
130 }
131
132 /**
133 * Returns a {@link PrintStream} for stdout to the debugger's internal
134 * Console window.
135 */
136 public PrintStream getOut() {
137 return debugGui.getConsole().getOut();
138 }
139
140 /**
141 * Returns a {@link PrintStream} for stderr in the Debugger's internal
142 * Console window.
143 */
144 public PrintStream getErr() {
145 return debugGui.getConsole().getErr();
146 }
147
148 /**
149 * Packs the debugger GUI frame.
150 */
151 public void pack() {
152 debugGui.pack();
153 }
154
155 /**
156 * Sets the debugger GUI frame dimensions.
157 */
158 public void setSize(int w, int h) {
159 debugGui.setSize(w, h);
160 }
161
162 /**
163 * Sets the visibility of the debugger GUI frame.
164 */
165 public void setVisible(boolean flag) {
166 debugGui.setVisible(flag);
167 }
168
169 /**
170 * Returns whether the debugger GUI frame is visible.
171 */
172 public boolean isVisible() {
173 return debugGui.isVisible();
174 }
175
176 /**
177 * Frees any resources held by the debugger.
178 */
179 public void dispose() {
180 clearAllBreakpoints();
181 dim.go();
182 debugGui.dispose();
183 dim = null;
184 }
185
186 /**
187 * Attaches the debugger to the given {@link ContextFactory}.
188 */
189 public void attachTo(ContextFactory factory) {
190 dim.attachTo(factory);
191 }
192
193 /**
194 * Detaches from the current {@link ContextFactory}.
195 */
196 public void detach() {
197 dim.detach();
198 }
199
200 /**
201 * Main entry point. Creates a debugger attached to a Rhino
202 * {@link org.mozilla.javascript.tools.shell.Main} shell session.
203 */
204 public static void main(String[] args) {
205 Main main = new Main("Rhino JavaScript Debugger");
206 main.doBreak();
207 main.setExitAction(new IProxy(IProxy.EXIT_ACTION));
208
209 System.setIn(main.getIn());
210 System.setOut(main.getOut());
211 System.setErr(main.getErr());
212
213 Global global = org.mozilla.javascript.tools.shell.Main.getGlobal();
214 global.setIn(main.getIn());
215 global.setOut(main.getOut());
216 global.setErr(main.getErr());
217
218 main.attachTo(
219 org.mozilla.javascript.tools.shell.Main.shellContextFactory);
220
221 main.setScope(global);
222
223 main.pack();
224 main.setSize(600, 460);
225 main.setVisible(true);
226
227 org.mozilla.javascript.tools.shell.Main.exec(args);
228 }
229
230 /**
231 * Entry point for embedded applications. This method attaches
232 * to the global {@link ContextFactory} with a scope of a newly
233 * created {@link Global} object. No I/O redirection is performed
234 * as with {@link #main(String[])}.
235 */
236 public static Main mainEmbedded(String title) {
237 ContextFactory factory = ContextFactory.getGlobal();
238 Global global = new Global();
239 global.init(factory);
240 return mainEmbedded(factory, global, title);
241 }
242
243 /**
244 * Entry point for embedded applications. This method attaches
245 * to the given {@link ContextFactory} with the given scope. No
246 * I/O redirection is performed as with {@link #main(String[])}.
247 */
248 public static Main mainEmbedded(ContextFactory factory,
249 Scriptable scope,
250 String title) {
251 return mainEmbeddedImpl(factory, scope, title);
252 }
253
254 /**
255 * Entry point for embedded applications. This method attaches
256 * to the given {@link ContextFactory} with the given scope. No
257 * I/O redirection is performed as with {@link #main(String[])}.
258 */
259 public static Main mainEmbedded(ContextFactory factory,
260 ScopeProvider scopeProvider,
261 String title) {
262 return mainEmbeddedImpl(factory, scopeProvider, title);
263 }
264
265 /**
266 * Helper method for {@link #mainEmbedded(String)}, etc.
267 */
268 private static Main mainEmbeddedImpl(ContextFactory factory,
269 Object scopeProvider,
270 String title) {
271 if (title == null) {
272 title = "Rhino JavaScript Debugger (embedded usage)";
273 }
274 Main main = new Main(title);
275 main.doBreak();
276 main.setExitAction(new IProxy(IProxy.EXIT_ACTION));
277
278 main.attachTo(factory);
279 if (scopeProvider instanceof ScopeProvider) {
280 main.setScopeProvider((ScopeProvider)scopeProvider);
281 } else {
282 Scriptable scope = (Scriptable)scopeProvider;
283 if (scope instanceof Global) {
284 Global global = (Global)scope;
285 global.setIn(main.getIn());
286 global.setOut(main.getOut());
287 global.setErr(main.getErr());
288 }
289 main.setScope(scope);
290 }
291
292 main.pack();
293 main.setSize(600, 460);
294 main.setVisible(true);
295 return main;
296 }
297
298 // Deprecated methods
299
300 /**
301 * @deprecated Use {@link #setSize(int, int)} instead.
302 */
303 @Deprecated
304 public void setSize(java.awt.Dimension dimension) {
305 debugGui.setSize(dimension.width, dimension.height);
306 }
307
308 /**
309 * @deprecated
310 * The method does nothing and is only present for compatibility.
311 */
312 @Deprecated
313 public void setOptimizationLevel(int level) {
314 }
315
316 /**
317 * @deprecated
318 * The method is only present for compatibility and should not be called.
319 */
320 @Deprecated
321 public void contextEntered(Context cx) {
322 throw new IllegalStateException();
323 }
324
325 /**
326 * @deprecated
327 * The method is only present for compatibility and should not be called.
328 */
329 @Deprecated
330 public void contextExited(Context cx) {
331 throw new IllegalStateException();
332 }
333
334 /**
335 * @deprecated
336 * The method is only present for compatibility and should not be called.
337 */
338 @Deprecated
339 public void contextCreated(Context cx) {
340 throw new IllegalStateException();
341 }
342
343 /**
344 * @deprecated
345 * The method is only present for compatibility and should not be called.
346 */
347 @Deprecated
348 public void contextReleased(Context cx)
349 {
350 throw new IllegalStateException();
351 }
352
353 /**
354 * Class to consolidate all internal implementations of interfaces
355 * to avoid class generation bloat.
356 */
357 private static class IProxy implements Runnable, ScopeProvider {
358
359 // Constants for 'type'.
360 public static final int EXIT_ACTION = 1;
361 public static final int SCOPE_PROVIDER = 2;
362
363 /**
364 * The type of interface.
365 */
366 private final int type;
367
368 /**
369 * The scope object to expose when {@link #type} =
370 * {@link #SCOPE_PROVIDER}.
371 */
372 private Scriptable scope;
373
374 /**
375 * Creates a new IProxy.
376 */
377 public IProxy(int type) {
378 this.type = type;
379 }
380
381 /**
382 * Creates a new IProxy that acts as a {@link ScopeProvider}.
383 */
384 public static ScopeProvider newScopeProvider(Scriptable scope) {
385 IProxy scopeProvider = new IProxy(SCOPE_PROVIDER);
386 scopeProvider.scope = scope;
387 return scopeProvider;
388 }
389
390 // ContextAction
391
392 /**
393 * Exit action.
394 */
395 public void run() {
396 if (type != EXIT_ACTION) Kit.codeBug();
397 System.exit(0);
398 }
399
400 // ScopeProvider
401
402 /**
403 * Returns the scope for script evaluations.
404 */
405 public Scriptable getScope() {
406 if (type != SCOPE_PROVIDER) Kit.codeBug();
407 if (scope == null) Kit.codeBug();
408 return scope;
409 }
410 }
411 }