2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 import com
.sun
.star
.awt
.XWindow
;
21 import com
.sun
.star
.beans
.PropertyValue
;
22 import com
.sun
.star
.container
.XIndexAccess
;
23 import com
.sun
.star
.container
.XEnumerationAccess
;
24 import com
.sun
.star
.container
.XEnumeration
;
26 import com
.sun
.star
.frame
.XComponentLoader
;
27 import com
.sun
.star
.frame
.XController
;
28 import com
.sun
.star
.frame
.XDesktop
;
29 import com
.sun
.star
.frame
.XFrame
;
30 import com
.sun
.star
.frame
.XModel
;
31 import com
.sun
.star
.frame
.XTasksSupplier
;
32 import com
.sun
.star
.frame
.XTask
;
34 import com
.sun
.star
.lang
.XComponent
;
35 import com
.sun
.star
.lang
.XMultiServiceFactory
;
36 import com
.sun
.star
.uno
.UnoRuntime
;
37 import com
.sun
.star
.uno
.XInterface
;
38 import com
.sun
.star
.drawing
.XDrawView
;
39 import com
.sun
.star
.drawing
.XDrawPage
;
40 import com
.sun
.star
.drawing
.XShape
;
41 import com
.sun
.star
.accessibility
.XAccessible
;
42 import com
.sun
.star
.accessibility
.XAccessibleContext
;
43 import com
.sun
.star
.awt
.XExtendedToolkit
;
46 /** This class tries to simplify some tasks like loading a document or
47 getting various objects.
49 public class SimpleOffice
51 XDesktop mxDesktop
= null;
52 OfficeConnection aConnection
;
55 public SimpleOffice (int nPortNumber
)
57 mnPortNumber
= nPortNumber
;
62 public void connect ()
64 aConnection
= new OfficeConnection (mnPortNumber
);
69 public XModel
loadDocument (String URL
)
74 // Load the document from the specified URL.
75 XComponentLoader xLoader
=
76 UnoRuntime
.queryInterface(
77 XComponentLoader
.class, mxDesktop
);
79 XComponent xComponent
= xLoader
.loadComponentFromURL (
86 xModel
= UnoRuntime
.queryInterface(
87 XModel
.class, xComponent
);
89 catch (java
.lang
.NullPointerException e
)
91 MessageArea
.println ("caught exception while loading "
96 MessageArea
.println ("caught exception while loading "
105 public XModel
getModel (String name
)
107 XModel xModel
= null;
110 XTasksSupplier xTasksSupplier
=
111 UnoRuntime
.queryInterface(
112 XTasksSupplier
.class, mxDesktop
);
113 XEnumerationAccess xEA
= xTasksSupplier
.getTasks();
114 XEnumeration xE
= xEA
.createEnumeration();
115 while (xE
.hasMoreElements())
117 XTask xTask
= UnoRuntime
.queryInterface(
118 XTask
.class, xE
.nextElement());
119 MessageArea
.print (xTask
.getName());
124 MessageArea
.println ("caught exception while getting Model " + name
131 public XModel
getModel (XDrawView xView
)
133 XController xController
= UnoRuntime
.queryInterface(
134 XController
.class, xView
);
135 if (xController
!= null)
136 return xController
.getModel();
139 MessageArea
.println ("can't cast view to controller");
144 public XDesktop
getDesktop ()
146 if (mxDesktop
!= null)
150 // Get the factory of the connected office.
151 XMultiServiceFactory xMSF
= aConnection
.getServiceManager ();
154 MessageArea
.println ("can't connect to office");
158 MessageArea
.println ("Connected successfully.");
160 // Create a new desktop.
161 mxDesktop
= UnoRuntime
.queryInterface(
163 xMSF
.createInstance ("com.sun.star.frame.Desktop")
168 MessageArea
.println ("caught exception while creating desktop: "
176 /** Return a reference to the extended toolkit which is a broadcaster of
177 top window, key, and focus events.
179 public XExtendedToolkit
getExtendedToolkit ()
181 XExtendedToolkit xToolkit
= null;
184 // Get the factory of the connected office.
185 XMultiServiceFactory xMSF
= aConnection
.getServiceManager ();
188 xToolkit
= UnoRuntime
.queryInterface(
189 XExtendedToolkit
.class,
190 xMSF
.createInstance ("stardiv.Toolkit.VCLXToolkit")
196 MessageArea
.println ("caught exception while creating extended toolkit: " + e
);
204 public XAccessible
getAccessibleObject (XInterface xObject
)
206 XAccessible xAccessible
= null;
209 xAccessible
= UnoRuntime
.queryInterface(
210 XAccessible
.class, xObject
);
214 MessageArea
.println (
215 "caught exception while getting accessible object" + e
);
221 /** Return the root object of the accessibility hierarchy.
223 public XAccessible
getAccessibleRoot (XAccessible xAccessible
)
227 XAccessible xParent
= null;
230 XAccessibleContext xContext
= xAccessible
.getAccessibleContext();
231 if (xContext
!= null)
232 xParent
= xContext
.getAccessibleParent();
234 xAccessible
= xParent
;
236 while (xParent
!= null);
240 MessageArea
.println (
241 "caught exception while getting accessible root" + e
);
250 /** @descr Return the current window associated with the given
253 public XWindow
getCurrentWindow ()
255 return getCurrentWindow (UnoRuntime
.queryInterface(
256 XModel
.class, getDesktop()));
263 public XWindow
getCurrentWindow (XModel xModel
)
265 XWindow xWindow
= null;
269 MessageArea
.println ("invalid model (==null)");
270 XController xController
= xModel
.getCurrentController();
271 if (xController
== null)
272 MessageArea
.println ("can't get controller from model");
273 XFrame xFrame
= xController
.getFrame();
275 MessageArea
.println ("can't get frame from controller");
276 xWindow
= xFrame
.getComponentWindow ();
278 MessageArea
.println ("can't get window from frame");
282 MessageArea
.println ("caught exception while getting current window" + e
);
289 /** @descr Return the current draw page of the given desktop.
291 public XDrawPage
getCurrentDrawPage ()
293 return getCurrentDrawPage (UnoRuntime
.queryInterface(
294 XDrawView
.class, getCurrentView()));
300 public XDrawPage
getCurrentDrawPage (XDrawView xView
)
302 XDrawPage xPage
= null;
306 MessageArea
.println ("can't get current draw page from null view");
308 xPage
= xView
.getCurrentPage();
312 MessageArea
.println ("caught exception while getting current draw page : " + e
);
321 /** @descr Return the current view of the given desktop.
323 public XDrawView
getCurrentView ()
325 return getCurrentView (getDesktop());
328 public XDrawView
getCurrentView (XDesktop xDesktop
)
330 if (xDesktop
== null)
331 MessageArea
.println ("can't get desktop to retrieve current view");
333 XDrawView xView
= null;
336 XComponent xComponent
= xDesktop
.getCurrentComponent();
337 if (xComponent
== null)
338 MessageArea
.println ("can't get component to retrieve current view");
340 XFrame xFrame
= xDesktop
.getCurrentFrame();
342 MessageArea
.println ("can't get frame to retrieve current view");
344 XController xController
= xFrame
.getController();
345 if (xController
== null)
346 MessageArea
.println ("can't get controller to retrieve current view");
348 xView
= UnoRuntime
.queryInterface(
349 XDrawView
.class, xController
);
351 MessageArea
.println ("could not cast controller into view");
355 MessageArea
.println ("caught exception while getting current view : " + e
);
364 // Return the accessible object of the document window.
365 public static XAccessible
getAccessibleDocumentWindow (XDrawPage xPage
)
367 XIndexAccess xShapeList
= UnoRuntime
.queryInterface(
368 XIndexAccess
.class, xPage
);
369 if (xShapeList
.getCount() > 0)
371 // All shapes return as accessible object the document window's
372 // accessible object. This is, of course, a hack and will be
373 // removed as soon as the missing infrastructure for obtaining
374 // the object directly is implemented.
375 XShape xShape
= null;
377 xShape
= UnoRuntime
.queryInterface(
378 XShape
.class, xShapeList
.getByIndex (0));
379 } catch (Exception e
)
381 XAccessible xAccessible
= UnoRuntime
.queryInterface (
382 XAccessible
.class, xShape
);