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 javax
.swing
.event
.TreeModelEvent
;
22 import javax
.swing
.tree
.TreePath
;
24 import com
.sun
.star
.accessibility
.XAccessible
;
25 import com
.sun
.star
.accessibility
.XAccessibleContext
;
26 import com
.sun
.star
.accessibility
.XAccessibleEventBroadcaster
;
27 import com
.sun
.star
.accessibility
.XAccessibleEventListener
;
28 import com
.sun
.star
.uno
.UnoRuntime
;
30 public class AccessibilityTreeModel
31 extends AccessibilityTreeModelBase
33 private boolean mbVerbose
= false;
35 public AccessibilityTreeModel (AccessibleTreeNode aRoot
)
37 // create default node (unless we have a 'proper' node)
40 maNodeMap
= new NodeMap();
42 mxListener
= new QueuedListener(new EventListener (this));
50 /** Lock the tree. While the tree is locked, events from the outside are
51 not processed. Lock the tree when you change its internal structure.
58 /** Unlock the tree. After unlocking the tree as many times as locking
59 it, a treeStructureChange event is sent to the event listeners.
61 If not null and treeStructureChange events are thrown then this
62 node is used as root of the modified subtree.
64 public void unlock (AccessibleTreeNode aNodeHint
)
68 fireTreeStructureChanged (
69 new TreeModelEvent (this,
70 new TreePath (aNodeHint
.createPath())));
75 public synchronized void setRoot (AccessibleTreeNode aRoot
)
77 if (getRoot() == null)
78 super.setRoot (aRoot
);
82 maNodeMap
.ForEach (new NodeMapCallback () {
84 public void Apply (AccTreeNode aNode
)
87 maCanvas
.removeNode (aNode
);
88 removeAccListener (aNode
);
105 /** Delegate the request to the parent and then register listeners at
106 the child and add the child to the canvas.
109 public synchronized Object
getChild (Object aParent
, int nIndex
)
111 AccessibleTreeNode aChild
= (AccessibleTreeNode
)super.getChild (aParent
, nIndex
);
114 System
.out
.println ("getChild: child not found");
116 // Keep translation table up-to-date.
122 /** Remove a node (and all children) from the tree model.
124 private boolean removeChild (AccessibleTreeNode aNode
)
130 System
.out
.println ("can't remove null node");
135 // depth-first removal of children
136 while (aNode
.getChildCount() > 0) {
137 if ( ! removeChild (aNode
.getChildNoCreate (0)))
141 // Remove node from its parent.
142 AccessibleTreeNode aParent
= aNode
.getParent();
145 int nIndex
= aParent
.indexOf(aNode
);
146 aParent
.removeChild (nIndex
);
149 maNodeMap
.RemoveNode (aNode
);
154 System
.out
.println ("caught exception while removing child "
155 + aNode
+ " : " + e
);
156 e
.printStackTrace ();
162 public void removeNode (XAccessibleContext xNode
)
166 AccessibleTreeNode aNode
= maNodeMap
.GetNode (xNode
);
167 AccessibleTreeNode aRootNode
= (AccessibleTreeNode
)getRoot();
168 TreeModelEvent aEvent
= createEvent (aRootNode
, aNode
);
171 System
.out
.println (aNode
);
172 fireTreeNodesRemoved (aEvent
);
178 /** Add a new child to a parent.
180 Returns the new or existing representation of the specified
183 private AccessibleTreeNode
addChild (AccTreeNode aParentNode
, XAccessible xNewChild
)
185 AccessibleTreeNode aChildNode
= null;
188 // First make sure that the accessible object does not already have
190 aChildNode
= maNodeMap
.GetNode(xNewChild
);
191 if (aChildNode
== null)
192 aChildNode
= aParentNode
.addAccessibleChild (xNewChild
);
194 System
.out
.println ("node already present");
198 System
.out
.println ("caught exception while adding child "
199 + xNewChild
+ " to parent " + aParentNode
+ ": " + e
);
200 e
.printStackTrace ();
205 public void addChild (XAccessibleContext xParent
, XAccessible xChild
)
207 AccessibleTreeNode aParentNode
= maNodeMap
.GetNode (xParent
);
208 if (aParentNode
instanceof AccTreeNode
)
210 AccessibleTreeNode aChild
= addChild ((AccTreeNode
)aParentNode
, xChild
);
211 if (addNode (aChild
))
213 if (maCanvas
!= null)
214 maCanvas
.updateNode ((AccTreeNode
)aParentNode
);
216 // A call to fireTreeNodesInserted for xNew
217 // should be sufficient but at least the
218 // StringNode object that contains the number of
219 // children also changes and we do not know its
220 // index relative to its parent. Therefore the
221 // more expensive fireTreeStructureChanged is
223 fireTreeNodesInserted (createChildUpdateEvent (xParent
));
224 updateNode (xParent
, AccessibleTreeHandler
.class);
231 /** Add the child node to the internal tree structure.
233 The node to insert into the internal tree structure.
235 private boolean addNode (AccessibleTreeNode aNode
)
237 boolean bRet
= false;
240 if ( ! maNodeMap
.ValueIsMember (aNode
))
242 if (aNode
instanceof AccTreeNode
)
244 AccTreeNode aChild
= (AccTreeNode
)aNode
;
245 XAccessibleContext xChild
= aChild
.getContext();
246 registerAccListener (aChild
);
247 if (maCanvas
!= null)
248 maCanvas
.addNode (aChild
);
249 maNodeMap
.InsertNode (xChild
, aChild
);
257 System
.out
.println ("caught exception while adding node "
259 e
.printStackTrace ();
267 /** create path to node, suitable for TreeModelEvent constructor
268 * @see javax.swing.event.TreeModelEvent#TreeModelEvent
270 private Object
[] createPath (AccessibleTreeNode aNode
)
272 ArrayList
<AccessibleTreeNode
> aPath
= new ArrayList
<AccessibleTreeNode
>();
273 aNode
.createPath (aPath
);
274 return aPath
.toArray();
278 // listeners (and helper methods)
280 // We are registered with listeners as soon as objects are in the
281 // tree cache, and we should get removed as soon as they are out.
284 private void fireTreeNodesChanged(TreeModelEvent e
)
286 for(int i
= 0; i
< maTMListeners
.size(); i
++)
288 maTMListeners
.get(i
).treeNodesChanged(e
);
292 protected void fireTreeNodesInserted(final TreeModelEvent e
)
294 for(int i
= 0; i
< maTMListeners
.size(); i
++)
296 maTMListeners
.get(i
).treeNodesInserted(e
);
300 private void fireTreeNodesRemoved(final TreeModelEvent e
)
302 for(int i
= 0; i
< maTMListeners
.size(); i
++)
304 maTMListeners
.get(i
).treeNodesRemoved(e
);
308 private void fireTreeStructureChanged(final TreeModelEvent e
)
310 for(int i
= 0; i
< maTMListeners
.size(); i
++)
312 maTMListeners
.get(i
).treeStructureChanged(e
);
316 /** Create a TreeModelEvent object that informs listeners that one child
317 has been removed from or inserted into its parent.
319 private TreeModelEvent
createChildUpdateEvent (XAccessibleContext xParent
)
321 AccessibleTreeNode aParentNode
= maNodeMap
.GetNode (xParent
);
322 return createEvent (aParentNode
, xParent
);
325 private TreeModelEvent
createEvent (AccessibleTreeNode aParentNode
, XAccessibleContext xChild
)
327 AccessibleTreeNode aChildNode
= null;
329 aChildNode
= maNodeMap
.GetNode (xChild
);
330 return createEvent (aParentNode
, aChildNode
);
335 protected TreeModelEvent
createEvent (
336 AccessibleTreeNode aParentNode
,
337 AccessibleTreeNode aChildNode
)
339 Object
[] aPathToParent
= createPath (aParentNode
);
341 int nIndexInParent
= -1;
342 if (aChildNode
!= null)
343 nIndexInParent
= aParentNode
.indexOf (aChildNode
);
345 System
.out
.println (aChildNode
+ " " + nIndexInParent
);
347 if (nIndexInParent
== -1)
348 // This event may be passed only to treeStructureChanged of the listeners.
349 return new TreeModelEvent (this,
352 // General purpose event for removing or inserting known nodes.
353 return new TreeModelEvent (this,
355 new int[] {nIndexInParent
},
356 new Object
[] {aChildNode
} );
362 /** Create a TreeModelEvent that indicates changes at those children of
363 the specified node with the specified indices.
365 private TreeModelEvent
createChangeEvent (AccTreeNode aNode
, java
.util
.List
<Integer
> aChildIndices
)
367 // Build a list of child objects that are indicated by the given indices.
368 int nCount
= aChildIndices
.size();
369 Object aChildObjects
[] = new Object
[nCount
];
370 int nChildIndices
[] = new int[nCount
];
371 for (int i
=0; i
<nCount
; i
++)
373 int nIndex
= aChildIndices
.get(i
);
374 aChildObjects
[i
] = aNode
.getChild (nIndex
);
375 nChildIndices
[i
] = nIndex
;
378 return new TreeModelEvent (this,
386 private XAccessibleEventBroadcaster
getBroadcaster (Object aObject
)
388 if (aObject
instanceof AccTreeNode
)
389 return UnoRuntime
.queryInterface (
390 XAccessibleEventBroadcaster
.class, ((AccTreeNode
)aObject
).getContext());
395 private void registerAccListener( Object aObject
)
397 // register this as listener for XAccessibleEventBroadcaster
399 XAccessibleEventBroadcaster xBroadcaster
= getBroadcaster( aObject
);
400 if (xBroadcaster
!= null)
402 xBroadcaster
.addAccessibleEventListener( mxListener
);
406 private void removeAccListener( Object aObject
)
408 XAccessibleEventBroadcaster xBroadcaster
= getBroadcaster( aObject
);
409 if (xBroadcaster
!= null)
411 xBroadcaster
.removeAccessibleEventListener( mxListener
);
417 public void setCanvas (Canvas aCanvas
)
422 public Canvas
getCanvas ()
427 public void updateNode (XAccessibleContext xSource
, java
.lang
.Class class1
)
429 updateNode (xSource
, class1
,null);
432 /** Get a list of children of the node associated with xSource that are
433 affected by the given handlers. Fire events that these children may
434 have changed in the tree view. Update the canvas representation of
437 public AccTreeNode
updateNode (XAccessibleContext xSource
,
438 java
.lang
.Class class1
, java
.lang
.Class
<AccessibleExtendedComponentHandler
> class2
)
440 AccessibleTreeNode aTreeNode
= maNodeMap
.GetNode (xSource
);
441 AccTreeNode aNode
= null;
443 System
.out
.println ("updating node " + xSource
+ " " + aTreeNode
);
444 if (aTreeNode
instanceof AccTreeNode
)
446 aNode
= (AccTreeNode
) aTreeNode
;
447 // Get list of affected children.
448 java
.util
.List
<Integer
> aChildIndices
= aNode
.updateChildren (
450 // Fire events that these children may have changed.
451 fireTreeNodesChanged (
452 createChangeEvent (aNode
, aChildIndices
));
457 /** The listener to be registered with the accessible objects.
458 * Could be set to 'this' for same-thread event delivery, or to an
459 * instance of QueuedListener for multi-threaded delivery. May
460 * not be changed, since this would trip the
461 * register/removeAccListener logic. */
462 private final XAccessibleEventListener mxListener
;
464 // Map to translate from accessible object to corresponding tree node.
465 private final NodeMap maNodeMap
;
467 // If the lock count is higher then zero, then no events are processed.
468 private int mnLockCount
;
470 private Canvas maCanvas
;