Branch libreoffice-5-0-4
[LibreOffice.git] / toolkit / test / accessibility / NodeMap.java
blobb8e8b3bf3300661e1cbaa849b52a78c439765195
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 import com.sun.star.accessibility.XAccessibleContext;
21 import java.util.HashMap;
23 abstract class NodeMapCallback
25 public abstract void Apply (AccTreeNode aNode);
28 /** This map translates from XAccessible objects to our internal
29 representations.
31 class NodeMap
33 public NodeMap ()
35 maXAccessibleToNode = new HashMap<XAccessibleContext, AccessibleTreeNode> ();
38 /** Clear the whole map.
40 public void Clear ()
42 maXAccessibleToNode.clear();
45 /** @return
46 whether the new node was different from a previous one
47 repspectively was the first one set.
49 public boolean InsertNode (XAccessibleContext xContext, AccessibleTreeNode aNode)
51 AccessibleTreeNode aPreviousNode = maXAccessibleToNode.put (
52 xContext,
53 aNode);
54 return aPreviousNode != aNode;
57 protected void RemoveNode (AccessibleTreeNode aNode)
59 try
61 if ((aNode != null) && (aNode instanceof AccTreeNode))
63 maXAccessibleToNode.remove (((AccTreeNode)aNode).getContext());
66 catch (Exception e)
68 System.out.println ("caught exception while removing node "
69 + aNode + " : " + e);
70 e.printStackTrace();
75 public void ForEach (NodeMapCallback aFunctor)
77 Object[] aNodes = maXAccessibleToNode.values().toArray();
78 for (int i=0; i<aNodes.length; i++)
80 if (aNodes[i] != null && (aNodes[i] instanceof AccTreeNode))
82 try
84 aFunctor.Apply ((AccTreeNode)aNodes[i]);
86 catch (Exception e)
88 System.out.println ("caught exception applying functor to "
89 + i + "th node " + aNodes[i] + " : " + e);
90 e.printStackTrace();
96 AccessibleTreeNode GetNode (XAccessibleContext xContext)
98 return maXAccessibleToNode.get (xContext);
101 AccessibleTreeNode GetNode (Object aObject)
103 if (aObject instanceof XAccessibleContext)
104 return GetNode ((XAccessibleContext)aObject);
105 else
106 return null;
109 boolean ValueIsMember (AccessibleTreeNode aNode)
111 return maXAccessibleToNode.containsValue(aNode);
116 private final HashMap<XAccessibleContext, AccessibleTreeNode> maXAccessibleToNode;