1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir package ifc.script.framework;
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir import com.sun.star.awt.*;
30*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
31*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
32*cdf0e10cSrcweir import com.sun.star.uno.XInterface;
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir import drafts.com.sun.star.accessibility.*;
35*cdf0e10cSrcweir import drafts.com.sun.star.awt.XExtendedToolkit;
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir // Jsuite classes
38*cdf0e10cSrcweir import util.AccessibilityTools;
39*cdf0e10cSrcweir import util.dbg;
40*cdf0e10cSrcweir /**
41*cdf0e10cSrcweir * Thread that pushes the buttons or checkbox
42*cdf0e10cSrcweir * on the message box that is on top.
43*cdf0e10cSrcweir */
44*cdf0e10cSrcweir public class SecurityDialogUtil extends Thread {
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir private XMultiServiceFactory xMSF = null;
47*cdf0e10cSrcweir private String errorMsg;
48*cdf0e10cSrcweir private boolean errorHappened;
49*cdf0e10cSrcweir private String btnName;
50*cdf0e10cSrcweir private boolean checkBox;
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir /**
53*cdf0e10cSrcweir  * Constructor.
54*cdf0e10cSrcweir  * @param xMSF A MultiServiceFactory.
55*cdf0e10cSrcweir  * @param log The log writer.
56*cdf0e10cSrcweir  */
57*cdf0e10cSrcweir public SecurityDialogUtil(XMultiServiceFactory xMSF, String btnName, boolean checkBox )
58*cdf0e10cSrcweir {
59*cdf0e10cSrcweir     this.xMSF = xMSF;
60*cdf0e10cSrcweir     this.btnName = btnName;
61*cdf0e10cSrcweir     this.checkBox = checkBox;
62*cdf0e10cSrcweir     errorMsg = "";
63*cdf0e10cSrcweir     errorHappened=false;
64*cdf0e10cSrcweir }
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir /**
67*cdf0e10cSrcweir  * Returns the error message that occured while
68*cdf0e10cSrcweir  * accessing and pressing the button.
69*cdf0e10cSrcweir  * @return Error message.
70*cdf0e10cSrcweir  */
71*cdf0e10cSrcweir public String getErrorMessage()
72*cdf0e10cSrcweir {
73*cdf0e10cSrcweir     return errorMsg;
74*cdf0e10cSrcweir }
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir /**
77*cdf0e10cSrcweir  * Is there an error message available?
78*cdf0e10cSrcweir  * @return true, if an error happened
79*cdf0e10cSrcweir  */
80*cdf0e10cSrcweir public boolean hasErrorMessage()
81*cdf0e10cSrcweir {
82*cdf0e10cSrcweir     return !errorMsg.equals("");
83*cdf0e10cSrcweir }
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir /**
86*cdf0e10cSrcweir  * Press the named button in the currently visible dialog box.
87*cdf0e10cSrcweir  */
88*cdf0e10cSrcweir public void run()
89*cdf0e10cSrcweir {
90*cdf0e10cSrcweir     // wait for the message box to appear
91*cdf0e10cSrcweir     try
92*cdf0e10cSrcweir     {
93*cdf0e10cSrcweir         Thread.currentThread().sleep(4000) ;
94*cdf0e10cSrcweir     }
95*cdf0e10cSrcweir     catch (InterruptedException e)
96*cdf0e10cSrcweir     {
97*cdf0e10cSrcweir         System.err.println("While waiting :" + e.getMessage()) ;
98*cdf0e10cSrcweir     }
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir     // access the message box
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir      XAccessibleContext xCon = null;
103*cdf0e10cSrcweir     try
104*cdf0e10cSrcweir     {
105*cdf0e10cSrcweir         XInterface x = (XInterface) xMSF.createInstance(
106*cdf0e10cSrcweir                                     "com.sun.star.awt.Toolkit") ;
107*cdf0e10cSrcweir         XExtendedToolkit tk =
108*cdf0e10cSrcweir                 (XExtendedToolkit)UnoRuntime.queryInterface(
109*cdf0e10cSrcweir                                         XExtendedToolkit.class,x);
110*cdf0e10cSrcweir         AccessibilityTools at = new AccessibilityTools();
111*cdf0e10cSrcweir         XWindow xWindow = (XWindow)UnoRuntime.queryInterface(
112*cdf0e10cSrcweir                                 XWindow.class,tk.getActiveTopWindow());
113*cdf0e10cSrcweir         XAccessible xRoot = at.getAccessibleObject(xWindow);
114*cdf0e10cSrcweir         xCon = xRoot.getAccessibleContext();
115*cdf0e10cSrcweir     }
116*cdf0e10cSrcweir     catch (Exception e)
117*cdf0e10cSrcweir     {
118*cdf0e10cSrcweir         errorMsg="Exception while using Accessibility\n"+
119*cdf0e10cSrcweir                                                     e.getMessage();
120*cdf0e10cSrcweir         return;
121*cdf0e10cSrcweir     }
122*cdf0e10cSrcweir     // get the button
123*cdf0e10cSrcweir     XInterface oObj = null;
124*cdf0e10cSrcweir     try
125*cdf0e10cSrcweir     {
126*cdf0e10cSrcweir         /* System.err.println("Name of the AccessibleContext:\n\t"+
127*cdf0e10cSrcweir                                         xCon.getAccessibleName()); */
128*cdf0e10cSrcweir         int count = xCon.getAccessibleChildCount();
129*cdf0e10cSrcweir         // System.err.println("Number of children: "+count);
130*cdf0e10cSrcweir         for (int i=0; i<count; i++) {
131*cdf0e10cSrcweir             XAccessible xAcc = xCon.getAccessibleChild(i);
132*cdf0e10cSrcweir             String name =
133*cdf0e10cSrcweir                     xAcc.getAccessibleContext().getAccessibleName();
134*cdf0e10cSrcweir             // System.out.println("Child "+i+": "+ name);
135*cdf0e10cSrcweir             // check for button
136*cdf0e10cSrcweir             if ( name.equals( btnName ) && ( UnoRuntime.queryInterface(
137*cdf0e10cSrcweir                                     XButton.class, xAcc ) != null ) )
138*cdf0e10cSrcweir             {
139*cdf0e10cSrcweir                 // System.out.println("Child "+i+": "+ name);
140*cdf0e10cSrcweir                 oObj = xAcc.getAccessibleContext();
141*cdf0e10cSrcweir             }
142*cdf0e10cSrcweir             // check for checkbox
143*cdf0e10cSrcweir             if ( checkBox &&  ( UnoRuntime.queryInterface( XCheckBox.class, xAcc ) != null ) )
144*cdf0e10cSrcweir             {
145*cdf0e10cSrcweir                 // want to do this action now
146*cdf0e10cSrcweir                 // probably equates to toggle cb
147*cdf0e10cSrcweir                 XAccessibleAction xAction =
148*cdf0e10cSrcweir                         (XAccessibleAction)UnoRuntime.queryInterface(
149*cdf0e10cSrcweir                         XAccessibleAction.class, xAcc.getAccessibleContext());
150*cdf0e10cSrcweir                 xAction.doAccessibleAction(0);
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir                 // might be worth using oObj2 to double check the new state??
153*cdf0e10cSrcweir             }
154*cdf0e10cSrcweir         }
155*cdf0e10cSrcweir         if (oObj == null) {
156*cdf0e10cSrcweir             errorMsg="No button has been found:\n"+
157*cdf0e10cSrcweir                      "No action is triggered.";
158*cdf0e10cSrcweir             return;
159*cdf0e10cSrcweir         }
160*cdf0e10cSrcweir         // press button
161*cdf0e10cSrcweir         XAccessibleAction xAction =
162*cdf0e10cSrcweir                 (XAccessibleAction)UnoRuntime.queryInterface(
163*cdf0e10cSrcweir                 XAccessibleAction.class, oObj);
164*cdf0e10cSrcweir         xAction.doAccessibleAction(0);
165*cdf0e10cSrcweir     }
166*cdf0e10cSrcweir     catch(com.sun.star.lang.IndexOutOfBoundsException e) {
167*cdf0e10cSrcweir         errorMsg="Exception\n"+
168*cdf0e10cSrcweir                         e.getMessage();
169*cdf0e10cSrcweir     }
170*cdf0e10cSrcweir }
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir }
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir 
177