1 import com
.sun
.star
.accessibility
.*;
2 import com
.sun
.star
.lang
.XServiceInfo
;
3 import com
.sun
.star
.lang
.IndexOutOfBoundsException
;
4 import com
.sun
.star
.uno
.UnoRuntime
;
6 import java
.util
.Vector
;
8 import java
.awt
.event
.*;
10 import javax
.swing
.tree
.*;
11 import javax
.swing
.event
.*;
15 /** This is the tree component that is responsible for displaying the
16 contents of the tree model on the screen.
18 public class AccessibilityTree
19 implements TreeExpansionListener
, TreeWillExpandListener
21 /** Create a new accessibility tree. Use the specified message display
22 for displaying messages and the specified canvas to draw the
23 graphical representations of accessible objects on.
25 public AccessibilityTree ()
27 maTree
= new JTree ();
29 AccessibilityTreeModel aModel
=
30 new AccessibilityTreeModel (
31 new StringNode ("Please press Update button", null));
32 maTree
.setModel (aModel
);
34 maCellRenderer
= new AccessibleTreeCellRenderer();
35 // setCellRenderer (maCellRenderer);
37 // allow editing of XAccessibleText interfaces
38 // setEditable (true);
39 // maTreeModel.addTreeModelListener( new TextUpdateListener() );
41 maTree
.addMouseListener (new MouseListener (this));
43 // Listen to expansions and collapses to change the mouse cursor.
45 maTree
.addTreeWillExpandListener (this);
46 maTree
.addTreeExpansionListener (this);
49 public JTree
getComponent ()
54 // Change cursor during expansions to show the user that this is a
56 public void treeWillExpand (TreeExpansionEvent e
)
58 if (mnExpandLevel
== 0)
60 maTree
.setCursor (new Cursor (Cursor
.WAIT_CURSOR
));
64 public void treeWillCollapse (TreeExpansionEvent e
)
66 if (mnExpandLevel
== 0)
68 maTree
.setCursor (new Cursor (Cursor
.WAIT_CURSOR
));
72 public void treeExpanded (TreeExpansionEvent e
)
75 if (mnExpandLevel
== 0)
77 maTree
.setCursor (new Cursor (Cursor
.DEFAULT_CURSOR
));
80 public void treeCollapsed (TreeExpansionEvent e
)
83 if (mnExpandLevel
== 0)
85 maTree
.setCursor (new Cursor (Cursor
.DEFAULT_CURSOR
));
91 public void SetCanvas (Canvas aCanvas
)
94 ((AccessibilityTreeModel
)maTree
.getModel()).setCanvas (maCanvas
);
97 /** Expand the nodes in the subtree rooted in aNode according to the the
98 specified expander. The tree is locked during the expansion.
100 protected void expandTree (AccessibleTreeNode aNode
, Expander aExpander
)
102 if (mnExpandLevel
== 0)
104 maTree
.setEnabled (false);
108 ((AccessibilityTreeModel
)maTree
.getModel()).lock ();
112 expandTree (new TreePath (aNode
.createPath()), aExpander
);
120 if (mnExpandLevel
== 0)
122 maTree
.setEnabled (true);
123 ((AccessibilityTreeModel
)maTree
.getModel()).unlock (aNode
);
127 private TreePath
expandTree( TreePath aPath
, Expander aExpander
)
129 // return first expanded object
130 TreePath aFirst
= null;
132 // System.out.print ("e");
137 Object aObj
= aPath
.getLastPathComponent();
139 // expand this object, if the Expander tells us so
140 if( aExpander
.expand( aObj
) )
142 maTree
.expandPath (aPath
);
147 // visit all children
148 if (aObj
instanceof AccessibleTreeNode
)
150 AccessibleTreeNode aNode
= (AccessibleTreeNode
)aObj
;
151 int nLength
= aNode
.getChildCount();
152 for( int i
= 0; i
< nLength
; i
++ )
154 TreePath aRet
= expandTree(
155 aPath
.pathByAddingChild( aNode
.getChild( i
) ),
164 System
.out
.println ("caught exception while expanding tree path "
166 e
.printStackTrace ();
173 /** Expand all nodes and their subtrees that represent shapes. Call
174 * this method from the outside. */
175 public void expandShapes ()
177 expandShapes ((AccessibleTreeNode
)maTree
.getModel().getRoot());
179 public void expandShapes (AccessibleTreeNode aNode
)
181 expandTree (aNode
, new ShapeExpander());
184 /** Expand all nodes */
185 public void expandAll ()
187 expandAll ((AccessibleTreeNode
)maTree
.getModel().getRoot());
189 public void expandAll (AccessibleTreeNode aNode
)
191 expandTree (aNode
, new AllExpander());
196 public void disposing (com
.sun
.star
.lang
.EventObject e
)
198 System
.out
.println ("disposing " + e
);
202 public Dimension getPreferredSize ()
204 Dimension aPreferredSize = super.getPreferredSize();
205 Dimension aMinimumSize = super.getMinimumSize();
206 if (aPreferredSize.width < aMinimumSize.width)
207 aPreferredSize.width = aMinimumSize.width;
208 return aPreferredSize;
212 class MouseListener
extends MouseAdapter
214 public MouseListener (AccessibilityTree aTree
)
218 public void mousePressed(MouseEvent e
) { popupTrigger(e
); }
219 public void mouseClicked(MouseEvent e
) { popupTrigger(e
); }
220 public void mouseEntered(MouseEvent e
) { popupTrigger(e
); }
221 public void mouseExited(MouseEvent e
) { popupTrigger(e
); }
222 public void mouseReleased(MouseEvent e
) { popupTrigger(e
); }
224 public boolean popupTrigger( MouseEvent e
)
226 boolean bIsPopup
= e
.isPopupTrigger();
229 int selRow
= maTree
.getComponent().getRowForLocation(e
.getX(), e
.getY());
232 TreePath aPath
= maTree
.getComponent().getPathForLocation(e
.getX(), e
.getY());
235 Object aObject
= aPath
.getLastPathComponent();
236 JPopupMenu aMenu
= new JPopupMenu();
237 if( aObject
instanceof AccTreeNode
)
239 AccTreeNode aNode
= (AccTreeNode
)aObject
;
241 Vector aActions
= new Vector();
242 aMenu
.add (new AccessibilityTree
.ShapeExpandAction(maTree
, aNode
));
243 aMenu
.add (new AccessibilityTree
.SubtreeExpandAction(maTree
, aNode
));
245 aNode
.getActions(aActions
);
246 for( int i
= 0; i
< aActions
.size(); i
++ )
248 aMenu
.add( new NodeAction(
249 aActions
.elementAt(i
).toString(),
253 else if (aObject
instanceof AccessibleTreeNode
)
255 AccessibleTreeNode aNode
= (AccessibleTreeNode
)aObject
;
256 String
[] aActionNames
= aNode
.getActions();
257 int nCount
=aActionNames
.length
;
260 for (int i
=0; i
<nCount
; i
++)
261 aMenu
.add( new NodeAction(
270 aMenu
.show (maTree
.getComponent(),
278 private AccessibilityTree maTree
;
281 class NodeAction
extends AbstractAction
284 private AccessibleTreeNode maNode
;
286 public NodeAction( String aName
, AccessibleTreeNode aNode
, int nIndex
)
293 public void actionPerformed(ActionEvent e
)
295 maNode
.performAction(mnIndex
);
299 // This action expands all shapes in the subtree rooted in the specified node.
300 class ShapeExpandAction
extends AbstractAction
302 private AccessibilityTree maTree
;
303 private AccTreeNode maNode
;
304 public ShapeExpandAction (AccessibilityTree aTree
, AccTreeNode aNode
)
306 super ("Expand Shapes");
310 public void actionPerformed (ActionEvent e
)
312 maTree
.expandShapes (maNode
);
316 // This action expands all nodes in the subtree rooted in the specified node.
317 class SubtreeExpandAction
extends AbstractAction
319 private AccessibilityTree maTree
;
320 private AccTreeNode maNode
;
321 public SubtreeExpandAction (AccessibilityTree aTree
, AccTreeNode aNode
)
323 super ("Expand Subtree");
327 public void actionPerformed (ActionEvent e
)
329 maTree
.expandAll (maNode
);
333 /** Predicate class to determine whether a node should be expanded
334 * For use with expandTree method */
335 abstract class Expander
337 abstract public boolean expand (Object aObject
);
340 /** expand all nodes */
341 class AllExpander
extends Expander
343 public boolean expand(Object aObject
) { return true; }
346 /** expand all nodes with accessibility roles > 100 */
347 class ShapeExpander
extends Expander
349 public boolean expand (Object aObject
)
351 if (aObject
instanceof AccTreeNode
)
353 AccTreeNode aNode
= (AccTreeNode
)aObject
;
354 XAccessibleContext xContext
= aNode
.getContext();
355 if (xContext
!= null)
356 if (xContext
.getAccessibleRole() >= 100)
365 protected AccessibleTreeCellRenderer