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
.io
.BufferedInputStream
;
23 import java
.io
.ByteArrayOutputStream
;
24 import java
.io
.FileInputStream
;
25 import java
.io
.InputStream
;
26 import java
.util
.ArrayList
;
28 import lib
.StatusException
;
30 import com
.sun
.star
.awt
.Rectangle
;
31 import com
.sun
.star
.awt
.WindowDescriptor
;
32 import com
.sun
.star
.awt
.XToolkit
;
33 import com
.sun
.star
.awt
.XTopWindow
;
34 import com
.sun
.star
.awt
.XWindowPeer
;
35 import com
.sun
.star
.beans
.PropertyValue
;
36 import com
.sun
.star
.beans
.XPropertySet
;
37 import com
.sun
.star
.container
.XEnumeration
;
38 import com
.sun
.star
.frame
.XComponentLoader
;
39 import com
.sun
.star
.frame
.XDesktop
;
40 import com
.sun
.star
.frame
.XFrame
;
41 import com
.sun
.star
.frame
.XModel
;
42 import com
.sun
.star
.io
.XInputStream
;
43 import com
.sun
.star
.lang
.XComponent
;
44 import com
.sun
.star
.lang
.XMultiServiceFactory
;
45 import com
.sun
.star
.lang
.XServiceInfo
;
46 import com
.sun
.star
.lib
.uno
.adapter
.ByteArrayToXInputStreamAdapter
;
47 import com
.sun
.star
.uno
.UnoRuntime
;
48 import com
.sun
.star
.uno
.XInterface
;
49 import com
.sun
.star
.util
.XCloseable
;
50 import com
.sun
.star
.util
.XModifiable
;
51 import com
.sun
.star
.view
.XViewSettingsSupplier
;
54 * contains helper methods for the Desktop
56 public class DesktopTools
60 * Queries the XComponentLoader
62 * @param xMSF the MultiServiceFactory
63 * @return the gained XComponentLoader
65 private static XComponentLoader
getCLoader(XMultiServiceFactory xMSF
)
67 XComponentLoader oCLoader
= UnoRuntime
.queryInterface(
68 XComponentLoader
.class, createDesktop(xMSF
));
74 * Creates an Instance of the Desktop service
76 * @param xMSF the MultiServiceFactory
77 * @return the gained XDesktop object
79 public static XDesktop
createDesktop(XMultiServiceFactory xMSF
)
85 xDesktop
= UnoRuntime
.queryInterface(
86 XDesktop
.class, xMSF
.createInstance("com.sun.star.comp.framework.Desktop"));
88 catch (com
.sun
.star
.uno
.Exception e
)
90 throw new IllegalArgumentException("Desktop Service not available", e
);
97 * returns a XEnumeration containing all components containing on the desktop
98 * @param xMSF the XMultiServiceFactory
99 * @return XEnumeration of all components on the desktop
101 public static XEnumeration
getAllComponents(XMultiServiceFactory xMSF
)
103 return createDesktop(xMSF
).getComponents().createEnumeration();
109 * returns the current component on the desktop
110 * @param xMSF the XMultiServiceFactory
111 * @return XComponent of the current component on the desktop
113 public static XFrame
getCurrentFrame(XMultiServiceFactory xMSF
)
115 return createDesktop(xMSF
).getCurrentFrame();
119 * returns an object array of all open documents
120 * @param xMSF the MultiServiceFactory
121 * @return returns an Array of document kinds like ["swriter"]
123 public static Object
[] getAllOpenDocuments(XMultiServiceFactory xMSF
)
125 ArrayList
<XComponent
> components
= new ArrayList
<XComponent
>();
127 XEnumeration allComp
= getAllComponents(xMSF
);
129 while (allComp
.hasMoreElements())
133 XComponent xComponent
= UnoRuntime
.queryInterface(
134 XComponent
.class, allComp
.nextElement());
136 if (getDocumentType(xComponent
) != null)
138 components
.add(xComponent
);
142 catch (com
.sun
.star
.container
.NoSuchElementException e
)
145 catch (com
.sun
.star
.lang
.WrappedTargetException e
)
149 return components
.toArray();
153 * Returns the document type for the given XComponent of a document
154 * @param xComponent the document to query for its type
162 * or <CODE>null</CODE>
164 private static String
getDocumentType(XComponent xComponent
)
166 XServiceInfo sInfo
= UnoRuntime
.queryInterface(
167 XServiceInfo
.class, xComponent
);
173 else if (sInfo
.supportsService("com.sun.star.sheet.SpreadsheetDocument"))
177 else if (sInfo
.supportsService("com.sun.star.text.TextDocument"))
181 else if (sInfo
.supportsService("com.sun.star.drawing.DrawingDocument"))
185 else if (sInfo
.supportsService("com.sun.star.presentation.PresentationDocument"))
189 else if (sInfo
.supportsService("com.sun.star.formula.FormulaProperties"))
200 * Opens a new document of a given kind
202 * @return the XComponent Interface of the document
203 * @param kind the kind of document to load.<br>
211 * @param Args arguments which passed to the document to load
212 * @param xMSF the MultiServiceFactory
214 public static XComponent
openNewDoc(XMultiServiceFactory xMSF
, String kind
,
215 PropertyValue
[] Args
)
217 XComponent oDoc
= null;
221 oDoc
= getCLoader(xMSF
).loadComponentFromURL("private:factory/" + kind
,
224 catch (com
.sun
.star
.uno
.Exception e
)
226 throw new IllegalArgumentException("Document could not be opened", e
);
233 * loads a document of from a given url
235 * @return the XComponent Interface of the document
236 * @param url the URL of the document to load.
237 * @param Args arguments which passed to the document to load
238 * @param xMSF the MultiServiceFactory
240 public static XComponent
loadDoc(XMultiServiceFactory xMSF
, String url
,
241 PropertyValue
[] Args
)
243 XComponent oDoc
= null;
246 Args
= new PropertyValue
[0];
250 oDoc
= getCLoader(xMSF
).loadComponentFromURL(url
, "_blank", 0, Args
);
252 catch (com
.sun
.star
.uno
.Exception e
)
254 throw new IllegalArgumentException("Document could not be loaded", e
);
257 bringWindowToFront(oDoc
);
262 * loads a document of from a given path using an input stream
264 * @param xMSF the MultiServiceFactory
265 * @param filePath the path of the document to load.
266 * @return the XComponent Interface of the document
268 public static XComponent
loadDocUsingStream(XMultiServiceFactory xMSF
, String filePath
)
270 XInputStream inputStream
= null;
272 final InputStream inputFile
= new BufferedInputStream(
273 new FileInputStream(filePath
));
275 final ByteArrayOutputStream bytes
= new ByteArrayOutputStream();
276 final byte[] byteBuffer
= new byte[4096];
277 int byteBufferLength
= 0;
278 while ((byteBufferLength
= inputFile
.read(byteBuffer
)) > 0)
279 bytes
.write(byteBuffer
, 0, byteBufferLength
);
280 inputStream
= new ByteArrayToXInputStreamAdapter(
281 bytes
.toByteArray());
285 } catch (java
.io
.IOException e
) {
289 PropertyValue
[] loadProps
= new PropertyValue
[1];
290 loadProps
[0] = new PropertyValue();
291 loadProps
[0].Name
= "InputStream";
292 loadProps
[0].Value
= inputStream
;
294 XComponent oDoc
= null;
297 oDoc
= getCLoader(xMSF
).loadComponentFromURL("private:stream", "_blank", 0, loadProps
);
299 catch (com
.sun
.star
.uno
.Exception e
)
301 throw new IllegalArgumentException("Document could not be loaded", e
);
307 * closes a given document
308 * @param DocumentToClose the document to close
310 public static void closeDoc(XInterface DocumentToClose
)
312 if (DocumentToClose
== null)
317 String kd
= System
.getProperty("KeepDocument");
320 System
.out
.println("The property 'KeepDocument' is set and so the document won't be disposed");
323 XModifiable modified
= UnoRuntime
.queryInterface(XModifiable
.class, DocumentToClose
);
324 XCloseable closer
= UnoRuntime
.queryInterface(XCloseable
.class, DocumentToClose
);
328 if (modified
!= null)
330 modified
.setModified(false);
334 catch (com
.sun
.star
.util
.CloseVetoException e
)
336 System
.out
.println("Couldn't close document");
338 catch (com
.sun
.star
.lang
.DisposedException e
)
340 System
.out
.println("Couldn't close document");
342 catch (NullPointerException e
)
344 System
.out
.println("Couldn't close document");
346 catch (com
.sun
.star
.beans
.PropertyVetoException e
)
348 System
.out
.println("Couldn't close document");
353 * Creates a floating XWindow with the size of X=500 Y=100 width=400 height=600
354 * @param xMSF the MultiServiceFactory
355 * @throws lib.StatusException if it is not possible to create a floating window a lib.StatusException was thrown
356 * @return a floating XWindow
358 public static XWindowPeer
createFloatingWindow(XMultiServiceFactory xMSF
)
359 throws StatusException
361 return createFloatingWindow(xMSF
, 500, 100, 400, 600);
365 * Creates a floating XWindow on the given position and size.
366 * @return a floating XWindow
367 * @param X the X-Position of the floating XWindow
368 * @param Y the Y-Position of the floating XWindow
369 * @param width the width of the floating XWindow
370 * @param height the height of the floating XWindow
371 * @param xMSF the MultiServiceFactory
372 * @throws lib.StatusException if it is not possible to create a floating window a lib.StatusException was thrown
374 public static XWindowPeer
createFloatingWindow(XMultiServiceFactory xMSF
, int X
, int Y
, int width
, int height
)
375 throws StatusException
378 XInterface oObj
= null;
382 oObj
= (XInterface
) xMSF
.createInstance("com.sun.star.awt.Toolkit");
384 catch (com
.sun
.star
.uno
.Exception e
)
386 throw new StatusException("Couldn't get toolkit", e
);
389 XToolkit tk
= UnoRuntime
.queryInterface(
390 XToolkit
.class, oObj
);
392 WindowDescriptor descriptor
= new com
.sun
.star
.awt
.WindowDescriptor();
394 descriptor
.Type
= com
.sun
.star
.awt
.WindowClass
.TOP
;
395 descriptor
.WindowServiceName
= "modelessdialog";
396 descriptor
.ParentIndex
= -1;
398 Rectangle bounds
= new com
.sun
.star
.awt
.Rectangle();
401 bounds
.Width
= width
;
402 bounds
.Height
= height
;
404 descriptor
.Bounds
= bounds
;
405 descriptor
.WindowAttributes
= (com
.sun
.star
.awt
.WindowAttribute
.BORDER
+
406 com
.sun
.star
.awt
.WindowAttribute
.MOVEABLE
+
407 com
.sun
.star
.awt
.WindowAttribute
.SIZEABLE
+
408 com
.sun
.star
.awt
.WindowAttribute
.CLOSEABLE
+
409 com
.sun
.star
.awt
.VclWindowPeerAttribute
.CLIPCHILDREN
);
411 XWindowPeer xWindow
= null;
415 xWindow
= tk
.createWindow(descriptor
);
417 catch (com
.sun
.star
.lang
.IllegalArgumentException e
)
419 throw new StatusException("Could not create window", e
);
427 * zoom to have a view over the hole page
428 * @param xDoc the document to zoom
430 public static void zoomToEntirePage(XMultiServiceFactory xMSF
, XInterface xDoc
)
434 XModel xMod
= UnoRuntime
.queryInterface(XModel
.class, xDoc
);
435 XInterface oCont
= xMod
.getCurrentController();
436 XViewSettingsSupplier oVSSupp
= UnoRuntime
.queryInterface(XViewSettingsSupplier
.class, oCont
);
438 XInterface oViewSettings
= oVSSupp
.getViewSettings();
439 XPropertySet oViewProp
= UnoRuntime
.queryInterface(XPropertySet
.class, oViewSettings
);
440 oViewProp
.setPropertyValue("ZoomType",
441 Short
.valueOf(com
.sun
.star
.view
.DocumentZoomType
.ENTIRE_PAGE
));
443 util
.utils
.waitForEventIdle(xMSF
);
447 System
.out
.println("Could not zoom to entire page: " + e
.toString());
453 * This function docks the Navigator onto the right side of the window.</p>
455 * Since the svt.viewoptions cache the view configuration at start up
456 * the change of the docking will be effective at a restart.
457 * @param xMSF the XMultiServiceFactory
459 public static void dockNavigator(XMultiServiceFactory xMSF
)
461 // prepare Window settings
464 ConfigHelper aConfig
= new ConfigHelper(xMSF
,
465 "org.openoffice.Office.Views", false);
467 aConfig
.getOrInsertGroup("Windows", "10366");
469 aConfig
.updateGroupProperty(
470 "Windows", "10366", "WindowState", "952,180,244,349;1;0,0,0,0;");
472 aConfig
.insertOrUpdateExtensibleGroupProperty(
473 "Windows", "10366", "UserData", "Data", "V2,V,0,AL:(5,16,0/0/244/349,244;610)");
475 // Is node "SplitWindow2" available? If not, insert it.
476 aConfig
.getOrInsertGroup("Windows", "SplitWindow2");
478 aConfig
.insertOrUpdateExtensibleGroupProperty(
479 "Windows", "SplitWindow2", "UserData", "UserItem", "V1,2,1,0,10366");
485 catch (com
.sun
.star
.uno
.Exception e
)
493 * This function brings a document to the front.<P>
494 * NOTE: it is not possible to change the window order of your Window-Manager!!
495 * Only the order of Office documents are changeable.
496 * @param xModel the XModel of the document to bring to top
498 public static void bringWindowToFront(XModel xModel
)
500 XTopWindow xTopWindow
=
501 UnoRuntime
.queryInterface(
503 xModel
.getCurrentController().getFrame().getContainerWindow());
505 xTopWindow
.toFront();
508 public static void bringWindowToFront(XComponent xComponent
)
510 XModel xModel
= UnoRuntime
.queryInterface(XModel
.class, xComponent
);
513 bringWindowToFront(xModel
);