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