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 package org.openoffice.accessibility.misc; 23 24 import java.lang.Thread; 25 26 import com.sun.star.awt.Rectangle; 27 import com.sun.star.awt.XExtendedToolkit; 28 import com.sun.star.awt.XWindow; 29 30 import com.sun.star.beans.PropertyValue; 31 import com.sun.star.beans.XPropertySet; 32 33 import com.sun.star.container.XIndexAccess; 34 import com.sun.star.container.XChild; 35 import com.sun.star.container.XEnumerationAccess; 36 import com.sun.star.container.XEnumeration; 37 38 import com.sun.star.frame.XComponentLoader; 39 import com.sun.star.frame.XController; 40 import com.sun.star.frame.XDesktop; 41 import com.sun.star.frame.XFrame; 42 import com.sun.star.frame.XModel; 43 import com.sun.star.frame.XTasksSupplier; 44 import com.sun.star.frame.XTask; 45 46 import com.sun.star.lang.XComponent; 47 import com.sun.star.lang.XMultiServiceFactory; 48 import com.sun.star.lang.XServiceInfo; 49 import com.sun.star.lang.XServiceName; 50 import com.sun.star.lang.XTypeProvider; 51 52 import com.sun.star.uno.UnoRuntime; 53 import com.sun.star.uno.XInterface; 54 import com.sun.star.uno.Type; 55 56 import com.sun.star.drawing.XDrawView; 57 import com.sun.star.drawing.XDrawPage; 58 import com.sun.star.drawing.XShapes; 59 import com.sun.star.drawing.XShape; 60 import com.sun.star.drawing.XShapeDescriptor; 61 62 import com.sun.star.accessibility.XAccessible; 63 import com.sun.star.accessibility.XAccessibleContext; 64 import com.sun.star.accessibility.XAccessibleComponent; 65 import com.sun.star.accessibility.XAccessibleRelationSet; 66 import com.sun.star.accessibility.XAccessibleStateSet; 67 68 69 /** This singleton class tries to simplify some tasks like loading a document 70 or getting various objects. 71 */ 72 public class SimpleOffice 73 { Instance()74 synchronized static public SimpleOffice Instance () 75 { 76 if (saInstance == null) 77 saInstance = new SimpleOffice (); 78 79 return saInstance; 80 } 81 Clear()82 synchronized static public void Clear () 83 { 84 saInstance = null; 85 } 86 87 LoadDocument(String URL)88 public XModel LoadDocument (String URL) 89 { 90 XModel xModel = null; 91 try 92 { 93 // Load the document from the specified URL. 94 XComponentLoader xLoader = 95 (XComponentLoader)UnoRuntime.queryInterface( 96 XComponentLoader.class, mxDesktop); 97 98 XComponent xComponent = xLoader.loadComponentFromURL ( 99 URL, 100 "_blank", 101 0, 102 new PropertyValue[0] 103 ); 104 105 xModel = (XModel) UnoRuntime.queryInterface( 106 XModel.class, xComponent); 107 } 108 catch (java.lang.NullPointerException e) 109 { 110 MessageArea.println ("caught exception while loading " 111 + URL + " : " + e); 112 } 113 catch (Exception e) 114 { 115 MessageArea.println ("caught exception while loading " 116 + URL + " : " + e); 117 } 118 return xModel; 119 } 120 121 122 123 GetModel(String name)124 public XModel GetModel (String name) 125 { 126 XModel xModel = null; 127 try 128 { 129 XTasksSupplier xTasksSupplier = 130 (XTasksSupplier) UnoRuntime.queryInterface( 131 XTasksSupplier.class, mxDesktop); 132 XEnumerationAccess xEA = xTasksSupplier.getTasks(); 133 XEnumeration xE = xEA.createEnumeration(); 134 while (xE.hasMoreElements()) 135 { 136 XTask xTask = (XTask) UnoRuntime.queryInterface( 137 XTask.class, xE.nextElement()); 138 MessageArea.print (xTask.getName()); 139 } 140 } 141 catch (Exception e) 142 { 143 MessageArea.println ("caught exception while getting Model " + name 144 + ": " + e); 145 } 146 return xModel; 147 } 148 149 GetModel(XDrawView xView)150 public XModel GetModel (XDrawView xView) 151 { 152 XController xController = (XController) UnoRuntime.queryInterface( 153 XController.class, xView); 154 if (xController != null) 155 return xController.getModel(); 156 else 157 { 158 MessageArea.println ("can't cast view to controller"); 159 return null; 160 } 161 } 162 163 164 165 GetDesktop()166 public XDesktop GetDesktop () 167 { 168 if (mxDesktop != null) 169 return mxDesktop; 170 try 171 { 172 // Get the factory of the connected office. 173 XMultiServiceFactory xMSF = 174 OfficeConnection.Instance().GetServiceManager (); 175 if (xMSF == null) 176 { 177 MessageArea.println ("can't connect to office"); 178 return null; 179 } 180 else 181 MessageArea.println ("Connected successfully."); 182 183 // Create a new desktop. 184 mxDesktop = (XDesktop) UnoRuntime.queryInterface( 185 XDesktop.class, 186 xMSF.createInstance ("com.sun.star.frame.Desktop") 187 ); 188 } 189 catch (Exception e) 190 { 191 MessageArea.println ("caught exception while creating desktop: " 192 + e); 193 } 194 195 return mxDesktop; 196 } 197 198 199 /** Return a reference to the extended toolkit which is a broadcaster of 200 top window, key, and focus events. 201 */ GetExtendedToolkit()202 public XExtendedToolkit GetExtendedToolkit () 203 { 204 XExtendedToolkit xToolkit = null; 205 if (this != null) 206 try 207 { 208 // Get the factory of the connected office. 209 XMultiServiceFactory xMSF = 210 OfficeConnection.Instance().GetServiceManager (); 211 if (xMSF != null) 212 { 213 xToolkit = (XExtendedToolkit) UnoRuntime.queryInterface( 214 XExtendedToolkit.class, 215 xMSF.createInstance ("stardiv.Toolkit.VCLXToolkit") 216 ); 217 } 218 } 219 catch (Exception e) 220 { 221 MessageArea.println ( 222 "caught exception while creating extended toolkit: " + e); 223 } 224 225 return xToolkit; 226 } 227 228 229 GetAccessibleObject(XInterface xObject)230 static public XAccessible GetAccessibleObject (XInterface xObject) 231 { 232 XAccessible xAccessible = null; 233 try 234 { 235 xAccessible = (XAccessible) UnoRuntime.queryInterface( 236 XAccessible.class, xObject); 237 } 238 catch (Exception e) 239 { 240 System.err.println ( 241 "caught exception while getting accessible object" + e); 242 e.printStackTrace (System.err); 243 } 244 return xAccessible; 245 } 246 GetAccessibleContext(XInterface xObject)247 static public XAccessibleContext GetAccessibleContext (XInterface xObject) 248 { 249 XAccessible xAccessible = GetAccessibleObject (xObject); 250 if (xAccessible != null) 251 return xAccessible.getAccessibleContext(); 252 else 253 return null; 254 } 255 256 /** Return the root object of the accessibility hierarchy. 257 */ GetAccessibleRoot(XAccessible xAccessible)258 public XAccessible GetAccessibleRoot (XAccessible xAccessible) 259 { 260 try 261 { 262 XAccessible xParent = null; 263 do 264 { 265 XAccessibleContext xContext = xAccessible.getAccessibleContext(); 266 if (xContext != null) 267 xParent = xContext.getAccessibleParent(); 268 if (xParent != null) 269 xAccessible = xParent; 270 } 271 while (xParent != null); 272 } 273 catch (Exception e) 274 { 275 MessageArea.println ( 276 "caught exception while getting accessible root" + e); 277 e.printStackTrace(); 278 } 279 return xAccessible; 280 } 281 282 283 284 285 /** @descr Return the current window associated with the given 286 model. 287 */ GetCurrentWindow()288 public XWindow GetCurrentWindow () 289 { 290 return GetCurrentWindow ((XModel) UnoRuntime.queryInterface( 291 XModel.class, GetDesktop())); 292 } 293 294 295 296 297 GetCurrentWindow(XModel xModel)298 public XWindow GetCurrentWindow (XModel xModel) 299 { 300 XWindow xWindow = null; 301 try 302 { 303 if (xModel == null) 304 MessageArea.println ("invalid model (==null)"); 305 XController xController = xModel.getCurrentController(); 306 if (xController == null) 307 MessageArea.println ("can't get controller from model"); 308 XFrame xFrame = xController.getFrame(); 309 if (xFrame == null) 310 MessageArea.println ("can't get frame from controller"); 311 xWindow = xFrame.getComponentWindow (); 312 if (xWindow == null) 313 MessageArea.println ("can't get window from frame"); 314 } 315 catch (Exception e) 316 { 317 MessageArea.println ("caught exception while getting current window" + e); 318 } 319 320 return xWindow; 321 } 322 323 324 /** @descr Return the current draw page of the given desktop. 325 */ GetCurrentDrawPage()326 public XDrawPage GetCurrentDrawPage () 327 { 328 return GetCurrentDrawPage ( 329 (XDrawView) UnoRuntime.queryInterface( 330 XDrawView.class, 331 GetCurrentView())); 332 } 333 334 335 336 GetCurrentDrawPage(XDrawView xView)337 public XDrawPage GetCurrentDrawPage (XDrawView xView) 338 { 339 XDrawPage xPage = null; 340 try 341 { 342 if (xView == null) 343 MessageArea.println ("can't get current draw page from null view"); 344 else 345 xPage = xView.getCurrentPage(); 346 } 347 catch (Exception e) 348 { 349 MessageArea.println ("caught exception while getting current draw page : " + e); 350 } 351 352 return xPage; 353 } 354 355 356 357 358 /** @descr Return the current view of the given desktop. 359 */ GetCurrentView()360 public XDrawView GetCurrentView () 361 { 362 return GetCurrentView (GetDesktop()); 363 } 364 GetCurrentView(XDesktop xDesktop)365 public XDrawView GetCurrentView (XDesktop xDesktop) 366 { 367 if (xDesktop == null) 368 MessageArea.println ("can't get desktop to retrieve current view"); 369 370 XDrawView xView = null; 371 try 372 { 373 XComponent xComponent = xDesktop.getCurrentComponent(); 374 if (xComponent == null) 375 MessageArea.println ("can't get component to retrieve current view"); 376 377 XFrame xFrame = xDesktop.getCurrentFrame(); 378 if (xFrame == null) 379 MessageArea.println ("can't get frame to retrieve current view"); 380 381 XController xController = xFrame.getController(); 382 if (xController == null) 383 MessageArea.println ("can't get controller to retrieve current view"); 384 385 xView = (XDrawView) UnoRuntime.queryInterface( 386 XDrawView.class, xController); 387 if (xView == null) 388 MessageArea.println ("could not cast controller into view"); 389 } 390 catch (Exception e) 391 { 392 MessageArea.println ("caught exception while getting current view : " + e); 393 } 394 395 return xView; 396 } 397 398 399 400 401 // Return the accessible object of the document window. GetAccessibleDocumentWindow(XDrawPage xPage)402 public static XAccessible GetAccessibleDocumentWindow (XDrawPage xPage) 403 { 404 XIndexAccess xShapeList = (XIndexAccess) UnoRuntime.queryInterface( 405 XIndexAccess.class, xPage); 406 if (xShapeList.getCount() > 0) 407 { 408 // All shapes return as accessible object the document window's 409 // accessible object. This is, of course, a hack and will be 410 // removed as soon as the missing infrastructure for obtaining 411 // the object directly is implemented. 412 XShape xShape = null; 413 try{ 414 xShape = (XShape) UnoRuntime.queryInterface( 415 XShape.class, xShapeList.getByIndex (0)); 416 } catch (Exception e) 417 {} 418 XAccessible xAccessible = (XAccessible) UnoRuntime.queryInterface ( 419 XAccessible.class, xShape); 420 return xAccessible; 421 } 422 else 423 return null; 424 } 425 SimpleOffice()426 private SimpleOffice () 427 { 428 } 429 430 431 432 private XDesktop mxDesktop; 433 private static SimpleOffice saInstance = null; 434 } 435