xref: /aoo4110/main/qadevOOo/runner/util/UITools.java (revision b1cdbd2c)
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 util;
25 
26 import com.sun.star.awt.Point;
27 import com.sun.star.awt.XTopWindow;
28 import com.sun.star.awt.XWindow;
29 import com.sun.star.text.XTextDocument;
30 import com.sun.star.uno.UnoRuntime;
31 
32 import com.sun.star.frame.XModel;
33 import com.sun.star.uno.XInterface;
34 import com.sun.star.accessibility.XAccessible;
35 import com.sun.star.accessibility.XAccessibleAction;
36 import com.sun.star.accessibility.AccessibleRole;
37 import com.sun.star.accessibility.XAccessibleComponent;
38 import com.sun.star.awt.XExtendedToolkit;
39 import com.sun.star.accessibility.XAccessibleContext;
40 import com.sun.star.accessibility.XAccessibleEditableText;
41 import com.sun.star.accessibility.XAccessibleSelection;
42 import com.sun.star.accessibility.XAccessibleText;
43 import com.sun.star.accessibility.XAccessibleValue;
44 import com.sun.star.lang.XMultiServiceFactory;
45 import java.awt.Robot;
46 import java.awt.event.InputEvent;
47 import java.io.PrintWriter;
48 import java.util.Vector;
49 import util.AccessibilityTools;
50 
51 
52 /**
53  * This class supports some functions to handle easily accessible objects
54  */
55 public class UITools {
56 
57     private static final AccessibilityTools mAT = new AccessibilityTools();
58     private final XAccessible mXRoot;
59     private final XMultiServiceFactory mMSF;
60 
UITools(XMultiServiceFactory msf, XModel xModel)61     public UITools(XMultiServiceFactory msf, XModel xModel)
62     {
63         mMSF = msf;
64         mXRoot = makeRoot(mMSF, xModel);
65     }
66 
UITools(XMultiServiceFactory msf, XTextDocument xTextDoc)67     public UITools(XMultiServiceFactory msf, XTextDocument xTextDoc)
68     {
69         mMSF = msf;
70         XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class, xTextDoc);
71         mXRoot = makeRoot(mMSF, xModel);
72     }
73 
UITools(XMultiServiceFactory msf, XWindow xWindow)74     public UITools(XMultiServiceFactory msf, XWindow xWindow)
75     {
76         mMSF = msf;
77         mXRoot = makeRoot(xWindow);
78     }
79 
makeRoot(XMultiServiceFactory msf, XModel aModel)80     private static XAccessible makeRoot(XMultiServiceFactory msf, XModel aModel)
81     {
82         XWindow xWindow = mAT.getCurrentWindow(msf, aModel);
83         return mAT.getAccessibleObject(xWindow);
84     }
85 
86 
getString(XInterface xInt)87     private static String getString(XInterface xInt)
88     {
89         XAccessibleText oText = (XAccessibleText)
90                          UnoRuntime.queryInterface(XAccessibleText.class, xInt);
91         return oText.getText();
92     }
93 
setString(XInterface xInt, String cText)94     private static void setString(XInterface xInt, String cText)
95     {
96         XAccessibleEditableText oText = (XAccessibleEditableText)
97                  UnoRuntime.queryInterface(XAccessibleEditableText.class, xInt);
98 
99         oText.setText(cText);
100     }
101 
getValue(XInterface xInt)102     private static Object getValue(XInterface xInt)
103     {
104          XAccessibleValue oValue = (XAccessibleValue)
105                         UnoRuntime.queryInterface(XAccessibleValue.class, xInt);
106          return oValue.getCurrentValue();
107     }
108 
makeRoot(XWindow xWindow)109     private static XAccessible makeRoot(XWindow xWindow)
110     {
111         return mAT.getAccessibleObject(xWindow);
112     }
113 
114     /**
115      * get the root element of the accessible tree
116      * @return the root element
117      */
getRoot()118     public XAccessible getRoot()
119     {
120         return mXRoot;
121     }
122 
123     /**
124      * Helper mathod: set a text into AccessibleEdit field
125      * @param textfiledName is the name of the text field
126      * @param stringToSet is the string to set
127      * @throws java.lang.Exception if something fail
128      */
setTextEditFiledText(String textfiledName, String stringToSet)129     public void setTextEditFiledText(String textfiledName, String stringToSet)
130                         throws java.lang.Exception
131     {
132         XInterface oTextField = mAT.getAccessibleObjectForRole(mXRoot,
133                                             AccessibleRole.TEXT, textfiledName);
134         setString(oTextField, stringToSet);
135     }
136 
137     /**
138      * returns the button by the given name
139      * @param buttonName is name name of the button to get
140      * @return a XAccessibleContext of the button
141      * @throws java.lang.Exception if something fail
142      */
getButton(String buttonName)143     public XAccessibleContext getButton(String buttonName) throws java.lang.Exception
144     {
145         return mAT.getAccessibleObjectForRole
146                                 (mXRoot, AccessibleRole.PUSH_BUTTON, buttonName);
147     }
148 
149     /**
150      * Helper method: gets button via accessibility and 'click' it</code>
151      * @param buttonName is name name of the button to click
152      * @throws java.lang.Exception if something fail
153      */
154 
clickButton(String buttonName)155      public void clickButton(String buttonName) throws java.lang.Exception
156      {
157 
158         XAccessibleContext oButton =mAT.getAccessibleObjectForRole
159                                 (mXRoot, AccessibleRole.PUSH_BUTTON, buttonName);
160         if (oButton == null){
161             throw new Exception("Could not get button '" + buttonName + "'");
162         }
163         XAccessibleAction oAction = (XAccessibleAction)
164                     UnoRuntime.queryInterface(XAccessibleAction.class, oButton);
165 
166         // "click" the button
167         try{
168             oAction.doAccessibleAction(0);
169         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
170           throw new Exception("Could not do accessible action with '" +
171                                buttonName + "'" + e.toString());
172         }
173      }
174 
175 
176 
177     /**
178      * Helper method: gets button via accessibility and 'click' it
179      *  @param buttonName   The name of the button in the accessibility tree
180      *  @param toBePressed  desired state of the toggle button
181      *
182      *  @return true if the state of the button could be changed in the desired manner
183      */
clickToggleButton(String buttonName, boolean toBePressed)184      private boolean clickToggleButton(String buttonName, boolean toBePressed)
185      {
186         XAccessibleContext oButton =mAT.getAccessibleObjectForRole
187                                 (mXRoot, AccessibleRole.TOGGLE_BUTTON, buttonName);
188 
189         if (oButton != null){
190             boolean isChecked = oButton.getAccessibleStateSet().contains(com.sun.star.accessibility.AccessibleStateType.CHECKED);
191             if((isChecked && !toBePressed) || (!isChecked && toBePressed)){
192                 XAccessibleAction oAction = (XAccessibleAction)
193                             UnoRuntime.queryInterface(XAccessibleAction.class, oButton);
194                 try{
195                     // "click" the button
196                     oAction.doAccessibleAction(0);
197                     return true;
198                 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
199                   System.out.println("Could not do accessible action with '"
200                         + buttonName + "'" + e.toString());
201                   return false;
202                 }
203             }else
204                 //no need to press togglebar, do nothing
205                 return true;
206         } else{
207             System.out.println("Could not get button '" + buttonName + "'");
208             return false;
209         }
210      }
211 
212     /**
213      * Deactivates toggle button via Accessibility
214      *  @param buttonName   The name of the button in the Accessibility tree
215      *
216      *  @return true if the button could be set to deactivated
217      */
deactivateToggleButton(String buttonName)218     public boolean deactivateToggleButton(String buttonName){
219         return clickToggleButton(buttonName, false);
220     }
221 
222     /**
223      * Activates toggle button via Accessibility
224      *  @param buttonName   The name of the button in the Accessibility tree
225      *
226      *  @return true if the button could be set to activated
227      */
activateToggleButton(String buttonName)228     public boolean activateToggleButton(String buttonName){
229         return clickToggleButton(buttonName, true);
230     }
231 
232      /**
233       * returns the value of named radio button
234       * @param buttonName the name of the button to get the value of
235       * @throws java.lang.Exception if something fail
236       * @return Integer
237       */
getRadioButtonValue(String buttonName)238      public Integer getRadioButtonValue(String buttonName)
239             throws java.lang.Exception
240      {
241         try {
242             XInterface xRB =mAT.getAccessibleObjectForRole(mXRoot,
243                                        AccessibleRole.RADIO_BUTTON, buttonName);
244 
245             return (Integer) getValue(xRB);
246         } catch (Exception e) {
247           throw new Exception("Could not get value from RadioButton '"
248                                           + buttonName + "' : " + e.toString());
249         }
250      }
251 
252      /**
253       * returns the named graphic
254       * @param GraphicName the name of the graphic
255       * @return XInterface
256       * @throws java.lang.Exception if something fail
257       */
getGraphic(String GraphicName)258      public XInterface getGraphic(String GraphicName) throws java.lang.Exception
259      {
260         return mAT.getAccessibleObjectForRole(mXRoot, AccessibleRole.GRAPHIC,
261                                              GraphicName);
262      }
263 
264 
265      /**
266       * set a named radio button the a given value
267       * @param buttonName the name of the button to set
268       * @param iValue the value to set
269       * @throws java.lang.Exception if something fail
270       */
setRadioButtonValue(String buttonName, int iValue)271      public void setRadioButtonValue(String buttonName, int iValue)
272             throws java.lang.Exception
273      {
274         try {
275             XInterface xRB =mAT.getAccessibleObjectForRole(mXRoot, AccessibleRole.RADIO_BUTTON, buttonName);
276             if(xRB == null)
277                 System.out.println("AccessibleObjectForRole couldn't be found for " + buttonName);
278             XAccessibleValue oValue = (XAccessibleValue)
279                          UnoRuntime.queryInterface(XAccessibleValue.class, xRB);
280             if(oValue == null)
281                 System.out.println("XAccessibleValue couldn't be queried for " + buttonName);
282             oValue.setCurrentValue(new Integer(iValue));
283         } catch (Exception e) {
284           e.printStackTrace();
285 
286           throw new Exception("Could not set value to RadioButton '"
287                                           + buttonName + "' : " + e.toString());
288         }
289 
290      }
291 
292      /**
293       * select an item in nanmed listbox
294       * @param ListBoxName the name of the listbox
295       * @param nChildIndex the index of the item to set
296       * @throws java.lang.Exception if something fail
297       */
selectListboxItem(String ListBoxName, int nChildIndex)298      public void selectListboxItem(String ListBoxName, int nChildIndex)
299             throws java.lang.Exception
300      {
301         try {
302             XAccessibleContext xListBox = null;
303 
304             xListBox =mAT.getAccessibleObjectForRole(mXRoot,
305                                          AccessibleRole.COMBO_BOX, ListBoxName);
306             if (xListBox == null){
307                 xListBox =mAT.getAccessibleObjectForRole(mXRoot,
308                                              AccessibleRole.PANEL, ListBoxName);
309             }
310             XAccessible xListBoxAccess = (XAccessible)
311                          UnoRuntime.queryInterface(XAccessible.class, xListBox);
312 
313             // if a List is not pulled to be open all entries are not visiblle, therefore the
314             // boolean argument
315             XAccessibleContext xList =mAT.getAccessibleObjectForRole(
316                                           xListBoxAccess, AccessibleRole.LIST, true);
317             XAccessibleSelection xListSelect = (XAccessibleSelection)
318                    UnoRuntime.queryInterface(XAccessibleSelection.class, xList);
319 
320             xListSelect.selectAccessibleChild(nChildIndex);
321 
322         } catch (Exception e) {
323           throw new Exception("Could not select item '" +nChildIndex+
324                         "' in listbox '" + ListBoxName + "' : " + e.toString());
325         }
326      }
327 
328      /**
329       * This method returns all entries as XInterface of a list box
330       * @param ListBoxName the name of the listbox
331       * @return Object[] containing XInterface
332       * @throws java.lang.Exception if something fail
333       */
334 
getListBoxObjects(String ListBoxName)335      public Object[] getListBoxObjects(String ListBoxName)
336             throws java.lang.Exception
337      {
338         Vector Items = new Vector();
339          try {
340             XAccessibleContext xListBox = null;
341             XAccessibleContext xList = null;
342 
343             xListBox =mAT.getAccessibleObjectForRole(mXRoot,
344                                          AccessibleRole.COMBO_BOX, ListBoxName);
345             if (xListBox == null){
346                 xListBox =mAT.getAccessibleObjectForRole(mXRoot,
347                                              AccessibleRole.PANEL, ListBoxName);
348             }
349 
350             if (xListBox == null){
351                 // get the list of TreeListBox
352                 xList =mAT.getAccessibleObjectForRole(mXRoot,
353                                               AccessibleRole.TREE, ListBoxName);
354 
355             // all other list boxes have a children of kind of LIST
356             } else {
357 
358                 XAccessible xListBoxAccess = (XAccessible)
359                              UnoRuntime.queryInterface(XAccessible.class, xListBox);
360                 // if a List is not pulled to be open all entries are not visiblle, therefore the
361                 // boolean argument
362                 xList =mAT.getAccessibleObjectForRole(
363                                               xListBoxAccess, AccessibleRole.LIST, true);
364             }
365 
366             for (int i=0;i<xList.getAccessibleChildCount();i++) {
367                 try {
368                     XAccessible xChild = xList.getAccessibleChild(i);
369                     XAccessibleContext xChildCont =
370                                                   xChild.getAccessibleContext();
371                     XInterface xChildInterface = (XInterface)
372                         UnoRuntime.queryInterface(XInterface.class, xChildCont);
373                     Items.add(xChildInterface);
374 
375                 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
376                       throw new Exception("Could not get child form list of '"
377                                          + ListBoxName + "' : " + e.toString());
378                 }
379             }
380 
381          } catch (Exception e) {
382             throw new Exception("Could not get list of items from '"
383                                          + ListBoxName + "' : " + e.toString());
384         }
385         Object[]ret = new XInterface[Items.size()];
386         for (int i=0;i<Items.size();i++){
387             ret[i] = Items.get(i);
388         }
389         return ret;
390      }
391 
392      /**
393       * Helper method: returns the entry manes of a List-Box
394       * @param ListBoxName the name of the listbox
395       * @return the listbox entry names
396       * @throws java.lang.Exception if something fail
397       */
398 
getListBoxItems(String ListBoxName)399      public String[] getListBoxItems(String ListBoxName)
400             throws java.lang.Exception
401      {
402         Vector Items = new Vector();
403          try {
404             XAccessibleContext xListBox = null;
405             XAccessibleContext xList = null;
406 
407             xListBox =mAT.getAccessibleObjectForRole(mXRoot,
408                                          AccessibleRole.COMBO_BOX, ListBoxName);
409             if (xListBox == null){
410                 xListBox =mAT.getAccessibleObjectForRole(mXRoot,
411                                              AccessibleRole.PANEL, ListBoxName);
412             }
413 
414             if (xListBox == null){
415                 // get the list of TreeListBox
416                 xList =mAT.getAccessibleObjectForRole(mXRoot,
417                                               AccessibleRole.TREE, ListBoxName);
418 
419             // all other list boxes have a children of kind of LIST
420             } else {
421 
422                 XAccessible xListBoxAccess = (XAccessible)
423                              UnoRuntime.queryInterface(XAccessible.class, xListBox);
424                 // if a List is not pulled to be open all entries are not visiblle, therefore the
425                 // boolean argument
426                 xList =mAT.getAccessibleObjectForRole(
427                                               xListBoxAccess, AccessibleRole.LIST, true);
428             }
429 
430             for (int i=0;i<xList.getAccessibleChildCount();i++) {
431                 try {
432                     XAccessible xChild = xList.getAccessibleChild(i);
433                     XAccessibleContext xChildCont =
434                                                   xChild.getAccessibleContext();
435                     XInterface xChildInterface = (XInterface)
436                         UnoRuntime.queryInterface(XInterface.class, xChildCont);
437                     Items.add(getString(xChildInterface));
438 
439                 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
440                       throw new Exception("Could not get child form list of '"
441                                          + ListBoxName + "' : " + e.toString());
442                 }
443             }
444 
445          } catch (Exception e) {
446             throw new Exception("Could not get list of items from '"
447                                          + ListBoxName + "' : " + e.toString());
448         }
449         String[]ret = new String[Items.size()];
450         return (String[])Items.toArray(ret);
451      }
452      /**
453       * set to a named nureric filed a given value
454       * @param NumericFieldName the name of the nureic field
455       * @param cValue the value to set
456       * @throws java.lang.Exception if something fail
457       */
setNumericFieldValue(String NumericFieldName, String cValue)458      public void setNumericFieldValue(String NumericFieldName, String cValue)
459         throws java.lang.Exception
460      {
461          try{
462             XInterface xNumericField =mAT.getAccessibleObjectForRole(
463                                   mXRoot, AccessibleRole.TEXT, NumericFieldName);
464             //util.dbg.printInterfaces(xNumericField);
465             XAccessibleEditableText oValue = (XAccessibleEditableText)
466                                          UnoRuntime.queryInterface(
467                                          XAccessibleEditableText.class, xNumericField);
468 
469             setString(xNumericField, cValue);
470          } catch (Exception e) {
471           throw new Exception("Could not set value '" + cValue +
472             "' into NumericField '" + NumericFieldName + "' : " + e.toString());
473         }
474      }
475 
476      /**
477       * returns the value of a numeric field
478       * @param NumericFieldName the name of the numreic field
479       * @throws java.lang.Exception if something fail
480       * @return the value of the named numeric filed
481       */
getNumericFieldValue(String NumericFieldName)482      public String getNumericFieldValue(String NumericFieldName)
483         throws java.lang.Exception
484      {
485          try{
486             XInterface xNumericField =mAT.getAccessibleObjectForRole(
487                                   mXRoot, AccessibleRole.TEXT, NumericFieldName);
488             return (String) getString(xNumericField);
489 
490          } catch (Exception e) {
491           throw new Exception("Could get value from NumericField '"
492                                     + NumericFieldName + "' : " + e.toString());
493         }
494      }
495 
removeCharactersFromCurrencyString(String stringVal)496      private String removeCharactersFromCurrencyString(String stringVal)
497         throws java.lang.Exception
498      {
499          try{
500             int beginIndex = 0;
501             int endIndex = 0;
502             boolean startFound = false;
503             // find the first numeric character in stringVal
504             for(int i = 0; i < stringVal.length(); i++){
505                 int numVal = Character.getNumericValue(stringVal.charAt(i));
506                 // if ascii is a numeric value
507                 if (numVal != -1){
508                     beginIndex = i;
509                     break;
510                 }
511             }
512             // find the last numeric character in stringVal
513             for(int i = stringVal.length()-1; i > 0; i--){
514                 int numVal = Character.getNumericValue(stringVal.charAt(i));
515                 if (numVal != -1){
516                     endIndex = i+1;
517                     break;
518                 }
519             }
520             String currencyVal = stringVal.substring(beginIndex, endIndex);
521 
522             currencyVal = currencyVal.substring(0, currencyVal.length()-3) +
523                           "#" + currencyVal.substring(currencyVal.length()-2);
524 
525             currencyVal = utils.replaceAll13(currencyVal, ",", "");
526             currencyVal = utils.replaceAll13(currencyVal, "\\.", "");
527             currencyVal = utils.replaceAll13(currencyVal, "#", ".");
528 
529             return currencyVal;
530          } catch (Exception e) {
531           throw new Exception("Could get remove characters from currency string '"
532                                     + stringVal + "' : " + e.toString());
533         }
534 
535      }
536 
537      /**
538       * returns the numeric value of a numeric filed. This is needed ie. for
539       * fileds include the moneytary unit.
540       * @param NumericFieldName the name of the numeric filed
541       * @return the value of the numeric filed
542       * @throws java.lang.Exception if something fail
543       */
getNumericFieldNumericValue(String NumericFieldName)544      public Double getNumericFieldNumericValue(String NumericFieldName)
545         throws java.lang.Exception
546      {
547          try{
548             Double retValue = null;
549             String sValue = getNumericFieldValue(NumericFieldName);
550             String sAmount = removeCharactersFromCurrencyString(sValue);
551             retValue = retValue.valueOf(sAmount);
552 
553             return retValue;
554 
555          } catch (Exception e) {
556           throw new Exception("Could get numeric value from NumericField '"
557                                     + NumericFieldName + "' : " + e.toString());
558         }
559      }
560 
561 
562       /**
563        * returns the content of a TextBox
564        * @param TextFieldName the name of the textbox
565        * @return the value of the text box
566        * @throws java.lang.Exception if something fail
567        */
getTextBoxText(String TextFieldName)568      public String getTextBoxText(String TextFieldName)
569         throws java.lang.Exception
570      {
571         String TextFieldText = null;
572         try{
573             XAccessibleContext xTextField =mAT.getAccessibleObjectForRole(mXRoot,
574                                      AccessibleRole.SCROLL_PANE, TextFieldName);
575             XAccessible xTextFieldAccess = (XAccessible)
576                        UnoRuntime.queryInterface(XAccessible.class, xTextField);
577             XAccessibleContext xFrame =mAT.getAccessibleObjectForRole(
578                                    xTextFieldAccess, AccessibleRole.TEXT_FRAME);
579             for (int i=0;i<xFrame.getAccessibleChildCount();i++) {
580                 try {
581                     XAccessible xChild = xFrame.getAccessibleChild(i);
582                     XAccessibleContext xChildCont =
583                                                   xChild.getAccessibleContext();
584                     XInterface xChildInterface = (XInterface)
585                         UnoRuntime.queryInterface(XInterface.class, xChildCont);
586                     TextFieldText += (getString(xChildInterface));
587 
588                 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
589                     throw new Exception("Could not get child fom TextFrame of '"
590                                        + TextFieldName + "' : " + e.toString());
591                 }
592             }
593             return TextFieldText;
594          } catch (Exception e) {
595             throw new Exception("Could not get content fom Textbox '"
596                                        + TextFieldName + "' : " + e.toString());
597         }
598      }
599 
600      /**
601       * set a value to a named check box
602       * @param CheckBoxName the name of the check box
603       * @param Value the value to set
604       *<ul>
605       *    <li>0: not checked </li>
606       *    <li>1: checked </li>
607       *    <li>2: don't know </li>
608       *</ul>
609       * @throws java.lang.Exception if something fail
610       */
setCheckBoxValue(String CheckBoxName, Integer Value)611      public void setCheckBoxValue(String CheckBoxName, Integer Value)
612             throws java.lang.Exception
613      {
614          try {
615             XInterface xCheckBox =mAT.getAccessibleObjectForRole(mXRoot,
616                                      AccessibleRole.CHECK_BOX, CheckBoxName);
617             XAccessibleValue xCheckBoxValue = (XAccessibleValue)
618                    UnoRuntime.queryInterface(XAccessibleValue.class, xCheckBox);
619             xCheckBoxValue.setCurrentValue(Value);
620 
621          } catch (Exception e) {
622             throw new Exception("Could not set value to CheckBox '"
623                                        + CheckBoxName + "' : " + e.toString());
624         }
625      }
626 
627      /**
628       * returns the value of the named check box
629       * @param CheckBoxName the name of the check box
630       * @return the value of the check box
631       * @throws java.lang.Exception if something fail
632       */
getCheckBoxValue(String CheckBoxName)633     public Integer getCheckBoxValue(String CheckBoxName)
634             throws java.lang.Exception
635      {
636          try {
637             XInterface xCheckBox =mAT.getAccessibleObjectForRole(mXRoot,
638                                      AccessibleRole.CHECK_BOX, CheckBoxName);
639             XAccessibleValue xCheckBoxValue = (XAccessibleValue)
640                    UnoRuntime.queryInterface(XAccessibleValue.class, xCheckBox);
641 
642             return (Integer) xCheckBoxValue.getCurrentValue();
643          } catch (Exception e) {
644             throw new Exception("Could not set value to CheckBox '"
645                                        + CheckBoxName + "' : " + e.toString());
646         }
647      }
648 
649       /**
650        * returns the message of a Basic-MessageBox
651        * @return the message of a Basic-MessageBox
652        * @throws java.lang.Exception if something fail
653        */
getMsgBoxText()654      public String getMsgBoxText()
655         throws java.lang.Exception
656      {
657         String cMessage = null;
658         try{
659             XAccessibleContext xMessage =mAT.getAccessibleObjectForRole(mXRoot,
660                                      AccessibleRole.LABEL);
661 
662             XInterface xMessageInterface = (XInterface)
663                 UnoRuntime.queryInterface(XInterface.class, xMessage);
664             cMessage += (getString(xMessageInterface));
665 
666 
667             return cMessage;
668          } catch (Exception e) {
669             throw new Exception("Could not get message from Basic-MessageBox: " + e.toString());
670         }
671      }
672 
673     /**
674      * fetch the window which is equal to the given <CODE>WindowName</CODE>
675      * @return the named window
676      * @throws java.lang.Exception if something fail
677      */
getTopWindow(String WindowName, boolean debug)678     public XWindow getTopWindow(String WindowName, boolean debug) throws java.lang.Exception
679     {
680         XInterface xToolKit = null;
681         try {
682             xToolKit = (XInterface) mMSF.createInstance("com.sun.star.awt.Toolkit") ;
683         } catch (com.sun.star.uno.Exception e) {
684           throw new Exception("Could not toolkit: " + e.toString());
685         }
686         XExtendedToolkit tk = (XExtendedToolkit)
687             UnoRuntime.queryInterface(XExtendedToolkit.class, xToolKit);
688 
689         int count = tk.getTopWindowCount();
690 
691         XTopWindow retWindow = null;
692 
693         if (debug) System.out.println("getTopWindow ->");
694 
695         for (int i=0; i < count ; i++){
696             XTopWindow xTopWindow = tk.getTopWindow(i);
697             XAccessible xAcc = mAT.getAccessibleObject(xTopWindow);
698             String accName = xAcc.getAccessibleContext().getAccessibleName();
699 
700             if (debug){
701                 System.out.println("AccessibleName: " + accName);
702             }
703 
704             if (WindowName.equals(accName)){
705                 if (debug) System.out.println("-> found window with name '" + WindowName + "'");
706                 retWindow = xTopWindow;
707             }
708         }
709 
710 
711         if (debug) {
712             if (retWindow == null) System.out.println("could not found window with name '" + WindowName + "'");
713             System.out.println("<- getTopWindow ");
714         }
715         return (XWindow) UnoRuntime.queryInterface(XWindow.class, retWindow);
716     }
717 
clickMiddleOfAccessibleObject(short role, String name)718     public void clickMiddleOfAccessibleObject(short role, String name){
719 
720         XAccessibleContext xAcc =mAT.getAccessibleObjectForRole(mXRoot, role, name);
721         XAccessibleComponent aComp = (XAccessibleComponent) UnoRuntime.queryInterface(
722                                              XAccessibleComponent.class, xAcc);
723 
724         System.out.println(xAcc.getAccessibleRole() + "," +
725                     xAcc.getAccessibleName() + "(" +
726                     xAcc.getAccessibleDescription() + "):" +
727                     utils.getImplName(xAcc));
728 
729         if (aComp != null) {
730             Point location = aComp.getLocationOnScreen();
731             String bounds = "(" + aComp.getBounds().X + "," +
732                             aComp.getBounds().Y + ")" + " (" +
733                             aComp.getBounds().Width + "," +
734                             aComp.getBounds().Height + ")";
735             System.out.println("The boundary Rectangle is " + bounds);
736                 try {
737                     Robot rob = new Robot();
738                     int x = aComp.getLocationOnScreen().X + (aComp.getBounds().Width / 2);
739                     int y = aComp.getLocationOnScreen().Y + (aComp.getBounds().Height / 2);
740                     System.out.println("try to click mouse button on x/y " + x + "/" + y);
741                     rob.mouseMove(x, y);
742                     rob.mousePress(InputEvent.BUTTON1_MASK);
743                     rob.mouseRelease(InputEvent.BUTTON1_MASK);
744                 } catch (java.awt.AWTException e) {
745                     System.out.println("couldn't press mouse button");
746                 }
747 
748         }
749     }
750 
doubleClickMiddleOfAccessibleObject(short role, String name)751     public void doubleClickMiddleOfAccessibleObject(short role, String name) {
752         XAccessibleContext xAcc =mAT.getAccessibleObjectForRole(mXRoot, role, name);
753         XAccessibleComponent aComp = (XAccessibleComponent) UnoRuntime.queryInterface(
754                                              XAccessibleComponent.class, xAcc);
755 
756         System.out.println(xAcc.getAccessibleRole() + "," +
757                     xAcc.getAccessibleName() + "(" +
758                     xAcc.getAccessibleDescription() + "):" +
759                     utils.getImplName(xAcc));
760 
761         if (aComp != null) {
762             Point location = aComp.getLocationOnScreen();
763             String bounds = "(" + aComp.getBounds().X + "," +
764                             aComp.getBounds().Y + ")" + " (" +
765                             aComp.getBounds().Width + "," +
766                             aComp.getBounds().Height + ")";
767             System.out.println("The boundary Rectangle is " + bounds);
768                 try {
769                     Robot rob = new Robot();
770                     int x = aComp.getLocationOnScreen().X + (aComp.getBounds().Width / 2);
771                     int y = aComp.getLocationOnScreen().Y + (aComp.getBounds().Height / 2);
772                     System.out.println("try to double click mouse button on x/y " + x + "/" + y);
773                     rob.mouseMove(x, y);
774                     rob.mousePress(InputEvent.BUTTON1_MASK);
775                     rob.mouseRelease(InputEvent.BUTTON1_MASK);
776                     utils.shortWait(100);
777                     rob.mousePress(InputEvent.BUTTON1_MASK);
778                     rob.mouseRelease(InputEvent.BUTTON1_MASK);
779                 } catch (java.awt.AWTException e) {
780                     System.out.println("couldn't press mouse button");
781                 }
782 
783         }
784     }
785 
786     /**
787      * <B>DEPRECATED</B>
788      * Since <CODE>AccessibilityTools</CODE> handle parameter <CODE>debugIsActive</CODE>
789      * this function does not work anymore.
790      * @deprecated Since <CODE>AccessibilityTools</CODE> handle parameter <CODE>debugIsActive</CODE>
791      * this function does not work anymore.
792      * @param log logWriter
793      */
printAccessibleTree(PrintWriter log)794     public void printAccessibleTree(PrintWriter log)
795     {
796         mAT.printAccessibleTree(log, mXRoot);
797     }
798 
799 
800     /**
801      * Prints the accessible tree to the <CODE>logWriter</CODE> only if <CODE>debugIsActive</CODE>
802      * is set to <CODE>true</CODE>
803      * @param log logWriter
804      * @param debugIsActive prints only if this parameter is set to TRUE
805      */
printAccessibleTree(PrintWriter log, boolean debugIsActive)806     public void printAccessibleTree(PrintWriter log, boolean debugIsActive) {
807         mAT.printAccessibleTree(log, mXRoot, debugIsActive);
808     }
809 
810 }
811