JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / site / j2s / swingjs / JSAppletPanel.js
1 Clazz.declarePackage ("swingjs");\r
2 Clazz.load (["java.applet.AppletContext", "$.AppletStub", "java.awt.Panel", "swingjs.api.JSInterface", "java.awt.Dimension"], "swingjs.JSAppletPanel", ["java.lang.InstantiationException", "java.net.URL", "java.awt.BorderLayout", "$.Font", "$.Toolkit", "javax.swing.JApplet", "jssun.applet.AppletEvent", "$.AppletEventMulticaster", "swingjs.JSAppletThread", "$.JSGraphics2D", "$.JSMouse", "$.JSThreadGroup", "$.JSToolkit", "$.JSUtil", "swingjs.api.Interface"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.params = null;\r
5 this.html5Applet = null;\r
6 this.fullName = null;\r
7 this.appletCodeBase = null;\r
8 this.appletIdiomaBase = null;\r
9 this.appletDocumentBase = null;\r
10 this.maximumSize = 2147483647;\r
11 this.appletName = null;\r
12 this.syncId = null;\r
13 this.testAsync = false;\r
14 this.async = false;\r
15 this.strJavaVersion = null;\r
16 this.strJavaVendor = null;\r
17 this.display = null;\r
18 this.canvas = null;\r
19 this.jsgraphics = null;\r
20 this.applet = null;\r
21 this.defaultAppletSize = null;\r
22 this.currentAppletSize = null;\r
23 this.nextStatus = 0;\r
24 this.status = 0;\r
25 this.listeners = null;\r
26 this.mouse = null;\r
27 Clazz.instantialize (this, arguments);\r
28 }, swingjs, "JSAppletPanel", java.awt.Panel, [java.applet.AppletStub, java.applet.AppletContext, swingjs.api.JSInterface]);\r
29 Clazz.prepareFields (c$, function () {\r
30 this.defaultAppletSize =  new java.awt.Dimension (10, 10);\r
31 this.currentAppletSize =  new java.awt.Dimension (10, 10);\r
32 });\r
33 Clazz.makeConstructor (c$, \r
34 function (params) {\r
35 Clazz.superConstructor (this, swingjs.JSAppletPanel, []);\r
36 this.setPanel ();\r
37 this.set (params);\r
38 }, "java.util.Hashtable");\r
39 Clazz.defineMethod (c$, "set", \r
40  function (params) {\r
41 System.out.println ("JSAppletPanel initializing");\r
42 this.params = params;\r
43 this.htmlName = swingjs.JSUtil.split ("" + this.getParameter ("name"), "_object")[0];\r
44 this.appletName = swingjs.JSUtil.split (this.htmlName + "_", "_")[0];\r
45 this.syncId = this.getParameter ("syncId");\r
46 this.fullName = this.htmlName + "__" + this.syncId + "__";\r
47 params.put ("fullName", this.fullName);\r
48 var o = params.get ("codePath");\r
49 if (o == null) o = "../java/";\r
50 this.appletCodeBase = o.toString ();\r
51 this.appletIdiomaBase = this.appletCodeBase.substring (0, this.appletCodeBase.lastIndexOf ("/", this.appletCodeBase.length - 2) + 1) + "idioma";\r
52 o = params.get ("documentBase");\r
53 this.appletDocumentBase = (o == null ? "" : o.toString ());\r
54 if (params.containsKey ("maximumSize")) Math.max ((params.get ("maximumSize")).intValue (), 100);\r
55 this.async = (this.testAsync || params.containsKey ("async"));\r
56 var applet = null;\r
57 var javaver = "?";\r
58 {\r
59 if(self.Jmol) { applet =\r
60 Jmol._applets[this.htmlName.split("_object")[0]]; javaver =\r
61 Jmol._version; }\r
62 }this.html5Applet = applet;\r
63 this.strJavaVersion = javaver;\r
64 this.strJavaVendor = "Java2Script/Java 1.6 (HTML5)";\r
65 this.display = params.get ("display");\r
66 this.threadGroup =  new swingjs.JSThreadGroup (this.appletName);\r
67 this.myThread =  new swingjs.JSAppletThread (this, this.threadGroup, this.appletName);\r
68 (this.threadGroup).setHtmlApplet (this.html5Applet);\r
69 {\r
70 Jmol._applets[this.appletName + "_thread"] =\r
71 java.lang.Thread.thisThread = this.myThread;\r
72 }this.appContext = swingjs.JSToolkit.createNewAppContext ();\r
73 java.awt.Toolkit.getDefaultToolkit ();\r
74 try {\r
75 java.net.URL.setURLStreamHandlerFactory (swingjs.api.Interface.getInstance ("JU.AjaxURLStreamHandlerFactory", false));\r
76 } catch (e) {\r
77 }\r
78 System.out.println ("JSAppletPanel initialized");\r
79 }, "java.util.Hashtable");\r
80 Clazz.defineMethod (c$, "start", \r
81 function () {\r
82 if (this.status == 0) this.myThread.start ();\r
83  else this.showStatus ("already started");\r
84 });\r
85 Clazz.defineMethod (c$, "addAppletListener", \r
86 function (l) {\r
87 this.listeners = jssun.applet.AppletEventMulticaster.add (this.listeners, l);\r
88 }, "jssun.applet.AppletListener");\r
89 Clazz.defineMethod (c$, "removeAppletListener", \r
90 function (l) {\r
91 this.listeners = jssun.applet.AppletEventMulticaster.remove (this.listeners, l);\r
92 }, "jssun.applet.AppletListener");\r
93 Clazz.defineMethod (c$, "dispatchAppletEvent", \r
94 function (id, argument) {\r
95 if (this.listeners != null) {\r
96 var evt =  new jssun.applet.AppletEvent (this, id, argument);\r
97 this.listeners.appletStateChanged (evt);\r
98 }}, "~N,~O");\r
99 Clazz.defineMethod (c$, "getCode", \r
100  function () {\r
101 return this.getParameter ("code");\r
102 });\r
103 Clazz.defineMethod (c$, "getCanvas", \r
104  function () {\r
105 return (this.canvas == null ? (this.canvas = this.html5Applet._getHtml5Canvas ()) : this.canvas);\r
106 });\r
107 Clazz.overrideMethod (c$, "isActive", \r
108 function () {\r
109 return true;\r
110 });\r
111 Clazz.overrideMethod (c$, "appletResize", \r
112 function (width, height) {\r
113 var currentSize =  new java.awt.Dimension (this.currentAppletSize.width, this.currentAppletSize.height);\r
114 this.currentAppletSize.width = width;\r
115 this.currentAppletSize.height = height;\r
116 this.applet.setBounds (0, 0, this.getWidth (), this.getHeight ());\r
117 this.applet.getRootPane ().setBounds (0, 0, this.getWidth (), this.getHeight ());\r
118 this.applet.getContentPane ().setBounds (0, 0, this.getWidth (), this.getHeight ());\r
119 (this.applet.getContentPane ()).revalidate ();\r
120 this.dispatchAppletEvent (51234, currentSize);\r
121 }, "~N,~N");\r
122 Clazz.overrideMethod (c$, "getDocumentBase", \r
123 function () {\r
124 try {\r
125 return  new java.net.URL (this.params.get ("documentBase"));\r
126 } catch (e) {\r
127 if (Clazz.exceptionOf (e, java.net.MalformedURLException)) {\r
128 return null;\r
129 } else {\r
130 throw e;\r
131 }\r
132 }\r
133 });\r
134 Clazz.overrideMethod (c$, "getCodeBase", \r
135 function () {\r
136 try {\r
137 return  new java.net.URL (this.params.get ("codePath"));\r
138 } catch (e) {\r
139 if (Clazz.exceptionOf (e, java.net.MalformedURLException)) {\r
140 return null;\r
141 } else {\r
142 throw e;\r
143 }\r
144 }\r
145 });\r
146 Clazz.overrideMethod (c$, "getParameter", \r
147 function (name) {\r
148 var s = this.params.get (name);\r
149 System.out.println ("get parameter: " + name + " = " + s);\r
150 return (s == null ? null : "" + s);\r
151 }, "~S");\r
152 Clazz.overrideMethod (c$, "getAppletContext", \r
153 function () {\r
154 return this;\r
155 });\r
156 Clazz.defineMethod (c$, "getHeight", \r
157 function () {\r
158 return this.html5Applet._getHeight ();\r
159 });\r
160 Clazz.defineMethod (c$, "getWidth", \r
161 function () {\r
162 return this.html5Applet._getWidth ();\r
163 });\r
164 Clazz.defineMethod (c$, "setBounds", \r
165 function (x, y, width, height) {\r
166 this.reshape (x, y, width, height);\r
167 this.currentAppletSize.width = width;\r
168 this.currentAppletSize.height = height;\r
169 }, "~N,~N,~N,~N");\r
170 Clazz.overrideMethod (c$, "getImage", \r
171 function (url) {\r
172 return null;\r
173 }, "java.net.URL");\r
174 Clazz.overrideMethod (c$, "getApplet", \r
175 function (name) {\r
176 var applet = null;\r
177 {\r
178 applet = Jmol._applets[name]; applet && (applet =\r
179 applet._applet);\r
180 }return applet;\r
181 }, "~S");\r
182 Clazz.overrideMethod (c$, "getApplets", \r
183 function () {\r
184 return null;\r
185 });\r
186 Clazz.defineMethod (c$, "showDocument", \r
187 function (url) {\r
188 {\r
189 window.open(url.toString());\r
190 }}, "java.net.URL");\r
191 Clazz.defineMethod (c$, "showDocument", \r
192 function (url, target) {\r
193 {\r
194 window.open(url.toString(), target);\r
195 }}, "java.net.URL,~S");\r
196 Clazz.defineMethod (c$, "showStatus", \r
197 function (status) {\r
198 swingjs.JSToolkit.log (status);\r
199 {\r
200 Clazz._LoaderProgressMonitor.showStatus(status, true);\r
201 }}, "~S");\r
202 Clazz.overrideMethod (c$, "cacheFileByName", \r
203 function (fileName, isAdd) {\r
204 return 0;\r
205 }, "~S,~B");\r
206 Clazz.overrideMethod (c$, "cachePut", \r
207 function (key, data) {\r
208 }, "~S,~O");\r
209 Clazz.overrideMethod (c$, "destroy", \r
210 function () {\r
211 });\r
212 Clazz.overrideMethod (c$, "getFullName", \r
213 function () {\r
214 return this.fullName;\r
215 });\r
216 Clazz.overrideMethod (c$, "openFileAsyncSpecial", \r
217 function (fileName, flags) {\r
218 }, "~S,~N");\r
219 Clazz.defineMethod (c$, "processMouseEvent", \r
220 function (id, x, y, modifiers, time) {\r
221 this.getMouse ().processEvent (id, x, y, modifiers, time);\r
222 return false;\r
223 }, "~N,~N,~N,~N,~N");\r
224 Clazz.defineMethod (c$, "getMouse", \r
225  function () {\r
226 return (this.mouse == null ? this.mouse =  new swingjs.JSMouse (this) : this.mouse);\r
227 });\r
228 Clazz.overrideMethod (c$, "processTwoPointGesture", \r
229 function (touches) {\r
230 this.getMouse ().processTwoPointGesture (touches);\r
231 }, "~A");\r
232 Clazz.overrideMethod (c$, "setDisplay", \r
233 function (canvas) {\r
234 this.canvas = canvas;\r
235 }, "swingjs.api.HTML5Canvas");\r
236 Clazz.overrideMethod (c$, "setScreenDimension", \r
237 function (width, height) {\r
238 this.setGraphics (this.jsgraphics = null);\r
239 if (this.applet != null) this.applet.resize (width, height);\r
240 }, "~N,~N");\r
241 Clazz.overrideMethod (c$, "setStatusDragDropped", \r
242 function (mode, x, y, fileName) {\r
243 return false;\r
244 }, "~N,~N,~N,~S");\r
245 Clazz.overrideMethod (c$, "startHoverWatcher", \r
246 function (enable) {\r
247 }, "~B");\r
248 Clazz.overrideMethod (c$, "paint", \r
249 function (g) {\r
250 this.applet.paint (this.setGraphics (g));\r
251 }, "java.awt.Graphics");\r
252 Clazz.defineMethod (c$, "setGraphics", \r
253  function (g) {\r
254 return (g == null ? this.getGraphics () : g);\r
255 }, "java.awt.Graphics");\r
256 Clazz.overrideMethod (c$, "getGraphics", \r
257 function () {\r
258 if (this.jsgraphics == null) {\r
259 this.jsgraphics =  new swingjs.JSGraphics2D (this.getCanvas ());\r
260 this.jsgraphics.setWindowParameters (this.getWidth (), this.getHeight ());\r
261 }return this.jsgraphics;\r
262 });\r
263 Clazz.defineMethod (c$, "showAppletStatus", \r
264  function (status) {\r
265 this.getAppletContext ().showStatus (this.htmlName + " " + status);\r
266 }, "~S");\r
267 Clazz.defineMethod (c$, "showAppletException", \r
268  function (t) {\r
269 {\r
270 this.showAppletStatus("error " + (t.getMessage ?\r
271 t.getMessage() : t)); t.printStackTrace &&\r
272 t.printStackTrace();\r
273 }this.repaint ();\r
274 }, "Throwable");\r
275 Clazz.defineMethod (c$, "run1", \r
276 function (mode) {\r
277 var ok = false;\r
278 switch (mode) {\r
279 case 0:\r
280 this.currentAppletSize.width = this.defaultAppletSize.width = this.getWidth ();\r
281 this.currentAppletSize.height = this.defaultAppletSize.height = this.getHeight ();\r
282 this.setLayout ( new java.awt.BorderLayout ());\r
283 this.nextStatus = 1;\r
284 ok = true;\r
285 break;\r
286 case 1:\r
287 switch (this.nextStatus) {\r
288 case 1:\r
289 if (this.status != 0) {\r
290 this.showAppletStatus ("notdisposed");\r
291 this.status = 7;\r
292 break;\r
293 }System.out.println ("JSAppletPanel runloader");\r
294 this.runLoader ();\r
295 this.nextStatus = 2;\r
296 ok = true;\r
297 break;\r
298 case 2:\r
299 if (this.status != 1 && this.status != 5) {\r
300 this.showAppletStatus ("notloaded");\r
301 break;\r
302 }System.out.println ("JSAppletPanel init");\r
303 this.setFont ( new java.awt.Font ("Dialog", 0, 12));\r
304 this.applet.resize (this.defaultAppletSize);\r
305 this.applet.init ();\r
306 this.validate ();\r
307 this.status = 2;\r
308 this.showAppletStatus ("initialized");\r
309 this.nextStatus = 3;\r
310 ok = true;\r
311 break;\r
312 case 3:\r
313 if (this.status != 2 && this.status != 4) {\r
314 this.showAppletStatus ("notstarted");\r
315 this.status = 7;\r
316 break;\r
317 }this.applet.getRootPane ().addNotify ();\r
318 System.out.println ("JSAppletPanel start" + this.currentAppletSize);\r
319 this.applet.resize (this.currentAppletSize);\r
320 this.applet.start ();\r
321 this.status = 3;\r
322 this.showAppletStatus ("started");\r
323 this.nextStatus = 35;\r
324 ok = true;\r
325 break;\r
326 case 35:\r
327 swingjs.JSToolkit.readyCallback (this.appletName, this.fullName, this.applet, this);\r
328 break;\r
329 case 4:\r
330 if (this.status == 3) {\r
331 this.status = 4;\r
332 this.applet.setVisible (false);\r
333 this.applet.stop ();\r
334 this.showAppletStatus ("stopped");\r
335 } else {\r
336 this.showAppletStatus ("notstopped");\r
337 this.status = 7;\r
338 }break;\r
339 case 5:\r
340 if (this.status == 4 || this.status == 2) {\r
341 this.status = 5;\r
342 this.applet.destroy ();\r
343 this.showAppletStatus ("destroyed");\r
344 } else {\r
345 this.showAppletStatus ("notdestroyed");\r
346 this.status = 7;\r
347 }break;\r
348 case 75:\r
349 if (this.status == 5 || this.status == 1) {\r
350 this.showAppletStatus ("notdisposed");\r
351 this.status = 7;\r
352 } else {\r
353 this.status = 0;\r
354 this.removeChild (this.applet);\r
355 this.applet = null;\r
356 this.showAppletStatus ("disposed");\r
357 }break;\r
358 case 6:\r
359 break;\r
360 default:\r
361 System.out.println ("unrecognized JSAppletPanel status: " + this.nextStatus);\r
362 break;\r
363 }\r
364 break;\r
365 default:\r
366 System.out.println ("unrecognized JSAppletThread mode: " + mode);\r
367 break;\r
368 }\r
369 return (ok ? 1 : 2);\r
370 }, "~N");\r
371 Clazz.defineMethod (c$, "runLoader", \r
372  function () {\r
373 this.dispatchAppletEvent (51235, null);\r
374 this.status = 1;\r
375 var code = this.getCode ();\r
376 try {\r
377 if (code == null) {\r
378 System.err.println ("runloader.err-- \"code\" must be specified.");\r
379 throw  new InstantiationException ("\"code\" must be specified.");\r
380 }this.applet = swingjs.JSToolkit.getInstance (code);\r
381 if (this.applet == null) {\r
382 System.out.println (code + " could not be launched");\r
383 this.status = 7;\r
384 } else if (!(Clazz.instanceOf (this.applet, javax.swing.JApplet))) {\r
385 System.out.println (code + " is not a JApplet!?");\r
386 this.status = 7;\r
387 }} catch (e$$) {\r
388 if (Clazz.exceptionOf (e$$, InstantiationException)) {\r
389 var e = e$$;\r
390 {\r
391 this.status = 7;\r
392 this.showAppletException (e);\r
393 return;\r
394 }\r
395 } else if (Clazz.exceptionOf (e$$, Exception)) {\r
396 var e = e$$;\r
397 {\r
398 this.status = 7;\r
399 this.showAppletException (e);\r
400 return;\r
401 }\r
402 } else if (Clazz.exceptionOf (e$$, ThreadDeath)) {\r
403 var e = e$$;\r
404 {\r
405 this.status = 7;\r
406 this.showAppletStatus ("death");\r
407 return;\r
408 }\r
409 } else if (Clazz.exceptionOf (e$$, Error)) {\r
410 var e = e$$;\r
411 {\r
412 this.status = 7;\r
413 this.showAppletException (e);\r
414 return;\r
415 }\r
416 } else {\r
417 throw e$$;\r
418 }\r
419 } finally {\r
420 this.dispatchAppletEvent (51236, null);\r
421 }\r
422 if (this.applet != null) {\r
423 this.applet.setStub (this);\r
424 this.applet.setVisible (false);\r
425 this.add ("Center", this.applet);\r
426 this.applet.setDispatcher ();\r
427 this.applet.addNotify ();\r
428 this.showAppletStatus ("loaded");\r
429 this.validate ();\r
430 }});\r
431 Clazz.defineMethod (c$, "getUI", \r
432 function () {\r
433 return null;\r
434 });\r
435 Clazz.overrideMethod (c$, "repaintImpl", \r
436 function (tm, x, y, width, height) {\r
437 if (this.applet.isVisible () && this.applet.getWidth () > 0 && this.applet.getHeight () > 0) {\r
438 this.applet.getContentPane ().repaint (tm, x, y, width, height);\r
439 }}, "~N,~N,~N,~N,~N");\r
440 Clazz.defineStatics (c$,\r
441 "APPLET_UNINITIALIZED", 0,\r
442 "APPLET_LOAD", 1,\r
443 "APPLET_INIT", 2,\r
444 "APPLET_START", 3,\r
445 "APPLET_READY", 35,\r
446 "APPLET_STOP", 4,\r
447 "APPLET_DESTROY", 5,\r
448 "APPLET_QUIT", 6,\r
449 "APPLET_ERROR", 7,\r
450 "APPLET_DISPOSE", 75,\r
451 "APPLET_RESIZE", 51234,\r
452 "APPLET_LOADING", 51235,\r
453 "APPLET_LOADING_COMPLETED", 51236);\r
454 });\r