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