2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.javascript;
22 import java.util.Vector;
24 import netscape.javascript.JSObject;
25 import jalview.bin.JalviewLite;
27 public class JSFunctionExec implements Runnable
29 public JalviewLite jvlite;
31 public JSFunctionExec(JalviewLite applet)
35 jsExecQueue = jvlite.getJsExecQueue();
36 jvlite.setExecutor(this);
39 public void finalize()
43 if (jsExecQueue != null)
50 private Vector jsExecQueue;
52 private Thread executor = null;
54 public void stopQueue()
56 if (jsExecQueue != null)
58 Vector<JSFunctionExec> q = null;
59 synchronized (jsExecQueue)
66 for (JSFunctionExec jx : q)
71 q.removeAllElements();
84 while (jsExecQueue != null)
86 if (jsExecQueue.size() > 0)
88 Runnable r = (Runnable) jsExecQueue.elementAt(0);
89 jsExecQueue.removeElementAt(0);
93 } catch (Exception ex)
105 synchronized (jsExecQueue)
107 jsExecQueue.wait(1000);
109 } catch (Exception ex)
119 * execute a javascript callback synchronously
125 public void executeJavascriptFunction(final String _listener,
126 final Object[] objects) throws Exception
128 executeJavascriptFunction(false, _listener, objects);
132 * execute a javascript callback synchronously or asynchronously
135 * - true to execute asynchronously (do this for gui events)
137 * - javascript function
141 * - only if call is synchronous
143 public void executeJavascriptFunction(final boolean async,
144 final String _listener, Object[] arguments) throws Exception
147 executeJavascriptFunction(async, _listener, arguments, null);
151 public void executeJavascriptFunction(final boolean async,
152 final String _listener, Object[] arguments, final String dbgMsg)
155 final Object[] objects = new Object[arguments != null ? arguments.length
157 if (arguments != null)
159 System.arraycopy(arguments, 0, objects, 0, arguments.length);
161 final Exception[] jsex = new Exception[1];
162 Runnable exec = new Runnable()
168 JSObject scriptObject = null;
171 scriptObject = JSObject.getWindow(jvlite);
172 } catch (Exception ex)
176 if (scriptObject != null)
178 if (jvlite.debug && dbgMsg != null)
180 System.err.println(dbgMsg);
182 scriptObject.call(_listener, objects);
184 } catch (Exception jex)
186 // squash any malformedURLExceptions thrown by windows/safari
187 if (!(jex instanceof java.net.MalformedURLException))
191 System.err.println(jex);
193 if (jex instanceof netscape.javascript.JSException
194 && jvlite.jsfallbackEnabled)
196 jsex[0] = (netscape.javascript.JSException) jex;
199 System.err.println("Falling back to javascript: url call");
201 StringBuffer sb = new StringBuffer("javascript:" + _listener
203 for (int i = 0; objects != null && i < objects.length; i++)
210 // strip out nulls and complex objects that we can't pass this
212 if (objects[i] != null
213 && !(objects[i].getClass().getName()
214 .indexOf("jalview") == 0))
216 sb.append(objects[i].toString());
223 System.err.println(sb.toString());
229 url = new URL(sb.toString());
230 jvlite.getAppletContext().showDocument(url);
232 } catch (Exception uex)
241 jex.printStackTrace();
256 if (executor == null)
258 executor = new Thread(new JSFunctionExec(jvlite));
261 synchronized (jsExecQueue)
263 jsExecQueue.addElement(exec);
264 jsExecQueue.notify();
269 // wat for executor to notify us if it's running.