View Javadoc

1   package fr.ove.applet;
2   
3   import java.applet.*;
4   import java.util.*;
5   import fr.ove.applet.AppletLoadingChecked;
6   import fr.ove.applet.ListenableApplet;
7   import fr.ove.applet.events.AppletListener;
8   import fr.ove.applet.events.AppletEvent;
9   
10  /***
11  * This is an implementation of the <CODE>AppletListener</CODE> interface.<BR>
12  * This applet takes as argument a parameter named <CODE>listened</CODE> which
13  * represents a list of <CODE>ListenableApplet</CODE> the instance will be listening to.<BR>
14  * Each element of the list is the name of the corresponding <CODE>ListenableApplet</CODE>.
15  * The elements of the list are separated by a coma (,).
16  * Ex: <PARAM NAME="listened" VALUE="{applet1, applet2}">
17  * <P>
18  * This class is an final class, so it can't be overloaded.
19  *
20  * @author © 1999 DIRAT Laurent
21  * @version 1.0  27/04/98
22  */
23  public final class AnAppletListener extends AppletLoadingChecked implements AppletListener {
24      int timeMax = 60;
25      int timeout = 0;
26      Thread timer;
27  
28      /***
29      * Initialyzes the instance by registering it to each <CODE>ListenableApplet</CODE>
30      * in the list passed as argument.
31      */
32      public void init() {
33          super.init();
34  
35          timer = new Thread() {
36              public void run() {
37                  while (timeout < timeMax) {
38                      try {
39                          Thread.sleep(1000);
40                      }
41                      catch (InterruptedException ie) {
42                      }
43                      timeout++;
44                  }
45              }
46          };
47  		timer.start();
48  
49  		Thread threadRegistering = new Thread() {
50              public void run() {
51                  String listened = getParameter("listened");
52  
53          		if ((listened != null) && !listened.equals("")) {
54              		Vector listOfListened = new Vector();
55              		StringTokenizer tokenizer = new StringTokenizer(listened, "{, }");
56              		while (tokenizer.hasMoreElements())
57              		    listOfListened.addElement(tokenizer.nextToken());
58  
59              		Enumeration listeApplets;
60                      Applet anApplet;
61                      ListenableApplet aListenableApplet;
62              		while ((listOfListened.size() != 0) && (timeout < timeMax)) {
63                          listeApplets = getAppletContext().getApplets();
64  
65                          for (Enumeration e = listeApplets; e.hasMoreElements(); ) {
66                              anApplet = (Applet) e.nextElement();
67  
68                              if (anApplet instanceof ListenableApplet) {
69                                  aListenableApplet = (ListenableApplet) anApplet;
70                                  String appletName = aListenableApplet.getName();
71  
72                                  if (listOfListened.contains(appletName)) {
73                                      int state = AppletLoadingChecked.LOADING;
74                                      if (aListenableApplet instanceof AppletLoadingChecked) {
75                                          state = ((AppletLoadingChecked) aListenableApplet).getState();
76                                          while (state == AppletLoadingChecked.LOADING) {
77                                              try {
78                                                  Thread.sleep(500);
79                                              }
80                                              catch (InterruptedException ie) {
81                                              }
82  
83                                              state = ((AppletLoadingChecked) aListenableApplet).getState();
84                                          }
85                                      }
86  
87                                      if (state == AppletLoadingChecked.LOADED) {
88                                          int instanceState = getState();
89                                          while (instanceState == AppletLoadingChecked.LOADING) {
90                                              try {
91                                                  Thread.sleep(500);
92                                              }
93                                              catch (InterruptedException ie) {
94                                              }
95  
96                                              instanceState = getState();
97                                          }
98                                          aListenableApplet.addAppletListener(AnAppletListener.this);
99                                          aListenableApplet.fireAppletEvent(new AppletEvent(aListenableApplet));
100                                     }
101 
102                                     listOfListened.removeElement(appletName);
103                                 }
104                             }
105                         }
106                     }
107 
108                     if (timeout == timeMax)
109                         setState(AppletLoadingChecked.ERROR);
110                     else
111                         timer.stop();
112 
113                 }
114 
115                 stop();
116             }
117         };
118         threadRegistering.start();
119     }
120 
121     /***
122     * Consumes (i.e. treats) the event received.
123     * @param AppletEvent the event to consume.
124     */
125     public void consumeAppletEvent(AppletEvent appletEvent) {
126         AppletListener appletListener = (AppletListener) getTheApplet();
127         if (appletListener != null)
128             appletListener.consumeAppletEvent(appletEvent);
129     }
130 }
131