2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.javascript;
23 import jalview.bin.JalviewLite;
26 import java.util.Vector;
28 import netscape.javascript.JSObject;
30 public class JSFunctionExec implements Runnable
32 public JalviewLite jvlite;
34 public JSFunctionExec(JalviewLite applet)
38 jsExecQueue = jvlite.getJsExecQueue();
39 jvlite.setExecutor(this);
42 private Vector jsExecQueue;
44 private Thread executor = null;
46 public void stopQueue()
48 if (jsExecQueue != null)
50 Vector<JSFunctionExec> q = null;
51 synchronized (jsExecQueue)
58 for (JSFunctionExec jx : q)
63 q.removeAllElements();
77 while (jsExecQueue != null)
79 if (jsExecQueue.size() > 0)
81 Runnable r = (Runnable) jsExecQueue.elementAt(0);
82 jsExecQueue.removeElementAt(0);
86 } catch (Exception ex)
98 synchronized (jsExecQueue)
100 jsExecQueue.wait(1000);
102 } catch (Exception ex)
112 * execute a javascript callback synchronously
118 public void executeJavascriptFunction(final String _listener,
119 final Object[] objects) throws Exception
121 executeJavascriptFunction(false, _listener, objects);
125 * execute a javascript callback synchronously or asynchronously
128 * - true to execute asynchronously (do this for gui events)
130 * - javascript function
134 * - only if call is synchronous
136 public void executeJavascriptFunction(final boolean async,
137 final String _listener, Object[] arguments) throws Exception
140 executeJavascriptFunction(async, _listener, arguments, null);
144 public void executeJavascriptFunction(final boolean async,
145 final String _listener, Object[] arguments, final String dbgMsg)
148 final Object[] objects = new Object[arguments != null ? arguments.length
150 if (arguments != null)
152 System.arraycopy(arguments, 0, objects, 0, arguments.length);
154 final Exception[] jsex = new Exception[1];
155 Runnable exec = new Runnable()
162 JSObject scriptObject = null;
165 scriptObject = JSObject.getWindow(jvlite);
166 } catch (Exception ex)
170 if (scriptObject != null)
172 if (jvlite.debug && dbgMsg != null)
174 System.err.println(dbgMsg);
176 scriptObject.call(_listener, objects);
178 } catch (Exception jex)
180 // squash any malformedURLExceptions thrown by windows/safari
181 if (!(jex instanceof java.net.MalformedURLException))
185 System.err.println(jex);
187 if (jex instanceof netscape.javascript.JSException
188 && jvlite.jsfallbackEnabled)
193 System.err.println("Falling back to javascript: url call");
195 StringBuffer sb = new StringBuffer(
196 "javascript:" + _listener + "(");
197 for (int i = 0; objects != null && i < objects.length; i++)
204 // strip out nulls and complex objects that we can't pass this
206 if (objects[i] != null && !(objects[i].getClass().getName()
207 .indexOf("jalview") == 0))
209 sb.append(objects[i].toString());
216 System.err.println(sb.toString());
222 url = new URL(sb.toString());
223 jvlite.getAppletContext().showDocument(url);
225 } catch (Exception uex)
234 jex.printStackTrace();
249 if (executor == null)
251 executor = new Thread(new JSFunctionExec(jvlite));
254 synchronized (jsExecQueue)
256 jsExecQueue.addElement(exec);
257 jsExecQueue.notify();
262 // wat for executor to notify us if it's running.