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