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
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
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
) {
53 final Main sdb
= new Main("Rhino JavaScript Debugger");
54 swingInvoke(new Runnable() {
57 sdb
.setSize(640, 640);
61 sdb
.setExitAction(new Runnable() {
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
);
78 } catch (Exception exc
) {
79 exc
.printStackTrace();
85 static void swingInvoke(Runnable f
) {
86 if (SwingUtilities
.isEventDispatchThread()) {
92 SwingUtilities
.invokeAndWait(f
);
93 } catch (Exception exc
) {
94 exc
.printStackTrace();