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
;
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
38 extends AccessibleTreeNode
40 private class HandlerDescriptor
42 private HandlerDescriptor (NodeHandler aHandler
)
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);
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.
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 ()
89 public XAccessibleComponent
getComponent ()
91 if (mxComponent
== null && mxContext
!= null)
92 mxComponent
= UnoRuntime
.queryInterface(
93 XAccessibleComponent
.class, mxContext
);
97 public XAccessibleText
getText ()
99 if (mxText
== null && mxContext
!= null)
100 mxText
= UnoRuntime
.queryInterface(
101 XAccessibleText
.class, mxContext
);
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
);
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 ();
144 public int getChildCount()
147 for (int i
= 0; i
< maHandlers
.size(); i
++)
149 HandlerDescriptor aDescriptor
= getHandlerDescriptor (i
);
150 nChildCount
+= aDescriptor
.mnChildCount
;
155 /** iterate over handlers until the child is found */
157 public AccessibleTreeNode
getChild (int nIndex
)
158 throws IndexOutOfBoundsException
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
);
170 nIndex
-= aDescriptor
.mnChildCount
;
174 throw new IndexOutOfBoundsException();
181 public AccessibleTreeNode
getChildNoCreate (int nIndex
)
182 throws IndexOutOfBoundsException
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
);
194 nIndex
-= aDescriptor
.mnChildCount
;
198 throw new IndexOutOfBoundsException();
205 public boolean removeChild (int nIndex
)
206 throws IndexOutOfBoundsException
208 boolean bStatus
= false;
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 ();
223 nIndex
-= aDescriptor
.mnChildCount
;
227 throw new IndexOutOfBoundsException();
234 public int indexOf (AccessibleTreeNode aNode
)
239 for (int i
=0; i
<maHandlers
.size(); i
++)
241 HandlerDescriptor aDescriptor
= getHandlerDescriptor (i
);
242 int nIndex
= aDescriptor
.maHandler
.indexOf (aNode
);
244 return nBaseIndex
+ nIndex
;
246 nBaseIndex
+= aDescriptor
.mnChildCount
;
253 /** this node is a leaf if have no handlers, or is those
254 handlers show no children */
256 public boolean isLeaf()
258 return maHandlers
.isEmpty();
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
));
281 public void performAction( int nIndex
)
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
);
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 ();
324 public java
.util
.List
<Integer
> updateChildren (java
.lang
.Class class1
, java
.lang
.Class
<AccessibleExtendedComponentHandler
> class2
)
326 ArrayList
<Integer
> aChildIndices
= new ArrayList
<Integer
>();
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
;