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