1*34dd1e25SAndrew Rist /**************************************************************
2*34dd1e25SAndrew Rist  *
3*34dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*34dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*34dd1e25SAndrew Rist  * distributed with this work for additional information
6*34dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*34dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*34dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
9*34dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*34dd1e25SAndrew Rist  *
11*34dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*34dd1e25SAndrew Rist  *
13*34dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*34dd1e25SAndrew Rist  * software distributed under the License is distributed on an
15*34dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*34dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
17*34dd1e25SAndrew Rist  * specific language governing permissions and limitations
18*34dd1e25SAndrew Rist  * under the License.
19*34dd1e25SAndrew Rist  *
20*34dd1e25SAndrew Rist  *************************************************************/
21*34dd1e25SAndrew Rist 
22*34dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import java.awt.event.ActionListener;
25cdf0e10cSrcweir import javax.swing.*;
26cdf0e10cSrcweir import java.awt.*;
27cdf0e10cSrcweir import java.util.*;
28cdf0e10cSrcweir 
29cdf0e10cSrcweir import com.sun.star.awt.XTopWindowListener;
30cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
31cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver;
32cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
33cdf0e10cSrcweir import com.sun.star.accessibility.*;
34cdf0e10cSrcweir import com.sun.star.awt.XExtendedToolkit;
35cdf0e10cSrcweir 
36cdf0e10cSrcweir /** This class is used as a thread and registers or unregsiters a listener
37cdf0e10cSrcweir     given the constructor at all nodes of a tree of accessibility objects.
38cdf0e10cSrcweir */
39cdf0e10cSrcweir public class RegistrationThread
40cdf0e10cSrcweir     implements Runnable
41cdf0e10cSrcweir {
42cdf0e10cSrcweir     /** Start a new thread that adds or removes the given listener at all
43cdf0e10cSrcweir         accessible objects in the sub-tree rooted in the given accessible
44cdf0e10cSrcweir         object.
45cdf0e10cSrcweir         @param aListener
46cdf0e10cSrcweir             The listener that is added or removed.
47cdf0e10cSrcweir         @param xRoot
48cdf0e10cSrcweir             The root of the sub-tree of accessibility objects.
49cdf0e10cSrcweir         @param bRegister
50cdf0e10cSrcweir             This flag decides whether to add or remove the listener.
51cdf0e10cSrcweir     */
RegistrationThread( EventListenerProxy aListener, XAccessibleContext xRoot, boolean bRegister, boolean bShowMessages)52cdf0e10cSrcweir     public RegistrationThread (
53cdf0e10cSrcweir         EventListenerProxy aListener,
54cdf0e10cSrcweir         XAccessibleContext xRoot,
55cdf0e10cSrcweir         boolean bRegister,
56cdf0e10cSrcweir         boolean bShowMessages)
57cdf0e10cSrcweir     {
58cdf0e10cSrcweir         maListener = aListener;
59cdf0e10cSrcweir         mxRoot = xRoot;
60cdf0e10cSrcweir         mbRegister = bRegister;
61cdf0e10cSrcweir         mbShowMessages = bShowMessages;
62cdf0e10cSrcweir 
63cdf0e10cSrcweir         if (mxRoot != null)
64cdf0e10cSrcweir         {
65cdf0e10cSrcweir             if (mbShowMessages)
66cdf0e10cSrcweir                 MessageArea.println ("starting to register at " + mxRoot.getAccessibleName());
67cdf0e10cSrcweir             new Thread (this, "RegistrationThread").start();
68cdf0e10cSrcweir         }
69cdf0e10cSrcweir     }
70cdf0e10cSrcweir 
71cdf0e10cSrcweir 
72cdf0e10cSrcweir 
run()73cdf0e10cSrcweir     public void run ()
74cdf0e10cSrcweir     {
75cdf0e10cSrcweir         System.out.println ("starting registration");
76cdf0e10cSrcweir         long nNodeCount = traverseTree (mxRoot);
77cdf0e10cSrcweir         System.out.println ("ending registration");
78cdf0e10cSrcweir         if (mbShowMessages)
79cdf0e10cSrcweir         {
80cdf0e10cSrcweir             if ( ! mbRegister)
81cdf0e10cSrcweir                 MessageArea.print ("un");
82cdf0e10cSrcweir             MessageArea.println ("registered at " + nNodeCount
83cdf0e10cSrcweir                 + " objects in accessibility tree of " + mxRoot.getAccessibleName());
84cdf0e10cSrcweir         }
85cdf0e10cSrcweir     }
86cdf0e10cSrcweir 
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 
89cdf0e10cSrcweir 
90cdf0e10cSrcweir     /** Register this object as listener for accessibility events at all nodes
91cdf0e10cSrcweir         of the given tree.
92cdf0e10cSrcweir         @param xRoot
93cdf0e10cSrcweir             The root node of the tree at which to register.
94cdf0e10cSrcweir     */
traverseTree(XAccessibleContext xRoot)95cdf0e10cSrcweir     public long traverseTree (XAccessibleContext xRoot)
96cdf0e10cSrcweir     {
97cdf0e10cSrcweir         long nNodeCount = 0;
98cdf0e10cSrcweir         if (xRoot != null)
99cdf0e10cSrcweir         {
100cdf0e10cSrcweir             // Register the root node.
101cdf0e10cSrcweir             XAccessibleEventBroadcaster xBroadcaster =
102cdf0e10cSrcweir                 (XAccessibleEventBroadcaster) UnoRuntime.queryInterface (
103cdf0e10cSrcweir                     XAccessibleEventBroadcaster.class,
104cdf0e10cSrcweir                     xRoot);
105cdf0e10cSrcweir             if (xBroadcaster != null)
106cdf0e10cSrcweir             {
107cdf0e10cSrcweir                 if (mbRegister)
108cdf0e10cSrcweir                     xBroadcaster.addEventListener (maListener);
109cdf0e10cSrcweir                 else
110cdf0e10cSrcweir                     xBroadcaster.removeEventListener (maListener);
111cdf0e10cSrcweir                 nNodeCount += 1;
112cdf0e10cSrcweir             }
113cdf0e10cSrcweir 
114cdf0e10cSrcweir             // Call this method recursively to register all sub-trees.
115cdf0e10cSrcweir             try
116cdf0e10cSrcweir             {
117cdf0e10cSrcweir                 int nChildCount = xRoot.getAccessibleChildCount();
118cdf0e10cSrcweir                 for (int i=0; i<nChildCount; i++)
119cdf0e10cSrcweir                 {
120cdf0e10cSrcweir                     XAccessible xChild = xRoot.getAccessibleChild (i);
121cdf0e10cSrcweir                     if (xChild != null)
122cdf0e10cSrcweir                         nNodeCount += traverseTree (xChild.getAccessibleContext());
123cdf0e10cSrcweir                 }
124cdf0e10cSrcweir             }
125cdf0e10cSrcweir             catch (com.sun.star.lang.IndexOutOfBoundsException aException)
126cdf0e10cSrcweir             {
127cdf0e10cSrcweir                 // The set of children has changed since our last call to
128cdf0e10cSrcweir                 // getAccesibleChildCount().  Don't try any further on this
129cdf0e10cSrcweir                 // sub-tree.
130cdf0e10cSrcweir             }
131cdf0e10cSrcweir             catch (com.sun.star.lang.DisposedException aException)
132cdf0e10cSrcweir             {
133cdf0e10cSrcweir                 // The child has been destroyed since our last call to
134cdf0e10cSrcweir                 // getAccesibleChildCount().  That is OK. Don't try any
135cdf0e10cSrcweir                 // further on this sub-tree.
136cdf0e10cSrcweir             }
137cdf0e10cSrcweir         }
138cdf0e10cSrcweir         return nNodeCount;
139cdf0e10cSrcweir     }
140cdf0e10cSrcweir 
141cdf0e10cSrcweir     private EventListenerProxy maListener;
142cdf0e10cSrcweir     private XAccessibleContext mxRoot;
143cdf0e10cSrcweir     private boolean mbRegister;
144cdf0e10cSrcweir     private boolean mbShowMessages;
145cdf0e10cSrcweir }
146