01: package net.xoetrope.swing;
02:
03: import java.awt.AWTEvent;
04: import java.awt.ActiveEvent;
05: import java.awt.Component;
06: import java.awt.EventQueue;
07: import java.awt.MenuComponent;
08:
09: public class XDialogEventDispatchThread extends Thread {
10: private EventQueue theQueue;
11: private boolean doDispatch = true;
12:
13: XDialogEventDispatchThread(String name, EventQueue queue) {
14: super (name);
15: theQueue = queue;
16: }
17:
18: public void stopDispatching(boolean bFireEmptyEvent) {
19: doDispatch = false;
20: // fix 4128923
21: // post an empty event to ensure getNextEvent
22: // is unblocked - rkhan 4/14/98
23: // TODO: Look into using Thread.interrupt() instead
24: if (bFireEmptyEvent)
25: theQueue.postEvent(new EmptyEvent());
26: // wait for the dispatcher to complete
27: if (Thread.currentThread() != this ) {
28: try {
29: join();
30: } catch (InterruptedException e) {
31: }
32: }
33: }
34:
35: class EmptyEvent extends AWTEvent //implements ActiveEvent
36: {
37: public EmptyEvent() {
38: super (XDialogEventDispatchThread.this , 0);
39: }
40:
41: public void dispatch() {
42: }
43: }
44:
45: public void run() {
46: while (doDispatch) {
47: try {
48: AWTEvent event = theQueue.getNextEvent();
49: if (false) {
50: // Not until 1.2...
51: // theQueue.dispatchEvent(event);
52: } else {
53: // old code...
54: Object src = event.getSource();
55: if (event instanceof ActiveEvent) {
56: // This could become the sole method of dispatching in time, and
57: // moved to the event queue's dispatchEvent() method.
58: ((ActiveEvent) event).dispatch();
59: }
60: if (src instanceof Component) {
61: ((Component) src).dispatchEvent(event);
62: } else if (src instanceof MenuComponent) {
63: ((MenuComponent) src).dispatchEvent(event);
64: }
65: }
66: } catch (ThreadDeath death) {
67: return;
68:
69: } catch (Throwable e) {
70: System.err
71: .println("Exception occurred during event dispatching:");
72: e.printStackTrace();
73: }
74: }
75: }
76: }
|