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 package ifc.script.framework;
24 
25 import com.sun.star.awt.*;
26 import com.sun.star.lang.XMultiServiceFactory;
27 import com.sun.star.uno.UnoRuntime;
28 import com.sun.star.uno.XInterface;
29 
30 import drafts.com.sun.star.accessibility.*;
31 import drafts.com.sun.star.awt.XExtendedToolkit;
32 
33 // Jsuite classes
34 import util.AccessibilityTools;
35 import util.dbg;
36 /**
37 * Thread that pushes the buttons or checkbox
38 * on the message box that is on top.
39 */
40 public class SecurityDialogUtil extends Thread {
41 
42 private XMultiServiceFactory xMSF = null;
43 private String errorMsg;
44 private boolean errorHappened;
45 private String btnName;
46 private boolean checkBox;
47 
48 /**
49  * Constructor.
50  * @param xMSF A MultiServiceFactory.
51  * @param log The log writer.
52  */
SecurityDialogUtil(XMultiServiceFactory xMSF, String btnName, boolean checkBox )53 public SecurityDialogUtil(XMultiServiceFactory xMSF, String btnName, boolean checkBox )
54 {
55     this.xMSF = xMSF;
56     this.btnName = btnName;
57     this.checkBox = checkBox;
58     errorMsg = "";
59     errorHappened=false;
60 }
61 
62 /**
63  * Returns the error message that occurred while
64  * accessing and pressing the button.
65  * @return Error message.
66  */
getErrorMessage()67 public String getErrorMessage()
68 {
69     return errorMsg;
70 }
71 
72 /**
73  * Is there an error message available?
74  * @return true, if an error happened
75  */
hasErrorMessage()76 public boolean hasErrorMessage()
77 {
78     return !errorMsg.equals("");
79 }
80 
81 /**
82  * Press the named button in the currently visible dialog box.
83  */
run()84 public void run()
85 {
86     // wait for the message box to appear
87     try
88     {
89         Thread.currentThread().sleep(4000) ;
90     }
91     catch (InterruptedException e)
92     {
93         System.err.println("While waiting :" + e.getMessage()) ;
94     }
95 
96     // access the message box
97 
98      XAccessibleContext xCon = null;
99     try
100     {
101         XInterface x = (XInterface) xMSF.createInstance(
102                                     "com.sun.star.awt.Toolkit") ;
103         XExtendedToolkit tk =
104                 (XExtendedToolkit)UnoRuntime.queryInterface(
105                                         XExtendedToolkit.class,x);
106         AccessibilityTools at = new AccessibilityTools();
107         XWindow xWindow = (XWindow)UnoRuntime.queryInterface(
108                                 XWindow.class,tk.getActiveTopWindow());
109         XAccessible xRoot = at.getAccessibleObject(xWindow);
110         xCon = xRoot.getAccessibleContext();
111     }
112     catch (Exception e)
113     {
114         errorMsg="Exception while using Accessibility\n"+
115                                                     e.getMessage();
116         return;
117     }
118     // get the button
119     XInterface oObj = null;
120     try
121     {
122         /* System.err.println("Name of the AccessibleContext:\n\t"+
123                                         xCon.getAccessibleName()); */
124         int count = xCon.getAccessibleChildCount();
125         // System.err.println("Number of children: "+count);
126         for (int i=0; i<count; i++) {
127             XAccessible xAcc = xCon.getAccessibleChild(i);
128             String name =
129                     xAcc.getAccessibleContext().getAccessibleName();
130             // System.out.println("Child "+i+": "+ name);
131             // check for button
132             if ( name.equals( btnName ) && ( UnoRuntime.queryInterface(
133                                     XButton.class, xAcc ) != null ) )
134             {
135                 // System.out.println("Child "+i+": "+ name);
136                 oObj = xAcc.getAccessibleContext();
137             }
138             // check for checkbox
139             if ( checkBox &&  ( UnoRuntime.queryInterface( XCheckBox.class, xAcc ) != null ) )
140             {
141                 // want to do this action now
142                 // probably equates to toggle cb
143                 XAccessibleAction xAction =
144                         (XAccessibleAction)UnoRuntime.queryInterface(
145                         XAccessibleAction.class, xAcc.getAccessibleContext());
146                 xAction.doAccessibleAction(0);
147 
148                 // might be worth using oObj2 to double check the new state??
149             }
150         }
151         if (oObj == null) {
152             errorMsg="No button has been found:\n"+
153                      "No action is triggered.";
154             return;
155         }
156         // press button
157         XAccessibleAction xAction =
158                 (XAccessibleAction)UnoRuntime.queryInterface(
159                 XAccessibleAction.class, oObj);
160         xAction.doAccessibleAction(0);
161     }
162     catch(com.sun.star.lang.IndexOutOfBoundsException e) {
163         errorMsg="Exception\n"+
164                         e.getMessage();
165     }
166 }
167 
168 }
169 
170 
171 
172 
173