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);
43 protected void finalize() throws Throwable
47 if (jsExecQueue != null)
55 private Vector jsExecQueue;
57 private Thread executor = null;
59 public void stopQueue()
61 if (jsExecQueue != null)
63 Vector<JSFunctionExec> q = null;
64 synchronized (jsExecQueue)
71 for (JSFunctionExec jx : q)
76 q.removeAllElements();
90 while (jsExecQueue != null)
92 if (jsExecQueue.size() > 0)
94 Runnable r = (Runnable) jsExecQueue.elementAt(0);
95 jsExecQueue.removeElementAt(0);
99 } catch (Exception ex)
101 ex.printStackTrace();
104 ex.printStackTrace();
111 synchronized (jsExecQueue)
113 jsExecQueue.wait(1000);
115 } catch (Exception ex)
125 * execute a javascript callback synchronously
131 public void executeJavascriptFunction(final String _listener,
132 final Object[] objects) throws Exception
134 executeJavascriptFunction(false, _listener, objects);
138 * execute a javascript callback synchronously or asynchronously
141 * - true to execute asynchronously (do this for gui events)
143 * - javascript function
147 * - only if call is synchronous
149 public void executeJavascriptFunction(final boolean async,
150 final String _listener, Object[] arguments) throws Exception
153 executeJavascriptFunction(async, _listener, arguments, null);
157 public void executeJavascriptFunction(final boolean async,
158 final String _listener, Object[] arguments, final String dbgMsg)
161 final Object[] objects = new Object[arguments != null ? arguments.length
163 if (arguments != null)
165 System.arraycopy(arguments, 0, objects, 0, arguments.length);
167 final Exception[] jsex = new Exception[1];
168 Runnable exec = new Runnable()
175 JSObject scriptObject = null;
178 scriptObject = JSObject.getWindow(jvlite);
179 } catch (Exception ex)
183 if (scriptObject != null)
185 if (jvlite.debug && dbgMsg != null)
187 System.err.println(dbgMsg);
189 scriptObject.call(_listener, objects);
191 } catch (Exception jex)
193 // squash any malformedURLExceptions thrown by windows/safari
194 if (!(jex instanceof java.net.MalformedURLException))
198 System.err.println(jex);
200 if (jex instanceof netscape.javascript.JSException
201 && jvlite.jsfallbackEnabled)
206 System.err.println("Falling back to javascript: url call");
208 StringBuffer sb = new StringBuffer("javascript:" + _listener
210 for (int i = 0; objects != null && i < objects.length; i++)
217 // strip out nulls and complex objects that we can't pass this
219 if (objects[i] != null
220 && !(objects[i].getClass().getName()
221 .indexOf("jalview") == 0))
223 sb.append(objects[i].toString());
230 System.err.println(sb.toString());
236 url = new URL(sb.toString());
237 jvlite.getAppletContext().showDocument(url);
239 } catch (Exception uex)
248 jex.printStackTrace();
263 if (executor == null)
265 executor = new Thread(new JSFunctionExec(jvlite));
268 synchronized (jsExecQueue)
270 jsExecQueue.addElement(exec);
271 jsExecQueue.notify();
276 // wat for executor to notify us if it's running.