add action/context menu support
This commit is contained in:
parent
75345d9b58
commit
be8e8745e0
3 changed files with 127 additions and 1 deletions
|
@ -68,6 +68,7 @@ class AccTreeNode
|
|||
return null;
|
||||
}
|
||||
|
||||
|
||||
/** this node is a leaf if have no handlers, or is those
|
||||
handlers show no children */
|
||||
public boolean isLeaf()
|
||||
|
@ -77,11 +78,48 @@ class AccTreeNode
|
|||
|
||||
public boolean equals(Object aOther)
|
||||
{
|
||||
return (this == aOther) || aDataObject.equals( aOther );
|
||||
return (this == aOther) || aOther.equals( aDataObject );
|
||||
}
|
||||
|
||||
public String toString()
|
||||
{
|
||||
return aDisplayObject.toString();
|
||||
}
|
||||
|
||||
|
||||
/** iterate over handlers until the child is found */
|
||||
public void getActions(Vector aActions)
|
||||
{
|
||||
for(int i = 0; i < aHandlers.size(); i++)
|
||||
{
|
||||
NodeHandler aHandler = (NodeHandler)aHandlers.get(i);
|
||||
String[] aHandlerActions = aHandler.getActions( aDataObject );
|
||||
for(int j = 0; j < aHandlerActions.length; j++ )
|
||||
{
|
||||
aActions.add( aHandlerActions[j] );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void performAction( int nIndex )
|
||||
{
|
||||
if( nIndex >= 0 )
|
||||
{
|
||||
for(int i = 0; i < aHandlers.size(); i++)
|
||||
{
|
||||
// check if this handler has the child, and if not
|
||||
// search with next handler
|
||||
NodeHandler aHandler = (NodeHandler)aHandlers.get(i);
|
||||
int nCount = aHandler.getActions( aDataObject ).length;
|
||||
if( nCount > nIndex )
|
||||
{
|
||||
aHandler.performAction( aDataObject, nIndex );
|
||||
return;
|
||||
}
|
||||
else
|
||||
nIndex -= nCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -55,6 +55,8 @@ public class AccessibilityTree
|
|||
// allow editing of XAccessibleText interfaces
|
||||
// setEditable (true);
|
||||
// maTreeModel.addTreeModelListener( new TextUpdateListener() );
|
||||
|
||||
addMouseListener( new MouseListener() );
|
||||
}
|
||||
|
||||
|
||||
|
@ -156,6 +158,67 @@ public class AccessibilityTree
|
|||
|
||||
|
||||
|
||||
class MouseListener extends MouseAdapter
|
||||
{
|
||||
public void mousePressed(MouseEvent e) { popupTrigger(e); }
|
||||
public void mouseClicked(MouseEvent e) { popupTrigger(e); }
|
||||
public void mouseEntered(MouseEvent e) { popupTrigger(e); }
|
||||
public void mouseExited(MouseEvent e) { popupTrigger(e); }
|
||||
public void mouseReleased(MouseEvent e) { popupTrigger(e); }
|
||||
|
||||
public boolean popupTrigger( MouseEvent e )
|
||||
{
|
||||
boolean bIsPopup = e.isPopupTrigger();
|
||||
if( bIsPopup )
|
||||
{
|
||||
int selRow = getRowForLocation(e.getX(), e.getY());
|
||||
TreePath aPath = getPathForLocation(e.getX(), e.getY());
|
||||
|
||||
// check for actions
|
||||
Object aObject = aPath.getLastPathComponent();
|
||||
if( aObject instanceof AccTreeNode )
|
||||
{
|
||||
AccTreeNode aNode = (AccTreeNode)aObject;
|
||||
|
||||
JPopupMenu aMenu = new JPopupMenu();
|
||||
|
||||
Vector aActions = new Vector();
|
||||
aNode.getActions(aActions);
|
||||
for( int i = 0; i < aActions.size(); i++ )
|
||||
{
|
||||
aMenu.add( new NodeAction(
|
||||
aActions.elementAt(i).toString(),
|
||||
aNode, i ) );
|
||||
}
|
||||
|
||||
// show menu (if we have actions)
|
||||
if( aActions.size() > 0 )
|
||||
aMenu.show( AccessibilityTree.this, e.getX(), e.getY() );
|
||||
}
|
||||
}
|
||||
|
||||
return bIsPopup;
|
||||
}
|
||||
}
|
||||
|
||||
class NodeAction extends AbstractAction
|
||||
{
|
||||
private int mnIndex;
|
||||
private AccTreeNode maNode;
|
||||
|
||||
public NodeAction( String aName, AccTreeNode aNode, int nIndex )
|
||||
{
|
||||
super( aName );
|
||||
maNode = aNode;
|
||||
mnIndex = nIndex;
|
||||
}
|
||||
|
||||
public void actionPerformed(ActionEvent e)
|
||||
{
|
||||
maNode.performAction(mnIndex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** listen to tree model changes in order to update XAccessibleText objects
|
||||
*/
|
||||
|
|
|
@ -13,4 +13,29 @@ abstract class NodeHandler
|
|||
* @see AccTreeNode
|
||||
*/
|
||||
public abstract Object getChild(Object aObject, int nIndex);
|
||||
|
||||
|
||||
//
|
||||
// The following methods support editing of children and actions.
|
||||
// They have default implementations for no actions and read-only.
|
||||
//
|
||||
|
||||
/** May this child be changed? */
|
||||
public boolean isChildEditable(Object aObject, int nIndex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/** change this child's value */
|
||||
public void setChild(Object aObject, int nIndex) { }
|
||||
|
||||
|
||||
/** get names of suported actions */
|
||||
public String[] getActions(Object aObject)
|
||||
{
|
||||
return new String[] {};
|
||||
}
|
||||
|
||||
/** perform action */
|
||||
public void performAction(Object aObject, int nIndex) { }
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue