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 .
20 import helper
.ConfigHelper
;
22 import java
.util
.ArrayList
;
24 import lib
.StatusException
;
26 import com
.sun
.star
.awt
.Rectangle
;
27 import com
.sun
.star
.awt
.WindowDescriptor
;
28 import com
.sun
.star
.awt
.XToolkit
;
29 import com
.sun
.star
.awt
.XTopWindow
;
30 import com
.sun
.star
.awt
.XWindowPeer
;
31 import com
.sun
.star
.beans
.PropertyValue
;
32 import com
.sun
.star
.beans
.XPropertySet
;
33 import com
.sun
.star
.container
.XEnumeration
;
34 import com
.sun
.star
.frame
.XComponentLoader
;
35 import com
.sun
.star
.frame
.XDesktop
;
36 import com
.sun
.star
.frame
.XFrame
;
37 import com
.sun
.star
.frame
.XModel
;
38 import com
.sun
.star
.lang
.XComponent
;
39 import com
.sun
.star
.lang
.XMultiServiceFactory
;
40 import com
.sun
.star
.lang
.XServiceInfo
;
41 import com
.sun
.star
.uno
.UnoRuntime
;
42 import com
.sun
.star
.uno
.XInterface
;
43 import com
.sun
.star
.util
.XCloseable
;
44 import com
.sun
.star
.util
.XModifiable
;
45 import com
.sun
.star
.view
.XViewSettingsSupplier
;
48 * contains helper methods for the Desktop
50 public class DesktopTools
54 * Queries the XComponentLoader
56 * @param xMSF the MultiServiceFactory
57 * @return the gained XComponentLoader
59 private static XComponentLoader
getCLoader(XMultiServiceFactory xMSF
)
61 XComponentLoader oCLoader
= UnoRuntime
.queryInterface(
62 XComponentLoader
.class, createDesktop(xMSF
));
68 * Creates an Instance of the Desktop service
70 * @param xMSF the MultiServiceFactory
71 * @return the gained XDesktop object
73 public static XDesktop
createDesktop(XMultiServiceFactory xMSF
)
79 xDesktop
= UnoRuntime
.queryInterface(
80 XDesktop
.class, xMSF
.createInstance("com.sun.star.comp.framework.Desktop"));
82 catch (com
.sun
.star
.uno
.Exception e
)
84 throw new IllegalArgumentException("Desktop Service not available", e
);
91 * returns a XEnumeration containing all components containing on the desktop
92 * @param xMSF the XMultiServiceFactory
93 * @return XEnumeration of all components on the desktop
95 public static XEnumeration
getAllComponents(XMultiServiceFactory xMSF
)
97 return createDesktop(xMSF
).getComponents().createEnumeration();
103 * returns the current component on the desktop
104 * @param xMSF the XMultiServiceFactory
105 * @return XComponent of the current component on the desktop
107 public static XFrame
getCurrentFrame(XMultiServiceFactory xMSF
)
109 return createDesktop(xMSF
).getCurrentFrame();
113 * returns an object array of all open documents
114 * @param xMSF the MultiServiceFactory
115 * @return returns an Array of document kinds like ["swriter"]
117 public static Object
[] getAllOpenDocuments(XMultiServiceFactory xMSF
)
119 ArrayList
<XComponent
> components
= new ArrayList
<XComponent
>();
121 XEnumeration allComp
= getAllComponents(xMSF
);
123 while (allComp
.hasMoreElements())
127 XComponent xComponent
= UnoRuntime
.queryInterface(
128 XComponent
.class, allComp
.nextElement());
130 if (getDocumentType(xComponent
) != null)
132 components
.add(xComponent
);
136 catch (com
.sun
.star
.container
.NoSuchElementException e
)
139 catch (com
.sun
.star
.lang
.WrappedTargetException e
)
143 return components
.toArray();
147 * Returns the document type for the given XComponent of an document
148 * @param xComponent the document to query for its type
156 * or <CODE>null</CODE>
158 private static String
getDocumentType(XComponent xComponent
)
160 XServiceInfo sInfo
= UnoRuntime
.queryInterface(
161 XServiceInfo
.class, xComponent
);
167 else if (sInfo
.supportsService("com.sun.star.sheet.SpreadsheetDocument"))
171 else if (sInfo
.supportsService("com.sun.star.text.TextDocument"))
175 else if (sInfo
.supportsService("com.sun.star.drawing.DrawingDocument"))
179 else if (sInfo
.supportsService("com.sun.star.presentation.PresentationDocument"))
183 else if (sInfo
.supportsService("com.sun.star.formula.FormulaProperties"))
194 * Opens a new document of a given kind
196 * @return the XComponent Interface of the document
197 * @param kind the kind of document to load.<br>
205 * @param Args arguments which passed to the document to load
206 * @param xMSF the MultiServiceFactory
208 public static XComponent
openNewDoc(XMultiServiceFactory xMSF
, String kind
,
209 PropertyValue
[] Args
)
211 XComponent oDoc
= null;
215 oDoc
= getCLoader(xMSF
).loadComponentFromURL("private:factory/" + kind
,
218 catch (com
.sun
.star
.uno
.Exception e
)
220 throw new IllegalArgumentException("Document could not be opened", e
);
227 * loads a document of from a given url
229 * @return the XComponent Interface of the document
230 * @param url the URL of the document to load.
231 * @param Args arguments which passed to the document to load
232 * @param xMSF the MultiServiceFactory
234 public static XComponent
loadDoc(XMultiServiceFactory xMSF
, String url
,
235 PropertyValue
[] Args
)
237 XComponent oDoc
= null;
240 Args
= new PropertyValue
[0];
244 oDoc
= getCLoader(xMSF
).loadComponentFromURL(url
, "_blank", 0, Args
);
246 catch (com
.sun
.star
.uno
.Exception e
)
248 throw new IllegalArgumentException("Document could not be loaded", e
);
251 bringWindowToFront(oDoc
);
256 * closes a given document
257 * @param DocumentToClose the document to close
259 public static void closeDoc(XInterface DocumentToClose
)
261 if (DocumentToClose
== null)
266 String kd
= System
.getProperty("KeepDocument");
269 System
.out
.println("The property 'KeepDocument' is set and so the document won't be disposed");
272 XModifiable modified
= UnoRuntime
.queryInterface(XModifiable
.class, DocumentToClose
);
273 XCloseable closer
= UnoRuntime
.queryInterface(XCloseable
.class, DocumentToClose
);
277 if (modified
!= null)
279 modified
.setModified(false);
283 catch (com
.sun
.star
.util
.CloseVetoException e
)
285 System
.out
.println("Couldn't close document");
287 catch (com
.sun
.star
.lang
.DisposedException e
)
289 System
.out
.println("Couldn't close document");
291 catch (NullPointerException e
)
293 System
.out
.println("Couldn't close document");
295 catch (com
.sun
.star
.beans
.PropertyVetoException e
)
297 System
.out
.println("Couldn't close document");
302 * Creates a floating XWindow with the size of X=500 Y=100 width=400 height=600
303 * @param xMSF the MultiServiceFactory
304 * @throws lib.StatusException if it is not possible to create a floating window a lib.StatusException was thrown
305 * @return a floating XWindow
307 public static XWindowPeer
createFloatingWindow(XMultiServiceFactory xMSF
)
308 throws StatusException
310 return createFloatingWindow(xMSF
, 500, 100, 400, 600);
314 * Creates a floating XWindow on the given position and size.
315 * @return a floating XWindow
316 * @param X the X-Position of the floating XWindow
317 * @param Y the Y-Position of the floating XWindow
318 * @param width the width of the floating XWindow
319 * @param height the height of the floating XWindow
320 * @param xMSF the MultiServiceFactory
321 * @throws lib.StatusException if it is not possible to create a floating window a lib.StatusException was thrown
323 public static XWindowPeer
createFloatingWindow(XMultiServiceFactory xMSF
, int X
, int Y
, int width
, int height
)
324 throws StatusException
327 XInterface oObj
= null;
331 oObj
= (XInterface
) xMSF
.createInstance("com.sun.star.awt.Toolkit");
333 catch (com
.sun
.star
.uno
.Exception e
)
335 throw new StatusException("Couldn't get toolkit", e
);
338 XToolkit tk
= UnoRuntime
.queryInterface(
339 XToolkit
.class, oObj
);
341 WindowDescriptor descriptor
= new com
.sun
.star
.awt
.WindowDescriptor();
343 descriptor
.Type
= com
.sun
.star
.awt
.WindowClass
.TOP
;
344 descriptor
.WindowServiceName
= "modelessdialog";
345 descriptor
.ParentIndex
= -1;
347 Rectangle bounds
= new com
.sun
.star
.awt
.Rectangle();
350 bounds
.Width
= width
;
351 bounds
.Height
= height
;
353 descriptor
.Bounds
= bounds
;
354 descriptor
.WindowAttributes
= (com
.sun
.star
.awt
.WindowAttribute
.BORDER
+
355 com
.sun
.star
.awt
.WindowAttribute
.MOVEABLE
+
356 com
.sun
.star
.awt
.WindowAttribute
.SIZEABLE
+
357 com
.sun
.star
.awt
.WindowAttribute
.CLOSEABLE
+
358 com
.sun
.star
.awt
.VclWindowPeerAttribute
.CLIPCHILDREN
);
360 XWindowPeer xWindow
= null;
364 xWindow
= tk
.createWindow(descriptor
);
366 catch (com
.sun
.star
.lang
.IllegalArgumentException e
)
368 throw new StatusException("Could not create window", e
);
376 * zoom to have a view over the hole page
377 * @param xDoc the document to zoom
379 public static void zoomToEntirePage(XMultiServiceFactory xMSF
, XInterface xDoc
)
383 XModel xMod
= UnoRuntime
.queryInterface(XModel
.class, xDoc
);
384 XInterface oCont
= xMod
.getCurrentController();
385 XViewSettingsSupplier oVSSupp
= UnoRuntime
.queryInterface(XViewSettingsSupplier
.class, oCont
);
387 XInterface oViewSettings
= oVSSupp
.getViewSettings();
388 XPropertySet oViewProp
= UnoRuntime
.queryInterface(XPropertySet
.class, oViewSettings
);
389 oViewProp
.setPropertyValue("ZoomType",
390 Short
.valueOf(com
.sun
.star
.view
.DocumentZoomType
.ENTIRE_PAGE
));
392 util
.utils
.waitForEventIdle(xMSF
);
396 System
.out
.println("Could not zoom to entire page: " + e
.toString());
402 * This function docks the Navigator onto the right side of the window.</p>
404 * Since the svt.viewoptions cache the view configuration at start up
405 * the change of the docking will be effective at a restart.
406 * @param xMSF the XMultiServiceFactory
408 public static void dockNavigator(XMultiServiceFactory xMSF
)
410 // prepare Window settings
413 ConfigHelper aConfig
= new ConfigHelper(xMSF
,
414 "org.openoffice.Office.Views", false);
416 aConfig
.getOrInsertGroup("Windows", "10366");
418 aConfig
.updateGroupProperty(
419 "Windows", "10366", "WindowState", "952,180,244,349;1;0,0,0,0;");
421 aConfig
.insertOrUpdateExtensibleGroupProperty(
422 "Windows", "10366", "UserData", "Data", "V2,V,0,AL:(5,16,0/0/244/349,244;610)");
424 // Is node "SplitWindow2" available? If not, insert it.
425 aConfig
.getOrInsertGroup("Windows", "SplitWindow2");
427 aConfig
.insertOrUpdateExtensibleGroupProperty(
428 "Windows", "SplitWindow2", "UserData", "UserItem", "V1,2,1,0,10366");
434 catch (com
.sun
.star
.uno
.Exception e
)
442 * This function brings a document to the front.<P>
443 * NOTE: it is not possible to change the window order of your Window-Manager!!
444 * Only the order of Office documents are changeable.
445 * @param xModel the XModel of the document to bring to top
447 public static void bringWindowToFront(XModel xModel
)
449 XTopWindow xTopWindow
=
450 UnoRuntime
.queryInterface(
452 xModel
.getCurrentController().getFrame().getContainerWindow());
454 xTopWindow
.toFront();
457 public static void bringWindowToFront(XComponent xComponent
)
459 XModel xModel
= UnoRuntime
.queryInterface(XModel
.class, xComponent
);
462 bringWindowToFront(xModel
);