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