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 public void finalize()
46 if (jsExecQueue != null)
53 private Vector jsExecQueue;
55 private Thread executor = null;
57 public void stopQueue()
59 if (jsExecQueue != null)
61 Vector<JSFunctionExec> q = null;
62 synchronized (jsExecQueue)
69 for (JSFunctionExec jx : q)
74 q.removeAllElements();
87 while (jsExecQueue != null)
89 if (jsExecQueue.size() > 0)
91 Runnable r = (Runnable) jsExecQueue.elementAt(0);
92 jsExecQueue.removeElementAt(0);
96 } catch (Exception ex)
101 ex.printStackTrace();
108 synchronized (jsExecQueue)
110 jsExecQueue.wait(1000);
112 } catch (Exception ex)
122 * execute a javascript callback synchronously
128 public void executeJavascriptFunction(final String _listener,
129 final Object[] objects) throws Exception
131 executeJavascriptFunction(false, _listener, objects);
135 * execute a javascript callback synchronously or asynchronously
138 * - true to execute asynchronously (do this for gui events)
140 * - javascript function
144 * - only if call is synchronous
146 public void executeJavascriptFunction(final boolean async,
147 final String _listener, Object[] arguments) throws Exception
150 executeJavascriptFunction(async, _listener, arguments, null);
154 public void executeJavascriptFunction(final boolean async,
155 final String _listener, Object[] arguments, final String dbgMsg)
158 final Object[] objects = new Object[arguments != null ? arguments.length
160 if (arguments != null)
162 System.arraycopy(arguments, 0, objects, 0, arguments.length);
164 final Exception[] jsex = new Exception[1];
165 Runnable exec = new Runnable()
171 JSObject scriptObject = null;
174 scriptObject = JSObject.getWindow(jvlite);
175 } catch (Exception ex)
179 if (scriptObject != null)
181 if (jvlite.debug && dbgMsg != null)
183 System.err.println(dbgMsg);
185 scriptObject.call(_listener, objects);
187 } catch (Exception jex)
189 // squash any malformedURLExceptions thrown by windows/safari
190 if (!(jex instanceof java.net.MalformedURLException))
194 System.err.println(jex);
196 if (jex instanceof netscape.javascript.JSException
197 && jvlite.jsfallbackEnabled)
199 jsex[0] = (netscape.javascript.JSException) jex;
202 System.err.println("Falling back to javascript: url call");
204 StringBuffer sb = new StringBuffer("javascript:" + _listener
206 for (int i = 0; objects != null && i < objects.length; i++)
213 // strip out nulls and complex objects that we can't pass this
215 if (objects[i] != null
216 && !(objects[i].getClass().getName()
217 .indexOf("jalview") == 0))
219 sb.append(objects[i].toString());
226 System.err.println(sb.toString());
232 url = new URL(sb.toString());
233 jvlite.getAppletContext().showDocument(url);
235 } catch (Exception uex)
244 jex.printStackTrace();
259 if (executor == null)
261 executor = new Thread(new JSFunctionExec(jvlite));
264 synchronized (jsExecQueue)
266 jsExecQueue.addElement(exec);
267 jsExecQueue.notify();
272 // wat for executor to notify us if it's running.