1 /************************************************************************* 2 * 3 * The Contents of this file are made available subject to the terms of 4 * the BSD license. 5 * 6 * Copyright 2000, 2010 Oracle and/or its affiliates. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 30 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 31 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 * 33 *************************************************************************/ 34 35 import java.awt.event.ActionListener; 36 import javax.swing.*; 37 import java.awt.*; 38 import java.util.*; 39 40 import com.sun.star.awt.XTopWindowListener; 41 import com.sun.star.uno.UnoRuntime; 42 import com.sun.star.bridge.XUnoUrlResolver; 43 import com.sun.star.lang.XMultiServiceFactory; 44 import com.sun.star.accessibility.*; 45 import com.sun.star.awt.XExtendedToolkit; 46 47 /** This class is used as a thread and registers or unregsiters a listener 48 given the constructor at all nodes of a tree of accessibility objects. 49 */ 50 public class RegistrationThread 51 implements Runnable 52 { 53 /** Start a new thread that adds or removes the given listener at all 54 accessible objects in the sub-tree rooted in the given accessible 55 object. 56 @param aListener 57 The listener that is added or removed. 58 @param xRoot 59 The root of the sub-tree of accessibility objects. 60 @param bRegister 61 This flag decides whether to add or remove the listener. 62 */ 63 public RegistrationThread ( 64 EventListenerProxy aListener, 65 XAccessibleContext xRoot, 66 boolean bRegister, 67 boolean bShowMessages) 68 { 69 maListener = aListener; 70 mxRoot = xRoot; 71 mbRegister = bRegister; 72 mbShowMessages = bShowMessages; 73 74 if (mxRoot != null) 75 { 76 if (mbShowMessages) 77 MessageArea.println ("starting to register at " + mxRoot.getAccessibleName()); 78 new Thread (this, "RegistrationThread").start(); 79 } 80 } 81 82 83 84 public void run () 85 { 86 System.out.println ("starting registration"); 87 long nNodeCount = traverseTree (mxRoot); 88 System.out.println ("ending registration"); 89 if (mbShowMessages) 90 { 91 if ( ! mbRegister) 92 MessageArea.print ("un"); 93 MessageArea.println ("registered at " + nNodeCount 94 + " objects in accessibility tree of " + mxRoot.getAccessibleName()); 95 } 96 } 97 98 99 100 101 /** Register this object as listener for accessibility events at all nodes 102 of the given tree. 103 @param xRoot 104 The root node of the tree at which to register. 105 */ 106 public long traverseTree (XAccessibleContext xRoot) 107 { 108 long nNodeCount = 0; 109 if (xRoot != null) 110 { 111 // Register the root node. 112 XAccessibleEventBroadcaster xBroadcaster = 113 (XAccessibleEventBroadcaster) UnoRuntime.queryInterface ( 114 XAccessibleEventBroadcaster.class, 115 xRoot); 116 if (xBroadcaster != null) 117 { 118 if (mbRegister) 119 xBroadcaster.addEventListener (maListener); 120 else 121 xBroadcaster.removeEventListener (maListener); 122 nNodeCount += 1; 123 } 124 125 // Call this method recursively to register all sub-trees. 126 try 127 { 128 int nChildCount = xRoot.getAccessibleChildCount(); 129 for (int i=0; i<nChildCount; i++) 130 { 131 XAccessible xChild = xRoot.getAccessibleChild (i); 132 if (xChild != null) 133 nNodeCount += traverseTree (xChild.getAccessibleContext()); 134 } 135 } 136 catch (com.sun.star.lang.IndexOutOfBoundsException aException) 137 { 138 // The set of children has changed since our last call to 139 // getAccesibleChildCount(). Don't try any further on this 140 // sub-tree. 141 } 142 catch (com.sun.star.lang.DisposedException aException) 143 { 144 // The child has been destroyed since our last call to 145 // getAccesibleChildCount(). That is OK. Don't try any 146 // further on this sub-tree. 147 } 148 } 149 return nNodeCount; 150 } 151 152 private EventListenerProxy maListener; 153 private XAccessibleContext mxRoot; 154 private boolean mbRegister; 155 private boolean mbShowMessages; 156 } 157