KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > util > EventDispatcher


1 /*
2
3    Copyright 2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.util;
19
20 import java.awt.EventQueue JavaDoc;
21 import java.util.List JavaDoc;
22 import java.lang.reflect.InvocationTargetException JavaDoc;
23
24 /**
25  * Generic class to dispatch events in a highly relyable way
26  *
27  * @author <a HREF="mailto:deweese@apache.org">l449433</a>
28  * @version $Id: EventDispatcher.java,v 1.5 2005/03/27 08:58:36 cam Exp $
29  */

30 public class EventDispatcher {
31
32     public interface Dispatcher {
33         public void dispatch(Object JavaDoc listener,
34                              Object JavaDoc event);
35     }
36
37
38     public static void fireEvent(final Dispatcher dispatcher,
39                                  final List JavaDoc listeners,
40                                  final Object JavaDoc evt,
41                                  final boolean useEventQueue) {
42         if (useEventQueue && !EventQueue.isDispatchThread()) {
43             Runnable JavaDoc r = new Runnable JavaDoc() {
44                     public void run() {
45                         fireEvent(dispatcher, listeners, evt, useEventQueue);
46                     }
47                 };
48             try {
49                 EventQueue.invokeAndWait(r);
50             } catch (InvocationTargetException JavaDoc e) {
51                 e.printStackTrace();
52             } catch (InterruptedException JavaDoc e) {
53                 // Assume they will get delivered????
54
// be nice to wait on List but how???
55
} catch (ThreadDeath JavaDoc td) {
56                 throw td;
57             } catch (Throwable JavaDoc t) {
58                 t.printStackTrace();
59             }
60             return;
61         }
62
63         Object JavaDoc [] ll = null;
64         Throwable JavaDoc err = null;
65         int retryCount = 10;
66         while (--retryCount != 0) {
67             // If the thread has been interrupted this can 'mess up'
68
// the class loader and cause this otherwise safe code to
69
// throw errors.
70
try {
71                 synchronized (listeners) {
72                     if (listeners.size() == 0)
73                         return;
74                     ll = listeners.toArray();
75                     break;
76                 }
77             } catch(Throwable JavaDoc t) {
78                 err = t;
79             }
80         }
81         if (ll == null) {
82             if (err != null)
83                 err.printStackTrace();
84             return;
85         }
86         dispatchEvent(dispatcher, ll, evt);
87     }
88
89     protected static void dispatchEvent(final Dispatcher dispatcher,
90                                         final Object JavaDoc [] ll,
91                                         final Object JavaDoc evt) {
92         ThreadDeath JavaDoc td = null;
93         try {
94             for (int i = 0; i < ll.length; i++) {
95                 try {
96                     Object JavaDoc l;
97                     synchronized (ll) {
98                         l = ll[i];
99                         if (l == null) continue;
100                         ll[i] = null;
101                     }
102                     dispatcher.dispatch(l, evt);
103                 } catch (ThreadDeath JavaDoc t) {
104                     // Keep delivering messages but remember to throw later.
105
td = t;
106                 } catch (Throwable JavaDoc t) {
107                     t.printStackTrace();
108                 }
109             }
110         } catch (ThreadDeath JavaDoc t) {
111             // Remember to throw later.
112
td = t;
113         } catch (Throwable JavaDoc t) {
114             if (ll[ll.length-1] != null)
115                 dispatchEvent(dispatcher, ll, evt);
116             t.printStackTrace();
117         }
118         if (td != null) throw td;
119     }
120 }
121
Popular Tags