X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fjavascript%2FJSFunctionExec.java;h=c17469694507108d0e370d62983bf7d4656e31b5;hb=8a2a0cda7067530f8481c2aec203e18d555f2dfd;hp=967492621cb5920e959a450899e3bf6c8ce56c2e;hpb=b97119fa4abbda0ef3d8e6e36f4634a06d79d411;p=jalview.git diff --git a/src/jalview/javascript/JSFunctionExec.java b/src/jalview/javascript/JSFunctionExec.java index 9674926..c174696 100644 --- a/src/jalview/javascript/JSFunctionExec.java +++ b/src/jalview/javascript/JSFunctionExec.java @@ -1,37 +1,77 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * Jalview 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ package jalview.javascript; +import jalview.bin.JalviewLite; + import java.net.URL; import java.util.Vector; -import netscape.javascript.JSException; import netscape.javascript.JSObject; -import jalview.bin.JalviewLite; public class JSFunctionExec implements Runnable { - JalviewLite jvlite; + public JalviewLite jvlite; public JSFunctionExec(JalviewLite applet) { jvlite = applet; + + jsExecQueue = jvlite.getJsExecQueue(); + jvlite.setExecutor(this); } - private static Vector jsExecQueue; + private Vector jsExecQueue; - private static Thread executor = null; + private Thread executor = null; - public static void stopQueue() + public void stopQueue() { if (jsExecQueue != null) { - Vector q = jsExecQueue; - jsExecQueue = null; - q.removeAllElements(); - executor.notify(); - executor = null; + Vector q = null; + synchronized (jsExecQueue) + { + q = jsExecQueue; + jsExecQueue = null; + } + if (q != null) + { + for (JSFunctionExec jx : q) + { + jx.jvlite = null; + + } + q.removeAllElements(); + synchronized (q) + { + q.notifyAll(); + } + } } + jvlite = null; + executor = null; } + @Override public void run() { while (jsExecQueue != null) @@ -69,7 +109,7 @@ public class JSFunctionExec implements Runnable } /** - * execute a javascript callback asynchronously + * execute a javascript callback synchronously * * @param _listener * @param objects @@ -96,6 +136,15 @@ public class JSFunctionExec implements Runnable public void executeJavascriptFunction(final boolean async, final String _listener, Object[] arguments) throws Exception { + + executeJavascriptFunction(async, _listener, arguments, null); + + } + + public void executeJavascriptFunction(final boolean async, + final String _listener, Object[] arguments, final String dbgMsg) + throws Exception + { final Object[] objects = new Object[arguments != null ? arguments.length : 0]; if (arguments != null) @@ -105,6 +154,7 @@ public class JSFunctionExec implements Runnable final Exception[] jsex = new Exception[1]; Runnable exec = new Runnable() { + @Override public void run() { try @@ -112,13 +162,17 @@ public class JSFunctionExec implements Runnable JSObject scriptObject = null; try { - scriptObject = JSObject.getWindow(jvlite.applet); + scriptObject = JSObject.getWindow(jvlite); } catch (Exception ex) { } ; if (scriptObject != null) { + if (jvlite.debug && dbgMsg != null) + { + jalview.bin.Console.errPrintln(dbgMsg); + } scriptObject.call(_listener, objects); } } catch (Exception jex) @@ -128,17 +182,18 @@ public class JSFunctionExec implements Runnable { if (jvlite.debug) { - System.err.println(jex); + jalview.bin.Console.errPrintln(jex); } - if (jex instanceof netscape.javascript.JSException) + if (jex instanceof netscape.javascript.JSException + && jvlite.jsfallbackEnabled) { - jsex[0] = (netscape.javascript.JSException) jex; + jsex[0] = jex; if (jvlite.debug) { - System.err.println("Falling back to javascript: url call"); + jalview.bin.Console.errPrintln("Falling back to javascript: url call"); } - StringBuffer sb = new StringBuffer("javascript:" + _listener - + "("); + StringBuffer sb = new StringBuffer( + "javascript:" + _listener + "("); for (int i = 0; objects != null && i < objects.length; i++) { if (i > 0) @@ -148,9 +203,8 @@ public class JSFunctionExec implements Runnable sb.append("\""); // strip out nulls and complex objects that we can't pass this // way. - if (objects[i] != null - && !(objects[i].getClass().getPackage().getName() - .indexOf("jalview") == 0)) + if (objects[i] != null && !(objects[i].getClass().getName() + .indexOf("jalview") == 0)) { sb.append(objects[i].toString()); } @@ -159,7 +213,7 @@ public class JSFunctionExec implements Runnable sb.append(")"); if (jvlite.debug) { - System.err.println(sb.toString()); + jalview.bin.Console.errPrintln(sb.toString()); } // alternate URL url = null; @@ -181,7 +235,7 @@ public class JSFunctionExec implements Runnable } else { - jsex[0] = new Exception(jex); + jsex[0] = jex; } } ; @@ -192,10 +246,9 @@ public class JSFunctionExec implements Runnable }; if (async) { - if (JSFunctionExec.executor == null) + if (executor == null) { - JSFunctionExec.jsExecQueue = new Vector(); - JSFunctionExec.executor = new Thread(new JSFunctionExec(jvlite)); + executor = new Thread(new JSFunctionExec(jvlite)); executor.start(); } synchronized (jsExecQueue) @@ -206,6 +259,7 @@ public class JSFunctionExec implements Runnable } else { + // wat for executor to notify us if it's running. exec.run(); if (jsex[0] != null) {