/************************************************************************* * * The Contents of this file are made available subject to the terms of * the BSD license. * * Copyright 2000, 2010 Oracle and/or its affiliates. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of Sun Microsystems, Inc. nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * *************************************************************************/ import java.util.Vector; import com.sun.star.uno.AnyConverter; import com.sun.star.uno.UnoRuntime; import com.sun.star.uno.Type; import com.sun.star.accessibility.*; /** Handle all the events send from accessibility objects. The events denoting new or removed top windows are handled as well. It does not implement any listener interface as does the EventListenerProxy class because it is interested only in a sub set of the event types. */ public class EventHandler { public EventHandler () { mnTopWindowCount = 0; maListenerProxy = new EventListenerProxy (this); maConnectionTask = new ConnectionTask (maListenerProxy); maObjectDisplays = new Vector (); } public synchronized void addObjectDisplay (IAccessibleObjectDisplay aDisplay) { maObjectDisplays.add (aDisplay); } public void finalize () { // When it is running then cancel the timer that tries to connect to // the Office. if (maConnectionTask != null) maConnectionTask.cancel(); } public void disposing (com.sun.star.lang.EventObject aEvent) { // Ignored: We are not holding references to accessibility objects. } /** This method is called back when a new top level window has been opened. */ public void windowOpened (XAccessible xAccessible) { if (xAccessible != null) { // Update the counter of currently open top level windows // observed by this object. mnTopWindowCount += 1; XAccessibleContext xContext = xAccessible.getAccessibleContext(); if (xContext != null) { MessageArea.println ("new top level window has accessible name " + xContext.getAccessibleName()); // Register at all accessible objects of the new window. new RegistrationThread ( maListenerProxy, xContext, true, true); } else MessageArea.println ("new top level window is not accessible."); } else MessageArea.println ("new top level window is not accessible."); } public void windowClosed (XAccessible xAccessible) { mnTopWindowCount -= 1; MessageArea.println ("window closed, " + mnTopWindowCount + " still open"); if (mnTopWindowCount == 0) { // This was the last window. Wait for a new connection. MessageArea.println ("lost connection to office"); new ConnectionTask (maListenerProxy); } if (xAccessible != null) new RegistrationThread ( maListenerProxy, xAccessible.getAccessibleContext(), false, true); } /** Print a message that the given object just received the focus. Call all accessible object diplays and tell them to update. */ private synchronized void focusGained (XAccessibleContext xContext) { if (xContext != null) { MessageArea.println ("focusGained: " + xContext.getAccessibleName() + " with role " + NameProvider.getRoleName (xContext.getAccessibleRole())); // Tell the object displays to update their views. for (int i=0; i= 0) { nState = nOldState; aNewValue = false; } else { nState = nNewState; aNewValue = true; } // Print a message about the changed state. MessageArea.print ("setting state " + NameProvider.getStateName(nState) + " to " + aNewValue); if (xContext != null) { MessageArea.println (" at " + xContext.getAccessibleName() + " with role " + NameProvider.getRoleName(xContext.getAccessibleRole())); } else MessageArea.println (" at null"); // Further handling of some states switch (nState) { case AccessibleStateType.FOCUSED: if (aNewValue) focusGained (xContext); else focusLost (xContext); } } /** Handle a child event that describes the creation of removal of a single child. */ private void handleChildEvent ( XAccessibleContext aOldChild, XAccessibleContext aNewChild) { if (aOldChild != null) // Remove event listener from the child and all of its descendants. new RegistrationThread (maListenerProxy, aOldChild, false, false); else if (aNewChild != null) // Add event listener to the new child and all of its descendants. new RegistrationThread (maListenerProxy, aNewChild, true, false); } /** Handle the change of some visible data of an object. */ private void handleVisibleDataEvent (XAccessibleContext xContext) { // The given object may affect the visible appearance of the focused // object even when the two are not identical when the given object // is an ancestor of the focused object. // In order to not check this we simply call an update on the // focused object. if (mxFocusedObject != null) for (int i=0; i