1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 import com.sun.star.accessibility.*;
23 import com.sun.star.lang.EventObject;
24 
25 import java.util.LinkedList;
26 
27 /** The event queue singleton dispatches events received from OpenOffice.org
28     applications in a thread separate from the AWB main thread.
29 
30     The queue of event objects, LinkedList<Runnable> The queue object will
31     also serve as lock for the consumer/producer type syncronization.
32 */
33 class EventQueue
34     implements Runnable
35 {
36     public boolean mbVerbose = false;
37     public boolean mbHandleDisposingEventsSynchronous = true;
38 
Instance()39     public synchronized static EventQueue Instance ()
40     {
41         if (maInstance == null)
42             maInstance = new EventQueue ();
43         return maInstance;
44     }
45 
addEvent(Runnable aEvent)46     public void addEvent (Runnable aEvent)
47     {
48         synchronized (maMonitor)
49         {
50             if (mbVerbose)
51                 System.out.println ("queing regular event " + aEvent);
52             maRegularQueue.addLast (aEvent);
53             maMonitor.notify ();
54         }
55     }
56 
57 
addDisposingEvent(Runnable aEvent)58     public void addDisposingEvent (Runnable aEvent)
59     {
60         if (mbHandleDisposingEventsSynchronous)
61             aEvent.run ();
62         else
63             synchronized (maMonitor)
64             {
65                 if (mbVerbose)
66                     System.out.println ("queing disposing event " + aEvent);
67                 maDisposingQueue.addLast (aEvent);
68                 maMonitor.notify ();
69             }
70     }
71 
72 
EventQueue()73     private EventQueue ()
74     {
75         maMonitor = new Boolean (true);
76         maRegularQueue = new LinkedList();
77         maDisposingQueue = new LinkedList();
78         new Thread(this, "AWB.EventQueue").start();
79     }
80 
81 
82     /// This thread's main method: deliver all events
run()83     public void run()
84     {
85         // in an infinite loop, check for events to deliver, then
86         // wait on lock (which will be notified when new events arrive)
87         while( true )
88         {
89             Runnable aEvent = null;
90             do
91             {
92                 synchronized (maMonitor)
93                 {
94                     if (maDisposingQueue.size() > 0)
95                     {
96                         aEvent = (Runnable)maDisposingQueue.removeFirst();
97                         if (mbVerbose)
98                             System.out.println ("delivering disposing event " + aEvent);
99                     }
100                     else if (maRegularQueue.size() > 0)
101                     {
102                         aEvent = (Runnable)maRegularQueue.removeFirst();
103                         if (mbVerbose)
104                             System.out.println ("delivering regular event " + aEvent);
105                     }
106                     else
107                         aEvent = null;
108                 }
109                 if (aEvent != null)
110                 {
111                     try
112                     {
113                         aEvent.run();
114                     }
115                     catch( Throwable e )
116                     {
117                         System.out.println(
118                             "caught exception during event delivery: " + e );
119                         e.printStackTrace();
120                     }
121                 }
122             }
123             while( aEvent != null );
124 
125             try
126             {
127                 synchronized (maMonitor)
128                 {
129                     maMonitor.wait();
130                 }
131             }
132             catch (Exception e)
133             {
134                 // can't wait? odd!
135                 System.err.println("Can't wait!");
136                 e.printStackTrace();
137             }
138         }
139     }
140 
141     private static EventQueue maInstance = null;
142     private Object maMonitor;
143     private LinkedList maRegularQueue;
144     private LinkedList maDisposingQueue;
145 }
146 
147 
148