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