Branch libreoffice-5-0-4
[LibreOffice.git] / scripting / examples / java / debugger / OORhinoDebugger.java
blob5144637acdf50c182884e8bb1eb725a5b4cadcbd
1 /*
2 * This file is part of the LibreOffice project.
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/.
8 * This file incorporates work covered by the following license notice:
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 .
19 package org.libreoffice.example.java_scripts;
21 import javax.swing.SwingUtilities;
22 import java.io.InputStream;
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;
30 import com.sun.star.script.provider.XScriptContext;
32 public class OORhinoDebugger implements OOScriptDebugger {
34 public void go(final XScriptContext xsctxt, String filename) {
35 Main sdb = initUI(xsctxt);
37 // This is the method we've added to open a file when starting
38 // the Rhino debugger
39 sdb.openFile(filename);
42 public void go(final XScriptContext xsctxt, InputStream in) {
43 Main sdb = initUI(xsctxt);
45 // Open a stream in the debugger
46 sdb.openStream(in);
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);
60 });
61 sdb.setExitAction(new Runnable() {
62 public void run() {
63 sdb.dispose();
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;
76 });
77 return sdb;
78 } catch (Exception exc) {
79 exc.printStackTrace();
82 return null;
85 static void swingInvoke(Runnable f) {
86 if (SwingUtilities.isEventDispatchThread()) {
87 f.run();
88 return;
91 try {
92 SwingUtilities.invokeAndWait(f);
93 } catch (Exception exc) {
94 exc.printStackTrace();