Branch libreoffice-5-0-4
[LibreOffice.git] / toolkit / test / accessibility / AccessibleTreeHandler.java
blobe022dbae8d6bea068c4e2dab3da09009c8061c8f
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 import com.sun.star.accessibility.XAccessible;
20 import com.sun.star.accessibility.XAccessibleContext;
21 import com.sun.star.lang.IndexOutOfBoundsException;
24 /**
25 * Map the tree of accessibility objects into their
26 * AccessibilityTreeModel counterparts.
28 class AccessibleTreeHandler
29 extends NodeHandler
31 private XAccessibleContext mxContext;
33 @Override
34 public NodeHandler createHandler (XAccessibleContext xContext)
36 if (xContext != null)
37 return new AccessibleTreeHandler (xContext);
38 else
39 return null;
42 public AccessibleTreeHandler ()
44 super();
45 mxContext = null;
48 private AccessibleTreeHandler (XAccessibleContext xContext)
50 super();
51 mxContext = xContext;
52 if (mxContext != null)
53 // Add one to the number of children to include the string node
54 // that tells you how many children there are.
55 synchronized (maChildList)
57 maChildList.setSize (1 + mxContext.getAccessibleChildCount());
61 @Override
62 public AccessibleTreeNode createChild (AccessibleTreeNode aParent, int nIndex)
64 AccessibleTreeNode aChild = null;
65 if (mxContext != null)
67 if (nIndex == 0)
68 aChild = new StringNode ("Child count: " + mxContext.getAccessibleChildCount(),
69 aParent);
70 else
72 // Lower index to skip the string node.
73 nIndex -= 1;
74 try
76 XAccessible xChild = mxContext.getAccessibleChild (nIndex);
77 aChild = NodeFactory.Instance().createDefaultNode (
78 xChild, aParent);
80 catch( IndexOutOfBoundsException e )
82 aChild = new StringNode ("ERROR: no child with index " + nIndex, aParent);
86 else
87 aChild = new StringNode ("XAccessibleContext interface not supported", aParent);
88 return aChild;
91 /** Try to add the specified accessible child into the lists of
92 children. The insertion position is determined from the
93 getIndexInParent method of the child.
95 public AccessibleTreeNode addAccessibleChild (AccessibleTreeNode aParent, XAccessible xChild)
97 AccessibleTreeNode aChild = null;
99 if (xChild != null)
101 XAccessibleContext xContext = xChild.getAccessibleContext();
102 if (xContext != null)
104 int nIndex = xContext.getAccessibleIndexInParent() + 1;
105 synchronized (maChildList)
107 if ((nIndex >= 0) || (nIndex <= maChildList.size()))
109 aChild = NodeFactory.Instance().createDefaultNode (xChild, aParent);
110 maChildList.add (nIndex, aChild);
115 return aChild;
119 /** Update only the child count node. Trust on other ways to update the
120 accessible children.
122 @Override
123 public void update (AccessibleTreeNode aNode)
125 synchronized (maChildList)
127 maChildList.set (0, null);