fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / toolkit / test / accessibility / AccTreeNode.java
bloba1ed20702953a62116ebf07a74e7d011244dd709
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;
21 import com.sun.star.accessibility.XAccessible;
22 import com.sun.star.accessibility.XAccessibleComponent;
23 import com.sun.star.accessibility.XAccessibleContext;
24 import com.sun.star.accessibility.XAccessibleEditableText;
25 import com.sun.star.accessibility.XAccessibleExtendedComponent;
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 class HandlerDescriptor
42 public HandlerDescriptor (NodeHandler aHandler)
44 maHandler = aHandler;
45 mnChildCount = -1;
47 public NodeHandler maHandler;
48 public int mnChildCount;
50 /// NodeHandlers for this node
51 private ArrayList<HandlerDescriptor> maHandlers;
53 // The accessible context of this node.
54 private XAccessible mxAccessible;
55 private XAccessibleContext mxContext;
56 private XAccessibleComponent mxComponent;
57 private XAccessibleText mxText;
58 private XAccessibleTable mxTable;
60 public AccTreeNode (XAccessible xAccessible, XAccessibleContext xContext, AccessibleTreeNode aParent)
62 this (xAccessible, xContext, xContext, aParent);
65 public AccTreeNode (XAccessible xAccessible, XAccessibleContext xContext, Object aDisplay, AccessibleTreeNode aParent)
67 super (aDisplay, aParent);
69 maHandlers = new ArrayList<HandlerDescriptor>(5);
70 mxContext = xContext;
71 mxAccessible = xAccessible;
74 /** Update the internal data extracted from the corresponding accessible
75 object. This is done by replacing every handler by a new one. An
76 update method at each handler would be better of course.
78 public void update ()
80 for (int i=0; i<maHandlers.size(); i++)
82 System.out.println ("replacing handler " + i);
83 HandlerDescriptor aDescriptor = maHandlers.get(i);
84 aDescriptor.maHandler = aDescriptor.maHandler.createHandler (mxContext);
85 aDescriptor.mnChildCount =
86 aDescriptor.maHandler.getChildCount (this);
90 public XAccessibleContext getContext ()
92 return mxContext;
95 public XAccessibleComponent getComponent ()
97 if (mxComponent == null && mxContext != null)
98 mxComponent = UnoRuntime.queryInterface(
99 XAccessibleComponent.class, mxContext);
100 return mxComponent;
103 public XAccessibleExtendedComponent getExtendedComponent ()
105 if (mxComponent == null)
106 getComponent();
107 if (mxComponent != null)
108 return UnoRuntime.queryInterface(
109 XAccessibleExtendedComponent.class, mxComponent);
110 else
111 return null;
114 public XAccessibleText getText ()
116 if (mxText == null && mxContext != null)
117 mxText = UnoRuntime.queryInterface(
118 XAccessibleText.class, mxContext);
119 return mxText;
122 public XAccessibleEditableText getEditText ()
124 return UnoRuntime.queryInterface(
125 XAccessibleEditableText.class, mxContext);
128 public XAccessibleTable getTable ()
130 if (mxTable == null && mxContext != null)
131 mxTable = UnoRuntime.queryInterface(
132 XAccessibleTable.class, mxContext);
133 return mxTable;
137 public XAccessible getAccessible()
139 if ((mxAccessible == null) && (mxContext != null))
140 mxAccessible = UnoRuntime.queryInterface(
141 XAccessible.class, mxContext);
142 return mxAccessible;
145 public XAccessibleSelection getSelection ()
147 return UnoRuntime.queryInterface(
148 XAccessibleSelection.class, mxContext);
151 public void addHandler( NodeHandler aHandler )
153 if (aHandler != null)
154 maHandlers.add (new HandlerDescriptor (aHandler));
158 /** iterate over handlers and return child sum */
159 protected HandlerDescriptor getHandlerDescriptor (int i)
161 HandlerDescriptor aDescriptor = maHandlers.get(i);
162 if (aDescriptor.mnChildCount < 0)
163 aDescriptor.mnChildCount =
164 aDescriptor.maHandler.getChildCount (this);
165 return aDescriptor;
168 public int getChildCount()
170 int nChildCount = 0;
171 for (int i = 0; i < maHandlers.size(); i++)
173 HandlerDescriptor aDescriptor = getHandlerDescriptor (i);
174 nChildCount += aDescriptor.mnChildCount;
176 return nChildCount;
179 /** iterate over handlers until the child is found */
180 public AccessibleTreeNode getChild (int nIndex)
181 throws IndexOutOfBoundsException
183 if( nIndex >= 0 )
185 for(int i = 0; i < maHandlers.size(); i++)
187 // check if this handler has the child, and if not
188 // search with next handler
189 HandlerDescriptor aDescriptor = getHandlerDescriptor (i);
190 if (nIndex < aDescriptor.mnChildCount)
191 return aDescriptor.maHandler.getChild (this, nIndex);
192 else
193 nIndex -= aDescriptor.mnChildCount;
196 else
197 throw new IndexOutOfBoundsException();
199 // nothing found?
200 return null;
203 public AccessibleTreeNode getChildNoCreate (int nIndex)
204 throws IndexOutOfBoundsException
206 if( nIndex >= 0 )
208 for(int i = 0; i < maHandlers.size(); i++)
210 // check if this handler has the child, and if not
211 // search with next handler
212 HandlerDescriptor aDescriptor = getHandlerDescriptor (i);
213 if (nIndex < aDescriptor.mnChildCount)
214 return aDescriptor.maHandler.getChildNoCreate (this, nIndex);
215 else
216 nIndex -= aDescriptor.mnChildCount;
219 else
220 throw new IndexOutOfBoundsException();
222 // nothing found?
223 return null;
226 public boolean removeChild (int nIndex)
227 throws IndexOutOfBoundsException
229 boolean bStatus = false;
230 if (nIndex >= 0)
232 for (int i=0; i<maHandlers.size(); i++)
234 // check if this handler has the child, and if not
235 // search with next handler
236 HandlerDescriptor aDescriptor = getHandlerDescriptor (i);
237 if (nIndex < aDescriptor.mnChildCount)
239 bStatus = aDescriptor.maHandler.removeChild (this, nIndex);
240 aDescriptor.mnChildCount = aDescriptor.maHandler.getChildCount (this);
241 break;
243 else
244 nIndex -= aDescriptor.mnChildCount;
247 else
248 throw new IndexOutOfBoundsException();
250 return bStatus;
254 public int indexOf (AccessibleTreeNode aNode)
256 int nBaseIndex = 0;
257 if (aNode != null)
259 for (int i=0; i<maHandlers.size(); i++)
261 HandlerDescriptor aDescriptor = getHandlerDescriptor (i);
262 int nIndex = aDescriptor.maHandler.indexOf (aNode);
263 if (nIndex >= 0)
264 return nBaseIndex + nIndex;
265 else
266 nBaseIndex += aDescriptor.mnChildCount;
270 return -1;
273 /** this node is a leaf if have no handlers, or is those
274 handlers show no children */
275 public boolean isLeaf()
277 return (maHandlers.size() == 0);// || (getChildCount() == 0);
280 public boolean equals (Object aOther)
282 return (this == aOther) || (aOther!=null && aOther.equals(mxContext));
286 /** iterate over handlers until the child is found */
287 public void getActions(java.util.List<String> aActions)
289 for(int i = 0; i < maHandlers.size(); i++)
291 HandlerDescriptor aDescriptor = getHandlerDescriptor (i);
292 NodeHandler aHandler = aDescriptor.maHandler;
293 String[] aHandlerActions = aHandler.getActions (this);
294 for(int j = 0; j < aHandlerActions.length; j++ )
296 aActions.add( aHandlerActions[j] );
301 public void performAction( int nIndex )
303 if( nIndex >= 0 )
305 for(int i = 0; i < maHandlers.size(); i++)
307 // check if this handler has the child, and if not
308 // search with next handler
309 HandlerDescriptor aDescriptor = getHandlerDescriptor (i);
310 NodeHandler aHandler = aDescriptor.maHandler;
311 int nCount = aHandler.getActions(this).length;
312 if( nCount > nIndex )
314 aHandler.performAction(this, nIndex );
315 return;
317 else
318 nIndex -= nCount;
323 /** Try to add the specified accessible object as new accessible child of the
324 AccessibleTreeHandler.
325 Note that child is used in another context than
326 it is used in the other methods of this class.
328 public AccessibleTreeNode addAccessibleChild (XAccessible xChild)
330 for(int i = 0; i < maHandlers.size(); i++)
332 HandlerDescriptor aDescriptor = getHandlerDescriptor (i);
333 if (aDescriptor.maHandler instanceof AccessibleTreeHandler)
335 AccessibleTreeHandler aHandler = (AccessibleTreeHandler)aDescriptor.maHandler;
336 AccessibleTreeNode aNode = aHandler.addAccessibleChild (this, xChild);
337 aDescriptor.mnChildCount = aHandler.getChildCount (this);
338 return aNode;
341 return null;
344 /** Update the specified handlers.
345 @return
346 The returned array containes the indices of the updated children
347 and can be used to create a TreeModelEvent.
349 public java.util.List<Integer> updateChildren (java.lang.Class class1)
351 return updateChildren (class1, null);
354 public java.util.List<Integer> updateChildren (java.lang.Class class1, java.lang.Class<AccessibleExtendedComponentHandler> class2)
356 ArrayList<Integer> aChildIndices = new ArrayList<Integer>();
357 int nOffset = 0;
358 for(int i=0; i < maHandlers.size(); i++)
360 HandlerDescriptor aDescriptor = getHandlerDescriptor (i);
361 if ((class1.isInstance(aDescriptor.maHandler))
362 || (class2 !=null && class2.isInstance(aDescriptor.maHandler)))
364 aDescriptor.maHandler.update(this);
365 // Get updated number of children.
366 int nChildCount = aDescriptor.maHandler.getChildCount (this);
367 aDescriptor.mnChildCount = nChildCount;
368 // Fill in the indices of the updated children.
369 for (int j=0; j<nChildCount; j++)
370 aChildIndices.add(j+nOffset);
372 nOffset += aDescriptor.mnChildCount;
374 return aChildIndices;