2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.javascript;
23 import jalview.api.JalviewApp;
26 import java.util.Hashtable;
27 import java.util.Vector;
29 import netscape.javascript.JSObject;
31 public class JSFunctionExec implements Runnable
33 public JalviewApp jvlite;
35 protected boolean debug;
37 public JSFunctionExec(JalviewApp applet, boolean debug)
41 jsExecQueue = jvlite.getJsExecQueue(this);
44 private Vector<Runnable> jsExecQueue;
46 private Thread executor = null;
48 public void stopQueue()
50 if (jsExecQueue != null)
52 Vector<Runnable> q = null;
53 synchronized (jsExecQueue)
62 ((JSFunctionExec) jx).jvlite = null;
65 q.removeAllElements();
79 while (jsExecQueue != null)
81 if (jsExecQueue.size() > 0)
83 Runnable r = jsExecQueue.elementAt(0);
84 jsExecQueue.removeElementAt(0);
88 } catch (Exception ex)
100 synchronized (jsExecQueue)
102 jsExecQueue.wait(1000);
104 } catch (Exception ex)
114 * execute a javascript callback synchronously
120 public void executeJavascriptFunction(final String _listener,
121 final Object[] objects) throws Exception
123 executeJavascriptFunction(false, _listener, objects);
127 * execute a javascript callback synchronously or asynchronously
130 * - true to execute asynchronously (do this for gui events)
132 * - javascript function
136 * - only if call is synchronous
138 public void executeJavascriptFunction(final boolean async,
139 final String _listener, Object[] arguments) throws Exception
142 executeJavascriptFunction(async, _listener, arguments, null);
146 public void executeJavascriptFunction(final boolean async,
147 final String _listener, Object[] arguments, final String dbgMsg)
150 final Object[] objects = new Object[arguments != null ? arguments.length
152 if (arguments != null)
154 System.arraycopy(arguments, 0, objects, 0, arguments.length);
156 final Exception[] jsex = new Exception[1];
157 Runnable exec = new Runnable()
164 JSObject scriptObject = null;
167 scriptObject = jvlite.getJSObject();
168 } catch (Exception ex)
172 if (scriptObject != null)
174 if (debug && dbgMsg != null)
176 System.err.println(dbgMsg);
178 scriptObject.call(_listener, objects);
180 } catch (Exception jex)
182 // squash any malformedURLExceptions thrown by windows/safari
183 if (!(jex instanceof java.net.MalformedURLException))
187 System.err.println(jex);
189 if (jex instanceof netscape.javascript.JSException
190 && jvlite.isJsfallbackEnabled())
195 System.err.println("Falling back to javascript: url call");
197 StringBuffer sb = new StringBuffer(
198 "javascript:" + _listener + "(");
199 for (int i = 0; objects != null && i < objects.length; i++)
206 // strip out nulls and complex objects that we can't pass this
208 if (objects[i] != null && !(objects[i].getClass().getName()
209 .indexOf("jalview") == 0))
211 sb.append(objects[i].toString());
218 System.err.println(sb.toString());
224 url = new URL(sb.toString());
225 jvlite.getAppletContext().showDocument(url);
227 } catch (Exception uex)
236 jex.printStackTrace();
251 if (executor == null)
253 executor = new Thread(new JSFunctionExec(jvlite, debug));
256 synchronized (jsExecQueue)
258 jsExecQueue.addElement(exec);
259 jsExecQueue.notify();
264 // wat for executor to notify us if it's running.
273 public static void setJsMessageSet(String messageclass, String viewId,
274 String[] colcommands, JalviewApp app)
276 Hashtable<String, Hashtable<String, String[]>> jsmessages = app
278 Hashtable<String, int[]> jshashes = app.getJSHashes();
280 Hashtable<String, String[]> msgset = jsmessages.get(messageclass);
283 msgset = new Hashtable<>();
284 jsmessages.put(messageclass, msgset);
286 msgset.put(viewId, colcommands);
287 int[] l = new int[colcommands.length];
288 for (int i = 0; i < colcommands.length; i++)
290 l[i] = colcommands[i].hashCode();
292 jshashes.put(messageclass + "|" + viewId, l);
298 * @see jalview.bin.JalviewLiteJsApi#getJsMessage(java.lang.String,
301 public static String getJsMessage(String messageclass, String viewId,
304 Hashtable<String, String[]> msgset = app.getJSMessages()
308 String[] msgs = msgset.get(viewId);
311 for (int i = 0; i < msgs.length; i++)
325 public static boolean isJsMessageSetChanged(String string, String string2,
326 String[] colcommands, JalviewApp app)
328 int[] l = app.getJSHashes().get(string + "|" + string2);
329 if (l == null && colcommands != null)
333 for (int i = 0; i < colcommands.length; i++)
335 if (l[i] != colcommands[i].hashCode())