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 Vector q = jsExecQueue;
29 q.removeAllElements();
37 while (jsExecQueue != null)
39 if (jsExecQueue.size() > 0)
41 Runnable r = (Runnable) jsExecQueue.elementAt(0);
42 jsExecQueue.removeElementAt(0);
46 } catch (Exception ex)
58 synchronized (jsExecQueue)
60 jsExecQueue.wait(1000);
62 } catch (Exception ex)
72 * execute a javascript callback asynchronously
78 public void executeJavascriptFunction(final String _listener,
79 final Object[] objects) throws Exception
81 executeJavascriptFunction(false, _listener, objects);
85 * execute a javascript callback synchronously or asynchronously
88 * - true to execute asynchronously (do this for gui events)
90 * - javascript function
94 * - only if call is synchronous
96 public void executeJavascriptFunction(final boolean async,
97 final String _listener, Object[] arguments) throws Exception
99 final Object[] objects = new Object[arguments != null ? arguments.length
101 if (arguments != null)
103 System.arraycopy(arguments, 0, objects, 0, arguments.length);
105 final Exception[] jsex = new Exception[1];
106 Runnable exec = new Runnable()
112 JSObject scriptObject = null;
115 scriptObject = JSObject.getWindow(jvlite.applet);
116 } catch (Exception ex)
120 if (scriptObject != null)
122 scriptObject.call(_listener, objects);
124 } catch (Exception jex)
126 // squash any malformedURLExceptions thrown by windows/safari
127 if (!(jex instanceof java.net.MalformedURLException))
131 System.err.println(jex);
133 if (jex instanceof netscape.javascript.JSException)
135 jsex[0] = (netscape.javascript.JSException) jex;
138 System.err.println("Falling back to javascript: url call");
140 StringBuffer sb = new StringBuffer("javascript:" + _listener
142 for (int i = 0; objects != null && i < objects.length; i++)
149 // strip out nulls and complex objects that we can't pass this
151 if (objects[i] != null
152 && !(objects[i].getClass().getPackage().getName()
153 .indexOf("jalview") == 0))
155 sb.append(objects[i].toString());
162 System.err.println(sb.toString());
168 url = new URL(sb.toString());
169 jvlite.getAppletContext().showDocument(url);
171 } catch (Exception uex)
180 jex.printStackTrace();
184 jsex[0] = new Exception(jex);
195 if (JSFunctionExec.executor == null)
197 JSFunctionExec.jsExecQueue = new Vector();
198 JSFunctionExec.executor = new Thread(new JSFunctionExec(jvlite));
201 synchronized (jsExecQueue)
203 jsExecQueue.addElement(exec);
204 jsExecQueue.notify();