2 * This file is part of the LibreOffice project.
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/.
8 * This file incorporates work covered by the following license notice:
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 .
21 import java
.io
.PrintWriter
;
22 import java
.util
.ArrayList
;
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
;
36 * This class supports some functions to handle easily accessible objects
38 public class UITools
{
40 private final XAccessible mXRoot
;
42 public UITools(XWindow xWindow
)
44 mXRoot
= makeRoot(xWindow
);
47 private static String
getString(XInterface xInt
)
49 XAccessibleText oText
= UnoRuntime
.queryInterface(XAccessibleText
.class, xInt
);
50 return oText
.getText();
53 private static void setString(XInterface xInt
, String cText
)
55 XAccessibleEditableText oText
= UnoRuntime
.queryInterface(XAccessibleEditableText
.class, xInt
);
60 private static XAccessible
makeRoot(XWindow xWindow
)
62 return AccessibilityTools
.getAccessibleObject(xWindow
);
66 * get the root element of the accessible tree
67 * @return the root element
69 public XAccessible
getRoot()
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
80 public void setTextEditFiledText(String textfiledName
, String stringToSet
)
81 throws java
.lang
.Exception
83 XInterface oTextField
= AccessibilityTools
.getAccessibleObjectForRole(mXRoot
,
84 AccessibleRole
.TEXT
, textfiledName
);
85 setString(oTextField
, stringToSet
);
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
94 public XAccessibleContext
getButton(String buttonName
) throws java
.lang
.Exception
96 return AccessibilityTools
.getAccessibleObjectForRole
97 (mXRoot
, AccessibleRole
.PUSH_BUTTON
, buttonName
);
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
105 public void clickButton(String buttonName
) throws java
.lang
.Exception
108 XAccessibleContext oButton
=AccessibilityTools
.getAccessibleObjectForRole
109 (mXRoot
, AccessibleRole
.PUSH_BUTTON
, buttonName
);
110 if (oButton
== null){
111 throw new Exception("Could not get button '" + buttonName
+ "'");
113 XAccessibleAction oAction
= UnoRuntime
.queryInterface(XAccessibleAction
.class, oButton
);
115 // "click" the button
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
);
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
130 public String
[] getListBoxItems(String ListBoxName
)
131 throws java
.lang
.Exception
133 ArrayList
<String
> Items
= new ArrayList
<String
>();
135 XAccessibleContext xListBox
= null;
136 XAccessibleContext xList
= null;
138 xListBox
=AccessibilityTools
.getAccessibleObjectForRole(mXRoot
,
139 AccessibleRole
.COMBO_BOX
, ListBoxName
);
140 if (xListBox
== null){
141 xListBox
=AccessibilityTools
.getAccessibleObjectForRole(mXRoot
,
142 AccessibleRole
.PANEL
, ListBoxName
);
145 if (xListBox
== null){
146 // get the list of TreeListBox
147 xList
=AccessibilityTools
.getAccessibleObjectForRole(mXRoot
,
148 AccessibleRole
.TREE
, ListBoxName
);
150 // all other list boxes have a children of kind of LIST
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
156 xList
=AccessibilityTools
.getAccessibleObjectForRole(
157 xListBoxAccess
, AccessibleRole
.LIST
, true);
160 for (long i
=0;i
<xList
.getAccessibleChildCount();i
++) {
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
));
168 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
169 throw new Exception("Could not get child form list of '"
170 + ListBoxName
+ "'", e
);
174 } catch (Exception e
) {
175 throw new Exception("Could not get list of items from '"
176 + ListBoxName
+ "'", e
);
178 String
[]ret
= new String
[Items
.size()];
179 return Items
.toArray(ret
);
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
187 * <li>0: not checked </li>
188 * <li>1: checked </li>
189 * <li>2: don't know </li>
191 * @throws java.lang.Exception if something fail
193 public void setCheckBoxValue(String CheckBoxName
, Integer Value
)
194 throws java
.lang
.Exception
197 XInterface xCheckBox
=AccessibilityTools
.getAccessibleObjectForRole(mXRoot
,
198 AccessibleRole
.CHECK_BOX
, CheckBoxName
);
199 XAccessibleValue xCheckBoxValue
= UnoRuntime
.queryInterface(XAccessibleValue
.class, xCheckBox
);
200 xCheckBoxValue
.setCurrentValue(Value
);
202 } catch (Exception e
) {
203 throw new Exception("Could not set value to CheckBox '"
204 + CheckBoxName
+ "'", e
);
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
214 public void printAccessibleTree(PrintWriter log
, boolean debugIsActive
) {
215 AccessibilityTools
.printAccessibleTree(log
, mXRoot
, debugIsActive
);