1cd519653SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3cd519653SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4cd519653SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5cd519653SAndrew Rist  * distributed with this work for additional information
6cd519653SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7cd519653SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8cd519653SAndrew Rist  * "License"); you may not use this file except in compliance
9cd519653SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cd519653SAndrew Rist  *
11cd519653SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cd519653SAndrew Rist  *
13cd519653SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14cd519653SAndrew Rist  * software distributed under the License is distributed on an
15cd519653SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16cd519653SAndrew Rist  * KIND, either express or implied.  See the License for the
17cd519653SAndrew Rist  * specific language governing permissions and limitations
18cd519653SAndrew Rist  * under the License.
19cd519653SAndrew Rist  *
20cd519653SAndrew Rist  *************************************************************/
21cd519653SAndrew Rist 
22cd519653SAndrew Rist 
23cdf0e10cSrcweir package ifc.script.framework;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.awt.*;
26cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
27cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
28cdf0e10cSrcweir import com.sun.star.uno.XInterface;
29cdf0e10cSrcweir 
30cdf0e10cSrcweir import drafts.com.sun.star.accessibility.*;
31cdf0e10cSrcweir import drafts.com.sun.star.awt.XExtendedToolkit;
32cdf0e10cSrcweir 
33cdf0e10cSrcweir // Jsuite classes
34cdf0e10cSrcweir import util.AccessibilityTools;
35cdf0e10cSrcweir import util.dbg;
36cdf0e10cSrcweir /**
37cdf0e10cSrcweir * Thread that pushes the buttons or checkbox
38cdf0e10cSrcweir * on the message box that is on top.
39cdf0e10cSrcweir */
40cdf0e10cSrcweir public class SecurityDialogUtil extends Thread {
41cdf0e10cSrcweir 
42cdf0e10cSrcweir private XMultiServiceFactory xMSF = null;
43cdf0e10cSrcweir private String errorMsg;
44cdf0e10cSrcweir private boolean errorHappened;
45cdf0e10cSrcweir private String btnName;
46cdf0e10cSrcweir private boolean checkBox;
47cdf0e10cSrcweir 
48cdf0e10cSrcweir /**
49cdf0e10cSrcweir  * Constructor.
50cdf0e10cSrcweir  * @param xMSF A MultiServiceFactory.
51cdf0e10cSrcweir  * @param log The log writer.
52cdf0e10cSrcweir  */
SecurityDialogUtil(XMultiServiceFactory xMSF, String btnName, boolean checkBox )53cdf0e10cSrcweir public SecurityDialogUtil(XMultiServiceFactory xMSF, String btnName, boolean checkBox )
54cdf0e10cSrcweir {
55cdf0e10cSrcweir     this.xMSF = xMSF;
56cdf0e10cSrcweir     this.btnName = btnName;
57cdf0e10cSrcweir     this.checkBox = checkBox;
58cdf0e10cSrcweir     errorMsg = "";
59cdf0e10cSrcweir     errorHappened=false;
60cdf0e10cSrcweir }
61cdf0e10cSrcweir 
62cdf0e10cSrcweir /**
63*86e1cf34SPedro Giffuni  * Returns the error message that occurred while
64cdf0e10cSrcweir  * accessing and pressing the button.
65cdf0e10cSrcweir  * @return Error message.
66cdf0e10cSrcweir  */
getErrorMessage()67cdf0e10cSrcweir public String getErrorMessage()
68cdf0e10cSrcweir {
69cdf0e10cSrcweir     return errorMsg;
70cdf0e10cSrcweir }
71cdf0e10cSrcweir 
72cdf0e10cSrcweir /**
73cdf0e10cSrcweir  * Is there an error message available?
74cdf0e10cSrcweir  * @return true, if an error happened
75cdf0e10cSrcweir  */
hasErrorMessage()76cdf0e10cSrcweir public boolean hasErrorMessage()
77cdf0e10cSrcweir {
78cdf0e10cSrcweir     return !errorMsg.equals("");
79cdf0e10cSrcweir }
80cdf0e10cSrcweir 
81cdf0e10cSrcweir /**
82cdf0e10cSrcweir  * Press the named button in the currently visible dialog box.
83cdf0e10cSrcweir  */
run()84cdf0e10cSrcweir public void run()
85cdf0e10cSrcweir {
86cdf0e10cSrcweir     // wait for the message box to appear
87cdf0e10cSrcweir     try
88cdf0e10cSrcweir     {
89cdf0e10cSrcweir         Thread.currentThread().sleep(4000) ;
90cdf0e10cSrcweir     }
91cdf0e10cSrcweir     catch (InterruptedException e)
92cdf0e10cSrcweir     {
93cdf0e10cSrcweir         System.err.println("While waiting :" + e.getMessage()) ;
94cdf0e10cSrcweir     }
95cdf0e10cSrcweir 
96cdf0e10cSrcweir     // access the message box
97cdf0e10cSrcweir 
98cdf0e10cSrcweir      XAccessibleContext xCon = null;
99cdf0e10cSrcweir     try
100cdf0e10cSrcweir     {
101cdf0e10cSrcweir         XInterface x = (XInterface) xMSF.createInstance(
102cdf0e10cSrcweir                                     "com.sun.star.awt.Toolkit") ;
103cdf0e10cSrcweir         XExtendedToolkit tk =
104cdf0e10cSrcweir                 (XExtendedToolkit)UnoRuntime.queryInterface(
105cdf0e10cSrcweir                                         XExtendedToolkit.class,x);
106cdf0e10cSrcweir         AccessibilityTools at = new AccessibilityTools();
107cdf0e10cSrcweir         XWindow xWindow = (XWindow)UnoRuntime.queryInterface(
108cdf0e10cSrcweir                                 XWindow.class,tk.getActiveTopWindow());
109cdf0e10cSrcweir         XAccessible xRoot = at.getAccessibleObject(xWindow);
110cdf0e10cSrcweir         xCon = xRoot.getAccessibleContext();
111cdf0e10cSrcweir     }
112cdf0e10cSrcweir     catch (Exception e)
113cdf0e10cSrcweir     {
114cdf0e10cSrcweir         errorMsg="Exception while using Accessibility\n"+
115cdf0e10cSrcweir                                                     e.getMessage();
116cdf0e10cSrcweir         return;
117cdf0e10cSrcweir     }
118cdf0e10cSrcweir     // get the button
119cdf0e10cSrcweir     XInterface oObj = null;
120cdf0e10cSrcweir     try
121cdf0e10cSrcweir     {
122cdf0e10cSrcweir         /* System.err.println("Name of the AccessibleContext:\n\t"+
123cdf0e10cSrcweir                                         xCon.getAccessibleName()); */
124cdf0e10cSrcweir         int count = xCon.getAccessibleChildCount();
125cdf0e10cSrcweir         // System.err.println("Number of children: "+count);
126cdf0e10cSrcweir         for (int i=0; i<count; i++) {
127cdf0e10cSrcweir             XAccessible xAcc = xCon.getAccessibleChild(i);
128cdf0e10cSrcweir             String name =
129cdf0e10cSrcweir                     xAcc.getAccessibleContext().getAccessibleName();
130cdf0e10cSrcweir             // System.out.println("Child "+i+": "+ name);
131cdf0e10cSrcweir             // check for button
132cdf0e10cSrcweir             if ( name.equals( btnName ) && ( UnoRuntime.queryInterface(
133cdf0e10cSrcweir                                     XButton.class, xAcc ) != null ) )
134cdf0e10cSrcweir             {
135cdf0e10cSrcweir                 // System.out.println("Child "+i+": "+ name);
136cdf0e10cSrcweir                 oObj = xAcc.getAccessibleContext();
137cdf0e10cSrcweir             }
138cdf0e10cSrcweir             // check for checkbox
139cdf0e10cSrcweir             if ( checkBox &&  ( UnoRuntime.queryInterface( XCheckBox.class, xAcc ) != null ) )
140cdf0e10cSrcweir             {
141cdf0e10cSrcweir                 // want to do this action now
142cdf0e10cSrcweir                 // probably equates to toggle cb
143cdf0e10cSrcweir                 XAccessibleAction xAction =
144cdf0e10cSrcweir                         (XAccessibleAction)UnoRuntime.queryInterface(
145cdf0e10cSrcweir                         XAccessibleAction.class, xAcc.getAccessibleContext());
146cdf0e10cSrcweir                 xAction.doAccessibleAction(0);
147cdf0e10cSrcweir 
148cdf0e10cSrcweir                 // might be worth using oObj2 to double check the new state??
149cdf0e10cSrcweir             }
150cdf0e10cSrcweir         }
151cdf0e10cSrcweir         if (oObj == null) {
152cdf0e10cSrcweir             errorMsg="No button has been found:\n"+
153cdf0e10cSrcweir                      "No action is triggered.";
154cdf0e10cSrcweir             return;
155cdf0e10cSrcweir         }
156cdf0e10cSrcweir         // press button
157cdf0e10cSrcweir         XAccessibleAction xAction =
158cdf0e10cSrcweir                 (XAccessibleAction)UnoRuntime.queryInterface(
159cdf0e10cSrcweir                 XAccessibleAction.class, oObj);
160cdf0e10cSrcweir         xAction.doAccessibleAction(0);
161cdf0e10cSrcweir     }
162cdf0e10cSrcweir     catch(com.sun.star.lang.IndexOutOfBoundsException e) {
163cdf0e10cSrcweir         errorMsg="Exception\n"+
164cdf0e10cSrcweir                         e.getMessage();
165cdf0e10cSrcweir     }
166cdf0e10cSrcweir }
167cdf0e10cSrcweir 
168cdf0e10cSrcweir }
169cdf0e10cSrcweir 
170cdf0e10cSrcweir 
171cdf0e10cSrcweir 
172cdf0e10cSrcweir 
173