1 /*****************************************************/
\r
2 /* This java file is a part of the */
\r
4 /* - Plouf's Java IRC Client - */
\r
6 /* Copyright (C) 2002 - 2004 Philippe Detournay */
\r
8 /* All contacts : theplouf@yahoo.com */
\r
10 /* PJIRC is free software; you can redistribute */
\r
11 /* it and/or modify it under the terms of the GNU */
\r
12 /* General Public License as published by the */
\r
13 /* Free Software Foundation; version 2 or later of */
\r
16 /* PJIRC is distributed in the hope that it will */
\r
17 /* be useful, but WITHOUT ANY WARRANTY; without */
\r
18 /* even the implied warranty of MERCHANTABILITY or */
\r
19 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
\r
20 /* General Public License for more details. */
\r
22 /* You should have received a copy of the GNU */
\r
23 /* General Public License along with PJIRC; if */
\r
24 /* not, write to the Free Software Foundation, */
\r
25 /* Inc., 59 Temple Place, Suite 330, Boston, */
\r
26 /* MA 02111-1307 USA */
\r
28 /*****************************************************/
\r
32 import java.util.Enumeration;
\r
33 import java.util.Vector;
\r
36 * Handles a group of listeners.
\r
38 public class ListenerGroup {
\r
39 private Vector _listeners;
\r
42 * Create a new ListenerGroup.
\r
44 public ListenerGroup() {
\r
45 _listeners = new Vector();
\r
49 * Add a listener to the group.
\r
54 public synchronized void addListener(Object listener) {
\r
55 _listeners.insertElementAt(listener, _listeners.size());
\r
59 * Remove a listener from the group.
\r
62 * listener to remove.
\r
64 public synchronized void removeListener(Object listener) {
\r
65 for (int i = 0; i < _listeners.size(); i++) {
\r
66 if (_listeners.elementAt(i) == listener) {
\r
67 _listeners.removeElementAt(i);
\r
74 * Send an event to all listeners in the group, in the event thread.
\r
77 * method to call on each listener.
\r
79 * parameters to pass to the called method.
\r
81 public synchronized void sendEventAsync(String method, Object[] params) {
\r
82 Enumeration e = _listeners.elements();
\r
83 while (e.hasMoreElements())
\r
84 EventDispatcher.dispatchEventAsync(e.nextElement(), method, params);
\r
88 * Send an event to all listeners in the group, in the event thread.
\r
91 * method to call on each listener.
\r
93 public synchronized void sendEventAsync(String method) {
\r
94 sendEventAsync(method, new Object[0]);
\r
98 * Send an event to all listeners in the group, in the event thread.
\r
101 * method to call on each listener.
\r
103 * first parameter to pass to the called method.
\r
105 public synchronized void sendEventAsync(String method, Object param1) {
\r
106 Object[] p = { param1 };
\r
107 sendEventAsync(method, p);
\r
111 * Send an event to all listeners in the group, in the event thread.
\r
114 * method to call on each listener.
\r
116 * first parameter to pass to the called method.
\r
118 * second parameter to pass to the called method.
\r
120 public synchronized void sendEventAsync(String method, Object param1, Object param2) {
\r
121 Object[] p = { param1, param2 };
\r
122 sendEventAsync(method, p);
\r
126 * Send an event to all listeners in the group, in the event thread.
\r
129 * method to call on each listener.
\r
131 * first parameter to pass to the called method.
\r
133 * second parameter to pass to the called method.
\r
135 * third parameter to pass to the called method.
\r
137 public synchronized void sendEventAsync(String method, Object param1, Object param2, Object param3) {
\r
138 Object[] p = { param1, param2, param3 };
\r
139 sendEventAsync(method, p);
\r
143 * Send an event to all listeners in the group. The event will be processed
\r
144 * synchrounsly in the current thread.
\r
147 * method to call on each listener.
\r
149 * parameters to pass to the called method.
\r
150 * @return array of returned resuluts for each method call.
\r
151 * @deprecated use sendEventEx instead.
\r
154 public synchronized Object[] sendEvent(String method, Object[] params) {
\r
156 return sendEventEx(method, params);
\r
157 } catch (Throwable ex) {
\r
158 ex.printStackTrace();
\r
164 * Send an event to all listeners in the group. The event will be processed
\r
165 * synchrounsly in the current thread.
\r
168 * method to call on each listener.
\r
170 * parameters to pass to the called method.
\r
171 * @return array of returned resuluts for each method call.
\r
172 * @throws Throwable
\r
174 public synchronized Object[] sendEventEx(String method, Object[] params) throws Throwable {
\r
175 Object[] res = new Object[_listeners.size()];
\r
177 Enumeration e = _listeners.elements();
\r
178 while (e.hasMoreElements()) {
\r
180 res[i++] = EventDispatcher.dispatchEventSyncEx(e.nextElement(), method, params);
\r
181 } catch (Throwable ex) {
\r
182 ex.printStackTrace();
\r
189 * Send an event to all listeners in the group in the current thread.
\r
192 * method to call on each listener.
\r
193 * @return array of returned resuluts for each method call.
\r
195 public synchronized Object[] sendEvent(String method) {
\r
196 return sendEvent(method, new Object[0]);
\r
200 * Send an event to all listeners in the group in the current thread.
\r
203 * method to call on each listener.
\r
205 * first parameter to pass to the called method.
\r
206 * @return array of returned resuluts for each method call.
\r
208 public synchronized Object[] sendEvent(String method, Object param1) {
\r
209 Object[] p = { param1 };
\r
210 return sendEvent(method, p);
\r
214 * Send an event to all listeners in the group in the current thread.
\r
217 * method to call on each listener.
\r
219 * first parameter to pass to the called method.
\r
221 * second parameter to pass to the called method.
\r
222 * @return array of returned resuluts for each method call.
\r
224 public synchronized Object[] sendEvent(String method, Object param1, Object param2) {
\r
225 Object[] p = { param1, param2 };
\r
226 return sendEvent(method, p);
\r
230 * Send an event to all listeners in the group in the current thread.
\r
233 * method to call on each listener.
\r
235 * first parameter to pass to the called method.
\r
237 * second parameter to pass to the called method.
\r
239 * third parameter to pass to the called method.
\r
240 * @return array of returned resuluts for each method call.
\r
242 public synchronized Object[] sendEvent(String method, Object param1, Object param2, Object param3) {
\r
243 Object[] p = { param1, param2, param3 };
\r
244 return sendEvent(method, p);
\r
248 * Send an event to all listeners in the group in the current thread.
\r
251 * method to call on each listener.
\r
252 * @return array of returned resuluts for each method call.
\r
253 * @throws Throwable
\r
255 public synchronized Object[] sendEventEx(String method) throws Throwable {
\r
256 return sendEventEx(method, new Object[0]);
\r
260 * Send an event to all listeners in the group in the current thread.
\r
263 * method to call on each listener.
\r
265 * first parameter to pass to the called method.
\r
266 * @return array of returned resuluts for each method call.
\r
267 * @throws Throwable
\r
269 public synchronized Object[] sendEventEx(String method, Object param1) throws Throwable {
\r
270 Object[] p = { param1 };
\r
271 return sendEventEx(method, p);
\r
275 * Send an event to all listeners in the group in the current thread.
\r
278 * method to call on each listener.
\r
280 * first parameter to pass to the called method.
\r
282 * second parameter to pass to the called method.
\r
283 * @return array of returned resuluts for each method call.
\r
284 * @throws Throwable
\r
286 public synchronized Object[] sendEventEx(String method, Object param1, Object param2) throws Throwable {
\r
287 Object[] p = { param1, param2 };
\r
288 return sendEventEx(method, p);
\r
292 * Send an event to all listeners in the group in the current thread.
\r
295 * method to call on each listener.
\r
297 * first parameter to pass to the called method.
\r
299 * second parameter to pass to the called method.
\r
301 * third parameter to pass to the called method.
\r
302 * @return array of returned resuluts for each method call.
\r
303 * @throws Throwable
\r
305 public synchronized Object[] sendEventEx(String method, Object param1, Object param2, Object param3) throws Throwable {
\r
306 Object[] p = { param1, param2, param3 };
\r
307 return sendEventEx(method, p);
\r