1 /*************************************************************************
3 * $RCSfile: EventListenerProxy.java,v $
7 * last change: $Author: hr $ $Date: 2003-06-30 15:05:47 $
9 * The Contents of this file are made available subject to the terms of
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 import com
.sun
.star
.accessibility
.XAccessible
;
42 import com
.sun
.star
.accessibility
.XAccessibleEventListener
;
43 import com
.sun
.star
.accessibility
.AccessibleEventObject
;
44 import com
.sun
.star
.lang
.EventObject
;
45 import com
.sun
.star
.awt
.XTopWindowListener
;
46 import com
.sun
.star
.uno
.UnoRuntime
;
48 import java
.util
.LinkedList
;
50 /** This class acts as a proxy for the simple screen reader. It waits for
52 1. Accessibility events signal modifications concerning accessibility
54 2. Top window events inform the listener about new or removed windows.
56 This class exists because events had to be handled in a seperate thread
57 to avoid dead locks: The thread that receives an event must no call back
58 to the Office directly.
60 Soon this should not be necessary anymore. There is now a flag which
61 switches between synchronous and asynchronous callbacks.
63 All reveived events are eventually forwarded to the actual listener. In
64 this way it decouples the Office from the listener.
66 class EventListenerProxy
67 implements Runnable
, XAccessibleEventListener
, XTopWindowListener
69 public EventListenerProxy (EventHandler aListener
)
71 maListener
= aListener
;
75 maEventQueue
= new LinkedList();
76 new Thread (this, "EventListenerProxy").start();
80 private void addEvent (Runnable aEventObject
)
83 synchronized (maEventQueue
)
85 maEventQueue
.addLast (aEventObject
);
86 // Tell the queue that there is a new event in the queue.
87 maEventQueue
.notify();
91 System
.out
.println ("running " + aEventObject
);
93 System
.out
.println (" done");
100 /** In an infinite loop, check for events to deliver, then wait on lock
101 (which will be notified when new events arrive)
107 // Process all events that are currently in the queue.
111 synchronized (maEventQueue
)
113 if (maEventQueue
.size() > 0)
114 aEvent
= (Runnable
)maEventQueue
.removeFirst();
125 catch (Throwable aException
)
128 "Exception during event delivery: " + aException
);
129 aException
.printStackTrace();
133 while (aEvent
!= null);
135 // Now that the queue is empty go to sleep again.
138 synchronized (maEventQueue
)
143 catch (Exception aException
)
145 // Ignore this exception since there is not much
146 // that we can do about it.
152 public void disposing( final EventObject aEvent
)
154 addEvent (new Runnable()
158 maListener
.disposing (aEvent
);
163 public void notifyEvent (final AccessibleEventObject aEvent
)
170 maListener
.notifyEvent (aEvent
);
175 public void windowOpened (final com
.sun
.star
.lang
.EventObject aEvent
)
182 maListener
.windowOpened (
183 (XAccessible
) UnoRuntime
.queryInterface(
189 public void windowClosing (final com
.sun
.star
.lang
.EventObject aEvent
)
193 public void windowClosed (final com
.sun
.star
.lang
.EventObject aEvent
)
200 maListener
.windowClosed (
201 (XAccessible
) UnoRuntime
.queryInterface(
207 public void windowMinimized (com
.sun
.star
.lang
.EventObject aEvent
)
211 public void windowNormalized (com
.sun
.star
.lang
.EventObject aEvent
)
215 public void windowActivated (com
.sun
.star
.lang
.EventObject aEvent
)
219 public void windowDeactivated (com
.sun
.star
.lang
.EventObject aEvent
)
224 /** The queue of event objects, LinkedList<Runnable>.
225 The queue object will also serve as lock for the consumer/producer type
228 private LinkedList maEventQueue
;
230 /** This is the actual listener that the events will eventually forwarded to.
232 private EventHandler maListener
;
234 /** This flag determines whether event forwarding is done asynchroniously
237 private boolean mbAsynchron
;