xref: /AOO41X/main/toolkit/test/accessibility/AccessibleTreeHandler.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 import com.sun.star.accessibility.XAccessible;
2 import com.sun.star.accessibility.XAccessibleContext;
3 import com.sun.star.uno.UnoRuntime;
4 import com.sun.star.lang.IndexOutOfBoundsException;
5 
6 
7 /**
8  * Map the tree of accessibility objects into their
9  * AccessibilityTreeModel counterparts.
10  */
11 class AccessibleTreeHandler
12     extends NodeHandler
13 {
14     protected XAccessibleContext mxContext;
15 
16     public NodeHandler createHandler (XAccessibleContext xContext)
17     {
18         if (xContext != null)
19             return new AccessibleTreeHandler (xContext);
20         else
21             return null;
22     }
23 
24     public AccessibleTreeHandler ()
25     {
26         super();
27         mxContext = null;
28     }
29 
30     public AccessibleTreeHandler (XAccessibleContext xContext)
31     {
32         super();
33         mxContext = xContext;
34         if (mxContext != null)
35             // Add one to the number of children to include the string node
36             // that tells you how many children there are.
37             synchronized (maChildList)
38             {
39                 maChildList.setSize (1 + mxContext.getAccessibleChildCount());
40             }
41     }
42 
43     public AccessibleTreeNode createChild (AccessibleTreeNode aParent, int nIndex)
44     {
45         AccessibleTreeNode aChild = null;
46         if (mxContext != null)
47         {
48             if (nIndex == 0)
49                 aChild = new StringNode ("Child count: " + mxContext.getAccessibleChildCount(),
50                     aParent);
51             else
52             {
53                 // Lower index to skip the string node.
54                 nIndex -= 1;
55                 try
56                 {
57                     XAccessible xChild = mxContext.getAccessibleChild (nIndex);
58                     aChild = NodeFactory.Instance().createDefaultNode (
59                         xChild, aParent);
60                 }
61                 catch( IndexOutOfBoundsException e )
62                 {
63                     aChild = new StringNode ("ERROR: no child with index " + nIndex, aParent);
64                 }
65             }
66         }
67         else
68             aChild = new StringNode ("XAccessibleContext interface not supported", aParent);
69         return aChild;
70     }
71 
72     /** Try to add the specified accessible child into the lists of
73         children.  The insertion position is determined from the
74         getIndexInParent method of the child.
75     */
76     public AccessibleTreeNode addAccessibleChild (AccessibleTreeNode aParent, XAccessible xChild)
77     {
78         AccessibleTreeNode aChild = null;
79 
80         if (xChild != null)
81         {
82             XAccessibleContext xContext = xChild.getAccessibleContext();
83             if (xContext != null)
84             {
85                 int nIndex = xContext.getAccessibleIndexInParent() + 1;
86                 synchronized (maChildList)
87                 {
88                     if ((nIndex >= 0) || (nIndex <= maChildList.size()))
89                     {
90                         aChild = NodeFactory.Instance().createDefaultNode (xChild, aParent);
91                         maChildList.insertElementAt (aChild, nIndex);
92                     }
93                 }
94             }
95         }
96         return aChild;
97     }
98 
99 
100     /** Update only the child count node.  Trust on other ways to update the
101         accessible children.
102     */
103     public void update (AccessibleTreeNode aNode)
104     {
105         synchronized (maChildList)
106         {
107             maChildList.setElementAt (null, 0);
108         }
109     }
110 }
111