merged tag ooo/DEV300_m102
[LibreOffice.git] / toolkit / test / accessibility / AccTreeNode.java
blob64a5a5a3c4f64af0813d13eee35727b9ed60528e
1 import com.sun.star.uno.UnoRuntime;
2 import com.sun.star.accessibility.*;
3 import java.util.Vector;
5 /**
6 * The node type for the AccessibleTreeModel.
7 * This implements all the child-handling based on the appropriate
8 * NodeHandlers. Trivial nodes can be implemented by any Object
9 * type.
11 class AccTreeNode
12 extends AccessibleTreeNode
14 class HandlerDescriptor
16 public HandlerDescriptor (NodeHandler aHandler)
18 maHandler = aHandler;
19 mnChildCount = -1;
21 public NodeHandler maHandler;
22 public int mnChildCount;
24 /// NodeHandlers for this node
25 private Vector maHandlers;
27 // The accessible context of this node.
28 private XAccessible mxAccessible;
29 private XAccessibleContext mxContext;
30 private XAccessibleComponent mxComponent;
31 private XAccessibleText mxText;
32 private XAccessibleTable mxTable;
34 public AccTreeNode (XAccessible xAccessible, XAccessibleContext xContext, AccessibleTreeNode aParent)
36 this (xAccessible, xContext, xContext, aParent);
39 public AccTreeNode (XAccessible xAccessible, XAccessibleContext xContext, Object aDisplay, AccessibleTreeNode aParent)
41 super (aDisplay, aParent);
43 maHandlers = new Vector(5);
44 mxContext = xContext;
45 mxAccessible = xAccessible;
48 /** Update the internal data extracted from the corresponding accessible
49 object. This is done by replacing every handler by a new one. An
50 update method at each handler would be better of course.
52 public void update ()
54 for (int i=0; i<maHandlers.size(); i++)
56 System.out.println ("replacing handler " + i);
57 HandlerDescriptor aDescriptor = (HandlerDescriptor)maHandlers.get(i);
58 aDescriptor.maHandler = aDescriptor.maHandler.createHandler (mxContext);
59 aDescriptor.mnChildCount =
60 aDescriptor.maHandler.getChildCount (this);
64 public XAccessibleContext getContext ()
66 return mxContext;
69 public XAccessibleComponent getComponent ()
71 if (mxComponent == null && mxContext != null)
72 mxComponent = (XAccessibleComponent)UnoRuntime.queryInterface(
73 XAccessibleComponent.class, mxContext);
74 return mxComponent;
77 public XAccessibleExtendedComponent getExtendedComponent ()
79 if (mxComponent == null)
80 getComponent();
81 if (mxComponent != null)
82 return (XAccessibleExtendedComponent)UnoRuntime.queryInterface(
83 XAccessibleExtendedComponent.class, mxComponent);
84 else
85 return null;
88 public XAccessibleText getText ()
90 if (mxText == null && mxContext != null)
91 mxText = (XAccessibleText)UnoRuntime.queryInterface(
92 XAccessibleText.class, mxContext);
93 return mxText;
96 public XAccessibleEditableText getEditText ()
98 return (XAccessibleEditableText)UnoRuntime.queryInterface(
99 XAccessibleEditableText.class, mxContext);
102 public XAccessibleTable getTable ()
104 if (mxTable == null && mxContext != null)
105 mxTable = (XAccessibleTable)UnoRuntime.queryInterface(
106 XAccessibleTable.class, mxContext);
107 return mxTable;
111 public XAccessible getAccessible()
113 if ((mxAccessible == null) && (mxContext != null))
114 mxAccessible = (XAccessible)UnoRuntime.queryInterface(
115 XAccessible.class, mxContext);
116 return mxAccessible;
119 public XAccessibleSelection getSelection ()
121 return (XAccessibleSelection)UnoRuntime.queryInterface(
122 XAccessibleSelection.class, mxContext);
125 public void addHandler( NodeHandler aHandler )
127 if (aHandler != null)
128 maHandlers.add (new HandlerDescriptor (aHandler));
132 /** iterate over handlers and return child sum */
133 protected HandlerDescriptor getHandlerDescriptor (int i)
135 HandlerDescriptor aDescriptor = (HandlerDescriptor)maHandlers.get(i);
136 if (aDescriptor.mnChildCount < 0)
137 aDescriptor.mnChildCount =
138 aDescriptor.maHandler.getChildCount (this);
139 return aDescriptor;
142 public int getChildCount()
144 int nChildCount = 0;
145 for (int i = 0; i < maHandlers.size(); i++)
147 HandlerDescriptor aDescriptor = getHandlerDescriptor (i);
148 nChildCount += aDescriptor.mnChildCount;
150 return nChildCount;
153 /** iterate over handlers until the child is found */
154 public AccessibleTreeNode getChild (int nIndex)
155 throws IndexOutOfBoundsException
157 if( nIndex >= 0 )
159 for(int i = 0; i < maHandlers.size(); i++)
161 // check if this handler has the child, and if not
162 // search with next handler
163 HandlerDescriptor aDescriptor = getHandlerDescriptor (i);
164 if (nIndex < aDescriptor.mnChildCount)
165 return aDescriptor.maHandler.getChild (this, nIndex);
166 else
167 nIndex -= aDescriptor.mnChildCount;
170 else
171 throw new IndexOutOfBoundsException();
173 // nothing found?
174 return null;
177 public AccessibleTreeNode getChildNoCreate (int nIndex)
178 throws IndexOutOfBoundsException
180 if( nIndex >= 0 )
182 for(int i = 0; i < maHandlers.size(); i++)
184 // check if this handler has the child, and if not
185 // search with next handler
186 HandlerDescriptor aDescriptor = getHandlerDescriptor (i);
187 if (nIndex < aDescriptor.mnChildCount)
188 return aDescriptor.maHandler.getChildNoCreate (this, nIndex);
189 else
190 nIndex -= aDescriptor.mnChildCount;
193 else
194 throw new IndexOutOfBoundsException();
196 // nothing found?
197 return null;
200 public boolean removeChild (int nIndex)
201 throws IndexOutOfBoundsException
203 boolean bStatus = false;
204 if (nIndex >= 0)
206 for (int i=0; i<maHandlers.size(); i++)
208 // check if this handler has the child, and if not
209 // search with next handler
210 HandlerDescriptor aDescriptor = getHandlerDescriptor (i);
211 if (nIndex < aDescriptor.mnChildCount)
213 bStatus = aDescriptor.maHandler.removeChild (this, nIndex);
214 aDescriptor.mnChildCount = aDescriptor.maHandler.getChildCount (this);
215 break;
217 else
218 nIndex -= aDescriptor.mnChildCount;
221 else
222 throw new IndexOutOfBoundsException();
224 return bStatus;
228 public int indexOf (AccessibleTreeNode aNode)
230 int nBaseIndex = 0;
231 if (aNode != null)
233 for (int i=0; i<maHandlers.size(); i++)
235 HandlerDescriptor aDescriptor = getHandlerDescriptor (i);
236 int nIndex = aDescriptor.maHandler.indexOf (aNode);
237 if (nIndex >= 0)
238 return nBaseIndex + nIndex;
239 else
240 nBaseIndex += aDescriptor.mnChildCount;
244 return -1;
247 /** this node is a leaf if have no handlers, or is those
248 handlers show no children */
249 public boolean isLeaf()
251 return (maHandlers.size() == 0);// || (getChildCount() == 0);
254 public boolean equals (Object aOther)
256 return (this == aOther) || (aOther!=null && aOther.equals(mxContext));
260 /** iterate over handlers until the child is found */
261 public void getActions(Vector aActions)
263 for(int i = 0; i < maHandlers.size(); i++)
265 HandlerDescriptor aDescriptor = getHandlerDescriptor (i);
266 NodeHandler aHandler = aDescriptor.maHandler;
267 String[] aHandlerActions = aHandler.getActions (this);
268 for(int j = 0; j < aHandlerActions.length; j++ )
270 aActions.add( aHandlerActions[j] );
275 public void performAction( int nIndex )
277 if( nIndex >= 0 )
279 for(int i = 0; i < maHandlers.size(); i++)
281 // check if this handler has the child, and if not
282 // search with next handler
283 HandlerDescriptor aDescriptor = getHandlerDescriptor (i);
284 NodeHandler aHandler = aDescriptor.maHandler;
285 int nCount = aHandler.getActions(this).length;
286 if( nCount > nIndex )
288 aHandler.performAction(this, nIndex );
289 return;
291 else
292 nIndex -= nCount;
297 /** Try to add the specified accessible object as new accessible child of the
298 AccessibleTreeHandler.
299 Note that child is used in another context than
300 it is used in the other methods of this class.
302 public AccessibleTreeNode addAccessibleChild (XAccessible xChild)
304 for(int i = 0; i < maHandlers.size(); i++)
306 HandlerDescriptor aDescriptor = getHandlerDescriptor (i);
307 if (aDescriptor.maHandler instanceof AccessibleTreeHandler)
309 AccessibleTreeHandler aHandler = (AccessibleTreeHandler)aDescriptor.maHandler;
310 AccessibleTreeNode aNode = aHandler.addAccessibleChild (this, xChild);
311 aDescriptor.mnChildCount = aHandler.getChildCount (this);
312 return aNode;
315 return null;
318 /** Update the specified handlers.
319 @return
320 The returned array containes the indices of the updated children
321 and can be used to create a TreeModelEvent.
323 public Vector updateChildren (java.lang.Class class1)
325 return updateChildren (class1, null);
328 public Vector updateChildren (java.lang.Class class1, java.lang.Class class2)
330 Vector aChildIndices = new Vector();
331 int nOffset = 0;
332 for(int i=0; i < maHandlers.size(); i++)
334 HandlerDescriptor aDescriptor = getHandlerDescriptor (i);
335 if ((class1.isInstance(aDescriptor.maHandler))
336 || (class2 !=null && class2.isInstance(aDescriptor.maHandler)))
338 aDescriptor.maHandler.update(this);
339 // Get updated number of children.
340 int nChildCount = aDescriptor.maHandler.getChildCount (this);
341 aDescriptor.mnChildCount = nChildCount;
342 // Fill in the indices of the updated children.
343 for (int j=0; j<nChildCount; j++)
344 aChildIndices.add(new Integer(j+nOffset));
346 nOffset += aDescriptor.mnChildCount;
348 return aChildIndices;