2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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;
24 import java.util.Vector;
26 import netscape.javascript.JSObject;
27 import jalview.bin.JalviewLite;
29 public class JSFunctionExec implements Runnable
31 public JalviewLite jvlite;
33 public JSFunctionExec(JalviewLite applet)
37 jsExecQueue = jvlite.getJsExecQueue();
38 jvlite.setExecutor(this);
41 public void finalize()
45 if (jsExecQueue != null)
52 private Vector jsExecQueue;
54 private Thread executor = null;
56 public void stopQueue()
58 if (jsExecQueue != null)
60 Vector<JSFunctionExec> q = null;
61 synchronized (jsExecQueue)
68 for (JSFunctionExec jx : q)
73 q.removeAllElements();
86 while (jsExecQueue != null)
88 if (jsExecQueue.size() > 0)
90 Runnable r = (Runnable) jsExecQueue.elementAt(0);
91 jsExecQueue.removeElementAt(0);
95 } catch (Exception ex)
100 ex.printStackTrace();
107 synchronized (jsExecQueue)
109 jsExecQueue.wait(1000);
111 } catch (Exception ex)
121 * execute a javascript callback synchronously
127 public void executeJavascriptFunction(final String _listener,
128 final Object[] objects) throws Exception
130 executeJavascriptFunction(false, _listener, objects);
134 * execute a javascript callback synchronously or asynchronously
137 * - true to execute asynchronously (do this for gui events)
139 * - javascript function
143 * - only if call is synchronous
145 public void executeJavascriptFunction(final boolean async,
146 final String _listener, Object[] arguments) throws Exception
149 executeJavascriptFunction(async, _listener, arguments, null);
153 public void executeJavascriptFunction(final boolean async,
154 final String _listener, Object[] arguments, final String dbgMsg)
157 final Object[] objects = new Object[arguments != null ? arguments.length
159 if (arguments != null)
161 System.arraycopy(arguments, 0, objects, 0, arguments.length);
163 final Exception[] jsex = new Exception[1];
164 Runnable exec = new Runnable()
170 JSObject scriptObject = null;
173 scriptObject = JSObject.getWindow(jvlite);
174 } catch (Exception ex)
178 if (scriptObject != null)
180 if (jvlite.debug && dbgMsg != null)
182 System.err.println(dbgMsg);
184 scriptObject.call(_listener, objects);
186 } catch (Exception jex)
188 // squash any malformedURLExceptions thrown by windows/safari
189 if (!(jex instanceof java.net.MalformedURLException))
193 System.err.println(jex);
195 if (jex instanceof netscape.javascript.JSException
196 && jvlite.jsfallbackEnabled)
198 jsex[0] = (netscape.javascript.JSException) jex;
201 System.err.println("Falling back to javascript: url call");
203 StringBuffer sb = new StringBuffer("javascript:" + _listener
205 for (int i = 0; objects != null && i < objects.length; i++)
212 // strip out nulls and complex objects that we can't pass this
214 if (objects[i] != null
215 && !(objects[i].getClass().getName()
216 .indexOf("jalview") == 0))
218 sb.append(objects[i].toString());
225 System.err.println(sb.toString());
231 url = new URL(sb.toString());
232 jvlite.getAppletContext().showDocument(url);
234 } catch (Exception uex)
243 jex.printStackTrace();
258 if (executor == null)
260 executor = new Thread(new JSFunctionExec(jvlite));
263 synchronized (jsExecQueue)
265 jsExecQueue.addElement(exec);
266 jsExecQueue.notify();
271 // wat for executor to notify us if it's running.