merged tag ooo/DEV300_m102
[LibreOffice.git] / toolkit / test / accessibility / EventQueue.java
blob9c90af9c63b737c890566ceb9942fb922f45f4bd
1 import com.sun.star.accessibility.*;
2 import com.sun.star.lang.EventObject;
4 import java.util.LinkedList;
6 /** The event queue singleton dispatches events received from OpenOffice.org
7 applications in a thread separate from the AWB main thread.
9 The queue of event objects, LinkedList<Runnable> The queue object will
10 also serve as lock for the consumer/producer type syncronization.
12 class EventQueue
13 implements Runnable
15 public boolean mbVerbose = false;
16 public boolean mbHandleDisposingEventsSynchronous = true;
18 public synchronized static EventQueue Instance ()
20 if (maInstance == null)
21 maInstance = new EventQueue ();
22 return maInstance;
25 public void addEvent (Runnable aEvent)
27 synchronized (maMonitor)
29 if (mbVerbose)
30 System.out.println ("queing regular event " + aEvent);
31 maRegularQueue.addLast (aEvent);
32 maMonitor.notify ();
37 public void addDisposingEvent (Runnable aEvent)
39 if (mbHandleDisposingEventsSynchronous)
40 aEvent.run ();
41 else
42 synchronized (maMonitor)
44 if (mbVerbose)
45 System.out.println ("queing disposing event " + aEvent);
46 maDisposingQueue.addLast (aEvent);
47 maMonitor.notify ();
52 private EventQueue ()
54 maMonitor = new Boolean (true);
55 maRegularQueue = new LinkedList();
56 maDisposingQueue = new LinkedList();
57 new Thread(this, "AWB.EventQueue").start();
61 /// This thread's main method: deliver all events
62 public void run()
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 )
68 Runnable aEvent = null;
71 synchronized (maMonitor)
73 if (maDisposingQueue.size() > 0)
75 aEvent = (Runnable)maDisposingQueue.removeFirst();
76 if (mbVerbose)
77 System.out.println ("delivering disposing event " + aEvent);
79 else if (maRegularQueue.size() > 0)
81 aEvent = (Runnable)maRegularQueue.removeFirst();
82 if (mbVerbose)
83 System.out.println ("delivering regular event " + aEvent);
85 else
86 aEvent = null;
88 if (aEvent != null)
90 try
92 aEvent.run();
94 catch( Throwable e )
96 System.out.println(
97 "caught exception during event delivery: " + e );
98 e.printStackTrace();
102 while( aEvent != null );
106 synchronized (maMonitor)
108 maMonitor.wait();
111 catch (Exception e)
113 // can't wait? odd!
114 System.err.println("Can't wait!");
115 e.printStackTrace();
120 private static EventQueue maInstance = null;
121 private Object maMonitor;
122 private LinkedList maRegularQueue;
123 private LinkedList maDisposingQueue;