Update ooo320-m1
[ooovba.git] / scripting / examples / java / debugger / OORhinoDebugger.java
blob0aea985cc5f230937d92920a54f0e9befd288d8b
1 import javax.swing.SwingUtilities;
2 import java.io.InputStream;
4 import org.mozilla.javascript.Context;
5 import org.mozilla.javascript.Scriptable;
6 import org.mozilla.javascript.ImporterTopLevel;
7 import org.mozilla.javascript.tools.debugger.Main;
8 import org.mozilla.javascript.tools.debugger.ScopeProvider;
10 import drafts.com.sun.star.script.framework.runtime.XScriptContext;
12 public class OORhinoDebugger implements OOScriptDebugger {
14 public void go(final XScriptContext xsctxt, String filename) {
15 Main sdb = initUI(xsctxt);
17 // This is the method we've added to open a file when starting
18 // the Rhino debugger
19 sdb.openFile(filename);
22 public void go(final XScriptContext xsctxt, InputStream in) {
23 Main sdb = initUI(xsctxt);
25 // Open a stream in the debugger
26 sdb.openStream(in);
29 // This code is based on the main method of the Rhino Debugger Main class
30 // We pass in the XScriptContext in the global scope for script execution
31 private Main initUI(final XScriptContext xsctxt) {
32 try {
33 final Main sdb = new Main("Rhino JavaScript Debugger");
34 swingInvoke(new Runnable() {
35 public void run() {
36 sdb.pack();
37 sdb.setSize(640, 640);
38 sdb.setVisible(true);
40 });
41 sdb.setExitAction(new Runnable() {
42 public void run() {
43 sdb.dispose();
45 });
46 Context.addContextListener(sdb);
47 sdb.setScopeProvider(new ScopeProvider() {
48 public Scriptable getScope() {
49 Context ctxt = Context.enter();
50 ImporterTopLevel scope = new ImporterTopLevel(ctxt);
51 Scriptable jsArgs = Context.toObject(xsctxt, scope);
52 scope.put("XSCRIPTCONTEXT", scope, jsArgs);
53 Context.exit();
54 return scope;
56 });
57 return sdb;
58 } catch (Exception exc) {
59 exc.printStackTrace();
61 return null;
64 static void swingInvoke(Runnable f) {
65 if (SwingUtilities.isEventDispatchThread()) {
66 f.run();
67 return;
69 try {
70 SwingUtilities.invokeAndWait(f);
71 } catch (Exception exc) {
72 exc.printStackTrace();