1 import javax.swing.SwingUtilities;
2 import java.io.InputStream;
3 
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;
9 
10 import drafts.com.sun.star.script.framework.runtime.XScriptContext;
11 
12 public class OORhinoDebugger implements OOScriptDebugger {
13 
14     public void go(final XScriptContext xsctxt, String filename) {
15         Main sdb = initUI(xsctxt);
16 
17         // This is the method we've added to open a file when starting
18         // the Rhino debugger
19         sdb.openFile(filename);
20     }
21 
22     public void go(final XScriptContext xsctxt, InputStream in) {
23         Main sdb = initUI(xsctxt);
24 
25         // Open a stream in the debugger
26         sdb.openStream(in);
27     }
28 
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);
39                     }
40                 });
41             sdb.setExitAction(new Runnable() {
42                     public void run() {
43                         sdb.dispose();
44                     }
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;
55                     }
56                 });
57             return sdb;
58         } catch (Exception exc) {
59             exc.printStackTrace();
60         }
61         return null;
62     }
63 
64     static void swingInvoke(Runnable f) {
65         if (SwingUtilities.isEventDispatchThread()) {
66             f.run();
67             return;
68         }
69         try {
70             SwingUtilities.invokeAndWait(f);
71         } catch (Exception exc) {
72             exc.printStackTrace();
73         }
74     }
75 }
76