2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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/>.
18 package jalview.javascript;
21 import java.util.Vector;
23 import netscape.javascript.JSObject;
24 import jalview.bin.JalviewLite;
26 public class JSFunctionExec implements Runnable
28 public JalviewLite jvlite;
30 public JSFunctionExec(JalviewLite applet)
34 jsExecQueue = jvlite.getJsExecQueue();
35 jvlite.setExecutor(this);
38 public void finalize()
42 if (jsExecQueue != null)
49 private Vector jsExecQueue;
51 private Thread executor = null;
53 public void stopQueue()
55 if (jsExecQueue != null)
57 Vector<JSFunctionExec> q = null;
58 synchronized (jsExecQueue)
65 for (JSFunctionExec jx : q)
70 q.removeAllElements();
83 while (jsExecQueue != null)
85 if (jsExecQueue.size() > 0)
87 Runnable r = (Runnable) jsExecQueue.elementAt(0);
88 jsExecQueue.removeElementAt(0);
92 } catch (Exception ex)
104 synchronized (jsExecQueue)
106 jsExecQueue.wait(1000);
108 } catch (Exception ex)
118 * execute a javascript callback synchronously
124 public void executeJavascriptFunction(final String _listener,
125 final Object[] objects) throws Exception
127 executeJavascriptFunction(false, _listener, objects);
131 * execute a javascript callback synchronously or asynchronously
134 * - true to execute asynchronously (do this for gui events)
136 * - javascript function
140 * - only if call is synchronous
142 public void executeJavascriptFunction(final boolean async,
143 final String _listener, Object[] arguments) throws Exception
146 executeJavascriptFunction(async, _listener, arguments, null);
150 public void executeJavascriptFunction(final boolean async,
151 final String _listener, Object[] arguments, final String dbgMsg)
154 final Object[] objects = new Object[arguments != null ? arguments.length
156 if (arguments != null)
158 System.arraycopy(arguments, 0, objects, 0, arguments.length);
160 final Exception[] jsex = new Exception[1];
161 Runnable exec = new Runnable()
167 JSObject scriptObject = null;
170 scriptObject = JSObject.getWindow(jvlite);
171 } catch (Exception ex)
175 if (scriptObject != null)
177 if (jvlite.debug && dbgMsg != null)
179 System.err.println(dbgMsg);
181 scriptObject.call(_listener, objects);
183 } catch (Exception jex)
185 // squash any malformedURLExceptions thrown by windows/safari
186 if (!(jex instanceof java.net.MalformedURLException))
190 System.err.println(jex);
192 if (jex instanceof netscape.javascript.JSException
193 && jvlite.jsfallbackEnabled)
195 jsex[0] = (netscape.javascript.JSException) jex;
198 System.err.println("Falling back to javascript: url call");
200 StringBuffer sb = new StringBuffer("javascript:" + _listener
202 for (int i = 0; objects != null && i < objects.length; i++)
209 // strip out nulls and complex objects that we can't pass this
211 if (objects[i] != null
212 && !(objects[i].getClass().getName()
213 .indexOf("jalview") == 0))
215 sb.append(objects[i].toString());
222 System.err.println(sb.toString());
228 url = new URL(sb.toString());
229 jvlite.getAppletContext().showDocument(url);
231 } catch (Exception uex)
240 jex.printStackTrace();
255 if (executor == null)
257 executor = new Thread(new JSFunctionExec(jvlite));
260 synchronized (jsExecQueue)
262 jsExecQueue.addElement(exec);
263 jsExecQueue.notify();
268 // wat for executor to notify us if it's running.