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>ListenableApplet</CODE> and
12 * <CODE>AppletListener</CODE> interfaces.<BR>
13 * This applet takes as argument a parameter named <CODE>name</CODE> which
14 * represents the name the instance to identify it for futher treatments and
15 * for registration.<BR>
16 * Maintains a list of listeners (<CODE>AppletListener</CODE>) of the instance
17 * and fires to them an event (<CODE>AppletEvent</CODE>) for each change.
18 * It also takes as argument a parameter named <CODE>listened</CODE> which
19 * represents a list of <CODE>ListenableApplet</CODE> the instance will be listening to.<BR>
20 * Each element of the list is the name of the corresponding <CODE>ListenableApplet</CODE>.
21 * <P>
22 * This class is an final class, so it can't be overloaded.
23 *
24 * @author © 1999 DIRAT Laurent
25 * @version 1.0 30/04/99
26 */
27 public final class AListenableAppletListener extends AppletLoadingChecked implements ListenableApplet, AppletListener {
28 /***
29 * The name of the applet.<BR>
30 * We need to identify the instance for further treatments
31 */
32 private String name;
33
34 /***
35 * The list of listeners of the instance.
36 */
37 private Vector listeners = new Vector();
38
39 int timeMax = 60;
40 int timeout = 0;
41 Thread timer;
42
43 /***
44 * Initialyzes the instance by registering it to each <CODE>ListenableApplet</CODE>
45 * in the list passed as argument.
46 */
47 public void init() {
48 super.init();
49
50 name = getParameter("name");
51
52 timer = new Thread() {
53 public void run() {
54 while (timeout < timeMax) {
55 try {
56 Thread.sleep(1000);
57 }
58 catch (InterruptedException ie) {
59 }
60 timeout++;
61 }
62 }
63 };
64 timer.start();
65
66 Thread threadRegistering = new Thread() {
67 public void run() {
68 String listened = getParameter("listened");
69
70 if ((listened != null) && !listened.equals("")) {
71 Vector listOfListened = new Vector();
72 StringTokenizer tokenizer = new StringTokenizer(listened, "{, }");
73 while (tokenizer.hasMoreElements())
74 listOfListened.addElement(tokenizer.nextToken());
75
76 Enumeration listeApplets;
77 Applet anApplet;
78 ListenableApplet aListenableApplet;
79 while ((listOfListened.size() != 0) && (timeout < timeMax)) {
80 listeApplets = getAppletContext().getApplets();
81
82 for (Enumeration e = listeApplets; e.hasMoreElements(); ) {
83 anApplet = (Applet) e.nextElement();
84
85 if (anApplet instanceof ListenableApplet) {
86 aListenableApplet = (ListenableApplet) anApplet;
87 String appletName = aListenableApplet.getName();
88
89 if (listOfListened.contains(appletName)) {
90 int state = AppletLoadingChecked.LOADING;
91 if (aListenableApplet instanceof AppletLoadingChecked) {
92 state = ((AppletLoadingChecked) aListenableApplet).getState();
93 while (state == AppletLoadingChecked.LOADING) {
94 try {
95 Thread.sleep(500);
96 }
97 catch (InterruptedException ie) {
98 }
99
100 state = ((AppletLoadingChecked) aListenableApplet).getState();
101 }
102 }
103
104 if (state == AppletLoadingChecked.LOADED) {
105 int instanceState = getState();
106 while (instanceState == AppletLoadingChecked.LOADING) {
107 try {
108 Thread.sleep(500);
109 }
110 catch (InterruptedException ie) {
111 }
112
113 instanceState = getState();
114 }
115 aListenableApplet.addAppletListener(AListenableAppletListener.this);
116 aListenableApplet.fireAppletEvent(new AppletEvent(aListenableApplet));
117 }
118
119 listOfListened.removeElement(appletName);
120 }
121 }
122 }
123 }
124
125 if (timeout == timeMax)
126 setState(AppletLoadingChecked.ERROR);
127 else
128 timer.stop();
129
130 }
131
132 stop();
133 }
134 };
135 threadRegistering.start();
136 }
137
138 /***
139 * Sets the name of the applet.
140 * @param name the name of the applet.
141 */
142 public void setName(String name) {
143 this.name = name;
144 }
145
146 /***
147 * Returns the name of the applet.
148 */
149 public String getName() {
150 return name;
151 }
152
153 /***
154 * Registers a listener of the instance.
155 */
156 public void addAppletListener(AppletListener appletListener) {
157 if (!listeners.contains(appletListener))
158 listeners.addElement(appletListener);
159 }
160
161 /***
162 * Unregisters the specified listener of the instance.
163 * @param appletListener the listener to remove.
164 */
165 public void removeAppletListener(AppletListener appletListener) {
166 listeners.removeElement(appletListener);
167 }
168
169 /***
170 * Fires an event to all the registered listeners of the instance.
171 * @param appletEvent the event to fire.
172 */
173 public void fireAppletEvent(AppletEvent appletEvent) {
174 for (int i = 0; i < listeners.size(); i++)
175 ((AppletListener) listeners.elementAt(i)).consumeAppletEvent(appletEvent);
176 }
177
178 /***
179 * Consumes (i.e. treats) the event received.
180 * @param AppletEvent the event to consume.
181 */
182 public void consumeAppletEvent(AppletEvent appletEvent) {
183 AppletListener appletListener = (AppletListener) getTheApplet();
184 if (appletListener != null)
185 appletListener.consumeAppletEvent(appletEvent);
186 }
187 }
188