KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > foxtrot > pumps > QueueEventPump


1 /**
2  * Copyright (c) 2002-2005, Simone Bordet
3  * All rights reserved.
4  *
5  * This software is distributable under the BSD license.
6  * See the terms of the BSD license in the documentation provided with this software.
7  */

8
9 package foxtrot.pumps;
10
11 import java.awt.AWTEvent JavaDoc;
12 import java.awt.ActiveEvent JavaDoc;
13 import java.awt.Component JavaDoc;
14 import java.awt.EventQueue JavaDoc;
15 import java.awt.MenuComponent JavaDoc;
16 import java.awt.Toolkit JavaDoc;
17 import java.lang.reflect.Method JavaDoc;
18 import java.security.AccessController JavaDoc;
19 import java.security.PrivilegedAction JavaDoc;
20
21 import foxtrot.EventPump;
22 import foxtrot.Task;
23
24 /**
25  * Implementation of an EventPump that should work with JDK 1.2 and 1.3
26  * and only uses the public API of the <code>java.awt.*</code> package.
27  * @version $Revision: 1.1 $
28  */

29 public class QueueEventPump implements EventPump, EventFilterable
30 {
31    private static final boolean debug = false;
32
33    private EventFilter filter;
34
35    public void setEventFilter(EventFilter filter)
36    {
37       this.filter = filter;
38    }
39
40    public EventFilter getEventFilter()
41    {
42       return filter;
43    }
44
45    public void pumpEvents(Task task)
46    {
47       if (task == null) return;
48
49       EventQueue JavaDoc queue = getEventQueue();
50
51       if (debug) System.out.println("[QueueEventPump] Start pumping events - Pump is " + this + " - Task is " + task);
52
53       while (!task.isCompleted())
54       {
55          try
56          {
57             AWTEvent JavaDoc event = queue.getNextEvent();
58
59             if (debug) System.out.println("[QueueEventPump] Next Event: " + event);
60
61             if (filter != null && !filter.accept(event))
62             {
63                if (debug) System.out.println("[QueueEventPump] Filtered out AWT Event: " + event + " by filter " + filter);
64                continue;
65             }
66
67             try
68             {
69                dispatchEvent(queue, event);
70             }
71             catch (Throwable JavaDoc x)
72             {
73                handleThrowable(x);
74             }
75          }
76          catch (InterruptedException JavaDoc x)
77          {
78             // AWT Thread has been interrupted, interrupt again to set again the interrupted flag
79
Thread.currentThread().interrupt();
80             break;
81          }
82       }
83
84       if (debug) System.out.println("[QueueEventPump] Stop pumping events - Pump is " + this + " - Task is " + task);
85    }
86
87    /**
88     * Copy/Paste of the Sun's JDK 1.3 implementation in java.awt.EventQueue.dispatchEvent(AWTEvent)
89     * @param queue The system EventQueue
90     * @param event The event to dispatch
91     */

92    private void dispatchEvent(EventQueue JavaDoc queue, AWTEvent JavaDoc event)
93    {
94       Object JavaDoc source = event.getSource();
95
96       if (event instanceof ActiveEvent JavaDoc)
97          ((ActiveEvent JavaDoc)event).dispatch();
98       else if (source instanceof Component JavaDoc)
99          ((Component JavaDoc)source).dispatchEvent(event);
100       else if (source instanceof MenuComponent JavaDoc)
101          ((MenuComponent JavaDoc)source).dispatchEvent(event);
102       else
103          System.err.println("[QueueEventPump] Unable to dispatch event " + event);
104    }
105
106    /**
107     * Handle a RuntimeException or Error happened during event dispatching.
108     * If the system property <code>sun.awt.exception.handler</code> is defined,
109     * that handler will be used, otherwise it simply logs on <code>System.err</code>.
110     */

111    private void handleThrowable(Throwable JavaDoc x)
112    {
113       String JavaDoc handlerName = (String JavaDoc)AccessController.doPrivileged(new PrivilegedAction JavaDoc()
114       {
115          public Object JavaDoc run()
116          {
117             return System.getProperty("sun.awt.exception.handler");
118          }
119       });
120
121       if (handlerName != null)
122       {
123          try
124          {
125             Object JavaDoc handler = Thread.currentThread().getContextClassLoader().loadClass(handlerName).newInstance();
126             Method JavaDoc handle = handler.getClass().getMethod("handle", new Class JavaDoc[]{Throwable JavaDoc.class});
127             handle.invoke(handler, new Object JavaDoc[]{x});
128             return;
129          }
130          catch (Throwable JavaDoc ignored)
131          {
132             System.err.println("[QueueEventPump] Exception occurred while invoking AWT exception handler: " + ignored);
133             // Fall through
134
}
135       }
136
137       System.err.println("[QueueEventPump] Exception occurred during event dispatching:");
138       x.printStackTrace();
139    }
140
141    private EventQueue JavaDoc getEventQueue()
142    {
143       return (EventQueue JavaDoc)AccessController.doPrivileged(new PrivilegedAction JavaDoc()
144       {
145          public Object JavaDoc run()
146          {
147             return Toolkit.getDefaultToolkit().getSystemEventQueue();
148          }
149       });
150    }
151 }
152
Popular Tags