-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJSAppletViewer.java
More file actions
640 lines (565 loc) · 17.8 KB
/
Copy pathJSAppletViewer.java
File metadata and controls
640 lines (565 loc) · 17.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
package swingjs;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLStreamHandlerFactory;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.swing.WindowConstants;
import javajs.util.JSThread;
import javajs.util.Lst;
import javajs.util.PT;
import jsjava.applet.Applet;
import jsjava.applet.AppletContext;
import jsjava.applet.AppletStub;
import jsjava.awt.Container;
import jsjava.awt.Dimension;
import jsjava.awt.Font;
import jsjava.awt.Frame;
import jsjava.awt.GraphicsConfiguration;
import jsjava.awt.Image;
import jsjava.awt.Insets;
import jsjava.awt.Toolkit;
import jsjava.awt.Window;
import jsjava.util.Locale;
import jsjavax.swing.JApplet;
import jsjavax.swing.JComponent;
import jsjavax.swing.JFrame;
import jssun.applet.AppletEvent;
import jssun.applet.AppletEventMulticaster;
import jssun.applet.AppletListener;
import jssun.awt.AppContext;
import swingjs.api.HTML5Applet;
import swingjs.api.Interface;
import swingjs.api.JSInterface;
import swingjs.plaf.Resizer;
/**
* JSAppletViewer
*
* SwingJS class to start an applet. Note that this must be a JApplet,
* not just java.awt.Applet. The SwingJS implementation does not allow
* "mixed" contents -- That is, no non-Swing Applet components are allowed.
*
* However, the package a2s has adapter classes that have names of
* the AWT components Label, Button, Applet, Frame, etc., which allow
* one to simply change the imports from java.awt or java.awt.applet to a2s
* and be done with it. This may take some development along the way, as we have
* not fully implemented all of the methods of AWT classes, and the JComponent
* does not subclass its AWT counterpart.
*
*
*
* The basic start up in JavaScript involves:
*
* Clazz.loadClass("swingjs.JSAppletViewer");
* var _appletViewer = new JSAppletViewer(viewerOptions);
* _appletViewer.start();
*
* where viewerOptions holds critical information needed to create this applet
*
*
* @author Bob Hanson
*
*/
public class JSAppletViewer extends JSFrameViewer implements AppletStub, AppletContext,
JSInterface {
/*
* the JavaScript testApplet._applet object
*/
private Hashtable params;
public int maximumSize = Integer.MAX_VALUE;
// /// AppletViewer fields //////
public String appletCodeBase;
public String appletIdiomaBase;
public String appletDocumentBase;
public String appletName;
public String syncId;
public boolean testAsync;
public boolean async;
public String strJavaVersion;
public Object strJavaVendor;
public GraphicsConfiguration graphicsConfig;
public JSThreadGroup threadGroup;
public JSThread myThread;
public boolean haveFrames = false;
/**
* The initial applet size.
*/
Dimension defaultAppletSize = new Dimension(10, 10);
/**
* The current applet size.
*/
Dimension currentAppletSize = new Dimension(10, 10);
private int nextStatus;
/* applet event ids */
public final static int APPLET_UNINITIALIZED = 0; // SwingJS
public final static int APPLET_LOAD = 1;
public final static int APPLET_INIT = 2;
public final static int APPLET_START = 3;
public final static int APPLET_READY = 35; // SwingJS
public final static int APPLET_STOP = 4;
public final static int APPLET_DESTROY = 5;
public final static int APPLET_QUIT = 6;
public final static int APPLET_ERROR = 7;
public final static int APPLET_DISPOSE = 75; // SwingJS
public final static int RUN_MAIN = 76; // SwingJS
/* send to the parent to force relayout */
public final static int APPLET_RESIZE = 51234;
/*
* sent to a (distant) parent to indicate that the applet is being loaded or
* as completed loading
*/
public final static int APPLET_LOADING = 51235;
public final static int APPLET_LOADING_COMPLETED = 51236;
/**
* The current status. One of: APPLET_DISPOSE, APPLET_LOAD, APPLET_INIT,
* APPLET_START, APPLET_STOP, APPLET_DESTROY, APPLET_ERROR.
*/
private int status = APPLET_UNINITIALIZED;
private AppletListener listeners;
public Lst<Window>allWindows = new Lst<Window>();
public void addWindow(Window window) {
// not entirely clear why we are getting multiples here
allWindows.removeObj(window);
allWindows.addLast(window);
}
public Frame sharedOwnerFrame;
public String htmlName;
public AppContext appContext;
private ArrayList<Object> timerQueue;
boolean isResizable, haveResizable;
private boolean addFrame;
private String main;
private JFrame jAppletFrame;
static {
try {
URL.setURLStreamHandlerFactory((URLStreamHandlerFactory) Interface
.getInstance("javajs.util.AjaxURLStreamHandlerFactory", false));
} catch (Throwable e) {
// that's fine -- already created
}
}
/**
* SwingJS initialization is through a Hashtable provided by the page
* JavaScript
*
* After the applet is instantiated is the opportunity to add a listener using
* setAppletListener(x), where x.appletStateChanged(AppletEvent evt) exists
*
* next command on page should be appletViewer.start();
*
* @param params
*/
public JSAppletViewer(Hashtable params) {
isApplet = true;
appletViewer = this;
set(params);
}
/**
* @param params
*/
@SuppressWarnings("static-access")
private void set(Hashtable<String, Object> params) {
isApplet = true;
System.out.println("JSAppletViewer initializing");
this.params = params;
String language = getParameter("language");
if (language == null)
language = JSToolkit.J2S._getDefaultLanguage(false);
Locale.setDefault(JSToolkit.getDefaultLocale(language));
htmlName = JSUtil.split("" + getParameter("name"), "_object")[0];
appletName = JSUtil.split(htmlName + "_", "_")[0];
// should be the same as htmlName; probably should point out that applet
// names cannot have _ in them.
syncId = getParameter("syncId");
fullName = htmlName + "__" + syncId + "__";
params.put("fullName", fullName);
Object o = params.get("codePath");
if (o == null)
o = "../java/";
appletCodeBase = o.toString();
appletIdiomaBase = appletCodeBase.substring(0,
appletCodeBase.lastIndexOf("/", appletCodeBase.length() - 2) + 1)
+ "idioma";
o = params.get("documentBase");
appletDocumentBase = (o == null ? "" : o.toString());
if (params.containsKey("maximumSize"))
Math.max(((Integer) params.get("maximumSize")).intValue(), 100);
async = (testAsync || params.containsKey("async"));
HTML5Applet applet = JSToolkit.J2S._findApplet(htmlName);
String javaver = JSToolkit.J2S._getJavaVersion();
html5Applet = applet;
strJavaVersion = javaver;
strJavaVendor = "Java2Script/Java 1.6 (HTML5)";
// String platform = (String) params.get("platform");
// if (platform != null && platform.length() > 0)
// apiPlatform = (GenericPlatform) Interface.getInterface(platform);
display = params.get("display");
String s = "" + params.get("isResizable");
isResizable = "true".equalsIgnoreCase(s);
haveResizable = (isResizable || "false".equalsIgnoreCase(s));
addFrame = "true".equalsIgnoreCase("" + params.get("addFrame"));
threadGroup = new JSThreadGroup(appletName);
myThread = new JSAppletThread(this, threadGroup, appletName);
// JSToolkit.J2S._setAppletThread(appletName, myThread);
jsjava.lang.Thread.thisThread = (jsjava.lang.Thread) ((Object) myThread);
appContext = JSToolkit.createNewAppContext();
// initialize toolkit and graphics configuration
Toolkit.getDefaultToolkit();
new JSGraphicsConfiguration().getDevice();
o = params.get("assets");
if (o != null)
JSToolkit.loadJavaResourcesFromZip(getClass().getClassLoader(), (String) o, null);
System.out.println("JSAppletViewer initialized");
insets = new Insets(0, 0, 0, 0);
}
public void start() {
if (status == APPLET_UNINITIALIZED)
myThread.start();
else
showStatus("already started");
japplet.repaint();
}
synchronized public void addAppletListener(AppletListener l) {
listeners = AppletEventMulticaster.add(listeners, l);
}
synchronized public void removeAppletListener(AppletListener l) {
listeners = AppletEventMulticaster.remove(listeners, l);
}
/**
* Dispatch event to the listeners..
*/
public void dispatchAppletEvent(int id, Object argument) {
if (listeners != null) {
AppletEvent evt = new AppletEvent(this, id, argument);
listeners.appletStateChanged(evt);
}
}
// ///////// AppletStub ////////////////
@Override
public boolean isActive() {
return true;
}
@Override
/**
* Is called when the applet wants to be resized.
*/
public void appletResize(int width, int height) {
final Dimension currentSize = new Dimension(currentAppletSize.width,
currentAppletSize.height);
currentAppletSize.width = width;
currentAppletSize.height = height;
japplet.setBounds(0, 0, getWidth(), getHeight());
japplet.getRootPane().setBounds(0, 0, getWidth(), getHeight());
japplet.getContentPane().setBounds(0, 0, getWidth(), getHeight());
((JComponent) japplet.getContentPane()).revalidate();
if (addFrame) {
jAppletFrame = new JFrame("SwingJS Applet Viewer");
Container pane = japplet.getContentPane();
jAppletFrame.setContentPane(pane);
japplet.setVisible(false);
jAppletFrame.pack();
jAppletFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
//if (wNew > 0 && hNew > 0)
japplet.repaint(0, 0, getWidth(), getHeight());
dispatchAppletEvent(APPLET_RESIZE, currentSize);
}
@Override
public URL getDocumentBase() {
try {
return new URL((String) params.get("documentBase"));
} catch (MalformedURLException e) {
return null;
}
}
@Override
public URL getCodeBase() {
try {
return new URL((String) params.get("codePath"));
} catch (MalformedURLException e) {
return null;
}
}
@Override
public String getParameter(String name) {
String s = (String) params.get(name);
System.out.println("get parameter: " + name + " = " + s);
return (s == null ? null : "" + s); // because it may not be a string in JavaScript if inherited from Info
}
@Override
public AppletContext getAppletContext() {
return this;
}
public int getHeight() {
return this.html5Applet._getHeight();
}
public int getWidth() {
return this.html5Applet._getWidth();
}
public void setBounds(int x, int y, int width, int height) {
japplet.reshape(x, y, width, height); // straight to component
currentAppletSize.width = width;
currentAppletSize.height = height;
}
// /// AppletContext /////
@Override
public Image getImage(URL url) {
// TODO Auto-generated method stub
return null;
}
@Override
public Applet getApplet(String name) {
Applet applet = null;
/**
* @j2sNative
*
* applet = SwingJS._applets[name]; applet && (applet =
* applet._applet);
*/
{
}
return applet;
}
@Override
public Enumeration<Applet> getApplets() {
// not supported for now
return null;
}
@Override
public void showDocument(URL url) {
/**
* @j2sNative window.open(url.toString());
*/
{
System.out.println(url);
}
}
@Override
public void showDocument(URL url, String target) {
/**
* @j2sNative window.open(url.toString(), target);
*/
{
System.out.println(url);
}
}
@Override
public void showStatus(String status) {
JSToolkit.log(status);
/**
* @j2sNative
*
* Clazz._LoaderProgressMonitor.showStatus(status, true);
*/
{
System.out.println(status);
}
}
private void showAppletStatus(String status) {
getAppletContext().showStatus(htmlName + " " + status);
}
private void showAppletException(Throwable t) {
/**
* @j2sNative
*
* this.showAppletStatus("error " + (t.getMessage ?
* t.getMessage() : t)); t.printStackTrace &&
* t.printStackTrace();
*/
{
}
//repaint();
}
/**
*
* @param mode
* @return LOOP or DONE
*/
public int run1(int mode) {
System.out.println("JSAppletViewer thread run1 mode=" + mode + " status=" + nextStatus);
boolean ok = false;
switch (mode) {
case JSThread.INIT:
currentAppletSize.width = defaultAppletSize.width = getWidth();
currentAppletSize.height = defaultAppletSize.height = getHeight();
nextStatus = APPLET_LOAD;
ok = true;
break;
case JSThread.LOOP:
switch (nextStatus) {
case APPLET_LOAD:
if (status != APPLET_UNINITIALIZED) {
showAppletStatus("notdisposed");
status = APPLET_ERROR;
break;
}
System.out.println("JSAppletViewer runloader");
runLoader(); // applet created here
nextStatus = (main == null ? APPLET_INIT : RUN_MAIN);
ok = true;
break;
case APPLET_INIT:
// AppletViewer "Restart" will jump from destroy method to
// init, that is why we need to check status w/ APPLET_DESTROY
if (status != APPLET_LOAD && status != APPLET_DESTROY) {
showAppletStatus("notloaded");
break;
}
System.out.println("JSAppletViewer init");
japplet.setFont(new Font(Font.DIALOG, Font.PLAIN, 12));
japplet.resize(defaultAppletSize);
japplet.init();
// Need the default(fallback) font to be created in this AppContext
japplet.validate(); // SwingJS
status = APPLET_INIT;
showAppletStatus("initialized");
nextStatus = APPLET_START;
ok = true;
break;
case APPLET_START:
if (status != APPLET_INIT && status != APPLET_STOP) {
showAppletStatus("notstarted");
status = APPLET_ERROR;
break;
}
japplet.getRootPane().addNotify();
// force peer creation now
System.out.println("JSAppletViewer start" + currentAppletSize);
japplet.resize(currentAppletSize);
japplet.start();
japplet.repaint();
status = APPLET_START;
showAppletStatus("started");
nextStatus = APPLET_READY;
ok = true;
break;
case APPLET_READY:
japplet.setVisible(true);
showAppletStatus("ready");
JSToolkit.readyCallback(appletName, fullName, applet, this);
if (isResizable && !addFrame) {
resizer = ((Resizer) JSToolkit.getInstance("swingjs.plaf.Resizer"))
.set(this);
if (resizer != null)
resizer.show();
}
japplet.repaint();
break;
case APPLET_STOP:
if (status == APPLET_START) {
status = APPLET_STOP;
japplet.setVisible(false);
japplet.stop();
showAppletStatus("stopped");
} else {
showAppletStatus("notstopped");
status = APPLET_ERROR;
}
break;
case APPLET_DESTROY:
if (status == APPLET_STOP || status == APPLET_INIT) {
status = APPLET_DESTROY;
japplet.destroy();
showAppletStatus("destroyed");
} else {
showAppletStatus("notdestroyed");
status = APPLET_ERROR;
}
break;
case APPLET_DISPOSE:
if (status == APPLET_DESTROY || status == APPLET_LOAD) {
showAppletStatus("notdisposed");
status = APPLET_ERROR;
} else {
status = APPLET_UNINITIALIZED;
// removeChild(japplet);
applet = null;
showAppletStatus("disposed");
}
break;
case RUN_MAIN:
showAppletStatus("running " + main);
// we still have an applet context for a variety of reasons
// but main(String[] args) is run as a static method.
Object args = params.get("args");
if (args instanceof String)
args = PT.split((String) args, " ");
((JSApplet) applet).runMain(main, (String[]) args);
JSToolkit.readyCallback(appletName, fullName, applet, this);
break;
case APPLET_QUIT:
break;
default:
System.out.println("unrecognized JSAppletViewer status: " + nextStatus);
break;
}
break;
default:
System.out.println("unrecognized JSAppletThread mode: " + mode);
break;
}
return (ok ? JSThread.LOOP : JSThread.DONE);
}
private void runLoader() {
dispatchAppletEvent(APPLET_LOADING, null);
status = APPLET_LOAD;
main = getParameter("main");
String code = (main == null ? getParameter("code") : null);
try {
if (code == null && main == null) {
System.err.println("runloader.err-- \"code\" or \"main\" must be specified.");
throw new InstantiationException("\"code\" or \"main\" must be specified.");
}
if (code == null)
code = "swingjs.JSApplet";
top = applet = japplet = (JApplet) JSToolkit.getInstance(code);
if (applet == null) {
System.out.println(code + " could not be launched");
status = APPLET_ERROR;
} else if (!(applet instanceof JApplet)) {
System.out.println(code + " is not a JApplet!?");
status = APPLET_ERROR;
}
} catch (InstantiationException e) {
status = APPLET_ERROR;
showAppletException(e);
return;
} catch (Exception e) {
status = APPLET_ERROR;
showAppletException(e);
return;
} catch (ThreadDeath e) {
status = APPLET_ERROR;
showAppletStatus("death");
return;
} catch (Error e) {
status = APPLET_ERROR;
showAppletException(e);
return;
} finally {
// notify that loading is no longer going on
dispatchAppletEvent(APPLET_LOADING_COMPLETED, null);
}
if (applet != null) {
japplet.setStub(this);
japplet.setVisible(false);
japplet.setDispatcher();
//japplet.addNotify(); // we need this here because there is no frame
showAppletStatus("loaded");
}
}
public JSFrameViewer newFrameViewer(boolean forceNew) {
return (haveFrames || forceNew ? new JSFrameViewer() : null);
}
public ArrayList<Object> getTimerQueue() {
return (timerQueue == null ? (timerQueue = new ArrayList<Object>()) : timerQueue);
}
public void exit() {
for (int i = allWindows.size(); --i >= 0;)
try {
allWindows.get(i).dispose();
} catch (Throwable e) {
}
}
}