xref: /AOO41X/main/toolkit/test/accessibility/NodeHandler.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir import java.util.Vector;
2*cdf0e10cSrcweir 
3*cdf0e10cSrcweir 
4*cdf0e10cSrcweir /**
5*cdf0e10cSrcweir  * Map an arbitrary object into parts of a tree node.
6*cdf0e10cSrcweir  */
7*cdf0e10cSrcweir abstract class NodeHandler
8*cdf0e10cSrcweir {
9*cdf0e10cSrcweir     /** This vector is used as cache for the child objects.
10*cdf0e10cSrcweir     */
11*cdf0e10cSrcweir     protected Vector maChildList;
12*cdf0e10cSrcweir 
13*cdf0e10cSrcweir 
14*cdf0e10cSrcweir     public abstract NodeHandler createHandler (
15*cdf0e10cSrcweir         com.sun.star.accessibility.XAccessibleContext xContext);
16*cdf0e10cSrcweir 
17*cdf0e10cSrcweir     public NodeHandler ()
18*cdf0e10cSrcweir     {
19*cdf0e10cSrcweir         maChildList = new Vector ();
20*cdf0e10cSrcweir     }
21*cdf0e10cSrcweir 
22*cdf0e10cSrcweir     /** Clear the cache of child objects.
23*cdf0e10cSrcweir     */
24*cdf0e10cSrcweir     public void clear ()
25*cdf0e10cSrcweir     {
26*cdf0e10cSrcweir         synchronized (maChildList)
27*cdf0e10cSrcweir         {
28*cdf0e10cSrcweir             maChildList = new Vector ();
29*cdf0e10cSrcweir         }
30*cdf0e10cSrcweir     }
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir     /** This factory method creates an individual handler for the specified
33*cdf0e10cSrcweir         object that may hold information to accelerate the access to its children.
34*cdf0e10cSrcweir     */
35*cdf0e10cSrcweir     //    public abstract NodeHandler createHandler (Object aObject);
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir     /** return the number of children this object has */
38*cdf0e10cSrcweir     public int getChildCount(Object aObject)
39*cdf0e10cSrcweir     {
40*cdf0e10cSrcweir         synchronized (maChildList)
41*cdf0e10cSrcweir         {
42*cdf0e10cSrcweir             return maChildList.size();
43*cdf0e10cSrcweir         }
44*cdf0e10cSrcweir     }
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir     /**
47*cdf0e10cSrcweir      * return a child object. Complex
48*cdf0e10cSrcweir      * children have to be AccTreeNode instances.
49*cdf0e10cSrcweir      * @see AccTreeNode
50*cdf0e10cSrcweir      */
51*cdf0e10cSrcweir     public AccessibleTreeNode getChild (AccessibleTreeNode aParent, int nIndex)
52*cdf0e10cSrcweir     {
53*cdf0e10cSrcweir         synchronized (maChildList)
54*cdf0e10cSrcweir         {
55*cdf0e10cSrcweir             AccessibleTreeNode aChild = (AccessibleTreeNode)maChildList.get(nIndex);
56*cdf0e10cSrcweir             if (aChild == null)
57*cdf0e10cSrcweir             {
58*cdf0e10cSrcweir                 aChild = createChild (aParent, nIndex);
59*cdf0e10cSrcweir                 if (aChild == null)
60*cdf0e10cSrcweir                     aChild = new StringNode ("could not create child", aParent);
61*cdf0e10cSrcweir                 maChildList.setElementAt (aChild, nIndex);
62*cdf0e10cSrcweir             }
63*cdf0e10cSrcweir             return aChild;
64*cdf0e10cSrcweir         }
65*cdf0e10cSrcweir     }
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir     public AccessibleTreeNode getChildNoCreate (AccessibleTreeNode aParent, int nIndex)
68*cdf0e10cSrcweir     {
69*cdf0e10cSrcweir         synchronized (maChildList)
70*cdf0e10cSrcweir         {
71*cdf0e10cSrcweir             return (AccessibleTreeNode)maChildList.get(nIndex);
72*cdf0e10cSrcweir         }
73*cdf0e10cSrcweir     }
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir     /** Remove the specified child from the list of children.
76*cdf0e10cSrcweir     */
77*cdf0e10cSrcweir     public boolean removeChild (AccessibleTreeNode aNode, int nIndex)
78*cdf0e10cSrcweir     {
79*cdf0e10cSrcweir         try
80*cdf0e10cSrcweir         {
81*cdf0e10cSrcweir             synchronized (maChildList)
82*cdf0e10cSrcweir             {
83*cdf0e10cSrcweir                 System.out.println ("    removing child at position " + nIndex + ": "
84*cdf0e10cSrcweir                     + maChildList.elementAt (nIndex));
85*cdf0e10cSrcweir                 maChildList.remove (nIndex);
86*cdf0e10cSrcweir             }
87*cdf0e10cSrcweir         }
88*cdf0e10cSrcweir         catch (Exception e)
89*cdf0e10cSrcweir         {
90*cdf0e10cSrcweir             return false;
91*cdf0e10cSrcweir         }
92*cdf0e10cSrcweir         return true;
93*cdf0e10cSrcweir     }
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir     public int indexOf (AccessibleTreeNode aNode)
96*cdf0e10cSrcweir     {
97*cdf0e10cSrcweir         synchronized (maChildList)
98*cdf0e10cSrcweir         {
99*cdf0e10cSrcweir             return maChildList.indexOf (aNode);
100*cdf0e10cSrcweir         }
101*cdf0e10cSrcweir     }
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir     /** Create a child object for the specified data.  This method is called
104*cdf0e10cSrcweir         usually from getChild and put there into the cache.
105*cdf0e10cSrcweir     */
106*cdf0e10cSrcweir     public abstract AccessibleTreeNode createChild (
107*cdf0e10cSrcweir         AccessibleTreeNode aParent, int nIndex);
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir     //
110*cdf0e10cSrcweir     // The following methods support editing of children and actions.
111*cdf0e10cSrcweir     // They have default implementations for no actions and read-only.
112*cdf0e10cSrcweir     //
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir     /** May this child be changed? */
115*cdf0e10cSrcweir     public boolean isChildEditable (AccessibleTreeNode aNode, int nIndex)
116*cdf0e10cSrcweir     {
117*cdf0e10cSrcweir         return false;
118*cdf0e10cSrcweir     }
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir     /** change this child's value */
121*cdf0e10cSrcweir     //    public void setChild(Object aObject, int nIndex) { }
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir     /** get names of suported actions */
125*cdf0e10cSrcweir     public String[] getActions (AccessibleTreeNode aNode)
126*cdf0e10cSrcweir     {
127*cdf0e10cSrcweir         return new String[] {};
128*cdf0e10cSrcweir     }
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir     /** perform action */
131*cdf0e10cSrcweir     public void performAction (AccessibleTreeNode aNode, int nIndex)
132*cdf0e10cSrcweir     {
133*cdf0e10cSrcweir     }
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir     /** Update all children.
136*cdf0e10cSrcweir     */
137*cdf0e10cSrcweir     public void update (AccessibleTreeNode aNode)
138*cdf0e10cSrcweir     {
139*cdf0e10cSrcweir     }
140*cdf0e10cSrcweir }
141