Clover icon

Coverage Report

  1. Project Clover database Fri Nov 1 2024 11:46:37 GMT
  2. Package jalview.javascript

File JSFunctionExec.java

 

Coverage histogram

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

Code metrics

42
83
7
1
272
209
38
0.46
11.86
7
5.43

Classes

Class Line # Actions
JSFunctionExec 30 83 38
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 jalview.bin.Console.errPrintln(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 jalview.bin.Console.errPrintln(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 jalview.bin.Console
194    .errPrintln("Falling back to javascript: url call");
195    }
196  0 StringBuffer sb = new StringBuffer(
197    "javascript:" + _listener + "(");
198  0 for (int i = 0; objects != null && i < objects.length; i++)
199    {
200  0 if (i > 0)
201    {
202  0 sb.append(",");
203    }
204  0 sb.append("\"");
205    // strip out nulls and complex objects that we can't pass this
206    // way.
207  0 if (objects[i] != null && !(objects[i].getClass().getName()
208    .indexOf("jalview") == 0))
209    {
210  0 sb.append(objects[i].toString());
211    }
212  0 sb.append("\"");
213    }
214  0 sb.append(")");
215  0 if (jvlite.debug)
216    {
217  0 jalview.bin.Console.errPrintln(sb.toString());
218    }
219    // alternate
220  0 URL url = null;
221  0 try
222    {
223  0 url = new URL(sb.toString());
224  0 jvlite.getAppletContext().showDocument(url);
225  0 jex = null;
226    } catch (Exception uex)
227    {
228  0 jex = uex;
229    }
230    }
231  0 if (jex != null)
232    {
233  0 if (async)
234    {
235  0 jex.printStackTrace();
236    }
237    else
238    {
239  0 jsex[0] = jex;
240    }
241    }
242  0 ;
243    }
244   
245    }
246    }
247    };
248  0 if (async)
249    {
250  0 if (executor == null)
251    {
252  0 executor = new Thread(new JSFunctionExec(jvlite));
253  0 executor.start();
254    }
255  0 synchronized (jsExecQueue)
256    {
257  0 jsExecQueue.addElement(exec);
258  0 jsExecQueue.notify();
259    }
260    }
261    else
262    {
263    // wat for executor to notify us if it's running.
264  0 exec.run();
265  0 if (jsex[0] != null)
266    {
267  0 throw (jsex[0]);
268    }
269    }
270    }
271   
272    }