summaryrefslogtreecommitdiff
path: root/toolkit/test/accessibility/AccTreeNode.java
blob: c8c861d6a77e0b3add9263d2ae24fb5ce95981d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

import java.util.Vector;

/**
 * The node type for the AccessibleTreeModel.
 * This implements all the child-handling based on the appropriate
 * NodeHandlers. Trivial nodes can be implemented by any Object
 * type.
 */
class AccTreeNode
{
    private Vector aHandlers;       /// NodeHandlers for this node
    private Object aDataObject;     /// the actual data object
    private Object aDisplayObject;  /// object to be displayed

    public AccTreeNode( Object aData )
    {
        this( aData, aData );
    }

    public AccTreeNode( Object aData, Object aDisplay )
    {
        aHandlers = new Vector();
        aDataObject = aData;
        aDisplayObject = aDisplay;
    }

    public Object getDataObject() { return aDataObject; }
    public Object getDisplayObject() { return aDisplayObject; }

    public void addHandler( NodeHandler aHandler )
    {
        aHandlers.add( aHandler );
    }


    /** iterate over handlers and return child sum */
    public int getChildCount()
    {
        int nRet = 0;
        for(int i = 0; i < aHandlers.size(); i++)
        {
            nRet += ((NodeHandler)aHandlers.get(i)).
                getChildCount( aDataObject );
        }
        return nRet;
    }

    /** iterate over handlers until the child is found */
    public Object getChild(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.getChildCount( aDataObject );
                if( nCount > nIndex )
                    return aHandler.getChild( aDataObject, nIndex );
                else
                    nIndex -= nCount;
            }
        }

        // nothing found?
        return null;
    }


    /** this node is a leaf if have no handlers, or is those
            handlers show no children */
    public boolean isLeaf()
    {
        return (aHandlers.size() == 0) || (getChildCount() == 0);
    }

    public boolean equals(Object 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;
            }
        }
    }

}