bump product version to 5.0.4.1
[LibreOffice.git] / toolkit / test / accessibility / AccTreeNode.java
blob1ce46768423b3e3aa07b91d57f1d7aa0e1666a87
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 java.util.ArrayList;
20 import java.util.Arrays;
22 import com.sun.star.accessibility.XAccessible;
23 import com.sun.star.accessibility.XAccessibleComponent;
24 import com.sun.star.accessibility.XAccessibleContext;
25 import com.sun.star.accessibility.XAccessibleEditableText;
26 import com.sun.star.accessibility.XAccessibleSelection;
27 import com.sun.star.accessibility.XAccessibleTable;
28 import com.sun.star.accessibility.XAccessibleText;
29 import com.sun.star.uno.UnoRuntime;
31 /**
32 * The node type for the AccessibleTreeModel.
33 * This implements all the child-handling based on the appropriate
34 * NodeHandlers. Trivial nodes can be implemented by any Object
35 * type.
37 class AccTreeNode
38 extends AccessibleTreeNode
40 private class HandlerDescriptor
42 private HandlerDescriptor (NodeHandler aHandler)
44 maHandler = aHandler;
45 mnChildCount = -1;
47 private NodeHandler maHandler;
48 private int mnChildCount;
50 /// NodeHandlers for this node
51 private ArrayList<HandlerDescriptor> maHandlers;
53 // The accessible context of this node.
54 private XAccessibleContext mxContext;
55 private XAccessibleComponent mxComponent;
56 private XAccessibleText mxText;
57 private XAccessibleTable mxTable;
59 public AccTreeNode (XAccessibleContext xContext, Object aDisplay, AccessibleTreeNode aParent)
61 super (aDisplay, aParent);
63 maHandlers = new ArrayList<HandlerDescriptor>(5);
64 mxContext = xContext;
67 /** Update the internal data extracted from the corresponding accessible
68 object. This is done by replacing every handler by a new one. An
69 update method at each handler would be better of course.
71 @Override
72 public void update ()
74 for (int i=0; i<maHandlers.size(); i++)
76 System.out.println ("replacing handler " + i);
77 HandlerDescriptor aDescriptor = maHandlers.get(i);
78 aDescriptor.maHandler = aDescriptor.maHandler.createHandler (mxContext);
79 aDescriptor.mnChildCount =
80 aDescriptor.maHandler.getChildCount ();
84 public XAccessibleContext getContext ()
86 return mxContext;
89 public XAccessibleComponent getComponent ()
91 if (mxComponent == null && mxContext != null)
92 mxComponent = UnoRuntime.queryInterface(
93 XAccessibleComponent.class, mxContext);
94 return mxComponent;
97 public XAccessibleText getText ()
99 if (mxText == null && mxContext != null)
100 mxText = UnoRuntime.queryInterface(
101 XAccessibleText.class, mxContext);
102 return mxText;
105 public XAccessibleEditableText getEditText ()
107 return UnoRuntime.queryInterface(
108 XAccessibleEditableText.class, mxContext);
111 public XAccessibleTable getTable ()
113 if (mxTable == null && mxContext != null)
114 mxTable = UnoRuntime.queryInterface(
115 XAccessibleTable.class, mxContext);
116 return mxTable;
120 public XAccessibleSelection getSelection ()
122 return UnoRuntime.queryInterface(
123 XAccessibleSelection.class, mxContext);
126 public void addHandler( NodeHandler aHandler )
128 if (aHandler != null)
129 maHandlers.add (new HandlerDescriptor (aHandler));
133 /** iterate over handlers and return child sum */
134 private HandlerDescriptor getHandlerDescriptor (int i)
136 HandlerDescriptor aDescriptor = maHandlers.get(i);
137 if (aDescriptor.mnChildCount < 0)
138 aDescriptor.mnChildCount =
139 aDescriptor.maHandler.getChildCount ();
140 return aDescriptor;
143 @Override
144 public int getChildCount()
146 int nChildCount = 0;
147 for (int i = 0; i < maHandlers.size(); i++)
149 HandlerDescriptor aDescriptor = getHandlerDescriptor (i);
150 nChildCount += aDescriptor.mnChildCount;
152 return nChildCount;
155 /** iterate over handlers until the child is found */
156 @Override
157 public AccessibleTreeNode getChild (int nIndex)
158 throws IndexOutOfBoundsException
160 if( nIndex >= 0 )
162 for(int i = 0; i < maHandlers.size(); i++)
164 // check if this handler has the child, and if not
165 // search with next handler
166 HandlerDescriptor aDescriptor = getHandlerDescriptor (i);
167 if (nIndex < aDescriptor.mnChildCount)
168 return aDescriptor.maHandler.getChild (this, nIndex);
169 else
170 nIndex -= aDescriptor.mnChildCount;
173 else
174 throw new IndexOutOfBoundsException();
176 // nothing found?
177 return null;
180 @Override
181 public AccessibleTreeNode getChildNoCreate (int nIndex)
182 throws IndexOutOfBoundsException
184 if( nIndex >= 0 )
186 for(int i = 0; i < maHandlers.size(); i++)
188 // check if this handler has the child, and if not
189 // search with next handler
190 HandlerDescriptor aDescriptor = getHandlerDescriptor (i);
191 if (nIndex < aDescriptor.mnChildCount)
192 return aDescriptor.maHandler.getChildNoCreate (nIndex);
193 else
194 nIndex -= aDescriptor.mnChildCount;
197 else
198 throw new IndexOutOfBoundsException();
200 // nothing found?
201 return null;
204 @Override
205 public boolean removeChild (int nIndex)
206 throws IndexOutOfBoundsException
208 boolean bStatus = false;
209 if (nIndex >= 0)
211 for (int i=0; i<maHandlers.size(); i++)
213 // check if this handler has the child, and if not
214 // search with next handler
215 HandlerDescriptor aDescriptor = getHandlerDescriptor (i);
216 if (nIndex < aDescriptor.mnChildCount)
218 bStatus = aDescriptor.maHandler.removeChild (nIndex);
219 aDescriptor.mnChildCount = aDescriptor.maHandler.getChildCount ();
220 break;
222 else
223 nIndex -= aDescriptor.mnChildCount;
226 else
227 throw new IndexOutOfBoundsException();
229 return bStatus;
233 @Override
234 public int indexOf (AccessibleTreeNode aNode)
236 int nBaseIndex = 0;
237 if (aNode != null)
239 for (int i=0; i<maHandlers.size(); i++)
241 HandlerDescriptor aDescriptor = getHandlerDescriptor (i);
242 int nIndex = aDescriptor.maHandler.indexOf (aNode);
243 if (nIndex >= 0)
244 return nBaseIndex + nIndex;
245 else
246 nBaseIndex += aDescriptor.mnChildCount;
250 return -1;
253 /** this node is a leaf if have no handlers, or is those
254 handlers show no children */
255 @Override
256 public boolean isLeaf()
258 return maHandlers.isEmpty();
261 @Override
262 public boolean equals (Object aOther)
264 return (this == aOther) || (aOther!=null && aOther.equals(mxContext));
268 /** iterate over handlers until the child is found */
269 public void getActions(java.util.List<String> aActions)
271 for(int i = 0; i < maHandlers.size(); i++)
273 HandlerDescriptor aDescriptor = getHandlerDescriptor (i);
274 NodeHandler aHandler = aDescriptor.maHandler;
275 String[] aHandlerActions = aHandler.getActions (this);
276 aActions.addAll(Arrays.asList(aHandlerActions));
280 @Override
281 public void performAction( int nIndex )
283 if( nIndex >= 0 )
285 for(int i = 0; i < maHandlers.size(); i++)
287 // check if this handler has the child, and if not
288 // search with next handler
289 HandlerDescriptor aDescriptor = getHandlerDescriptor (i);
290 NodeHandler aHandler = aDescriptor.maHandler;
291 int nCount = aHandler.getActions(this).length;
292 if( nCount > nIndex )
294 aHandler.performAction(this, nIndex );
295 return;
297 else
298 nIndex -= nCount;
303 /** Try to add the specified accessible object as new accessible child of the
304 AccessibleTreeHandler.
305 Note that child is used in another context than
306 it is used in the other methods of this class.
308 public AccessibleTreeNode addAccessibleChild (XAccessible xChild)
310 for(int i = 0; i < maHandlers.size(); i++)
312 HandlerDescriptor aDescriptor = getHandlerDescriptor (i);
313 if (aDescriptor.maHandler instanceof AccessibleTreeHandler)
315 AccessibleTreeHandler aHandler = (AccessibleTreeHandler)aDescriptor.maHandler;
316 AccessibleTreeNode aNode = aHandler.addAccessibleChild (this, xChild);
317 aDescriptor.mnChildCount = aHandler.getChildCount ();
318 return aNode;
321 return null;
324 public java.util.List<Integer> updateChildren (java.lang.Class class1, java.lang.Class<AccessibleExtendedComponentHandler> class2)
326 ArrayList<Integer> aChildIndices = new ArrayList<Integer>();
327 int nOffset = 0;
328 for(int i=0; i < maHandlers.size(); i++)
330 HandlerDescriptor aDescriptor = getHandlerDescriptor (i);
331 if ((class1.isInstance(aDescriptor.maHandler))
332 || (class2 !=null && class2.isInstance(aDescriptor.maHandler)))
334 aDescriptor.maHandler.update(this);
335 // Get updated number of children.
336 int nChildCount = aDescriptor.maHandler.getChildCount ();
337 aDescriptor.mnChildCount = nChildCount;
338 // Fill in the indices of the updated children.
339 for (int j=0; j<nChildCount; j++)
340 aChildIndices.add(j+nOffset);
342 nOffset += aDescriptor.mnChildCount;
344 return aChildIndices;