Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.javascript

File JSFunctionExec.java

 

Coverage histogram

../../img/srcFileCovDistChart0.png
56% of files have more coverage

Code metrics

42
83
7
1
271
208
38
0.46
11.86
7
5.43

Classes

Class Line # Actions
JSFunctionExec 30 83 38 132
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
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.
11    *
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.
16    *
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.
20    */
21    package jalview.javascript;
22   
23    import jalview.bin.JalviewLite;
24   
25    import java.net.URL;
26    import java.util.Vector;
27   
28    import netscape.javascript.JSObject;
29   
 
30    public class JSFunctionExec implements Runnable
31    {
32    public JalviewLite jvlite;
33   
 
34  0 toggle public JSFunctionExec(JalviewLite applet)
35    {
36  0 jvlite = applet;
37   
38  0 jsExecQueue = jvlite.getJsExecQueue();
39  0 jvlite.setExecutor(this);
40    }
41   
42    private Vector jsExecQueue;
43   
44    private Thread executor = null;
45   
 
46  0 toggle public void stopQueue()
47    {
48  0 if (jsExecQueue != null)
49    {
50  0 Vector<JSFunctionExec> q = null;
51  0 synchronized (jsExecQueue)
52    {
53  0 q = jsExecQueue;
54  0 jsExecQueue = null;
55    }
56  0 if (q != null)
57    {
58  0 for (JSFunctionExec jx : q)
59    {
60  0 jx.jvlite = null;
61   
62    }
63  0 q.removeAllElements();
64  0 synchronized (q)
65    {
66  0 q.notifyAll();
67    }
68    }
69    }
70  0 jvlite = null;
71  0 executor = null;
72    }
73   
 
74  0 toggle @Override
75    public void run()
76    {
77  0 while (jsExecQueue != null)
78    {
79  0 if (jsExecQueue.size() > 0)
80    {
81  0 Runnable r = (Runnable) jsExecQueue.elementAt(0);
82  0 jsExecQueue.removeElementAt(0);
83  0 try
84    {
85  0 r.run();
86    } catch (Exception ex)
87    {
88  0 ex.printStackTrace();
89    } catch (Error ex)
90    {
91  0 ex.printStackTrace();
92    }
93    }
94    else
95    {
96  0 try
97    {
98  0 synchronized (jsExecQueue)
99    {
100  0 jsExecQueue.wait(1000);
101    }
102    } catch (Exception ex)
103    {
104    }
105  0 ;
106    }
107    }
108   
109    }
110   
111    /**
112    * execute a javascript callback synchronously
113    *
114    * @param _listener
115    * @param objects
116    * @throws Exception
117    */
 
118  0 toggle public void executeJavascriptFunction(final String _listener,
119    final Object[] objects) throws Exception
120    {
121  0 executeJavascriptFunction(false, _listener, objects);
122    }
123   
124    /**
125    * execute a javascript callback synchronously or asynchronously
126    *
127    * @param async
128    * - true to execute asynchronously (do this for gui events)
129    * @param _listener
130    * - javascript function
131    * @param objects
132    * - arguments
133    * @throws Exception
134    * - only if call is synchronous
135    */
 
136  0 toggle public void executeJavascriptFunction(final boolean async,
137    final String _listener, Object[] arguments) throws Exception
138    {
139   
140  0 executeJavascriptFunction(async, _listener, arguments, null);
141   
142    }
143   
 
144  0 toggle public void executeJavascriptFunction(final boolean async,
145    final String _listener, Object[] arguments, final String dbgMsg)
146    throws Exception
147    {
148  0 final Object[] objects = new Object[arguments != null ? arguments.length
149    : 0];
150  0 if (arguments != null)
151    {
152  0 System.arraycopy(arguments, 0, objects, 0, arguments.length);
153    }
154  0 final Exception[] jsex = new Exception[1];
155  0 Runnable exec = new Runnable()
156    {
 
157  0 toggle @Override
158    public void run()
159    {
160  0 try
161    {
162  0 JSObject scriptObject = null;
163  0 try
164    {
165  0 scriptObject = JSObject.getWindow(jvlite);
166    } catch (Exception ex)
167    {
168    }
169  0 ;
170  0 if (scriptObject != null)
171    {
172  0 if (jvlite.debug && dbgMsg != null)
173    {
174  0 System.err.println(dbgMsg);
175    }
176  0 scriptObject.call(_listener, objects);
177    }
178    } catch (Exception jex)
179    {
180    // squash any malformedURLExceptions thrown by windows/safari
181  0 if (!(jex instanceof java.net.MalformedURLException))
182    {
183  0 if (jvlite.debug)
184    {
185  0 System.err.println(jex);
186    }
187  0 if (jex instanceof netscape.javascript.JSException
188    && jvlite.jsfallbackEnabled)
189    {
190  0 jsex[0] = jex;
191  0 if (jvlite.debug)
192    {
193  0 System.err.println("Falling back to javascript: url call");
194    }
195  0 StringBuffer sb = new StringBuffer(
196    "javascript:" + _listener + "(");
197  0 for (int i = 0; objects != null && i < objects.length; i++)
198    {
199  0 if (i > 0)
200    {
201  0 sb.append(",");
202    }
203  0 sb.append("\"");
204    // strip out nulls and complex objects that we can't pass this
205    // way.
206  0 if (objects[i] != null && !(objects[i].getClass().getName()
207    .indexOf("jalview") == 0))
208    {
209  0 sb.append(objects[i].toString());
210    }
211  0 sb.append("\"");
212    }
213  0 sb.append(")");
214  0 if (jvlite.debug)
215    {
216  0 System.err.println(sb.toString());
217    }
218    // alternate
219  0 URL url = null;
220  0 try
221    {
222  0 url = new URL(sb.toString());
223  0 jvlite.getAppletContext().showDocument(url);
224  0 jex = null;
225    } catch (Exception uex)
226    {
227  0 jex = uex;
228    }
229    }
230  0 if (jex != null)
231    {
232  0 if (async)
233    {
234  0 jex.printStackTrace();
235    }
236    else
237    {
238  0 jsex[0] = jex;
239    }
240    }
241  0 ;
242    }
243   
244    }
245    }
246    };
247  0 if (async)
248    {
249  0 if (executor == null)
250    {
251  0 executor = new Thread(new JSFunctionExec(jvlite));
252  0 executor.start();
253    }
254  0 synchronized (jsExecQueue)
255    {
256  0 jsExecQueue.addElement(exec);
257  0 jsExecQueue.notify();
258    }
259    }
260    else
261    {
262    // wat for executor to notify us if it's running.
263  0 exec.run();
264  0 if (jsex[0] != null)
265    {
266  0 throw (jsex[0]);
267    }
268    }
269    }
270   
271    }