/** This is the tree component that is responsible for displaying the contentsofthetreemodelonthescreen.
*/ publicclass AccessibilityTree implements TreeExpansionListener, TreeWillExpandListener
{ /** Create a new accessibility tree. Use the specified message display fordisplayingmessagesandthespecifiedcanvastodrawthe graphicalrepresentationsofaccessibleobjectson.
*/ public AccessibilityTree ()
{
maTree = new JTree ();
AccessibilityTreeModel aModel = new AccessibilityTreeModel ( new StringNode ("Please press Update button", null));
maTree.setModel (aModel);
maTree.addMouseListener (new MouseListener (this));
// Listen to expansions and collapses to change the mouse cursor.
mnExpandLevel = 0;
maTree.addTreeWillExpandListener (this);
maTree.addTreeExpansionListener (this);
}
public JTree getComponent ()
{ return maTree;
}
// Change cursor during expansions to show the user that this is a // lengthy operation. publicvoid treeWillExpand (TreeExpansionEvent e)
{ if (mnExpandLevel == 0)
{
maTree.setCursor (new Cursor (Cursor.WAIT_CURSOR));
}
mnExpandLevel += 1;
} publicvoid treeWillCollapse (TreeExpansionEvent e)
{ if (mnExpandLevel == 0)
{
maTree.setCursor (new Cursor (Cursor.WAIT_CURSOR));
}
mnExpandLevel += 1;
} publicvoid treeExpanded (TreeExpansionEvent e)
{
mnExpandLevel -= 1; if (mnExpandLevel == 0)
{
maTree.setCursor (new Cursor (Cursor.DEFAULT_CURSOR));
}
} publicvoid treeCollapsed (TreeExpansionEvent e)
{
mnExpandLevel -= 1; if (mnExpandLevel == 0)
{
maTree.setCursor (new Cursor (Cursor.DEFAULT_CURSOR));
}
}
/** Expand the nodes in the subtree rooted in aNode according to the specifiedexpander.Thetreeislockedduringtheexpansion.
*/ privatevoid expandTree (AccessibleTreeNode aNode, Expander aExpander)
{ if (mnExpandLevel == 0)
{
maTree.setEnabled (false);
}
mnExpandLevel += 1;
try
{ // get 'our' object
Object aObj = aPath.getLastPathComponent();
// expand this object, if the Expander tells us so if( aExpander.expand( aObj ) )
{
maTree.expandPath (aPath);
aFirst = aPath;
}
// visit all children if (aObj instanceof AccessibleTreeNode)
{
AccessibleTreeNode aNode = (AccessibleTreeNode)aObj; int nLength = aNode.getChildCount(); for( int i = 0; i < nLength; i++ )
{
TreePath aRet = expandTree(
aPath.pathByAddingChild( aNode.getChild( i ) ),
aExpander ); if( aFirst == null )
aFirst = aRet;
}
}
} catch (Exception e)
{
System.out.println ("caught exception while expanding tree path "
+ aPath + ": " + e);
e.printStackTrace ();
}
return aFirst;
}
/** Expand all nodes and their subtrees that represent shapes. Call
* this method from the outside. */ publicvoid expandShapes ()
{
expandShapes ((AccessibleTreeNode)maTree.getModel().getRoot());
} privatevoid expandShapes (AccessibleTreeNode aNode)
{
expandTree (aNode, new ShapeExpander());
}
ArrayList<String> aActions = new ArrayList<String>();
aMenu.add (new AccessibilityTree.ShapeExpandAction(maTree, aNode));
aMenu.add (new AccessibilityTree.SubtreeExpandAction(maTree, aNode));
aNode.getActions(aActions); for( int i = 0; i < aActions.size(); i++ )
{
aMenu.add( new NodeAction(
aActions.get(i),
aNode, i ) );
}
} elseif (aObject instanceof AccessibleTreeNode)
{
AccessibleTreeNode aNode = (AccessibleTreeNode)aObject;
String[] aActionNames = aNode.getActions(); int nCount=aActionNames.length; if (nCount > 0)
{ for (int i=0; i<nCount; i++)
aMenu.add( new NodeAction(
aActionNames[i],
aNode,
i));
} else
aMenu = null;
} if (aMenu != null)
aMenu.show (maTree.getComponent(),
e.getX(), e.getY());
// This action expands all shapes in the subtree rooted in the specified node. privateclass ShapeExpandAction extends AbstractAction
{ privatefinal AccessibilityTree maTree; privatefinal AccTreeNode maNode; public ShapeExpandAction (AccessibilityTree aTree, AccTreeNode aNode)
{ super ("Expand Shapes");
maTree = aTree;
maNode = aNode;
} publicvoid actionPerformed (ActionEvent e)
{
maTree.expandShapes (maNode);
}
}
// This action expands all nodes in the subtree rooted in the specified node. privateclass SubtreeExpandAction extends AbstractAction
{ privatefinal AccessibilityTree maTree; privatefinal AccTreeNode maNode; public SubtreeExpandAction (AccessibilityTree aTree, AccTreeNode aNode)
{ super ("Expand Subtree");
maTree = aTree;
maNode = aNode;
} publicvoid actionPerformed (ActionEvent e)
{
maTree.expandAll (maNode);
}
}
/** Predicate class to determine whether a node should be expanded
* For use with expandTree method */ privateabstractclass Expander
{ abstractpublicboolean expand (Object aObject);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.