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 23 24 package org.openoffice.accessibility.awb.tree; 25 26 import com.sun.star.accessibility.XAccessible; 27 import com.sun.star.awt.XExtendedToolkit; 28 import com.sun.star.lang.DisposedException; 29 import com.sun.star.lang.IndexOutOfBoundsException; 30 31 import org.openoffice.accessibility.misc.OfficeConnection; 32 import org.openoffice.accessibility.awb.event.EventQueue; 33 34 import javax.swing.tree.DefaultTreeModel; 35 import javax.swing.tree.TreeNode; 36 37 38 public class AccessibilityTreeModel 39 extends DefaultTreeModel 40 { AccessibilityTreeModel()41 public AccessibilityTreeModel () 42 { 43 super (null); 44 setAsksAllowsChildren (false); 45 46 SetRootNode(); 47 } 48 49 50 51 /** Release all resources. 52 */ Dispose()53 synchronized public void Dispose () 54 { 55 Clear (); 56 } 57 58 59 60 /** Calls to this method are dispatched to the given node but are 61 observed for exceptions. 62 */ isLeaf(Object aObject)63 synchronized public boolean isLeaf (Object aObject) 64 { 65 boolean bIsLeaf = true; 66 67 if (aObject != null) 68 { 69 AccessibilityNode aNode = (AccessibilityNode)aObject; 70 try 71 { 72 bIsLeaf = aNode.isLeaf(); 73 } 74 catch (DisposedException aException) 75 { 76 System.out.println ("node is disposed. removing it"); 77 /* TreeNode aParent = aNode.GetParent(); 78 int nIndexInParent = aParent.getIndex (aNode); 79 aNode.removeFromParent (); 80 System.out.println ("" + aParent + " # " + aNode + " # "+ nIndexInParent); 81 nodesWereRemoved ( 82 aParent, new int[]{nIndexInParent}, new 83 Object[]{aNode}); 84 */ 85 } 86 catch (Exception aException) 87 { 88 System.err.println ("caught exception in AccessibilityTreeModel.isLeaf():" 89 + aException); 90 aException.printStackTrace (System.err); 91 } 92 } 93 94 return bIsLeaf; 95 } 96 97 98 99 getChildCount(Object aObject)100 synchronized public int getChildCount (Object aObject) 101 { 102 AccessibilityNode aNode = (AccessibilityNode)aObject; 103 return aNode.getChildCount(); 104 } 105 106 107 108 109 /** Return the requested child of aParent. If that child is not yet 110 known to the parent then try to create it. 111 */ getChild(Object aParent, final int nIndex)112 synchronized public Object getChild (Object aParent, final int nIndex) 113 { 114 AccessibilityNode aChild = null; 115 116 final AccessibilityNode aParentNode = (AccessibilityNode)aParent; 117 118 // Try to get an existing child from the super class object. 119 aChild = aParentNode.GetChildNoCreate (nIndex); 120 121 // When the requested child does not yet exist and this node is not a 122 // special node then create a new node. 123 if (aChild == null) 124 { 125 aChild = aParentNode.CreateChild (nIndex); 126 aParentNode.SetChild ((AccessibilityNode)aChild, nIndex); 127 /* EventQueue.Instance().AddEvent (new Runnable() { public void run() { 128 AccessibilityTreeModel.this.nodeWasInserted ( 129 aParentNode, nIndex); 130 }}); 131 */ } 132 133 return aChild; 134 } 135 136 nodeWasInserted(AccessibilityNode aParent, int nIndex)137 synchronized public void nodeWasInserted (AccessibilityNode aParent, int nIndex) 138 { 139 nodesWereInserted (aParent, new int[]{nIndex}); 140 nodeStructureChanged (aParent); 141 142 } 143 144 145 146 147 /** Clear the tree so that afterwards it has only the root node. 148 */ Clear()149 public void Clear () 150 { 151 AccessibilityNode aRoot = (AccessibilityNode)getRoot(); 152 aRoot.RemoveAllChildren(); 153 SetRootNode(); 154 nodeStructureChanged (aRoot); 155 } 156 157 158 159 SetRootNode()160 private void SetRootNode () 161 { 162 OfficeConnection aConnection = OfficeConnection.Instance(); 163 AccessibilityNode aRoot; 164 if (aConnection!=null && aConnection.IsValid()) 165 aRoot = new AccessibilityNode ("<connected>"); 166 else 167 aRoot = new AccessibilityNode ("<not connected>"); 168 setRoot (aRoot); 169 } 170 171 172 173 174 /** Add a new child to the root node. 175 */ AddTopLevelNode(AccessibilityNode aNode)176 public synchronized void AddTopLevelNode (AccessibilityNode aNode) 177 { 178 if (aNode != null) 179 { 180 if ( ! OfficeConnection.Instance().IsValid()) 181 { 182 setRoot (null); 183 } 184 185 AccessibilityNode aRoot = (AccessibilityNode)getRoot(); 186 if (aRoot == null) 187 { 188 aRoot = new AccessibilityNode ("<connected>"); 189 setRoot (aRoot); 190 } 191 192 aNode.SetParent (aRoot); 193 aRoot.Append (aNode); 194 nodesWereInserted (aRoot, new int[]{aRoot.getIndex (aNode)}); 195 } 196 } 197 198 199 200 201 /** Remove a node that is a direct child of the root. 202 */ RemoveTopLevelNode(AccessibilityNode aNode)203 public synchronized void RemoveTopLevelNode (AccessibilityNode aNode) 204 { 205 AccessibilityNode aRoot = (AccessibilityNode)getRoot(); 206 if (aRoot != null) 207 { 208 int nIndex = aRoot.getIndex (aNode); 209 aRoot.Remove (aNode); 210 nodesWereRemoved (aRoot, new int[]{nIndex}, new Object[]{aNode}); 211 } 212 } 213 } 214