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 javax.swing.tree.TreeModel;
23 import javax.swing.event.TreeModelListener;
24 import javax.swing.tree.TreePath;
25 import javax.swing.event.TreeModelEvent;
26 import java.util.Vector;
27 
28 public class AccessibilityTreeModelBase
29     implements TreeModel
30 {
AccessibilityTreeModelBase()31     public AccessibilityTreeModelBase ()
32     {
33         setRoot (null);
34         maTMListeners = new Vector();
35     }
36 
addTreeModelListener(TreeModelListener l)37     public synchronized void addTreeModelListener(TreeModelListener l)
38     {
39         maTMListeners.add(l);
40     }
41 
removeTreeModelListener(TreeModelListener l)42     public synchronized void removeTreeModelListener(TreeModelListener l)
43     {
44         maTMListeners.remove(l);
45     }
46 
getChildCount(Object aParent)47     public synchronized int getChildCount(Object aParent)
48     {
49         return (aParent instanceof AccessibleTreeNode) ?
50             ((AccessibleTreeNode)aParent).getChildCount() : 0;
51     }
52 
getChild(Object aParent, int nIndex)53     public synchronized Object getChild (Object aParent, int nIndex)
54     {
55         Object aChild = null;
56         try
57         {
58             if (aParent != null && aParent instanceof AccessibleTreeNode)
59                 aChild = ((AccessibleTreeNode)aParent).getChild(nIndex);
60             else
61                 System.out.println ("getChild called for unknown parent node");
62         }
63         catch (com.sun.star.lang.IndexOutOfBoundsException e)
64         {
65             aChild = ("no child " + nIndex + " from " + aParent + ": " + e);
66         }
67         return aChild;
68     }
69 
getChildNoCreate(Object aParent, int nIndex)70     public synchronized Object getChildNoCreate (Object aParent, int nIndex)
71     {
72         Object aChild = null;
73         try
74         {
75             if (aParent != null && aParent instanceof AccessibleTreeNode)
76                 aChild = ((AccessibleTreeNode)aParent).getChildNoCreate(nIndex);
77             else
78                 System.out.println ("getChild called for unknown parent node");
79         }
80         catch (com.sun.star.lang.IndexOutOfBoundsException e)
81         { }
82         return aChild;
83     }
84 
85     /** iterate over all children and look for child */
getIndexOfChild(Object aParent, Object aChild)86     public synchronized int getIndexOfChild (Object aParent, Object aChild)
87     {
88         int nIndex = -1;
89         try
90         {
91             if ((aParent instanceof AccessibleTreeNode) && (aChild instanceof AccessibleTreeNode))
92             {
93                 AccessibleTreeNode aParentNode = (AccessibleTreeNode) aParent;
94                 AccessibleTreeNode aChildNode = (AccessibleTreeNode) aChild;
95 
96                 int nChildCount = aParentNode.getChildCount();
97                 for( int i = 0; i < nChildCount; i++ )
98                 {
99                     if (aChildNode.equals (aParentNode.getChild (i)))
100                     {
101                         nIndex = i;
102                         break;
103                     }
104                 }
105             }
106         }
107         catch (com.sun.star.lang.IndexOutOfBoundsException e)
108         {
109             // Return -1 by falling through.
110         }
111 
112         // not found?
113         return nIndex;
114     }
115 
isLeaf(Object aNode)116     public boolean isLeaf (Object aNode)
117     {
118         return (aNode instanceof AccessibleTreeNode) ?
119             ((AccessibleTreeNode)aNode).isLeaf() : true;
120     }
121 
122 
123 
getRoot()124     public synchronized Object getRoot()
125     {
126         return maRoot;
127     }
128 
valueForPathChanged(TreePath path, Object newValue)129     public void valueForPathChanged(TreePath path, Object newValue)
130     { }
131 
setRoot(AccessibleTreeNode aRoot)132     protected synchronized void setRoot (AccessibleTreeNode aRoot)
133     {
134         maRoot = aRoot;
135     }
136 
137 
138     // The list of TreeModelListener objects.
139     protected Vector maTMListeners;
140 
141     // The root node of the tree.  Use setRoot to change it.
142     private AccessibleTreeNode maRoot = null;
143 }
144