01: package net.xoetrope.awt;
02:
03: import java.awt.AWTEvent; // Do not optimize these imports..........it will give problems with early JDKs
04: import java.awt.*;
05: import java.awt.peer.*; //------------------------------------------------------------------------------
06: import java.awt.Component;
07: import java.awt.EventQueue;
08: import java.awt.MenuComponent;
09:
10: public class XDialogEventDispatchThread extends Thread {
11: private EventQueue theQueue;
12: private boolean doDispatch = true;
13:
14: XDialogEventDispatchThread(String name, EventQueue queue) {
15: super (name);
16: theQueue = queue;
17: }
18:
19: public void stopDispatching(boolean bFireEmptyEvent) {
20: doDispatch = false;
21: // fix 4128923
22: // post an empty event to ensure getNextEvent
23: // is unblocked - rkhan 4/14/98
24: // TODO: Look into using Thread.interrupt() instead
25: if (bFireEmptyEvent)
26: theQueue.postEvent(new EmptyEvent());
27: // wait for the dispatcher to complete
28: if (Thread.currentThread() != this ) {
29: try {
30: join();
31: } catch (InterruptedException e) {
32: }
33: }
34: }
35:
36: class EmptyEvent extends AWTEvent implements ActiveEvent {
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: } else if (src instanceof Component) {
60: ((Component) src).dispatchEvent(event);
61: } else if (src instanceof MenuComponent) {
62: ((MenuComponent) src).dispatchEvent(event);
63: }
64: }
65: } catch (ThreadDeath death) {
66: return;
67:
68: } catch (Throwable e) {
69: System.err
70: .println("Exception occurred during event dispatching:");
71: e.printStackTrace();
72: }
73: }
74: }
75: }
|