xref: /AOO41X/main/toolkit/test/accessibility/AccessibleTreeHandler.java (revision 1b0aaa91df402a6f20c4769a008573eb958e886d)
1*1b0aaa91SAndrew Rist /**************************************************************
2*1b0aaa91SAndrew Rist  *
3*1b0aaa91SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*1b0aaa91SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*1b0aaa91SAndrew Rist  * distributed with this work for additional information
6*1b0aaa91SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*1b0aaa91SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*1b0aaa91SAndrew Rist  * "License"); you may not use this file except in compliance
9*1b0aaa91SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*1b0aaa91SAndrew Rist  *
11*1b0aaa91SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*1b0aaa91SAndrew Rist  *
13*1b0aaa91SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*1b0aaa91SAndrew Rist  * software distributed under the License is distributed on an
15*1b0aaa91SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*1b0aaa91SAndrew Rist  * KIND, either express or implied.  See the License for the
17*1b0aaa91SAndrew Rist  * specific language governing permissions and limitations
18*1b0aaa91SAndrew Rist  * under the License.
19*1b0aaa91SAndrew Rist  *
20*1b0aaa91SAndrew Rist  *************************************************************/
21*1b0aaa91SAndrew Rist 
22cdf0e10cSrcweir import com.sun.star.accessibility.XAccessible;
23cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleContext;
24cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
25cdf0e10cSrcweir import com.sun.star.lang.IndexOutOfBoundsException;
26cdf0e10cSrcweir 
27cdf0e10cSrcweir 
28cdf0e10cSrcweir /**
29cdf0e10cSrcweir  * Map the tree of accessibility objects into their
30cdf0e10cSrcweir  * AccessibilityTreeModel counterparts.
31cdf0e10cSrcweir  */
32cdf0e10cSrcweir class AccessibleTreeHandler
33cdf0e10cSrcweir     extends NodeHandler
34cdf0e10cSrcweir {
35cdf0e10cSrcweir     protected XAccessibleContext mxContext;
36cdf0e10cSrcweir 
createHandler(XAccessibleContext xContext)37cdf0e10cSrcweir     public NodeHandler createHandler (XAccessibleContext xContext)
38cdf0e10cSrcweir     {
39cdf0e10cSrcweir         if (xContext != null)
40cdf0e10cSrcweir             return new AccessibleTreeHandler (xContext);
41cdf0e10cSrcweir         else
42cdf0e10cSrcweir             return null;
43cdf0e10cSrcweir     }
44cdf0e10cSrcweir 
AccessibleTreeHandler()45cdf0e10cSrcweir     public AccessibleTreeHandler ()
46cdf0e10cSrcweir     {
47cdf0e10cSrcweir         super();
48cdf0e10cSrcweir         mxContext = null;
49cdf0e10cSrcweir     }
50cdf0e10cSrcweir 
AccessibleTreeHandler(XAccessibleContext xContext)51cdf0e10cSrcweir     public AccessibleTreeHandler (XAccessibleContext xContext)
52cdf0e10cSrcweir     {
53cdf0e10cSrcweir         super();
54cdf0e10cSrcweir         mxContext = xContext;
55cdf0e10cSrcweir         if (mxContext != null)
56cdf0e10cSrcweir             // Add one to the number of children to include the string node
57cdf0e10cSrcweir             // that tells you how many children there are.
58cdf0e10cSrcweir             synchronized (maChildList)
59cdf0e10cSrcweir             {
60cdf0e10cSrcweir                 maChildList.setSize (1 + mxContext.getAccessibleChildCount());
61cdf0e10cSrcweir             }
62cdf0e10cSrcweir     }
63cdf0e10cSrcweir 
createChild(AccessibleTreeNode aParent, int nIndex)64cdf0e10cSrcweir     public AccessibleTreeNode createChild (AccessibleTreeNode aParent, int nIndex)
65cdf0e10cSrcweir     {
66cdf0e10cSrcweir         AccessibleTreeNode aChild = null;
67cdf0e10cSrcweir         if (mxContext != null)
68cdf0e10cSrcweir         {
69cdf0e10cSrcweir             if (nIndex == 0)
70cdf0e10cSrcweir                 aChild = new StringNode ("Child count: " + mxContext.getAccessibleChildCount(),
71cdf0e10cSrcweir                     aParent);
72cdf0e10cSrcweir             else
73cdf0e10cSrcweir             {
74cdf0e10cSrcweir                 // Lower index to skip the string node.
75cdf0e10cSrcweir                 nIndex -= 1;
76cdf0e10cSrcweir                 try
77cdf0e10cSrcweir                 {
78cdf0e10cSrcweir                     XAccessible xChild = mxContext.getAccessibleChild (nIndex);
79cdf0e10cSrcweir                     aChild = NodeFactory.Instance().createDefaultNode (
80cdf0e10cSrcweir                         xChild, aParent);
81cdf0e10cSrcweir                 }
82cdf0e10cSrcweir                 catch( IndexOutOfBoundsException e )
83cdf0e10cSrcweir                 {
84cdf0e10cSrcweir                     aChild = new StringNode ("ERROR: no child with index " + nIndex, aParent);
85cdf0e10cSrcweir                 }
86cdf0e10cSrcweir             }
87cdf0e10cSrcweir         }
88cdf0e10cSrcweir         else
89cdf0e10cSrcweir             aChild = new StringNode ("XAccessibleContext interface not supported", aParent);
90cdf0e10cSrcweir         return aChild;
91cdf0e10cSrcweir     }
92cdf0e10cSrcweir 
93cdf0e10cSrcweir     /** Try to add the specified accessible child into the lists of
94cdf0e10cSrcweir         children.  The insertion position is determined from the
95cdf0e10cSrcweir         getIndexInParent method of the child.
96cdf0e10cSrcweir     */
addAccessibleChild(AccessibleTreeNode aParent, XAccessible xChild)97cdf0e10cSrcweir     public AccessibleTreeNode addAccessibleChild (AccessibleTreeNode aParent, XAccessible xChild)
98cdf0e10cSrcweir     {
99cdf0e10cSrcweir         AccessibleTreeNode aChild = null;
100cdf0e10cSrcweir 
101cdf0e10cSrcweir         if (xChild != null)
102cdf0e10cSrcweir         {
103cdf0e10cSrcweir             XAccessibleContext xContext = xChild.getAccessibleContext();
104cdf0e10cSrcweir             if (xContext != null)
105cdf0e10cSrcweir             {
106cdf0e10cSrcweir                 int nIndex = xContext.getAccessibleIndexInParent() + 1;
107cdf0e10cSrcweir                 synchronized (maChildList)
108cdf0e10cSrcweir                 {
109cdf0e10cSrcweir                     if ((nIndex >= 0) || (nIndex <= maChildList.size()))
110cdf0e10cSrcweir                     {
111cdf0e10cSrcweir                         aChild = NodeFactory.Instance().createDefaultNode (xChild, aParent);
112cdf0e10cSrcweir                         maChildList.insertElementAt (aChild, nIndex);
113cdf0e10cSrcweir                     }
114cdf0e10cSrcweir                 }
115cdf0e10cSrcweir             }
116cdf0e10cSrcweir         }
117cdf0e10cSrcweir         return aChild;
118cdf0e10cSrcweir     }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir 
121cdf0e10cSrcweir     /** Update only the child count node.  Trust on other ways to update the
122cdf0e10cSrcweir         accessible children.
123cdf0e10cSrcweir     */
update(AccessibleTreeNode aNode)124cdf0e10cSrcweir     public void update (AccessibleTreeNode aNode)
125cdf0e10cSrcweir     {
126cdf0e10cSrcweir         synchronized (maChildList)
127cdf0e10cSrcweir         {
128cdf0e10cSrcweir             maChildList.setElementAt (null, 0);
129cdf0e10cSrcweir         }
130cdf0e10cSrcweir     }
131cdf0e10cSrcweir }
132