1 package jalview.javascript;
4 import java.util.Vector;
6 import netscape.javascript.JSException;
7 import netscape.javascript.JSObject;
8 import jalview.bin.JalviewLite;
10 public class JSFunctionExec implements Runnable
14 public JSFunctionExec(JalviewLite applet)
19 private static Vector jsExecQueue;
21 private static Thread executor = null;
23 public static void stopQueue()
25 if (jsExecQueue != null)
27 synchronized (jsExecQueue)
29 Vector q = jsExecQueue;
30 q.removeAllElements();
43 while (jsExecQueue != null)
45 if (jsExecQueue.size() > 0)
47 Runnable r = (Runnable) jsExecQueue.elementAt(0);
48 jsExecQueue.removeElementAt(0);
52 } catch (Exception ex)
64 synchronized (jsExecQueue)
66 jsExecQueue.wait(1000);
68 } catch (Exception ex)
78 * execute a javascript callback asynchronously
84 public void executeJavascriptFunction(final String _listener,
85 final Object[] objects) throws Exception
87 executeJavascriptFunction(false, _listener, objects);
91 * execute a javascript callback synchronously or asynchronously
94 * - true to execute asynchronously (do this for gui events)
96 * - javascript function
100 * - only if call is synchronous
102 public void executeJavascriptFunction(final boolean async,
103 final String _listener, Object[] arguments) throws Exception
106 executeJavascriptFunction(async, _listener, arguments, null);
110 public void executeJavascriptFunction(final boolean async,
111 final String _listener, Object[] arguments, final String dbgMsg)
114 final Object[] objects = new Object[arguments != null ? arguments.length
116 if (arguments != null)
118 System.arraycopy(arguments, 0, objects, 0, arguments.length);
120 final Exception[] jsex = new Exception[1];
121 Runnable exec = new Runnable()
127 JSObject scriptObject = null;
130 scriptObject = JSObject.getWindow(jvlite);
131 } catch (Exception ex)
135 if (scriptObject != null)
137 if (jvlite.debug && dbgMsg != null)
139 System.err.println(dbgMsg);
141 scriptObject.call(_listener, objects);
143 } catch (Exception jex)
145 // squash any malformedURLExceptions thrown by windows/safari
146 if (!(jex instanceof java.net.MalformedURLException))
150 System.err.println(jex);
152 if (jex instanceof netscape.javascript.JSException)
154 jsex[0] = (netscape.javascript.JSException) jex;
157 System.err.println("Falling back to javascript: url call");
159 StringBuffer sb = new StringBuffer("javascript:" + _listener
161 for (int i = 0; objects != null && i < objects.length; i++)
168 // strip out nulls and complex objects that we can't pass this
170 if (objects[i] != null
171 && !(objects[i].getClass().getName()
172 .indexOf("jalview") == 0))
174 sb.append(objects[i].toString());
181 System.err.println(sb.toString());
187 url = new URL(sb.toString());
188 jvlite.getAppletContext().showDocument(url);
190 } catch (Exception uex)
199 jex.printStackTrace();
214 if (JSFunctionExec.executor == null)
216 JSFunctionExec.jsExecQueue = new Vector();
217 JSFunctionExec.executor = new Thread(new JSFunctionExec(jvlite));
220 synchronized (jsExecQueue)
222 jsExecQueue.addElement(exec);
223 jsExecQueue.notify();