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 javax.swing.SwingUtilities; 27 import javax.swing.tree.TreeNode; 28 import javax.swing.tree.MutableTreeNode; 29 import javax.swing.tree.DefaultMutableTreeNode; 30 31 import com.sun.star.uno.UnoRuntime; 32 import com.sun.star.awt.XExtendedToolkit; 33 import com.sun.star.awt.XTopWindow; 34 import com.sun.star.awt.XTopWindowListener; 35 import com.sun.star.accessibility.AccessibleRole; 36 import com.sun.star.accessibility.XAccessible; 37 import com.sun.star.accessibility.XAccessibleContext; 38 import com.sun.star.accessibility.XAccessibleEventBroadcaster; 39 import com.sun.star.accessibility.XAccessibleEventListener; 40 41 /** 42 * 43 */ 44 public abstract class AccessibilityModel extends javax.swing.tree.DefaultTreeModel { 45 46 protected java.util.Hashtable nodeList; 47 protected static DefaultMutableTreeNode disconnectedRootNode = 48 new DefaultMutableTreeNode("<not connected>"); 49 50 /** Creates a new instance of AccessibilityModel */ AccessibilityModel()51 public AccessibilityModel() { 52 super(disconnectedRootNode, false); 53 nodeList = new java.util.Hashtable(); 54 } 55 56 /* Convenience method that creates a new Toolkit node from xToolkit 57 * and sets as the new root object of the tree. 58 */ setRoot(XExtendedToolkit xToolkit)59 public synchronized void setRoot(XExtendedToolkit xToolkit) { 60 if (xToolkit != null) { 61 try { 62 // remove old root node as topwindow listener 63 if (getRoot() instanceof ToolkitNode) { 64 ToolkitNode tn = (ToolkitNode) getRoot(); 65 if (tn.xToolkit != null) { 66 tn.xToolkit.removeTopWindowListener(tn); 67 } 68 } 69 nodeList.clear(); 70 setRoot(new ToolkitNode(xToolkit, this)); 71 xToolkit.addTopWindowListener((ToolkitNode) getRoot()); 72 } catch (com.sun.star.uno.RuntimeException e) { 73 // FIXME: error message ! 74 } 75 } 76 } 77 78 /* Appends the new child to parent's child list */ addNodeInto(MutableTreeNode newChild, MutableTreeNode parent)79 public void addNodeInto(MutableTreeNode newChild, MutableTreeNode parent) { 80 int index = parent.getChildCount(); 81 if (newChild != null && newChild.getParent() == parent) { 82 index -= 1; 83 } 84 insertNodeInto(newChild, parent, index); 85 } 86 87 /** Adds listener to the listener chain of node */ addEventListener(TreeNode node, XAccessibleEventListener listener)88 public static void addEventListener(TreeNode node, XAccessibleEventListener listener) { 89 if (node instanceof AccessibilityNode) { 90 ((AccessibilityNode) node).addEventListener(listener); 91 } 92 } 93 94 /** Removes listener from the listener chain of node */ removeEventListener(TreeNode node, XAccessibleEventListener listener)95 public static void removeEventListener(TreeNode node, XAccessibleEventListener listener) { 96 if (node instanceof AccessibilityNode) { 97 ((AccessibilityNode) node).removeEventListener(listener); 98 } 99 } 100 createWindowNode(XAccessible xAccessible, XAccessibleContext xAccessibleContext)101 protected abstract AccessibilityNode createWindowNode(XAccessible xAccessible, 102 XAccessibleContext xAccessibleContext); createNode(XAccessible xAccessible)103 protected abstract AccessibilityNode createNode(XAccessible xAccessible); 104 105 /** Adds xAccessible,node to the internal hashtable */ putNode(XAccessible xAccessible, AccessibilityNode node)106 public AccessibilityNode putNode(XAccessible xAccessible, AccessibilityNode node) { 107 if (xAccessible != null) { 108 String oid = UnoRuntime.generateOid(xAccessible); 109 java.lang.ref.WeakReference ref = (java.lang.ref.WeakReference) 110 nodeList.put(oid, new java.lang.ref.WeakReference(node)); 111 if (ref != null) { 112 return (AccessibilityNode) ref.get(); 113 } 114 } 115 return null; 116 } 117 118 /** Returns the AccessibilityNode for xAccessible */ findNode(XAccessible xAccessible)119 public AccessibilityNode findNode(XAccessible xAccessible) { 120 if (xAccessible != null) { 121 String oid = UnoRuntime.generateOid(xAccessible); 122 java.lang.ref.WeakReference ref = 123 (java.lang.ref.WeakReference) nodeList.get(oid); 124 if (ref != null) { 125 return (AccessibilityNode) ref.get(); 126 } 127 } 128 return null; 129 } 130 131 /** Removes the AccessibilityNode for xAccessible from the internal hashtable */ removeNode(XAccessible xAccessible)132 public AccessibilityNode removeNode(XAccessible xAccessible) { 133 if (xAccessible != null) { 134 String oid = UnoRuntime.generateOid(xAccessible); 135 java.lang.ref.WeakReference ref = 136 (java.lang.ref.WeakReference) nodeList.remove(oid); 137 if (ref != null) { 138 return (AccessibilityNode) ref.get(); 139 } 140 } 141 return null; 142 } 143 removeNode(Object o)144 public AccessibilityNode removeNode(Object o) { 145 if (o instanceof XAccessible) { 146 return removeNode((XAccessible) o); 147 } 148 return null; 149 } 150 } 151