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 org.openoffice.accessibility.misc.NameProvider; 27 28 import javax.swing.tree.DefaultTreeModel; 29 import javax.swing.tree.DefaultMutableTreeNode; 30 31 import com.sun.star.awt.XExtendedToolkit; 32 import com.sun.star.accessibility.XAccessible; 33 import com.sun.star.accessibility.XAccessibleContext; 34 35 /** 36 * 37 */ 38 public class AccessibilityTree extends javax.swing.JTree { 39 40 /** Creates a new instance of AccessibilityTree */ AccessibilityTree(javax.swing.tree.TreeModel model)41 public AccessibilityTree(javax.swing.tree.TreeModel model) { 42 super(model); 43 // always show handles to indicate expandable / collapsable 44 showsRootHandles = true; 45 } 46 setToolkit(XExtendedToolkit xToolkit)47 public void setToolkit(XExtendedToolkit xToolkit) { 48 AccessibilityModel model = (AccessibilityModel) getModel(); 49 if (model != null) { 50 // hide the root node when connected 51 setRootVisible(xToolkit == null); 52 // update the root node 53 model.setRoot(xToolkit); 54 model.reload(); 55 } 56 } 57 convertValueToText(Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus)58 public String convertValueToText(Object value, boolean selected, 59 boolean expanded, boolean leaf, int row, boolean hasFocus) { 60 61 if (value instanceof DefaultMutableTreeNode) { 62 DefaultMutableTreeNode node = (DefaultMutableTreeNode) value; 63 64 Object userObject = node.getUserObject(); 65 if (userObject != null && userObject instanceof XAccessible) { 66 XAccessible xAccessible = (XAccessible) userObject; 67 try { 68 XAccessibleContext xAC = xAccessible.getAccessibleContext(); 69 if (xAC != null) { 70 String name = xAC.getAccessibleName(); 71 if (name.length() == 0) { 72 name = new String ("<no name>"); 73 } 74 value = name + " / " + NameProvider.getRoleName(xAC.getAccessibleRole()); 75 } 76 } catch (com.sun.star.uno.RuntimeException e) { 77 value = "???"; 78 } 79 } 80 } 81 82 return super.convertValueToText(value, selected, expanded, leaf, row, hasFocus); 83 } 84 85 } 86