1 /*************************************************************************
3 * $RCSfile: EventHandler.java,v $
7 * last change: $Author: hr $ $Date: 2003-06-30 15:05:26 $
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 java
.util
.Vector
;
43 import com
.sun
.star
.uno
.AnyConverter
;
44 import com
.sun
.star
.uno
.UnoRuntime
;
45 import com
.sun
.star
.uno
.Type
;
46 import com
.sun
.star
.accessibility
.*;
48 /** Handle all the events send from accessibility objects. The events
49 denoting new or removed top windows are handled as well.
51 It does not implement any listener interface as does the
52 EventListenerProxy class because it is interested only in a sub set of
55 public class EventHandler
57 public EventHandler ()
60 maListenerProxy
= new EventListenerProxy (this);
61 maConnectionTask
= new ConnectionTask (maListenerProxy
);
62 maObjectDisplays
= new Vector ();
65 public synchronized void addObjectDisplay (IAccessibleObjectDisplay aDisplay
)
67 maObjectDisplays
.add (aDisplay
);
71 public void finalize ()
73 // When it is running then cancel the timer that tries to connect to
75 if (maConnectionTask
!= null)
76 maConnectionTask
.cancel();
81 public void disposing (com
.sun
.star
.lang
.EventObject aEvent
)
83 // Ignored: We are not holding references to accessibility objects.
89 /** This method is called back when a new top level window has been opened.
91 public void windowOpened (XAccessible xAccessible
)
93 if (xAccessible
!= null)
95 // Update the counter of currently open top level windows
96 // observed by this object.
97 mnTopWindowCount
+= 1;
99 XAccessibleContext xContext
= xAccessible
.getAccessibleContext();
100 if (xContext
!= null)
102 MessageArea
.println ("new top level window has accessible name "
103 + xContext
.getAccessibleName());
105 // Register at all accessible objects of the new window.
106 new RegistrationThread (
113 MessageArea
.println ("new top level window is not accessible.");
116 MessageArea
.println ("new top level window is not accessible.");
122 public void windowClosed (XAccessible xAccessible
)
124 mnTopWindowCount
-= 1;
125 MessageArea
.println ("window closed, " + mnTopWindowCount
+ " still open");
126 if (mnTopWindowCount
== 0)
128 // This was the last window. Wait for a new connection.
129 MessageArea
.println ("lost connection to office");
130 new ConnectionTask (maListenerProxy
);
132 if (xAccessible
!= null)
133 new RegistrationThread (
135 xAccessible
.getAccessibleContext(),
143 /** Print a message that the given object just received the focus. Call
144 all accessible object diplays and tell them to update.
146 private synchronized void focusGained (XAccessibleContext xContext
)
148 if (xContext
!= null)
150 MessageArea
.println ("focusGained: " + xContext
.getAccessibleName()
152 + NameProvider
.getRoleName (xContext
.getAccessibleRole()));
154 // Tell the object displays to update their views.
155 for (int i
=0; i
<maObjectDisplays
.size(); i
++)
157 IAccessibleObjectDisplay aDisplay
=
158 (IAccessibleObjectDisplay
)maObjectDisplays
.get(i
);
159 if (aDisplay
!= null)
160 aDisplay
.setAccessibleObject (xContext
);
163 // Remember the currently focused object.
164 mxFocusedObject
= xContext
;
167 MessageArea
.println ("focusGained: null");
173 /** Print a message that the given object just lost the focus. Call
174 all accessible object diplays and tell them to update.
176 private synchronized void focusLost (XAccessibleContext xContext
)
178 if (xContext
!= null)
180 MessageArea
.println ("focusLost: "
181 + xContext
.getAccessibleName()
183 + NameProvider
.getRoleName (xContext
.getAccessibleRole()));
185 // Tell the object displays to update their views.
186 for (int i
=0; i
<maObjectDisplays
.size(); i
++)
188 IAccessibleObjectDisplay aDisplay
=
189 (IAccessibleObjectDisplay
)maObjectDisplays
.get(i
);
190 if (aDisplay
!= null)
191 aDisplay
.setAccessibleObject (null);
193 mxFocusedObject
= null;
196 MessageArea
.println ("focusLost: null");
202 /** Handle a change of the caret position. Ignore this on all objects
203 but the one currently focused.
205 private void handleCaretEvent (XAccessibleContext xContext
,
206 long nOldPosition
, long nNewPosition
)
208 if (xContext
== mxFocusedObject
)
209 MessageArea
.println ("caret moved from " + nOldPosition
+ " to " + nNewPosition
);
215 /** Print a message that a state has been changed.
217 The accessible context of the object whose state has changed.
219 When not zero then this value describes a state that has been reset.
221 When not zero then this value describes a state that has been set.
223 private void handleStateChange (XAccessibleContext xContext
, short nOldState
, short nNewState
)
225 // Determine which state has changed and what is its new value.
239 // Print a message about the changed state.
240 MessageArea
.print ("setting state " + NameProvider
.getStateName(nState
)
241 + " to " + aNewValue
);
242 if (xContext
!= null)
244 MessageArea
.println (" at " + xContext
.getAccessibleName() + " with role "
245 + NameProvider
.getRoleName(xContext
.getAccessibleRole()));
248 MessageArea
.println (" at null");
250 // Further handling of some states
253 case AccessibleStateType
.FOCUSED
:
255 focusGained (xContext
);
257 focusLost (xContext
);
264 /** Handle a child event that describes the creation of removal of a
267 private void handleChildEvent (
268 XAccessibleContext aOldChild
,
269 XAccessibleContext aNewChild
)
271 if (aOldChild
!= null)
272 // Remove event listener from the child and all of its descendants.
273 new RegistrationThread (maListenerProxy
, aOldChild
, false, false);
274 else if (aNewChild
!= null)
275 // Add event listener to the new child and all of its descendants.
276 new RegistrationThread (maListenerProxy
, aNewChild
, true, false);
282 /** Handle the change of some visible data of an object.
284 private void handleVisibleDataEvent (XAccessibleContext xContext
)
286 // The given object may affect the visible appearance of the focused
287 // object even when the two are not identical when the given object
288 // is an ancestor of the focused object.
289 // In order to not check this we simply call an update on the
291 if (mxFocusedObject
!= null)
292 for (int i
=0; i
<maObjectDisplays
.size(); i
++)
294 IAccessibleObjectDisplay aDisplay
=
295 (IAccessibleObjectDisplay
)maObjectDisplays
.get(i
);
296 if (aDisplay
!= null)
297 aDisplay
.updateAccessibleObject (mxFocusedObject
);
304 /** Print some information about an event that is not handled by any
305 more specialized handler.
307 private void handleGenericEvent (
313 // Print event to message area.
314 MessageArea
.print ("received event "
315 + NameProvider
.getEventName (nEventId
) + " from ");
316 XAccessibleContext xContext
= objectToContext (aSource
);
317 if (xContext
!= null)
318 MessageArea
.print (xContext
.getAccessibleName());
320 MessageArea
.print ("null");
321 MessageArea
.println (" / "
322 + NameProvider
.getRoleName(xContext
.getAccessibleRole()));
327 /** This is the main method for handling accessibility events. It is
328 assumed that it is not called directly from the Office but from a
329 listener proxy that runs in a separate thread so that calls back to
330 the Office do not result in dead-locks.
332 public void notifyEvent (com
.sun
.star
.accessibility
.AccessibleEventObject aEvent
)
334 try // Guard against disposed objects.
336 switch (aEvent
.EventId
)
338 case AccessibleEventId
.CHILD
:
340 objectToContext (aEvent
.OldValue
),
341 objectToContext (aEvent
.NewValue
));
344 case AccessibleEventId
.STATE_CHANGED
:
346 short nOldState
= -1;
347 short nNewState
= -1;
350 if (AnyConverter
.isShort (aEvent
.NewValue
))
351 nNewState
= AnyConverter
.toShort (aEvent
.NewValue
);
352 if (AnyConverter
.isShort (aEvent
.OldValue
))
353 nOldState
= AnyConverter
.toShort (aEvent
.OldValue
);
355 catch (com
.sun
.star
.lang
.IllegalArgumentException e
)
358 objectToContext (aEvent
.Source
),
364 case AccessibleEventId
.VISIBLE_DATA_CHANGED
:
365 case AccessibleEventId
.BOUNDRECT_CHANGED
:
366 handleVisibleDataEvent (objectToContext (aEvent
.Source
));
369 case AccessibleEventId
.CARET_CHANGED
:
373 objectToContext (aEvent
.Source
),
374 AnyConverter
.toLong(aEvent
.OldValue
),
375 AnyConverter
.toLong(aEvent
.NewValue
));
377 catch (com
.sun
.star
.lang
.IllegalArgumentException e
)
382 handleGenericEvent (aEvent
.EventId
,
383 aEvent
.Source
, aEvent
.OldValue
, aEvent
.NewValue
);
387 catch (com
.sun
.star
.lang
.DisposedException e
)
394 /** Convert the given object into an accessible context. The object is
395 interpreted as UNO Any and may contain either an XAccessible or
396 XAccessibleContext reference.
398 The returned value is null when the given object can not be
399 converted to an XAccessibleContext reference.
401 private XAccessibleContext
objectToContext (Object aObject
)
403 XAccessibleContext xContext
= null;
404 XAccessible xAccessible
= null;
407 xAccessible
= (XAccessible
)AnyConverter
.toObject(
408 new Type(XAccessible
.class), aObject
);
410 catch (com
.sun
.star
.lang
.IllegalArgumentException e
)
412 if (xAccessible
!= null)
413 xContext
= xAccessible
.getAccessibleContext();
417 xContext
= (XAccessibleContext
)AnyConverter
.toObject(
418 new Type(XAccessibleContext
.class), aObject
);
420 catch (com
.sun
.star
.lang
.IllegalArgumentException e
)
428 /** The proxy that runs in a seperate thread and allows to call back to
429 the Office without running into dead-locks.
431 private EventListenerProxy maListenerProxy
;
433 /** The currently focused object. A value of null means that no object
436 private XAccessibleContext mxFocusedObject
;
438 /** Keep track of the currently open top windows to start a registration
439 loop when the last window (and the Office) is closed.
441 private long mnTopWindowCount
;
443 /** A list of objects that can display accessible objects in specific
444 ways such as showing a graphical representation or some textual
447 private Vector maObjectDisplays
;
449 /** The timer task that attempts in regular intervals to connect to a
450 running Office application.
452 private ConnectionTask maConnectionTask
;