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