fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / toolkit / test / accessibility / AccessibilityTree.java
blob4d90adf1b2c2e2e2695736058201b0220a5f9543
1 /*
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.awt.Cursor;
20 import java.awt.event.ActionEvent;
21 import java.awt.event.MouseAdapter;
22 import java.awt.event.MouseEvent;
23 import java.util.ArrayList;
25 import javax.swing.AbstractAction;
26 import javax.swing.JPopupMenu;
27 import javax.swing.JTree;
28 import javax.swing.event.TreeExpansionEvent;
29 import javax.swing.event.TreeExpansionListener;
30 import javax.swing.event.TreeWillExpandListener;
31 import javax.swing.tree.TreePath;
33 import com.sun.star.accessibility.XAccessibleContext;
37 /** This is the tree component that is responsible for displaying the
38 contents of the tree model on the screen.
40 public class AccessibilityTree
41 implements TreeExpansionListener, TreeWillExpandListener
43 /** Create a new accessibility tree. Use the specified message display
44 for displaying messages and the specified canvas to draw the
45 graphical representations of accessible objects on.
47 public AccessibilityTree ()
49 maTree = new JTree ();
51 AccessibilityTreeModel aModel =
52 new AccessibilityTreeModel (
53 new StringNode ("Please press Update button", null));
54 maTree.setModel (aModel);
56 maCellRenderer = new AccessibleTreeCellRenderer();
57 // setCellRenderer (maCellRenderer);
59 // allow editing of XAccessibleText interfaces
60 // setEditable (true);
61 // maTreeModel.addTreeModelListener( new TextUpdateListener() );
63 maTree.addMouseListener (new MouseListener (this));
65 // Listen to expansions and collapses to change the mouse cursor.
66 mnExpandLevel = 0;
67 maTree.addTreeWillExpandListener (this);
68 maTree.addTreeExpansionListener (this);
71 public JTree getComponent ()
73 return maTree;
76 // Change cursor during expansions to show the user that this is a
77 // lengthy operation.
78 public void treeWillExpand (TreeExpansionEvent e)
80 if (mnExpandLevel == 0)
82 maTree.setCursor (new Cursor (Cursor.WAIT_CURSOR));
84 mnExpandLevel += 1;
86 public void treeWillCollapse (TreeExpansionEvent e)
88 if (mnExpandLevel == 0)
90 maTree.setCursor (new Cursor (Cursor.WAIT_CURSOR));
92 mnExpandLevel += 1;
94 public void treeExpanded (TreeExpansionEvent e)
96 mnExpandLevel -= 1;
97 if (mnExpandLevel == 0)
99 maTree.setCursor (new Cursor (Cursor.DEFAULT_CURSOR));
102 public void treeCollapsed (TreeExpansionEvent e)
104 mnExpandLevel -= 1;
105 if (mnExpandLevel == 0)
107 maTree.setCursor (new Cursor (Cursor.DEFAULT_CURSOR));
113 public void SetCanvas (Canvas aCanvas)
115 maCanvas = aCanvas;
116 ((AccessibilityTreeModel)maTree.getModel()).setCanvas (maCanvas);
119 /** Expand the nodes in the subtree rooted in aNode according to the the
120 specified expander. The tree is locked during the expansion.
122 protected void expandTree (AccessibleTreeNode aNode, Expander aExpander)
124 if (mnExpandLevel == 0)
126 maTree.setEnabled (false);
128 mnExpandLevel += 1;
130 ((AccessibilityTreeModel)maTree.getModel()).lock ();
134 expandTree (new TreePath (aNode.createPath()), aExpander);
136 catch (Exception e)
138 // Ignore
141 mnExpandLevel -= 1;
142 if (mnExpandLevel == 0)
144 maTree.setEnabled (true);
145 ((AccessibilityTreeModel)maTree.getModel()).unlock (aNode);
149 private TreePath expandTree( TreePath aPath, Expander aExpander )
151 // return first expanded object
152 TreePath aFirst = null;
154 // System.out.print ("e");
158 // get 'our' object
159 Object aObj = aPath.getLastPathComponent();
161 // expand this object, if the Expander tells us so
162 if( aExpander.expand( aObj ) )
164 maTree.expandPath (aPath);
165 if( aFirst == null )
166 aFirst = aPath;
169 // visit all children
170 if (aObj instanceof AccessibleTreeNode)
172 AccessibleTreeNode aNode = (AccessibleTreeNode)aObj;
173 int nLength = aNode.getChildCount();
174 for( int i = 0; i < nLength; i++ )
176 TreePath aRet = expandTree(
177 aPath.pathByAddingChild( aNode.getChild( i ) ),
178 aExpander );
179 if( aFirst == null )
180 aFirst = aRet;
184 catch (Exception e)
186 System.out.println ("caught exception while expanding tree path "
187 + aPath + ": " + e);
188 e.printStackTrace ();
191 return aFirst;
195 /** Expand all nodes and their subtrees that represent shapes. Call
196 * this method from the outside. */
197 public void expandShapes ()
199 expandShapes ((AccessibleTreeNode)maTree.getModel().getRoot());
201 public void expandShapes (AccessibleTreeNode aNode)
203 expandTree (aNode, new ShapeExpander());
206 /** Expand all nodes */
207 public void expandAll ()
209 expandAll ((AccessibleTreeNode)maTree.getModel().getRoot());
211 public void expandAll (AccessibleTreeNode aNode)
213 expandTree (aNode, new AllExpander());
218 public void disposing (com.sun.star.lang.EventObject e)
220 System.out.println ("disposing " + e);
224 public Dimension getPreferredSize ()
226 Dimension aPreferredSize = super.getPreferredSize();
227 Dimension aMinimumSize = super.getMinimumSize();
228 if (aPreferredSize.width < aMinimumSize.width)
229 aPreferredSize.width = aMinimumSize.width;
230 return aPreferredSize;
234 class MouseListener extends MouseAdapter
236 public MouseListener (AccessibilityTree aTree)
238 maTree=aTree;
240 public void mousePressed(MouseEvent e) { popupTrigger(e); }
241 public void mouseClicked(MouseEvent e) { popupTrigger(e); }
242 public void mouseEntered(MouseEvent e) { popupTrigger(e); }
243 public void mouseExited(MouseEvent e) { popupTrigger(e); }
244 public void mouseReleased(MouseEvent e) { popupTrigger(e); }
246 public boolean popupTrigger( MouseEvent e )
248 boolean bIsPopup = e.isPopupTrigger();
249 if( bIsPopup )
251 int selRow = maTree.getComponent().getRowForLocation(e.getX(), e.getY());
252 if (selRow != -1)
254 TreePath aPath = maTree.getComponent().getPathForLocation(e.getX(), e.getY());
256 // check for actions
257 Object aObject = aPath.getLastPathComponent();
258 JPopupMenu aMenu = new JPopupMenu();
259 if( aObject instanceof AccTreeNode )
261 AccTreeNode aNode = (AccTreeNode)aObject;
263 ArrayList<String> aActions = new ArrayList<String>();
264 aMenu.add (new AccessibilityTree.ShapeExpandAction(maTree, aNode));
265 aMenu.add (new AccessibilityTree.SubtreeExpandAction(maTree, aNode));
267 aNode.getActions(aActions);
268 for( int i = 0; i < aActions.size(); i++ )
270 aMenu.add( new NodeAction(
271 aActions.get(i).toString(),
272 aNode, i ) );
275 else if (aObject instanceof AccessibleTreeNode)
277 AccessibleTreeNode aNode = (AccessibleTreeNode)aObject;
278 String[] aActionNames = aNode.getActions();
279 int nCount=aActionNames.length;
280 if (nCount > 0)
282 for (int i=0; i<nCount; i++)
283 aMenu.add( new NodeAction(
284 aActionNames[i],
285 aNode,
286 i));
288 else
289 aMenu = null;
291 if (aMenu != null)
292 aMenu.show (maTree.getComponent(),
293 e.getX(), e.getY());
297 return bIsPopup;
300 private AccessibilityTree maTree;
303 class NodeAction extends AbstractAction
305 private int mnIndex;
306 private AccessibleTreeNode maNode;
308 public NodeAction( String aName, AccessibleTreeNode aNode, int nIndex )
310 super( aName );
311 maNode = aNode;
312 mnIndex = nIndex;
315 public void actionPerformed(ActionEvent e)
317 maNode.performAction(mnIndex);
321 // This action expands all shapes in the subtree rooted in the specified node.
322 class ShapeExpandAction extends AbstractAction
324 private AccessibilityTree maTree;
325 private AccTreeNode maNode;
326 public ShapeExpandAction (AccessibilityTree aTree, AccTreeNode aNode)
328 super ("Expand Shapes");
329 maTree = aTree;
330 maNode = aNode;
332 public void actionPerformed (ActionEvent e)
334 maTree.expandShapes (maNode);
338 // This action expands all nodes in the subtree rooted in the specified node.
339 class SubtreeExpandAction extends AbstractAction
341 private AccessibilityTree maTree;
342 private AccTreeNode maNode;
343 public SubtreeExpandAction (AccessibilityTree aTree, AccTreeNode aNode)
345 super ("Expand Subtree");
346 maTree = aTree;
347 maNode = aNode;
349 public void actionPerformed (ActionEvent e)
351 maTree.expandAll (maNode);
355 /** Predicate class to determine whether a node should be expanded
356 * For use with expandTree method */
357 abstract class Expander
359 abstract public boolean expand (Object aObject);
362 /** expand all nodes */
363 class AllExpander extends Expander
365 public boolean expand(Object aObject) { return true; }
368 /** expand all nodes with accessibility roles > 100 */
369 class ShapeExpander extends Expander
371 public boolean expand (Object aObject)
373 if (aObject instanceof AccTreeNode)
375 AccTreeNode aNode = (AccTreeNode)aObject;
376 XAccessibleContext xContext = aNode.getContext();
377 if (xContext != null)
378 if (xContext.getAccessibleRole() >= 100)
379 return true;
381 return false;
387 protected AccessibleTreeCellRenderer
388 maCellRenderer;
391 private JTree
392 maTree;
393 private Canvas
394 maCanvas;
395 private boolean
396 mbFirstShapeSeen;
397 private int
398 mnExpandLevel;