1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
29 import com
.sun
.star
.awt
.Rectangle
;
30 import com
.sun
.star
.awt
.WindowDescriptor
;
31 import com
.sun
.star
.awt
.XToolkit
;
32 import com
.sun
.star
.awt
.XWindowPeer
;
33 import com
.sun
.star
.awt
.XTopWindow
;
34 import com
.sun
.star
.beans
.PropertyValue
;
35 import com
.sun
.star
.beans
.XPropertySet
;
36 import com
.sun
.star
.container
.XEnumeration
;
37 import com
.sun
.star
.container
.XNameReplace
;
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
.lang
.XComponent
;
43 import com
.sun
.star
.lang
.XMultiServiceFactory
;
44 import com
.sun
.star
.lang
.XServiceInfo
;
45 import com
.sun
.star
.uno
.UnoRuntime
;
47 // access the implementations via names
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
;
52 import helper
.ConfigHelper
;
53 import java
.util
.Vector
;
54 import lib
.StatusException
;
57 * contains helper methods for the Desktop
59 public class DesktopTools
63 * Queries the XComponentLoader
65 * @param xMSF the MultiServiceFactory
66 * @return the gained XComponentLoader
68 public static XComponentLoader
getCLoader(XMultiServiceFactory xMSF
)
70 XDesktop oDesktop
= (XDesktop
) UnoRuntime
.queryInterface(
71 XDesktop
.class, createDesktop(xMSF
));
73 XComponentLoader oCLoader
= (XComponentLoader
) UnoRuntime
.queryInterface(
74 XComponentLoader
.class, oDesktop
);
77 } // finish getCLoader
80 * Creates an Instance of the Desktop service
82 * @param xMSF the MultiServiceFactory
83 * @return the gained Object
85 public static Object
createDesktop(XMultiServiceFactory xMSF
)
91 oInterface
= xMSF
.createInstance("com.sun.star.comp.framework.Desktop");
93 catch (com
.sun
.star
.uno
.Exception e
)
95 throw new IllegalArgumentException("Desktop Service not available");
99 } //finish createDesktop
102 * returns a XEnumeration containing all components containing on the desktop
103 * @param xMSF the XMultiServiceFactory
104 * @return XEnumeration of all components on the desktop
106 public static XEnumeration
getAllComponents(XMultiServiceFactory xMSF
)
108 XDesktop xDesktop
= (XDesktop
) UnoRuntime
.queryInterface(
109 XDesktop
.class, createDesktop(xMSF
));
110 return xDesktop
.getComponents().createEnumeration();
114 * returns the current component on the desktop
115 * @param xMSF the XMultiServiceFactory
116 * @return XComponent of the current component on the desktop
118 public static XComponent
getCurrentComponent(XMultiServiceFactory xMSF
)
120 XDesktop xDesktop
= (XDesktop
) UnoRuntime
.queryInterface(
121 XDesktop
.class, createDesktop(xMSF
));
122 return xDesktop
.getCurrentComponent();
126 * returns the current component on the desktop
127 * @param xMSF the XMultiServiceFactory
128 * @return XComponent of the current component on the desktop
130 public static XFrame
getCurrentFrame(XMultiServiceFactory xMSF
)
132 XDesktop xDesktop
= (XDesktop
) UnoRuntime
.queryInterface(
133 XDesktop
.class, createDesktop(xMSF
));
134 return xDesktop
.getCurrentFrame();
138 * returns an object arrary of all open documents
139 * @param xMSF the MultiServiceFactory
140 * @return returns an Array of document kinds like ["swriter"]
143 * returns an array of all open documents
144 * @param xMSF the XMultiSerivceFactory
145 * @return returns an array of all open documents
147 public static Object
[] getAllOpenDocuments(XMultiServiceFactory xMSF
)
149 Vector components
= new Vector();
150 XDesktop xDesktop
= (XDesktop
) UnoRuntime
.queryInterface(
151 XDesktop
.class, createDesktop(xMSF
));
153 XEnumeration allComp
= getAllComponents(xMSF
);
155 while (allComp
.hasMoreElements())
159 XComponent xComponent
= (XComponent
) UnoRuntime
.queryInterface(
160 XComponent
.class, allComp
.nextElement());
162 if (getDocumentType(xComponent
) != null)
164 components
.add(xComponent
);
168 catch (com
.sun
.star
.container
.NoSuchElementException e
)
171 catch (com
.sun
.star
.lang
.WrappedTargetException e
)
175 return components
.toArray();
179 * Returns the document type for the given XComponent of an document
180 * @param xComponent the document to query for its type
188 * or <CODE>null</CODE>
190 public static String
getDocumentType(XComponent xComponent
)
192 XServiceInfo sInfo
= (XServiceInfo
) UnoRuntime
.queryInterface(
193 XServiceInfo
.class, xComponent
);
199 else if (sInfo
.supportsService("com.sun.star.sheet.SpreadsheetDocument"))
203 else if (sInfo
.supportsService("com.sun.star.text.TextDocument"))
207 else if (sInfo
.supportsService("com.sun.star.drawing.DrawingDocument"))
211 else if (sInfo
.supportsService("com.sun.star.presentation.PresentationDocument"))
215 else if (sInfo
.supportsService("com.sun.star.formula.FormulaProperties"))
226 * Opens a new document of a given kind
228 * @return the XComponent Interface of the document
229 * @param kind the kind of document to load.<br>
237 * @param Args arguments which passed to the document to load
238 * @param xMSF the MultiServiceFactory
240 public static XComponent
openNewDoc(XMultiServiceFactory xMSF
, String kind
,
241 PropertyValue
[] Args
)
243 XComponent oDoc
= null;
247 oDoc
= getCLoader(xMSF
).loadComponentFromURL("private:factory/" + kind
,
250 catch (com
.sun
.star
.uno
.Exception e
)
252 throw new IllegalArgumentException("Document could not be opened");
256 } //finish openNewDoc
259 * loads a document of from a given url
261 * @return the XComponent Interface of the document
262 * @param url the URL of the document to load.
263 * @param Args arguments which passed to the document to load
264 * @param xMSF the MultiServiceFactory
266 public static XComponent
loadDoc(XMultiServiceFactory xMSF
, String url
,
267 PropertyValue
[] Args
)
269 XComponent oDoc
= null;
272 Args
= new PropertyValue
[0];
276 oDoc
= getCLoader(xMSF
).loadComponentFromURL(url
, "_blank", 0, Args
);
278 catch (com
.sun
.star
.uno
.Exception e
)
280 throw new IllegalArgumentException("Document could not be loaded");
283 bringWindowToFront(oDoc
);
285 } //finish openNewDoc
288 * closes a given document
289 * @param DocumentToClose the document to close
291 public static void closeDoc(XInterface DocumentToClose
)
293 if (DocumentToClose
== null)
298 String kd
= System
.getProperty("KeepDocument");
301 System
.out
.println("The property 'KeepDocument' is set and so the document won't be disposed");
304 XModifiable modified
= (XModifiable
) UnoRuntime
.queryInterface(XModifiable
.class, DocumentToClose
);
305 XCloseable closer
= (XCloseable
) UnoRuntime
.queryInterface(XCloseable
.class, DocumentToClose
);
309 if (modified
!= null)
311 modified
.setModified(false);
315 catch (com
.sun
.star
.util
.CloseVetoException e
)
317 // e.printStackTrace();
318 System
.out
.println("Couldn't close document");
320 catch (com
.sun
.star
.lang
.DisposedException e
)
322 // e.printStackTrace();
323 System
.out
.println("Couldn't close document");
325 catch (java
.lang
.NullPointerException e
)
327 // e.printStackTrace();
328 System
.out
.println("Couldn't close document");
330 catch (com
.sun
.star
.beans
.PropertyVetoException e
)
332 // e.printStackTrace();
333 System
.out
.println("Couldn't close document");
338 * Creates a floating XWindow with the size of X=500 Y=100 width=400 height=600
339 * @param xMSF the MultiServiceFactory
340 * @throws lib.StatusException if it is not possible to create a floating window a lib.StatusException was thrown
341 * @return a floating XWindow
343 public static XWindowPeer
createFloatingWindow(XMultiServiceFactory xMSF
)
344 throws StatusException
346 return createFloatingWindow(xMSF
, 500, 100, 400, 600);
350 * Creates a floating XWindow on the given position and size.
351 * @return a floating XWindow
352 * @param X the X-Postion of the floating XWindow
353 * @param Y the Y-Postion of the floating XWindow
354 * @param width the width of the floating XWindow
355 * @param height the height of the floating XWindow
356 * @param xMSF the MultiServiceFactory
357 * @throws lib.StatusException if it is not possible to create a floating window a lib.StatusException was thrown
359 public static XWindowPeer
createFloatingWindow(XMultiServiceFactory xMSF
, int X
, int Y
, int width
, int height
)
360 throws StatusException
363 XInterface oObj
= null;
367 oObj
= (XInterface
) xMSF
.createInstance("com.sun.star.awt.Toolkit");
369 catch (com
.sun
.star
.uno
.Exception e
)
371 throw new StatusException("Couldn't get toolkit", e
);
374 XToolkit tk
= (XToolkit
) UnoRuntime
.queryInterface(
375 XToolkit
.class, oObj
);
377 WindowDescriptor descriptor
= new com
.sun
.star
.awt
.WindowDescriptor();
379 descriptor
.Type
= com
.sun
.star
.awt
.WindowClass
.TOP
;
380 descriptor
.WindowServiceName
= "modelessdialog";
381 descriptor
.ParentIndex
= -1;
383 Rectangle bounds
= new com
.sun
.star
.awt
.Rectangle();
386 bounds
.Width
= width
;
387 bounds
.Height
= height
;
389 descriptor
.Bounds
= bounds
;
390 descriptor
.WindowAttributes
= (com
.sun
.star
.awt
.WindowAttribute
.BORDER
+
391 com
.sun
.star
.awt
.WindowAttribute
.MOVEABLE
+
392 com
.sun
.star
.awt
.WindowAttribute
.SIZEABLE
+
393 com
.sun
.star
.awt
.WindowAttribute
.CLOSEABLE
+
394 com
.sun
.star
.awt
.VclWindowPeerAttribute
.CLIPCHILDREN
);
396 XWindowPeer xWindow
= null;
400 xWindow
= tk
.createWindow(descriptor
);
402 catch (com
.sun
.star
.lang
.IllegalArgumentException e
)
404 throw new StatusException("Could not create window", e
);
412 * zoom to have a view over the hole page
413 * @param xDoc the document to zoom
415 public static void zoomToEntirePage(XInterface xDoc
)
419 XModel xMod
= (XModel
) UnoRuntime
.queryInterface(XModel
.class, xDoc
);
420 XInterface oCont
= xMod
.getCurrentController();
421 XViewSettingsSupplier oVSSupp
= (XViewSettingsSupplier
) UnoRuntime
.queryInterface(XViewSettingsSupplier
.class, oCont
);
423 XInterface oViewSettings
= oVSSupp
.getViewSettings();
424 XPropertySet oViewProp
= (XPropertySet
) UnoRuntime
.queryInterface(XPropertySet
.class, oViewSettings
);
425 oViewProp
.setPropertyValue("ZoomType",
426 new Short(com
.sun
.star
.view
.DocumentZoomType
.ENTIRE_PAGE
));
428 utils
.shortWait(5000);
432 System
.out
.println("Could not zoom to entire page: " + e
.toString());
438 * This function docks the Stylist onto the right side of the window.</p>
440 * Since the svt.viewoptions cache the view configuration at start up
441 * the chage of the docking will be effective at a restart.
442 * @param xMSF the XMultiServiceFactory
444 public static void dockStylist(XMultiServiceFactory xMSF
)
446 // prepare Window-Settings
449 ConfigHelper aConfig
= new ConfigHelper(xMSF
,
450 "org.openoffice.Office.Views", false);
452 // Is node "5539" (slot-id for navigator) available? If not, insert it
453 XNameReplace x5539
= aConfig
.getOrInsertGroup("Windows", "5539");
455 aConfig
.updateGroupProperty(
456 "Windows", "5539", "WindowState", "952,180,244,349;1;0,0,0,0;");
458 aConfig
.insertOrUpdateExtensibleGroupProperty(
459 "Windows", "5539", "UserData", "Data", "V2,V,0,AL:(5,16,0/0/244/349,244;610)");
461 // Is node "SplitWindow2" available? If not, instert it.
462 aConfig
.getOrInsertGroup("Windows", "SplitWindow2");
464 aConfig
.insertOrUpdateExtensibleGroupProperty(
465 "Windows", "SplitWindow2", "UserData", "UserItem", "V1,2,1,0,5539");
471 catch (com
.sun
.star
.uno
.Exception e
)
478 * Due to typo deprecated
483 public static void bringWindowToFromt(XModel xModel
)
485 bringWindowToFront(xModel
);
489 * This function brings a document to the front.<P>
490 * NOTE: it is not possible to change the window order of your Window-Manager!!
491 * Only the order of Office documents are changeable.
492 * @param xModel the XModel of the document to bring to top
494 public static void bringWindowToFront(XModel xModel
)
496 // System.out.println("DEBUG: bring to front xModel");
498 XTopWindow xTopWindow
=
499 (XTopWindow
) UnoRuntime
.queryInterface(
501 xModel
.getCurrentController().getFrame().getContainerWindow());
503 xTopWindow
.toFront();
506 public static void bringWindowToFront(XComponent xComponent
)
508 // System.out.println("DEBUG: bring to front xComponent");
509 XModel xModel
= (XModel
) UnoRuntime
.queryInterface(XModel
.class, xComponent
);
512 bringWindowToFront(xModel
);