1 import com.sun.star.accessibility.*; 2 import com.sun.star.lang.EventObject; 3 4 import java.util.LinkedList; 5 6 /** The event queue singleton dispatches events received from OpenOffice.org 7 applications in a thread separate from the AWB main thread. 8 9 The queue of event objects, LinkedList<Runnable> The queue object will 10 also serve as lock for the consumer/producer type syncronization. 11 */ 12 class EventQueue 13 implements Runnable 14 { 15 public boolean mbVerbose = false; 16 public boolean mbHandleDisposingEventsSynchronous = true; 17 18 public synchronized static EventQueue Instance () 19 { 20 if (maInstance == null) 21 maInstance = new EventQueue (); 22 return maInstance; 23 } 24 25 public void addEvent (Runnable aEvent) 26 { 27 synchronized (maMonitor) 28 { 29 if (mbVerbose) 30 System.out.println ("queing regular event " + aEvent); 31 maRegularQueue.addLast (aEvent); 32 maMonitor.notify (); 33 } 34 } 35 36 37 public void addDisposingEvent (Runnable aEvent) 38 { 39 if (mbHandleDisposingEventsSynchronous) 40 aEvent.run (); 41 else 42 synchronized (maMonitor) 43 { 44 if (mbVerbose) 45 System.out.println ("queing disposing event " + aEvent); 46 maDisposingQueue.addLast (aEvent); 47 maMonitor.notify (); 48 } 49 } 50 51 52 private EventQueue () 53 { 54 maMonitor = new Boolean (true); 55 maRegularQueue = new LinkedList(); 56 maDisposingQueue = new LinkedList(); 57 new Thread(this, "AWB.EventQueue").start(); 58 } 59 60 61 /// This thread's main method: deliver all events 62 public void run() 63 { 64 // in an infinite loop, check for events to deliver, then 65 // wait on lock (which will be notified when new events arrive) 66 while( true ) 67 { 68 Runnable aEvent = null; 69 do 70 { 71 synchronized (maMonitor) 72 { 73 if (maDisposingQueue.size() > 0) 74 { 75 aEvent = (Runnable)maDisposingQueue.removeFirst(); 76 if (mbVerbose) 77 System.out.println ("delivering disposing event " + aEvent); 78 } 79 else if (maRegularQueue.size() > 0) 80 { 81 aEvent = (Runnable)maRegularQueue.removeFirst(); 82 if (mbVerbose) 83 System.out.println ("delivering regular event " + aEvent); 84 } 85 else 86 aEvent = null; 87 } 88 if (aEvent != null) 89 { 90 try 91 { 92 aEvent.run(); 93 } 94 catch( Throwable e ) 95 { 96 System.out.println( 97 "caught exception during event delivery: " + e ); 98 e.printStackTrace(); 99 } 100 } 101 } 102 while( aEvent != null ); 103 104 try 105 { 106 synchronized (maMonitor) 107 { 108 maMonitor.wait(); 109 } 110 } 111 catch (Exception e) 112 { 113 // can't wait? odd! 114 System.err.println("Can't wait!"); 115 e.printStackTrace(); 116 } 117 } 118 } 119 120 private static EventQueue maInstance = null; 121 private Object maMonitor; 122 private LinkedList maRegularQueue; 123 private LinkedList maDisposingQueue; 124 } 125 126 127