View Javadoc

1   package fr.ove.applet;
2   
3   import java.applet.*;
4   import java.awt.*;
5   import fr.ove.applet.ALCAppletStub;
6   
7   /***
8   * A "wrapping" applet displaying information about the status of
9   * the loading of the real applet to load.<BR>
10  * This applet takes as parameters :
11  * <UL>
12  * <LI> <CODE>doCheck11</CODE> to force checking of support of JDK 1.1. Must be setted as <CODE>true</CODE>
13  * or <CODE>false</CODE> </LI>.
14  * <LI> <CODE>theAppletName</CODE> which is the name of the applet to load </LI>.
15  * </UL>
16  *
17  * @author © 1999 DIRAT Laurent
18  * @version 1.0  30/04/99
19  */
20  public class AppletLoadingChecked extends Applet {
21      /***
22      * The different states during the loading of the applet.
23      */
24      public final static int LOADING                = 0;
25      public final static int LOADED                 = 1;
26      public final static int ERROR                  = 2;
27      public final static int JDK11_NOT_SUPPORTED    = 3;
28  
29      /***
30      * The message to display during the loading.
31      */
32      private String message;
33      private int messageWidth;
34      private FontMetrics fm = null;
35  
36      /***
37      * The image to display during the loading (typically a timer)
38      */
39      private Image image;
40      private int imageWidth;
41      private int imageHeight;
42  
43      /***
44      * The applet we really want to load
45      */
46      private Applet theApplet;
47  
48      /***
49      * The name of the applet (More exactly the name of the class)
50      */
51      private String theAppletName;
52  
53      /***
54      * The state of the applet.
55      */
56      private int state = LOADING;
57  
58      public void init() {
59          setBackground(Color.white);
60          setLayout(new BorderLayout(0, 0));
61  
62          updateMessage();
63  
64          image = getImage(getCodeBase(), "sablier.gif");
65  
66          MediaTracker tracker = new MediaTracker(this);
67          tracker.addImage(image,0);
68          try {
69              tracker.waitForAll();
70          }
71          catch (InterruptedException e) {
72          }
73  
74          if (image != null) {
75              imageWidth = image.getWidth(this);
76              imageHeight = image.getHeight(this);
77          }
78  
79          String doCheck11 = getParameter("doCheck11");
80          if ((doCheck11 != null) && (doCheck11.equals("true"))) {
81              try {
82                  Class class1 = Class.forName("java.awt.AWTEvent");
83                  class1 = Class.forName("java.awt.event.ActionEvent");
84              }
85              catch (Exception e) {
86                  state = JDK11_NOT_SUPPORTED;
87                  updateMessage();
88                  return;
89              }
90          }
91  
92          Thread t = new Thread() {
93              public void run() {
94                  while (true) {
95                      if (state != LOADING) {
96                          this.stop();
97                      }
98                      repaint();
99                      try {
100                         Thread.sleep(500);
101                     }
102                     catch (InterruptedException e) {
103                     }
104                 }
105             }
106         };
107         t.start();
108 
109         Thread t2 = new Thread() {
110             public void run() {
111                 try {
112                     theAppletName = getParameter("theAppletName");
113                     theApplet = (Applet) Class.forName(theAppletName).newInstance();
114                     theApplet.setStub(new ALCAppletStub(AppletLoadingChecked.this));
115                     Dimension size = getSize();
116                     theApplet.reshape(0, 0, size.width - 1, size.height - 1);
117                     theApplet.hide();
118                     add("Center", theApplet);
119                     theApplet.init();
120                     theApplet.show();
121                     validate();
122                     state = LOADED;
123                 } catch (Exception e) {
124                     state = ERROR;
125                     updateMessage();
126                     System.err.println(e);
127                 }
128             }
129         };
130         t2.start();
131     }
132 
133     public void start() {
134         if (state == LOADED)
135           theApplet.start();
136     }
137 
138     public void stop() {
139         if (state == LOADED)
140           theApplet.stop();
141     }
142 
143     public void paint(Graphics g) {
144         Dimension size = getSize();
145 
146         if (state == LOADED) {
147             theApplet.paint(g);
148         }
149         else if (state == LOADING) {
150             if (image != null)
151                 g.drawImage(image, size.width/2 - imageWidth/2, size.height/2 - imageHeight, this);
152             g.drawString(message, size.width/2 - messageWidth/2, size.height/2 + 20);
153         }
154         else {
155             g.setColor(Color.white);
156             g.fillRect(0, 0, size.width - 1, size.height - 1);
157             g.setColor(Color.red);
158             g.drawString(message, size.width/2 - messageWidth/2, size.height/2);
159         }
160     }
161 
162     public void update(Graphics g) {
163         paint(g);
164     }
165 
166     private void updateMessage() {
167         if (fm == null)
168             fm = getFontMetrics(getFont());
169 
170         switch (state) {
171             case LOADING :
172                 message = "Please wait while loading...";
173                 break;
174             case ERROR :
175                 message = "Something wrong occurs :o(";
176                 break;
177             case JDK11_NOT_SUPPORTED :
178                 message = "Your browser do not support Java 1.1";
179                 break;
180         }
181 
182         messageWidth = fm.stringWidth(message);
183     }
184 
185     /***
186     * Returns the state of the applet.
187     */
188     public int getState() {
189         return state;
190     }
191 
192     /***
193     * Sets the state of the applet.
194     */
195     public void setState(int state) {
196         this.state = state;
197         updateMessage();
198         repaint();
199     }
200 
201     /***
202     * Returns the "wrapped" applet
203     */
204     public Applet getTheApplet() {
205         return theApplet;
206     }
207 }