48 import sun.misc.Queue;
49 import sun.security.util.SecurityConstants;
50
51 /**
52 * Applet panel class. The panel manages and manipulates the
53 * applet as it is being loaded. It forks a separate thread in a new
54 * thread group to call the applet's init(), start(), stop(), and
55 * destroy() methods.
56 *
57 * @author Arthur van Hoff
58 */
59 @SuppressWarnings("serial") // JDK implementation class
60 public
61 abstract class AppletPanel extends Panel implements AppletStub, Runnable {
62
63 /**
64 * The applet (if loaded).
65 */
66 Applet applet;
67
68 /**
69 * Applet will allow initialization. Should be
70 * set to false if loading a serialized applet
71 * that was pickled in the init=true state.
72 */
73 protected boolean doInit = true;
74
75
76 /**
77 * The classloader for the applet.
78 */
79 protected AppletClassLoader loader;
80
81 /* applet event ids */
82 public final static int APPLET_DISPOSE = 0;
83 public final static int APPLET_LOAD = 1;
84 public final static int APPLET_INIT = 2;
85 public final static int APPLET_START = 3;
86 public final static int APPLET_STOP = 4;
87 public final static int APPLET_DESTROY = 5;
88 public final static int APPLET_QUIT = 6;
89 public final static int APPLET_ERROR = 7;
90
91 /* send to the parent to force relayout */
92 public final static int APPLET_RESIZE = 51234;
93
94 /* sent to a (distant) parent to indicate that the applet is being
124 * The current applet size.
125 */
126 Dimension currentAppletSize = new Dimension(10, 10);
127
128 MessageUtils mu = new MessageUtils();
129
130 /**
131 * The thread to use during applet loading
132 */
133
134 Thread loaderThread = null;
135
136 /**
137 * Flag to indicate that a loading has been cancelled
138 */
139 boolean loadAbortRequest = false;
140
141 /* abstract classes */
142 abstract protected String getCode();
143 abstract protected String getJarFiles();
144 abstract protected String getSerializedObject();
145
146 @Override
147 abstract public int getWidth();
148 @Override
149 abstract public int getHeight();
150 abstract public boolean hasInitialFocus();
151
152 private static int threadGroupNumber = 0;
153
154 protected void setupAppletAppContext() {
155 // do nothing
156 }
157
158 /*
159 * Creates a thread to run the applet. This method is called
160 * each time an applet is loaded and reloaded.
161 */
162 synchronized void createAppletThread() {
163 // Create a thread group for the applet, and start a new
164 // thread to load the applet.
413 //System.out.println("------------------- loading applet");
414 setLoaderThread(new ManagedLocalsThread(this));
415 loaderThread.start();
416 // we get to go to sleep while this runs
417 loaderThread.join();
418 setLoaderThread(null);
419 } else {
420 // REMIND: issue an error -- this case should never
421 // occur.
422 }
423 break;
424
425 case APPLET_INIT:
426 // AppletViewer "Restart" will jump from destroy method to
427 // init, that is why we need to check status w/ APPLET_DESTROY
428 if (status != APPLET_LOAD && status != APPLET_DESTROY) {
429 showAppletStatus("notloaded");
430 break;
431 }
432 applet.resize(defaultAppletSize);
433 if (doInit) {
434 if (PerformanceLogger.loggingEnabled()) {
435 PerformanceLogger.setTime("Applet Init");
436 PerformanceLogger.outputLog();
437 }
438 applet.init();
439 }
440
441 //Need the default(fallback) font to be created in this AppContext
442 Font f = getFont();
443 if (f == null ||
444 "dialog".equals(f.getFamily().toLowerCase(Locale.ENGLISH)) &&
445 f.getSize() == 12 && f.getStyle() == Font.PLAIN) {
446 setFont(new Font(Font.DIALOG, Font.PLAIN, 12));
447 }
448
449 doInit = true; // allow restarts
450
451 // Validate the applet in event dispatch thread
452 // to avoid deadlock.
453 try {
454 final AppletPanel p = this;
455 Runnable r = new Runnable() {
456 @Override
457 public void run() {
458 p.validate();
459 }
460 };
461 AWTAccessor.getEventQueueAccessor().invokeAndWait(applet, r);
462 }
463 catch(InterruptedException ie) {
464 }
465 catch(InvocationTargetException ite) {
466 }
467
468 status = APPLET_INIT;
469 showAppletStatus("inited");
470 break;
769 // notify that loading is no longer going on
770 dispatchAppletEvent(APPLET_LOADING_COMPLETED, null);
771 }
772
773 // Fixed #4508194: NullPointerException thrown during
774 // quick page switch
775 //
776 if (applet != null)
777 {
778 // Stick it in the frame
779 applet.setStub(this);
780 applet.hide();
781 add("Center", applet);
782 showAppletStatus("loaded");
783 validate();
784 }
785 }
786
787 protected Applet createApplet(final AppletClassLoader loader) throws ClassNotFoundException,
788 IllegalAccessException, IOException, InstantiationException, InterruptedException {
789 final String serName = getSerializedObject();
790 String code = getCode();
791
792 if (code != null && serName != null) {
793 System.err.println(amh.getMessage("runloader.err"));
794 // return null;
795 throw new InstantiationException("Either \"code\" or \"object\" should be specified, but not both.");
796 }
797 if (code == null && serName == null) {
798 String msg = "nocode";
799 status = APPLET_ERROR;
800 showAppletStatus(msg);
801 showAppletLog(msg);
802 repaint();
803 }
804 if (code != null) {
805 applet = (Applet)loader.loadCode(code).newInstance();
806 doInit = true;
807 } else {
808 // serName is not null;
809 try (InputStream is = AccessController.doPrivileged(
810 (PrivilegedAction<InputStream>)() -> loader.getResourceAsStream(serName));
811 ObjectInputStream ois = new AppletObjectInputStream(is, loader)) {
812
813 applet = (Applet) ois.readObject();
814 doInit = false; // skip over the first init
815 }
816 }
817
818 // Determine the JDK level that the applet targets.
819 // This is critical for enabling certain backward
820 // compatibility switch if an applet is a JDK 1.1
821 // applet. [stanley.ho]
822 findAppletJDKLevel(applet);
823
824 if (Thread.interrupted()) {
825 try {
826 status = APPLET_DISPOSE; // APPLET_ERROR?
827 applet = null;
828 // REMIND: This may not be exactly the right thing: the
829 // status is set by the stop button and not necessarily
830 // here.
831 showAppletStatus("death");
832 } finally {
833 Thread.currentThread().interrupt(); // resignal interrupt
834 }
835 return null;
836 }
|
48 import sun.misc.Queue;
49 import sun.security.util.SecurityConstants;
50
51 /**
52 * Applet panel class. The panel manages and manipulates the
53 * applet as it is being loaded. It forks a separate thread in a new
54 * thread group to call the applet's init(), start(), stop(), and
55 * destroy() methods.
56 *
57 * @author Arthur van Hoff
58 */
59 @SuppressWarnings("serial") // JDK implementation class
60 public
61 abstract class AppletPanel extends Panel implements AppletStub, Runnable {
62
63 /**
64 * The applet (if loaded).
65 */
66 Applet applet;
67
68
69 /**
70 * The classloader for the applet.
71 */
72 protected AppletClassLoader loader;
73
74 /* applet event ids */
75 public final static int APPLET_DISPOSE = 0;
76 public final static int APPLET_LOAD = 1;
77 public final static int APPLET_INIT = 2;
78 public final static int APPLET_START = 3;
79 public final static int APPLET_STOP = 4;
80 public final static int APPLET_DESTROY = 5;
81 public final static int APPLET_QUIT = 6;
82 public final static int APPLET_ERROR = 7;
83
84 /* send to the parent to force relayout */
85 public final static int APPLET_RESIZE = 51234;
86
87 /* sent to a (distant) parent to indicate that the applet is being
117 * The current applet size.
118 */
119 Dimension currentAppletSize = new Dimension(10, 10);
120
121 MessageUtils mu = new MessageUtils();
122
123 /**
124 * The thread to use during applet loading
125 */
126
127 Thread loaderThread = null;
128
129 /**
130 * Flag to indicate that a loading has been cancelled
131 */
132 boolean loadAbortRequest = false;
133
134 /* abstract classes */
135 abstract protected String getCode();
136 abstract protected String getJarFiles();
137
138 @Override
139 abstract public int getWidth();
140 @Override
141 abstract public int getHeight();
142 abstract public boolean hasInitialFocus();
143
144 private static int threadGroupNumber = 0;
145
146 protected void setupAppletAppContext() {
147 // do nothing
148 }
149
150 /*
151 * Creates a thread to run the applet. This method is called
152 * each time an applet is loaded and reloaded.
153 */
154 synchronized void createAppletThread() {
155 // Create a thread group for the applet, and start a new
156 // thread to load the applet.
405 //System.out.println("------------------- loading applet");
406 setLoaderThread(new ManagedLocalsThread(this));
407 loaderThread.start();
408 // we get to go to sleep while this runs
409 loaderThread.join();
410 setLoaderThread(null);
411 } else {
412 // REMIND: issue an error -- this case should never
413 // occur.
414 }
415 break;
416
417 case APPLET_INIT:
418 // AppletViewer "Restart" will jump from destroy method to
419 // init, that is why we need to check status w/ APPLET_DESTROY
420 if (status != APPLET_LOAD && status != APPLET_DESTROY) {
421 showAppletStatus("notloaded");
422 break;
423 }
424 applet.resize(defaultAppletSize);
425
426 if (PerformanceLogger.loggingEnabled()) {
427 PerformanceLogger.setTime("Applet Init");
428 PerformanceLogger.outputLog();
429 }
430 applet.init();
431
432
433 //Need the default(fallback) font to be created in this AppContext
434 Font f = getFont();
435 if (f == null ||
436 "dialog".equals(f.getFamily().toLowerCase(Locale.ENGLISH)) &&
437 f.getSize() == 12 && f.getStyle() == Font.PLAIN) {
438 setFont(new Font(Font.DIALOG, Font.PLAIN, 12));
439 }
440
441 // Validate the applet in event dispatch thread
442 // to avoid deadlock.
443 try {
444 final AppletPanel p = this;
445 Runnable r = new Runnable() {
446 @Override
447 public void run() {
448 p.validate();
449 }
450 };
451 AWTAccessor.getEventQueueAccessor().invokeAndWait(applet, r);
452 }
453 catch(InterruptedException ie) {
454 }
455 catch(InvocationTargetException ite) {
456 }
457
458 status = APPLET_INIT;
459 showAppletStatus("inited");
460 break;
759 // notify that loading is no longer going on
760 dispatchAppletEvent(APPLET_LOADING_COMPLETED, null);
761 }
762
763 // Fixed #4508194: NullPointerException thrown during
764 // quick page switch
765 //
766 if (applet != null)
767 {
768 // Stick it in the frame
769 applet.setStub(this);
770 applet.hide();
771 add("Center", applet);
772 showAppletStatus("loaded");
773 validate();
774 }
775 }
776
777 protected Applet createApplet(final AppletClassLoader loader) throws ClassNotFoundException,
778 IllegalAccessException, IOException, InstantiationException, InterruptedException {
779 String code = getCode();
780
781 if (code != null) {
782 applet = (Applet)loader.loadCode(code).newInstance();
783 } else {
784 String msg = "nocode";
785 status = APPLET_ERROR;
786 showAppletStatus(msg);
787 showAppletLog(msg);
788 repaint();
789 }
790
791 // Determine the JDK level that the applet targets.
792 // This is critical for enabling certain backward
793 // compatibility switch if an applet is a JDK 1.1
794 // applet. [stanley.ho]
795 findAppletJDKLevel(applet);
796
797 if (Thread.interrupted()) {
798 try {
799 status = APPLET_DISPOSE; // APPLET_ERROR?
800 applet = null;
801 // REMIND: This may not be exactly the right thing: the
802 // status is set by the stop button and not necessarily
803 // here.
804 showAppletStatus("death");
805 } finally {
806 Thread.currentThread().interrupt(); // resignal interrupt
807 }
808 return null;
809 }
|