merged tag ooo/DEV300_m102
[LibreOffice.git] / toolkit / test / accessibility / AccessibilityTree.java
blob819631b41eb89e5944b48f32fa29ee5de997cb02
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;
7 import java.awt.*;
8 import java.awt.event.*;
9 import javax.swing.*;
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.
44 mnExpandLevel = 0;
45 maTree.addTreeWillExpandListener (this);
46 maTree.addTreeExpansionListener (this);
49 public JTree getComponent ()
51 return maTree;
54 // Change cursor during expansions to show the user that this is a
55 // lengthy operation.
56 public void treeWillExpand (TreeExpansionEvent e)
58 if (mnExpandLevel == 0)
60 maTree.setCursor (new Cursor (Cursor.WAIT_CURSOR));
62 mnExpandLevel += 1;
64 public void treeWillCollapse (TreeExpansionEvent e)
66 if (mnExpandLevel == 0)
68 maTree.setCursor (new Cursor (Cursor.WAIT_CURSOR));
70 mnExpandLevel += 1;
72 public void treeExpanded (TreeExpansionEvent e)
74 mnExpandLevel -= 1;
75 if (mnExpandLevel == 0)
77 maTree.setCursor (new Cursor (Cursor.DEFAULT_CURSOR));
80 public void treeCollapsed (TreeExpansionEvent e)
82 mnExpandLevel -= 1;
83 if (mnExpandLevel == 0)
85 maTree.setCursor (new Cursor (Cursor.DEFAULT_CURSOR));
91 public void SetCanvas (Canvas aCanvas)
93 maCanvas = 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);
106 mnExpandLevel += 1;
108 ((AccessibilityTreeModel)maTree.getModel()).lock ();
112 expandTree (new TreePath (aNode.createPath()), aExpander);
114 catch (Exception e)
116 // Ignore
119 mnExpandLevel -= 1;
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");
136 // get 'our' object
137 Object aObj = aPath.getLastPathComponent();
139 // expand this object, if the Expander tells us so
140 if( aExpander.expand( aObj ) )
142 maTree.expandPath (aPath);
143 if( aFirst == null )
144 aFirst = 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 ) ),
156 aExpander );
157 if( aFirst == null )
158 aFirst = aRet;
162 catch (Exception e)
164 System.out.println ("caught exception while expanding tree path "
165 + aPath + ": " + e);
166 e.printStackTrace ();
169 return aFirst;
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)
216 maTree=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();
227 if( bIsPopup )
229 int selRow = maTree.getComponent().getRowForLocation(e.getX(), e.getY());
230 if (selRow != -1)
232 TreePath aPath = maTree.getComponent().getPathForLocation(e.getX(), e.getY());
234 // check for actions
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(),
250 aNode, i ) );
253 else if (aObject instanceof AccessibleTreeNode)
255 AccessibleTreeNode aNode = (AccessibleTreeNode)aObject;
256 String[] aActionNames = aNode.getActions();
257 int nCount=aActionNames.length;
258 if (nCount > 0)
260 for (int i=0; i<nCount; i++)
261 aMenu.add( new NodeAction(
262 aActionNames[i],
263 aNode,
264 i));
266 else
267 aMenu = null;
269 if (aMenu != null)
270 aMenu.show (maTree.getComponent(),
271 e.getX(), e.getY());
275 return bIsPopup;
278 private AccessibilityTree maTree;
281 class NodeAction extends AbstractAction
283 private int mnIndex;
284 private AccessibleTreeNode maNode;
286 public NodeAction( String aName, AccessibleTreeNode aNode, int nIndex )
288 super( aName );
289 maNode = aNode;
290 mnIndex = 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");
307 maTree = aTree;
308 maNode = aNode;
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");
324 maTree = aTree;
325 maNode = aNode;
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)
357 return true;
359 return false;
365 protected AccessibleTreeCellRenderer
366 maCellRenderer;
369 private JTree
370 maTree;
371 private Canvas
372 maCanvas;
373 private boolean
374 mbFirstShapeSeen;
375 private int
376 mnExpandLevel;