sb141: #i92926# fixed ScriptEdtiorForBeanShell/JavaScript calling Swing on Mac OS X

This commit is contained in:
sb
2011-02-18 16:02:07 +01:00
parent cd0d6a5a67
commit c51b48970f
3 changed files with 94 additions and 71 deletions

View File

@@ -0,0 +1,44 @@
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2000, 2011 Oracle and/or its affiliates.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
package com.sun.star.script.framework.provider;
import javax.swing.SwingUtilities;
// On Mac OS X, AWT/Swing must not be accessed from the AppKit thread, so call
// SwingUtilities.invokeLater always on a fresh thread to avoid that problem
// (also, the current thread must not wait for that fresh thread to terminate,
// as that would cause a deadlock if this thread is the AppKit thread):
public final class SwingInvocation {
public static void invoke(final Runnable doRun) {
new Thread("SwingInvocation") {
public void run() { SwingUtilities.invokeLater(doRun); }
}.start();
}
private SwingInvocation() {}
}

View File

@@ -47,6 +47,7 @@ import java.util.HashMap;
import com.sun.star.script.provider.XScriptContext; import com.sun.star.script.provider.XScriptContext;
import com.sun.star.script.framework.provider.ScriptEditor; import com.sun.star.script.framework.provider.ScriptEditor;
import com.sun.star.script.framework.provider.SwingInvocation;
import com.sun.star.script.framework.container.ScriptMetaData; import com.sun.star.script.framework.container.ScriptMetaData;
import com.sun.star.script.framework.provider.ClassLoaderFactory; import com.sun.star.script.framework.provider.ClassLoaderFactory;
@@ -128,7 +129,9 @@ public class ScriptEditorForBeanShell
*/ */
public static ScriptEditorForBeanShell getEditor(URL url) public static ScriptEditorForBeanShell getEditor(URL url)
{ {
return (ScriptEditorForBeanShell)BEING_EDITED.get(url); synchronized (BEING_EDITED) {
return (ScriptEditorForBeanShell)BEING_EDITED.get(url);
}
} }
/** /**
@@ -194,8 +197,7 @@ public class ScriptEditorForBeanShell
* @param context The context in which to execute the script * @param context The context in which to execute the script
* *
*/ */
public void edit(XScriptContext context, ScriptMetaData entry) { public void edit(final XScriptContext context, ScriptMetaData entry) {
if (entry != null ) { if (entry != null ) {
try { try {
ClassLoader cl = null; ClassLoader cl = null;
@@ -205,26 +207,30 @@ public class ScriptEditorForBeanShell
catch (Exception ignore) // TODO re-examine error handling catch (Exception ignore) // TODO re-examine error handling
{ {
} }
final ClassLoader theCl = cl;
String sUrl = entry.getParcelLocation(); String sUrl = entry.getParcelLocation();
if ( !sUrl.endsWith( "/" ) ) if ( !sUrl.endsWith( "/" ) )
{ {
sUrl += "/"; sUrl += "/";
} }
sUrl += entry.getLanguageName(); sUrl += entry.getLanguageName();
URL url = entry.getSourceURL(); final URL url = entry.getSourceURL();
SwingInvocation.invoke(
// check if there is already an editing session for this script new Runnable() {
if (BEING_EDITED.containsKey(url)) public void run() {
{ ScriptEditorForBeanShell editor;
ScriptEditorForBeanShell editor = synchronized (BEING_EDITED) {
(ScriptEditorForBeanShell) BEING_EDITED.get(url); editor = (ScriptEditorForBeanShell)
BEING_EDITED.get(url);
editor.frame.toFront(); if (editor == null) {
} editor = new ScriptEditorForBeanShell(
else context, theCl, url);
{ BEING_EDITED.put(url, editor);
new ScriptEditorForBeanShell(context, cl, url); }
} }
editor.frame.toFront();
}
});
} }
catch (IOException ioe) { catch (IOException ioe) {
showErrorMessage( "Error loading file: " + ioe.getMessage() ); showErrorMessage( "Error loading file: " + ioe.getMessage() );
@@ -269,8 +275,6 @@ public class ScriptEditorForBeanShell
this.model.setView(this.view); this.model.setView(this.view);
initUI(); initUI();
frame.show(); frame.show();
BEING_EDITED.put(url, this);
} }
private void showErrorMessage(String message) { private void showErrorMessage(String message) {
@@ -384,7 +388,7 @@ public class ScriptEditorForBeanShell
private void shutdown() private void shutdown()
{ {
if (BEING_EDITED.containsKey(scriptURL)) { synchronized (BEING_EDITED) {
BEING_EDITED.remove(scriptURL); BEING_EDITED.remove(scriptURL);
} }
} }

View File

@@ -36,6 +36,7 @@ import org.mozilla.javascript.tools.debugger.ScopeProvider;
import com.sun.star.script.provider.XScriptContext; import com.sun.star.script.provider.XScriptContext;
import com.sun.star.script.framework.container.ScriptMetaData; import com.sun.star.script.framework.container.ScriptMetaData;
import com.sun.star.script.framework.provider.ScriptEditor; import com.sun.star.script.framework.provider.ScriptEditor;
import com.sun.star.script.framework.provider.SwingInvocation;
import com.sun.star.script.framework.log.LogUtils; import com.sun.star.script.framework.log.LogUtils;
import java.io.InputStream; import java.io.InputStream;
@@ -45,7 +46,6 @@ import java.net.URL;
import java.util.Map; import java.util.Map;
import java.util.HashMap; import java.util.HashMap;
import javax.swing.SwingUtilities;
import java.awt.event.WindowAdapter; import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
@@ -117,7 +117,9 @@ public class ScriptEditorForJavaScript implements ScriptEditor
*/ */
public static ScriptEditorForJavaScript getEditor(URL url) public static ScriptEditorForJavaScript getEditor(URL url)
{ {
return (ScriptEditorForJavaScript)BEING_EDITED.get(url); synchronized (BEING_EDITED) {
return (ScriptEditorForJavaScript)BEING_EDITED.get(url);
}
} }
/** /**
@@ -187,31 +189,25 @@ public class ScriptEditorForJavaScript implements ScriptEditor
sUrl += "/"; sUrl += "/";
} }
sUrl += entry.getLanguageName(); sUrl += entry.getLanguageName();
URL url = entry.getSourceURL(); final URL url = entry.getSourceURL();
SwingInvocation.invoke(
// check if there is already an editing session for this script new Runnable() {
//if (BEING_EDITED.containsKey(url)) public void run() {
if ( rhinoWindow != null ) synchronized (BEING_EDITED) {
{ ScriptEditorForJavaScript editor =
ScriptEditorForJavaScript editor = (ScriptEditorForJavaScript) BEING_EDITED.get(
(ScriptEditorForJavaScript) BEING_EDITED.get(url); url);
if ( editor == null ) if (editor == null) {
{ editor = new ScriptEditorForJavaScript(
editor = new ScriptEditorForJavaScript( context, url ); context, url);
editor.edit( context, entry ); BEING_EDITED.put(url, editor);
} }
else }
{ assert rhinoWindow != null;
rhinoWindow.showScriptWindow( url ); rhinoWindow.showScriptWindow(url);
} rhinoWindow.toFront();
} }
else });
{
ScriptEditorForJavaScript editor =
new ScriptEditorForJavaScript( context, url );
}
rhinoWindow.toFront();
} }
catch ( IOException e ) catch ( IOException e )
{ {
@@ -234,11 +230,6 @@ public class ScriptEditorForJavaScript implements ScriptEditor
this.scriptURL = url; this.scriptURL = url;
synchronized( ScriptEditorForJavaScript.class )
{
BEING_EDITED.put(url, this);
}
} }
/** /**
@@ -274,13 +265,9 @@ public class ScriptEditorForJavaScript implements ScriptEditor
} }
final Main sdb = new Main("Rhino JavaScript Debugger"); final Main sdb = new Main("Rhino JavaScript Debugger");
swingInvoke(new Runnable() { sdb.pack();
public void run() { sdb.setSize(640, 640);
sdb.pack(); sdb.setVisible(true);
sdb.setSize(640, 640);
sdb.setVisible(true);
}
});
sdb.setExitAction(new Runnable() { sdb.setExitAction(new Runnable() {
public void run() { public void run() {
sdb.clearAllBreakpoints(); sdb.clearAllBreakpoints();
@@ -306,18 +293,6 @@ public class ScriptEditorForJavaScript implements ScriptEditor
} }
} }
private static void swingInvoke(Runnable f) {
if (SwingUtilities.isEventDispatchThread()) {
f.run();
return;
}
try {
SwingUtilities.invokeAndWait(f);
} catch (Exception exc) {
LogUtils.DEBUG( LogUtils.getTrace( exc ) );
}
}
private void shutdown() private void shutdown()
{ {
// dereference Rhino Debugger window // dereference Rhino Debugger window