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