1 /*******************************************************************************
2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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/>.
17 *******************************************************************************/
18 package jalview.javascript;
\r
20 import java.net.URL;
\r
21 import java.util.Vector;
\r
23 import netscape.javascript.JSException;
\r
24 import netscape.javascript.JSObject;
\r
25 import jalview.bin.JalviewLite;
\r
27 public class JSFunctionExec implements Runnable
\r
29 public JalviewLite jvlite;
\r
31 public JSFunctionExec(JalviewLite applet)
\r
35 jsExecQueue = jvlite.getJsExecQueue();
\r
36 jvlite.setExecutor(this);
\r
38 public void finalize() {
\r
41 if (jsExecQueue!=null)
\r
43 jsExecQueue.clear();
\r
47 private Vector jsExecQueue;
\r
49 private Thread executor = null;
\r
51 public void stopQueue()
\r
53 if (jsExecQueue != null)
\r
55 Vector<JSFunctionExec> q=null;
\r
56 synchronized (jsExecQueue)
\r
62 for (JSFunctionExec jx : q)
\r
67 q.removeAllElements();
\r
80 while (jsExecQueue != null)
\r
82 if (jsExecQueue.size() > 0)
\r
84 Runnable r = (Runnable) jsExecQueue.elementAt(0);
\r
85 jsExecQueue.removeElementAt(0);
\r
89 } catch (Exception ex)
\r
91 ex.printStackTrace();
\r
94 ex.printStackTrace();
\r
101 synchronized (jsExecQueue)
\r
103 jsExecQueue.wait(1000);
\r
105 } catch (Exception ex)
\r
115 * execute a javascript callback synchronously
\r
119 * @throws Exception
\r
121 public void executeJavascriptFunction(final String _listener,
\r
122 final Object[] objects) throws Exception
\r
124 executeJavascriptFunction(false, _listener, objects);
\r
128 * execute a javascript callback synchronously or asynchronously
\r
131 * - true to execute asynchronously (do this for gui events)
\r
133 * - javascript function
\r
136 * @throws Exception
\r
137 * - only if call is synchronous
\r
139 public void executeJavascriptFunction(final boolean async,
\r
140 final String _listener, Object[] arguments) throws Exception
\r
143 executeJavascriptFunction(async, _listener, arguments, null);
\r
147 public void executeJavascriptFunction(final boolean async,
\r
148 final String _listener, Object[] arguments, final String dbgMsg)
\r
151 final Object[] objects = new Object[arguments != null ? arguments.length
\r
153 if (arguments != null)
\r
155 System.arraycopy(arguments, 0, objects, 0, arguments.length);
\r
157 final Exception[] jsex = new Exception[1];
\r
158 Runnable exec = new Runnable()
\r
164 JSObject scriptObject = null;
\r
167 scriptObject = JSObject.getWindow(jvlite);
\r
168 } catch (Exception ex)
\r
172 if (scriptObject != null)
\r
174 if (jvlite.debug && dbgMsg != null)
\r
176 System.err.println(dbgMsg);
\r
178 scriptObject.call(_listener, objects);
\r
180 } catch (Exception jex)
\r
182 // squash any malformedURLExceptions thrown by windows/safari
\r
183 if (!(jex instanceof java.net.MalformedURLException))
\r
187 System.err.println(jex);
\r
189 if (jex instanceof netscape.javascript.JSException && jvlite.jsfallbackEnabled)
\r
191 jsex[0] = (netscape.javascript.JSException) jex;
\r
194 System.err.println("Falling back to javascript: url call");
\r
196 StringBuffer sb = new StringBuffer("javascript:" + _listener
\r
198 for (int i = 0; objects != null && i < objects.length; i++)
\r
205 // strip out nulls and complex objects that we can't pass this
\r
207 if (objects[i] != null
\r
208 && !(objects[i].getClass().getName()
\r
209 .indexOf("jalview") == 0))
\r
211 sb.append(objects[i].toString());
\r
218 System.err.println(sb.toString());
\r
224 url = new URL(sb.toString());
\r
225 jvlite.getAppletContext().showDocument(url);
\r
227 } catch (Exception uex)
\r
236 jex.printStackTrace();
\r
251 if (executor == null)
\r
253 executor = new Thread(new JSFunctionExec(jvlite));
\r
256 synchronized (jsExecQueue)
\r
258 jsExecQueue.addElement(exec);
\r
259 jsExecQueue.notify();
\r
264 // wat for executor to notify us if it's running.
\r
266 if (jsex[0] != null)
\r