1 /* 2 * This file is part of the LibreOffice project. 3 * 4 * This Source Code Form is subject to the terms of the Mozilla Public 5 * License, v. 2.0. If a copy of the MPL was not distributed with this 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 * 8 * This file incorporates work covered by the following license notice: 9 * 10 * Licensed to the Apache Software Foundation (ASF) under one or more 11 * contributor license agreements. See the NOTICE file distributed 12 * with this work for additional information regarding copyright 13 * ownership. The ASF licenses this file to you under the Apache 14 * License, Version 2.0 (the "License"); you may not use this file 15 * except in compliance with the License. You may obtain a copy of 16 * the License at http://www.apache.org/licenses/LICENSE-2.0 . 17 */ 18 19 package util; 20 21 import java.io.PrintWriter; 22 import java.util.ArrayList; 23 24 import com.sun.star.accessibility.AccessibleRole; 25 import com.sun.star.accessibility.XAccessible; 26 import com.sun.star.accessibility.XAccessibleAction; 27 import com.sun.star.accessibility.XAccessibleContext; 28 import com.sun.star.accessibility.XAccessibleEditableText; 29 import com.sun.star.accessibility.XAccessibleText; 30 import com.sun.star.accessibility.XAccessibleValue; 31 import com.sun.star.awt.XWindow; 32 import com.sun.star.uno.UnoRuntime; 33 import com.sun.star.uno.XInterface; 34 35 /** 36 * This class supports some functions to handle easily accessible objects 37 */ 38 public class UITools { 39 40 private final XAccessible mXRoot; 41 UITools(XWindow xWindow)42 public UITools(XWindow xWindow) 43 { 44 mXRoot = makeRoot(xWindow); 45 } 46 getString(XInterface xInt)47 private static String getString(XInterface xInt) 48 { 49 XAccessibleText oText = UnoRuntime.queryInterface(XAccessibleText.class, xInt); 50 return oText.getText(); 51 } 52 setString(XInterface xInt, String cText)53 private static void setString(XInterface xInt, String cText) 54 { 55 XAccessibleEditableText oText = UnoRuntime.queryInterface(XAccessibleEditableText.class, xInt); 56 57 oText.setText(cText); 58 } 59 makeRoot(XWindow xWindow)60 private static XAccessible makeRoot(XWindow xWindow) 61 { 62 return AccessibilityTools.getAccessibleObject(xWindow); 63 } 64 65 /** 66 * get the root element of the accessible tree 67 * @return the root element 68 */ getRoot()69 public XAccessible getRoot() 70 { 71 return mXRoot; 72 } 73 74 /** 75 * Helper method: set a text into AccessibleEdit field 76 * @param textfiledName is the name of the text field 77 * @param stringToSet is the string to set 78 * @throws java.lang.Exception if something fail 79 */ setTextEditFiledText(String textfiledName, String stringToSet)80 public void setTextEditFiledText(String textfiledName, String stringToSet) 81 throws java.lang.Exception 82 { 83 XInterface oTextField = AccessibilityTools.getAccessibleObjectForRole(mXRoot, 84 AccessibleRole.TEXT, textfiledName); 85 setString(oTextField, stringToSet); 86 } 87 88 /** 89 * returns the button by the given name 90 * @param buttonName is the name of the button to get 91 * @return a XAccessibleContext of the button 92 * @throws java.lang.Exception if something fail 93 */ getButton(String buttonName)94 public XAccessibleContext getButton(String buttonName) throws java.lang.Exception 95 { 96 return AccessibilityTools.getAccessibleObjectForRole 97 (mXRoot, AccessibleRole.PUSH_BUTTON, buttonName); 98 } 99 100 /** 101 * Helper method: gets button via accessibility and 'click' it</code> 102 * @param buttonName is the name of the button to click 103 * @throws java.lang.Exception if something fail 104 */ clickButton(String buttonName)105 public void clickButton(String buttonName) throws java.lang.Exception 106 { 107 108 XAccessibleContext oButton =AccessibilityTools.getAccessibleObjectForRole 109 (mXRoot, AccessibleRole.PUSH_BUTTON, buttonName); 110 if (oButton == null){ 111 throw new Exception("Could not get button '" + buttonName + "'"); 112 } 113 XAccessibleAction oAction = UnoRuntime.queryInterface(XAccessibleAction.class, oButton); 114 115 // "click" the button 116 try{ 117 oAction.doAccessibleAction(0); 118 } catch (com.sun.star.lang.IndexOutOfBoundsException e) { 119 throw new Exception("Could not do accessible action with '" + 120 buttonName + "'", e); 121 } 122 } 123 124 /** 125 * Helper method: returns the entry manes of a List-Box 126 * @param ListBoxName the name of the listbox 127 * @return the listbox entry names 128 * @throws java.lang.Exception if something fail 129 */ getListBoxItems(String ListBoxName)130 public String[] getListBoxItems(String ListBoxName) 131 throws java.lang.Exception 132 { 133 ArrayList<String> Items = new ArrayList<String>(); 134 try { 135 XAccessibleContext xListBox = null; 136 XAccessibleContext xList = null; 137 138 xListBox =AccessibilityTools.getAccessibleObjectForRole(mXRoot, 139 AccessibleRole.COMBO_BOX, ListBoxName); 140 if (xListBox == null){ 141 xListBox =AccessibilityTools.getAccessibleObjectForRole(mXRoot, 142 AccessibleRole.PANEL, ListBoxName); 143 } 144 145 if (xListBox == null){ 146 // get the list of TreeListBox 147 xList =AccessibilityTools.getAccessibleObjectForRole(mXRoot, 148 AccessibleRole.TREE, ListBoxName); 149 150 // all other list boxes have a children of kind of LIST 151 } else { 152 153 XAccessible xListBoxAccess = UnoRuntime.queryInterface(XAccessible.class, xListBox); 154 // if a List is not pulled to be open all entries are not visible, therefore the 155 // boolean argument 156 xList =AccessibilityTools.getAccessibleObjectForRole( 157 xListBoxAccess, AccessibleRole.LIST, true); 158 } 159 160 for (int i=0;i<xList.getAccessibleChildCount();i++) { 161 try { 162 XAccessible xChild = xList.getAccessibleChild(i); 163 XAccessibleContext xChildCont = 164 xChild.getAccessibleContext(); 165 XInterface xChildInterface = UnoRuntime.queryInterface(XInterface.class, xChildCont); 166 Items.add(getString(xChildInterface)); 167 168 } catch (com.sun.star.lang.IndexOutOfBoundsException e) { 169 throw new Exception("Could not get child form list of '" 170 + ListBoxName + "'", e); 171 } 172 } 173 174 } catch (Exception e) { 175 throw new Exception("Could not get list of items from '" 176 + ListBoxName + "'", e); 177 } 178 String[]ret = new String[Items.size()]; 179 return Items.toArray(ret); 180 } 181 182 /** 183 * set a value to a named check box 184 * @param CheckBoxName the name of the check box 185 * @param Value the value to set 186 *<ul> 187 * <li>0: not checked </li> 188 * <li>1: checked </li> 189 * <li>2: don't know </li> 190 *</ul> 191 * @throws java.lang.Exception if something fail 192 */ setCheckBoxValue(String CheckBoxName, Integer Value)193 public void setCheckBoxValue(String CheckBoxName, Integer Value) 194 throws java.lang.Exception 195 { 196 try { 197 XInterface xCheckBox =AccessibilityTools.getAccessibleObjectForRole(mXRoot, 198 AccessibleRole.CHECK_BOX, CheckBoxName); 199 XAccessibleValue xCheckBoxValue = UnoRuntime.queryInterface(XAccessibleValue.class, xCheckBox); 200 xCheckBoxValue.setCurrentValue(Value); 201 202 } catch (Exception e) { 203 throw new Exception("Could not set value to CheckBox '" 204 + CheckBoxName + "'", e); 205 } 206 } 207 208 /** 209 * Prints the accessible tree to the <CODE>logWriter</CODE> only if <CODE>debugIsActive</CODE> 210 * is set to <CODE>true</CODE> 211 * @param log logWriter 212 * @param debugIsActive prints only if this parameter is set to TRUE 213 */ printAccessibleTree(PrintWriter log, boolean debugIsActive)214 public void printAccessibleTree(PrintWriter log, boolean debugIsActive) { 215 AccessibilityTools.printAccessibleTree(log, mXRoot, debugIsActive); 216 } 217 218 } 219