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