1*cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleContext;
2*cdf0e10cSrcweir 
3*cdf0e10cSrcweir import java.util.HashMap;
4*cdf0e10cSrcweir 
5*cdf0e10cSrcweir abstract class NodeMapCallback
6*cdf0e10cSrcweir {
7*cdf0e10cSrcweir     public abstract void Apply (AccTreeNode aNode);
8*cdf0e10cSrcweir }
9*cdf0e10cSrcweir 
10*cdf0e10cSrcweir /** This map translates from XAccessible objects to our internal
11*cdf0e10cSrcweir     representations.
12*cdf0e10cSrcweir */
13*cdf0e10cSrcweir class NodeMap
14*cdf0e10cSrcweir {
15*cdf0e10cSrcweir     public NodeMap ()
16*cdf0e10cSrcweir     {
17*cdf0e10cSrcweir         maXAccessibleToNode = new HashMap ();
18*cdf0e10cSrcweir     }
19*cdf0e10cSrcweir 
20*cdf0e10cSrcweir     /** Clear the whole map.
21*cdf0e10cSrcweir     */
22*cdf0e10cSrcweir     public void Clear ()
23*cdf0e10cSrcweir     {
24*cdf0e10cSrcweir         maXAccessibleToNode.clear();
25*cdf0e10cSrcweir     }
26*cdf0e10cSrcweir 
27*cdf0e10cSrcweir     /** @return
28*cdf0e10cSrcweir             whether the new node was different from a previous one
29*cdf0e10cSrcweir             repspectively was the first one set.
30*cdf0e10cSrcweir     */
31*cdf0e10cSrcweir     public boolean InsertNode (XAccessibleContext xContext, AccessibleTreeNode aNode)
32*cdf0e10cSrcweir     {
33*cdf0e10cSrcweir         AccessibleTreeNode aPreviousNode = (AccessibleTreeNode)maXAccessibleToNode.put (
34*cdf0e10cSrcweir             xContext,
35*cdf0e10cSrcweir             aNode);
36*cdf0e10cSrcweir         return aPreviousNode != aNode;
37*cdf0e10cSrcweir     }
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir     protected void RemoveNode (AccessibleTreeNode aNode)
40*cdf0e10cSrcweir     {
41*cdf0e10cSrcweir         try
42*cdf0e10cSrcweir         {
43*cdf0e10cSrcweir             if ((aNode != null) && (aNode instanceof AccTreeNode))
44*cdf0e10cSrcweir             {
45*cdf0e10cSrcweir                 maXAccessibleToNode.remove (((AccTreeNode)aNode).getContext());
46*cdf0e10cSrcweir             }
47*cdf0e10cSrcweir         }
48*cdf0e10cSrcweir         catch (Exception e)
49*cdf0e10cSrcweir         {
50*cdf0e10cSrcweir             System.out.println ("caught exception while removing node "
51*cdf0e10cSrcweir                 + aNode + " : " + e);
52*cdf0e10cSrcweir             e.printStackTrace();
53*cdf0e10cSrcweir         }
54*cdf0e10cSrcweir     }
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir     public void ForEach (NodeMapCallback aFunctor)
58*cdf0e10cSrcweir     {
59*cdf0e10cSrcweir         Object[] aNodes = maXAccessibleToNode.values().toArray();
60*cdf0e10cSrcweir         for (int i=0; i<aNodes.length; i++)
61*cdf0e10cSrcweir         {
62*cdf0e10cSrcweir             if (aNodes[i] != null && (aNodes[i] instanceof AccTreeNode))
63*cdf0e10cSrcweir             {
64*cdf0e10cSrcweir                 try
65*cdf0e10cSrcweir                 {
66*cdf0e10cSrcweir                     aFunctor.Apply ((AccTreeNode)aNodes[i]);
67*cdf0e10cSrcweir                 }
68*cdf0e10cSrcweir                 catch (Exception e)
69*cdf0e10cSrcweir                 {
70*cdf0e10cSrcweir                     System.out.println ("caught exception applying functor to "
71*cdf0e10cSrcweir                         + i + "th node " + aNodes[i] + " : " + e);
72*cdf0e10cSrcweir                     e.printStackTrace();
73*cdf0e10cSrcweir                 }
74*cdf0e10cSrcweir             }
75*cdf0e10cSrcweir         }
76*cdf0e10cSrcweir     }
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir     AccessibleTreeNode GetNode (XAccessibleContext xContext)
79*cdf0e10cSrcweir     {
80*cdf0e10cSrcweir         return (AccessibleTreeNode)maXAccessibleToNode.get (xContext);
81*cdf0e10cSrcweir     }
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir     AccessibleTreeNode GetNode (Object aObject)
84*cdf0e10cSrcweir     {
85*cdf0e10cSrcweir         if (aObject instanceof XAccessibleContext)
86*cdf0e10cSrcweir             return GetNode ((XAccessibleContext)aObject);
87*cdf0e10cSrcweir         else
88*cdf0e10cSrcweir             return null;
89*cdf0e10cSrcweir     }
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir     XAccessibleContext GetAccessible (AccessibleTreeNode aNode)
92*cdf0e10cSrcweir     {
93*cdf0e10cSrcweir         if ((aNode != null) && (aNode instanceof AccTreeNode))
94*cdf0e10cSrcweir             return ((AccTreeNode)aNode).getContext();
95*cdf0e10cSrcweir         else
96*cdf0e10cSrcweir             return null;
97*cdf0e10cSrcweir     }
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir     boolean IsMember (XAccessibleContext xContext)
100*cdf0e10cSrcweir     {
101*cdf0e10cSrcweir         return maXAccessibleToNode.containsKey(xContext);
102*cdf0e10cSrcweir     }
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir     boolean ValueIsMember (AccessibleTreeNode aNode)
105*cdf0e10cSrcweir     {
106*cdf0e10cSrcweir         return maXAccessibleToNode.containsValue(aNode);
107*cdf0e10cSrcweir     }
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir     private HashMap maXAccessibleToNode;
112*cdf0e10cSrcweir }
113