1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 import javax.swing.SwingUtilities;
23 import java.io.InputStream;
24 
25 import org.mozilla.javascript.Context;
26 import org.mozilla.javascript.Scriptable;
27 import org.mozilla.javascript.ImporterTopLevel;
28 import org.mozilla.javascript.tools.debugger.Main;
29 import org.mozilla.javascript.tools.debugger.ScopeProvider;
30 
31 import drafts.com.sun.star.script.framework.runtime.XScriptContext;
32 
33 public class OORhinoDebugger implements OOScriptDebugger {
34 
go(final XScriptContext xsctxt, String filename)35     public void go(final XScriptContext xsctxt, String filename) {
36         Main sdb = initUI(xsctxt);
37 
38         // This is the method we've added to open a file when starting
39         // the Rhino debugger
40         sdb.openFile(filename);
41     }
42 
go(final XScriptContext xsctxt, InputStream in)43     public void go(final XScriptContext xsctxt, InputStream in) {
44         Main sdb = initUI(xsctxt);
45 
46         // Open a stream in the debugger
47         sdb.openStream(in);
48     }
49 
50     // This code is based on the main method of the Rhino Debugger Main class
51     // We pass in the XScriptContext in the global scope for script execution
initUI(final XScriptContext xsctxt)52     private Main initUI(final XScriptContext xsctxt) {
53         try {
54             final Main sdb = new Main("Rhino JavaScript Debugger");
55             swingInvoke(new Runnable() {
56                     public void run() {
57                         sdb.pack();
58                         sdb.setSize(640, 640);
59                         sdb.setVisible(true);
60                     }
61                 });
62             sdb.setExitAction(new Runnable() {
63                     public void run() {
64                         sdb.dispose();
65                     }
66                 });
67             Context.addContextListener(sdb);
68             sdb.setScopeProvider(new ScopeProvider() {
69                     public Scriptable getScope() {
70                         Context ctxt = Context.enter();
71                         ImporterTopLevel scope = new ImporterTopLevel(ctxt);
72                         Scriptable jsArgs = Context.toObject(xsctxt, scope);
73                         scope.put("XSCRIPTCONTEXT", scope, jsArgs);
74                         Context.exit();
75                         return scope;
76                     }
77                 });
78             return sdb;
79         } catch (Exception exc) {
80             exc.printStackTrace();
81         }
82         return null;
83     }
84 
swingInvoke(Runnable f)85     static void swingInvoke(Runnable f) {
86         if (SwingUtilities.isEventDispatchThread()) {
87             f.run();
88             return;
89         }
90         try {
91             SwingUtilities.invokeAndWait(f);
92         } catch (Exception exc) {
93             exc.printStackTrace();
94         }
95     }
96 }
97