1*1b0aaa91SAndrew Rist /************************************************************** 2*1b0aaa91SAndrew Rist * 3*1b0aaa91SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*1b0aaa91SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*1b0aaa91SAndrew Rist * distributed with this work for additional information 6*1b0aaa91SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*1b0aaa91SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*1b0aaa91SAndrew Rist * "License"); you may not use this file except in compliance 9*1b0aaa91SAndrew Rist * with the License. You may obtain a copy of the License at 10*1b0aaa91SAndrew Rist * 11*1b0aaa91SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*1b0aaa91SAndrew Rist * 13*1b0aaa91SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*1b0aaa91SAndrew Rist * software distributed under the License is distributed on an 15*1b0aaa91SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*1b0aaa91SAndrew Rist * KIND, either express or implied. See the License for the 17*1b0aaa91SAndrew Rist * specific language governing permissions and limitations 18*1b0aaa91SAndrew Rist * under the License. 19*1b0aaa91SAndrew Rist * 20*1b0aaa91SAndrew Rist *************************************************************/ 21*1b0aaa91SAndrew Rist 22cdf0e10cSrcweir import javax.swing.tree.TreeModel; 23cdf0e10cSrcweir import javax.swing.event.TreeModelListener; 24cdf0e10cSrcweir import javax.swing.tree.TreePath; 25cdf0e10cSrcweir import javax.swing.event.TreeModelEvent; 26cdf0e10cSrcweir import java.util.Vector; 27cdf0e10cSrcweir 28cdf0e10cSrcweir public class AccessibilityTreeModelBase 29cdf0e10cSrcweir implements TreeModel 30cdf0e10cSrcweir { AccessibilityTreeModelBase()31cdf0e10cSrcweir public AccessibilityTreeModelBase () 32cdf0e10cSrcweir { 33cdf0e10cSrcweir setRoot (null); 34cdf0e10cSrcweir maTMListeners = new Vector(); 35cdf0e10cSrcweir } 36cdf0e10cSrcweir addTreeModelListener(TreeModelListener l)37cdf0e10cSrcweir public synchronized void addTreeModelListener(TreeModelListener l) 38cdf0e10cSrcweir { 39cdf0e10cSrcweir maTMListeners.add(l); 40cdf0e10cSrcweir } 41cdf0e10cSrcweir removeTreeModelListener(TreeModelListener l)42cdf0e10cSrcweir public synchronized void removeTreeModelListener(TreeModelListener l) 43cdf0e10cSrcweir { 44cdf0e10cSrcweir maTMListeners.remove(l); 45cdf0e10cSrcweir } 46cdf0e10cSrcweir getChildCount(Object aParent)47cdf0e10cSrcweir public synchronized int getChildCount(Object aParent) 48cdf0e10cSrcweir { 49cdf0e10cSrcweir return (aParent instanceof AccessibleTreeNode) ? 50cdf0e10cSrcweir ((AccessibleTreeNode)aParent).getChildCount() : 0; 51cdf0e10cSrcweir } 52cdf0e10cSrcweir getChild(Object aParent, int nIndex)53cdf0e10cSrcweir public synchronized Object getChild (Object aParent, int nIndex) 54cdf0e10cSrcweir { 55cdf0e10cSrcweir Object aChild = null; 56cdf0e10cSrcweir try 57cdf0e10cSrcweir { 58cdf0e10cSrcweir if (aParent != null && aParent instanceof AccessibleTreeNode) 59cdf0e10cSrcweir aChild = ((AccessibleTreeNode)aParent).getChild(nIndex); 60cdf0e10cSrcweir else 61cdf0e10cSrcweir System.out.println ("getChild called for unknown parent node"); 62cdf0e10cSrcweir } 63cdf0e10cSrcweir catch (com.sun.star.lang.IndexOutOfBoundsException e) 64cdf0e10cSrcweir { 65cdf0e10cSrcweir aChild = ("no child " + nIndex + " from " + aParent + ": " + e); 66cdf0e10cSrcweir } 67cdf0e10cSrcweir return aChild; 68cdf0e10cSrcweir } 69cdf0e10cSrcweir getChildNoCreate(Object aParent, int nIndex)70cdf0e10cSrcweir public synchronized Object getChildNoCreate (Object aParent, int nIndex) 71cdf0e10cSrcweir { 72cdf0e10cSrcweir Object aChild = null; 73cdf0e10cSrcweir try 74cdf0e10cSrcweir { 75cdf0e10cSrcweir if (aParent != null && aParent instanceof AccessibleTreeNode) 76cdf0e10cSrcweir aChild = ((AccessibleTreeNode)aParent).getChildNoCreate(nIndex); 77cdf0e10cSrcweir else 78cdf0e10cSrcweir System.out.println ("getChild called for unknown parent node"); 79cdf0e10cSrcweir } 80cdf0e10cSrcweir catch (com.sun.star.lang.IndexOutOfBoundsException e) 81cdf0e10cSrcweir { } 82cdf0e10cSrcweir return aChild; 83cdf0e10cSrcweir } 84cdf0e10cSrcweir 85cdf0e10cSrcweir /** iterate over all children and look for child */ getIndexOfChild(Object aParent, Object aChild)86cdf0e10cSrcweir public synchronized int getIndexOfChild (Object aParent, Object aChild) 87cdf0e10cSrcweir { 88cdf0e10cSrcweir int nIndex = -1; 89cdf0e10cSrcweir try 90cdf0e10cSrcweir { 91cdf0e10cSrcweir if ((aParent instanceof AccessibleTreeNode) && (aChild instanceof AccessibleTreeNode)) 92cdf0e10cSrcweir { 93cdf0e10cSrcweir AccessibleTreeNode aParentNode = (AccessibleTreeNode) aParent; 94cdf0e10cSrcweir AccessibleTreeNode aChildNode = (AccessibleTreeNode) aChild; 95cdf0e10cSrcweir 96cdf0e10cSrcweir int nChildCount = aParentNode.getChildCount(); 97cdf0e10cSrcweir for( int i = 0; i < nChildCount; i++ ) 98cdf0e10cSrcweir { 99cdf0e10cSrcweir if (aChildNode.equals (aParentNode.getChild (i))) 100cdf0e10cSrcweir { 101cdf0e10cSrcweir nIndex = i; 102cdf0e10cSrcweir break; 103cdf0e10cSrcweir } 104cdf0e10cSrcweir } 105cdf0e10cSrcweir } 106cdf0e10cSrcweir } 107cdf0e10cSrcweir catch (com.sun.star.lang.IndexOutOfBoundsException e) 108cdf0e10cSrcweir { 109cdf0e10cSrcweir // Return -1 by falling through. 110cdf0e10cSrcweir } 111cdf0e10cSrcweir 112cdf0e10cSrcweir // not found? 113cdf0e10cSrcweir return nIndex; 114cdf0e10cSrcweir } 115cdf0e10cSrcweir isLeaf(Object aNode)116cdf0e10cSrcweir public boolean isLeaf (Object aNode) 117cdf0e10cSrcweir { 118cdf0e10cSrcweir return (aNode instanceof AccessibleTreeNode) ? 119cdf0e10cSrcweir ((AccessibleTreeNode)aNode).isLeaf() : true; 120cdf0e10cSrcweir } 121cdf0e10cSrcweir 122cdf0e10cSrcweir 123cdf0e10cSrcweir getRoot()124cdf0e10cSrcweir public synchronized Object getRoot() 125cdf0e10cSrcweir { 126cdf0e10cSrcweir return maRoot; 127cdf0e10cSrcweir } 128cdf0e10cSrcweir valueForPathChanged(TreePath path, Object newValue)129cdf0e10cSrcweir public void valueForPathChanged(TreePath path, Object newValue) 130cdf0e10cSrcweir { } 131cdf0e10cSrcweir setRoot(AccessibleTreeNode aRoot)132cdf0e10cSrcweir protected synchronized void setRoot (AccessibleTreeNode aRoot) 133cdf0e10cSrcweir { 134cdf0e10cSrcweir maRoot = aRoot; 135cdf0e10cSrcweir } 136cdf0e10cSrcweir 137cdf0e10cSrcweir 138cdf0e10cSrcweir // The list of TreeModelListener objects. 139cdf0e10cSrcweir protected Vector maTMListeners; 140cdf0e10cSrcweir 141cdf0e10cSrcweir // The root node of the tree. Use setRoot to change it. 142cdf0e10cSrcweir private AccessibleTreeNode maRoot = null; 143cdf0e10cSrcweir } 144