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