1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 import com.sun.star.accessibility.XAccessibleContext;
23 
24 import java.util.HashMap;
25 
26 abstract class NodeMapCallback
27 {
Apply(AccTreeNode aNode)28     public abstract void Apply (AccTreeNode aNode);
29 }
30 
31 /** This map translates from XAccessible objects to our internal
32     representations.
33 */
34 class NodeMap
35 {
NodeMap()36     public NodeMap ()
37     {
38         maXAccessibleToNode = new HashMap ();
39     }
40 
41     /** Clear the whole map.
42     */
Clear()43     public void Clear ()
44     {
45         maXAccessibleToNode.clear();
46     }
47 
48     /** @return
49             whether the new node was different from a previous one
50             repspectively was the first one set.
51     */
InsertNode(XAccessibleContext xContext, AccessibleTreeNode aNode)52     public boolean InsertNode (XAccessibleContext xContext, AccessibleTreeNode aNode)
53     {
54         AccessibleTreeNode aPreviousNode = (AccessibleTreeNode)maXAccessibleToNode.put (
55             xContext,
56             aNode);
57         return aPreviousNode != aNode;
58     }
59 
RemoveNode(AccessibleTreeNode aNode)60     protected void RemoveNode (AccessibleTreeNode aNode)
61     {
62         try
63         {
64             if ((aNode != null) && (aNode instanceof AccTreeNode))
65             {
66                 maXAccessibleToNode.remove (((AccTreeNode)aNode).getContext());
67             }
68         }
69         catch (Exception e)
70         {
71             System.out.println ("caught exception while removing node "
72                 + aNode + " : " + e);
73             e.printStackTrace();
74         }
75     }
76 
77 
ForEach(NodeMapCallback aFunctor)78     public void ForEach (NodeMapCallback aFunctor)
79     {
80         Object[] aNodes = maXAccessibleToNode.values().toArray();
81         for (int i=0; i<aNodes.length; i++)
82         {
83             if (aNodes[i] != null && (aNodes[i] instanceof AccTreeNode))
84             {
85                 try
86                 {
87                     aFunctor.Apply ((AccTreeNode)aNodes[i]);
88                 }
89                 catch (Exception e)
90                 {
91                     System.out.println ("caught exception applying functor to "
92                         + i + "th node " + aNodes[i] + " : " + e);
93                     e.printStackTrace();
94                 }
95             }
96         }
97     }
98 
GetNode(XAccessibleContext xContext)99     AccessibleTreeNode GetNode (XAccessibleContext xContext)
100     {
101         return (AccessibleTreeNode)maXAccessibleToNode.get (xContext);
102     }
103 
GetNode(Object aObject)104     AccessibleTreeNode GetNode (Object aObject)
105     {
106         if (aObject instanceof XAccessibleContext)
107             return GetNode ((XAccessibleContext)aObject);
108         else
109             return null;
110     }
111 
GetAccessible(AccessibleTreeNode aNode)112     XAccessibleContext GetAccessible (AccessibleTreeNode aNode)
113     {
114         if ((aNode != null) && (aNode instanceof AccTreeNode))
115             return ((AccTreeNode)aNode).getContext();
116         else
117             return null;
118     }
119 
IsMember(XAccessibleContext xContext)120     boolean IsMember (XAccessibleContext xContext)
121     {
122         return maXAccessibleToNode.containsKey(xContext);
123     }
124 
ValueIsMember(AccessibleTreeNode aNode)125     boolean ValueIsMember (AccessibleTreeNode aNode)
126     {
127         return maXAccessibleToNode.containsValue(aNode);
128     }
129 
130 
131 
132     private HashMap maXAccessibleToNode;
133 }
134