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 complex.sfx2.tools; 24 25 import com.sun.star.accessibility.AccessibleRole; 26 import com.sun.star.accessibility.XAccessible; 27 import com.sun.star.accessibility.XAccessibleAction; 28 import com.sun.star.accessibility.XAccessibleContext; 29 import com.sun.star.accessibility.XAccessibleSelection; 30 import com.sun.star.awt.XExtendedToolkit; 31 import com.sun.star.awt.XWindow; 32 import com.sun.star.frame.XDesktop; 33 import com.sun.star.lang.XComponent; 34 import com.sun.star.lang.XMultiServiceFactory; 35 import com.sun.star.text.XTextDocument; 36 import com.sun.star.uno.UnoRuntime; 37 import com.sun.star.util.XCloseable; 38 39 import java.io.PrintWriter; 40 41 import util.AccessibilityTools; 42 import util.WriterTools; 43 44 45 /** 46 * Methods to open Writer docs 47 * 48 */ 49 public class WriterHelper { 50 XMultiServiceFactory m_xMSF = null; 51 52 /** Creates a new instance of WriterHelper 53 * @param m_xMSF The MultiServiceFactory gained from the office 54 */ WriterHelper(XMultiServiceFactory m_xMSF)55 public WriterHelper(XMultiServiceFactory m_xMSF) { 56 this.m_xMSF = m_xMSF; 57 } 58 59 /** Opens an empty document 60 * @return a reference to the opened document is returned 61 */ openEmptyDoc()62 public XTextDocument openEmptyDoc() { 63 return WriterTools.createTextDoc(m_xMSF); 64 } 65 66 /** Closes a given XTextDocument 67 * @param xTextDoc the text document to be closed 68 * @return if an error occurs the errormessage is returned and an empty String if not 69 */ closeDoc(XTextDocument xTextDoc)70 public String closeDoc(XTextDocument xTextDoc) { 71 XCloseable closer = UnoRuntime.queryInterface(XCloseable.class, xTextDoc); 72 String err = ""; 73 74 try { 75 closer.close(true); 76 } catch (com.sun.star.util.CloseVetoException e) { 77 err = "couldn't close document " + e; 78 System.out.println(err); 79 } 80 81 return err; 82 } 83 84 private XTextDocument xLocalDoc = null; 85 /** a TextDocument is opened by pressing a button in a dialog given by uno-URL 86 * @param url the uno-URL of the dialog to be opened 87 * @param createButton the language dependend label of the button to be pressed 88 * @param destroyLocal if true the document that has been opened to dispatch the dialog is closed before the method returns, 89 * otherwise this document remains open 90 * @return returns the created Textdocument 91 */ openFromDialog(String url, String createButton, boolean destroyLocal)92 public XTextDocument openFromDialog(String url, String createButton, 93 boolean destroyLocal) { 94 xLocalDoc = WriterTools.createTextDoc(m_xMSF); 95 XComponent comp = UnoRuntime.queryInterface(XComponent.class, xLocalDoc); 96 DialogThread diagThread = new DialogThread(comp, m_xMSF, url); 97 diagThread.start(); 98 shortWait(); 99 100 if (createButton.length() > 1) { 101 XExtendedToolkit tk = getToolkit(); 102 Object atw = tk.getActiveTopWindow(); 103 104 XWindow xWindow = UnoRuntime.queryInterface(XWindow.class, atw); 105 106 XAccessible xRoot = AccessibilityTools.getAccessibleObject(xWindow); 107 XAccessibleContext buttonContext = AccessibilityTools.getAccessibleObjectForRole( 108 xRoot, 109 AccessibleRole.PUSH_BUTTON, 110 createButton); 111 112 XAccessibleAction buttonAction = UnoRuntime.queryInterface(XAccessibleAction.class, buttonContext); 113 114 try { 115 System.out.println("Name: " + 116 buttonContext.getAccessibleName()); 117 buttonAction.doAccessibleAction(0); 118 } catch (com.sun.star.lang.IndexOutOfBoundsException e) { 119 System.out.println("Couldn't press button"); 120 } 121 122 shortWait(); 123 } 124 125 XDesktop xDesktop = getDesktop(); 126 127 XTextDocument returnDoc = UnoRuntime.queryInterface(XTextDocument.class, xDesktop.getCurrentComponent()); 128 129 if (destroyLocal) { 130 closeDoc(xLocalDoc); 131 xLocalDoc = null; 132 } 133 134 return returnDoc; 135 } closeFromDialog()136 public void closeFromDialog() 137 { 138 closeDoc(xLocalDoc); 139 xLocalDoc = null; 140 } kill()141 public void kill() 142 { 143 XDesktop xDesktop = getDesktop(); 144 xDesktop.terminate(); 145 } 146 147 DocByAutopilot(XMultiServiceFactory msf, int[] indexes, boolean destroyLocal, String bName)148 public XTextDocument DocByAutopilot(XMultiServiceFactory msf, 149 int[] indexes, boolean destroyLocal, 150 String bName) { 151 XTextDocument xTextDoc = WriterTools.createTextDoc(m_xMSF); 152 Object toolkit = null; 153 154 try { 155 toolkit = msf.createInstance("com.sun.star.awt.Toolkit"); 156 } catch (com.sun.star.uno.Exception e) { 157 e.printStackTrace( System.err ); 158 } 159 160 XExtendedToolkit tk = UnoRuntime.queryInterface(XExtendedToolkit.class, toolkit); 161 162 shortWait(); 163 164 Object atw = tk.getActiveTopWindow(); 165 166 XWindow xWindow = UnoRuntime.queryInterface(XWindow.class, atw); 167 168 XAccessible xRoot = AccessibilityTools.getAccessibleObject(xWindow); 169 170 XAccessibleContext ARoot = AccessibilityTools.getAccessibleObjectForRole(xRoot, 171 AccessibleRole.MENU_BAR); 172 XAccessibleSelection sel = UnoRuntime.queryInterface(XAccessibleSelection.class, ARoot); 173 174 for (int k = 0; k < indexes.length; k++) { 175 try { 176 sel.selectAccessibleChild(indexes[k]); 177 shortWait(); 178 ARoot = ARoot.getAccessibleChild(indexes[k]) 179 .getAccessibleContext(); 180 sel = UnoRuntime.queryInterface(XAccessibleSelection.class, ARoot); 181 } catch (com.sun.star.lang.IndexOutOfBoundsException e) { 182 } 183 } 184 185 shortWait(); 186 187 atw = tk.getActiveTopWindow(); 188 189 xWindow = UnoRuntime.queryInterface(XWindow.class, atw); 190 191 xRoot = AccessibilityTools.getAccessibleObject(xWindow); 192 193 //at.printAccessibleTree(new PrintWriter(System.out),xRoot); 194 195 XAccessibleAction action = UnoRuntime.queryInterface(XAccessibleAction.class, AccessibilityTools.getAccessibleObjectForRole(xRoot, AccessibleRole.PUSH_BUTTON, bName)); 196 197 try { 198 action.doAccessibleAction(0); 199 } catch (com.sun.star.lang.IndexOutOfBoundsException e) { 200 } 201 202 shortWait(); 203 204 atw = tk.getActiveTopWindow(); 205 206 xWindow = UnoRuntime.queryInterface(XWindow.class, atw); 207 208 xRoot = AccessibilityTools.getAccessibleObject(xWindow); 209 210 AccessibilityTools.printAccessibleTree(new PrintWriter(System.out),xRoot); 211 212 action = UnoRuntime.queryInterface(XAccessibleAction.class, AccessibilityTools.getAccessibleObjectForRole(xRoot, AccessibleRole.PUSH_BUTTON, "Yes")); 213 214 try { 215 if (action != null) action.doAccessibleAction(0); 216 } catch (com.sun.star.lang.IndexOutOfBoundsException e) { 217 } 218 219 shortWait(); 220 221 XDesktop xDesktop = getDesktop(); 222 223 XTextDocument returnDoc = UnoRuntime.queryInterface(XTextDocument.class, xDesktop.getCurrentComponent()); 224 225 if (destroyLocal) { 226 closeDoc(xTextDoc); 227 } 228 229 return returnDoc; 230 } 231 232 /** 233 * Sleeps for 2 sec. to allow StarOffice to react 234 */ shortWait()235 private void shortWait() { 236 try { 237 Thread.sleep(4000); 238 } catch (InterruptedException e) { 239 System.out.println("While waiting :" + e); 240 } 241 } 242 243 /** creates an instance of com.sun.star.awt.Toolkit to query the XExtendedToolkit 244 * interface 245 * @return returns the gained XExtendedToolkit Interface 246 */ getToolkit()247 public XExtendedToolkit getToolkit() { 248 Object toolkit = null; 249 250 try { 251 toolkit = m_xMSF.createInstance("com.sun.star.awt.Toolkit"); 252 } catch (com.sun.star.uno.Exception e) { 253 System.out.println("Couldn't get toolkit"); 254 e.printStackTrace( System.err ); 255 } 256 257 XExtendedToolkit tk = UnoRuntime.queryInterface(XExtendedToolkit.class, toolkit); 258 259 return tk; 260 } 261 262 /** creates an instance of com.sun.star.frame.Desktop to query the XDesktop interface 263 * @return returns the gained XDesktop interface 264 */ getDesktop()265 protected XDesktop getDesktop() { 266 Object desk = null; 267 268 try { 269 desk = m_xMSF.createInstance("com.sun.star.frame.Desktop"); 270 } catch (com.sun.star.uno.Exception e) { 271 System.out.println("Couldn't get desktop"); 272 e.printStackTrace( System.err ); 273 } 274 275 XDesktop xDesktop = UnoRuntime.queryInterface(XDesktop.class, desk); 276 277 return xDesktop; 278 } 279 }