1 8 9 package foxtrot.pumps; 10 11 import java.awt.AWTEvent ; 12 import java.awt.ActiveEvent ; 13 import java.awt.Component ; 14 import java.awt.EventQueue ; 15 import java.awt.MenuComponent ; 16 import java.awt.Toolkit ; 17 import java.lang.reflect.Method ; 18 import java.security.AccessController ; 19 import java.security.PrivilegedAction ; 20 21 import foxtrot.EventPump; 22 import foxtrot.Task; 23 24 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 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 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 x) 72 { 73 handleThrowable(x); 74 } 75 } 76 catch (InterruptedException x) 77 { 78 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 92 private void dispatchEvent(EventQueue queue, AWTEvent event) 93 { 94 Object source = event.getSource(); 95 96 if (event instanceof ActiveEvent ) 97 ((ActiveEvent )event).dispatch(); 98 else if (source instanceof Component ) 99 ((Component )source).dispatchEvent(event); 100 else if (source instanceof MenuComponent ) 101 ((MenuComponent )source).dispatchEvent(event); 102 else 103 System.err.println("[QueueEventPump] Unable to dispatch event " + event); 104 } 105 106 111 private void handleThrowable(Throwable x) 112 { 113 String handlerName = (String )AccessController.doPrivileged(new PrivilegedAction () 114 { 115 public Object run() 116 { 117 return System.getProperty("sun.awt.exception.handler"); 118 } 119 }); 120 121 if (handlerName != null) 122 { 123 try 124 { 125 Object handler = Thread.currentThread().getContextClassLoader().loadClass(handlerName).newInstance(); 126 Method handle = handler.getClass().getMethod("handle", new Class []{Throwable .class}); 127 handle.invoke(handler, new Object []{x}); 128 return; 129 } 130 catch (Throwable ignored) 131 { 132 System.err.println("[QueueEventPump] Exception occurred while invoking AWT exception handler: " + ignored); 133 } 135 } 136 137 System.err.println("[QueueEventPump] Exception occurred during event dispatching:"); 138 x.printStackTrace(); 139 } 140 141 private EventQueue getEventQueue() 142 { 143 return (EventQueue )AccessController.doPrivileged(new PrivilegedAction () 144 { 145 public Object run() 146 { 147 return Toolkit.getDefaultToolkit().getSystemEventQueue(); 148 } 149 }); 150 } 151 } 152 | Popular Tags |