bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / runner / util / DesktopTools.java
blobacb24c2904753b6d7af62939a95d7c3bc34eb2a4
1 /*
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 .
18 package util;
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;
47 /**
48 * contains helper methods for the Desktop
50 public class DesktopTools
53 /**
54 * Queries the XComponentLoader
56 * @param xMSF the MultiServiceFactory
57 * @return the gained XComponentLoader
59 public static XComponentLoader getCLoader(XMultiServiceFactory xMSF)
61 XDesktop oDesktop = UnoRuntime.queryInterface(
62 XDesktop.class, createDesktop(xMSF));
64 XComponentLoader oCLoader = UnoRuntime.queryInterface(
65 XComponentLoader.class, oDesktop);
67 return oCLoader;
68 } // finish getCLoader
70 /**
71 * Creates an Instance of the Desktop service
73 * @param xMSF the MultiServiceFactory
74 * @return the gained Object
76 public static Object createDesktop(XMultiServiceFactory xMSF)
78 Object oInterface;
80 try
82 oInterface = xMSF.createInstance("com.sun.star.comp.framework.Desktop");
84 catch (com.sun.star.uno.Exception e)
86 throw new IllegalArgumentException("Desktop Service not available");
89 return oInterface;
90 } //finish createDesktop
92 /**
93 * returns a XEnumeration containing all components containing on the desktop
94 * @param xMSF the XMultiServiceFactory
95 * @return XEnumeration of all components on the desktop
97 public static XEnumeration getAllComponents(XMultiServiceFactory xMSF)
99 XDesktop xDesktop = UnoRuntime.queryInterface(
100 XDesktop.class, createDesktop(xMSF));
101 return xDesktop.getComponents().createEnumeration();
105 * returns the current component on the desktop
106 * @param xMSF the XMultiServiceFactory
107 * @return XComponent of the current component on the desktop
109 public static XComponent getCurrentComponent(XMultiServiceFactory xMSF)
111 XDesktop xDesktop = UnoRuntime.queryInterface(
112 XDesktop.class, createDesktop(xMSF));
113 return xDesktop.getCurrentComponent();
117 * returns the current component on the desktop
118 * @param xMSF the XMultiServiceFactory
119 * @return XComponent of the current component on the desktop
121 public static XFrame getCurrentFrame(XMultiServiceFactory xMSF)
123 XDesktop xDesktop = UnoRuntime.queryInterface(
124 XDesktop.class, createDesktop(xMSF));
125 return xDesktop.getCurrentFrame();
129 * returns an object arrary of all open documents
130 * @param xMSF the MultiServiceFactory
131 * @return returns an Array of document kinds like ["swriter"]
134 * returns an array of all open documents
135 * @param xMSF the XMultiSerivceFactory
136 * @return returns an array of all open documents
138 public static Object[] getAllOpenDocuments(XMultiServiceFactory xMSF)
140 ArrayList<XComponent> components = new ArrayList<XComponent>();
141 UnoRuntime.queryInterface(
142 XDesktop.class, createDesktop(xMSF));
144 XEnumeration allComp = getAllComponents(xMSF);
146 while (allComp.hasMoreElements())
150 XComponent xComponent = UnoRuntime.queryInterface(
151 XComponent.class, allComp.nextElement());
153 if (getDocumentType(xComponent) != null)
155 components.add(xComponent);
159 catch (com.sun.star.container.NoSuchElementException e)
162 catch (com.sun.star.lang.WrappedTargetException e)
166 return components.toArray();
170 * Returns the document type for the given XComponent of an document
171 * @param xComponent the document to query for its type
172 * @return possible:
173 * <ul>
174 * <li>swriter</li>
175 * <li>scalc</li>
176 * <li>sdraw</li>
177 * <li>smath</li>
178 * </ul>
179 * or <CODE>null</CODE>
181 public static String getDocumentType(XComponent xComponent)
183 XServiceInfo sInfo = UnoRuntime.queryInterface(
184 XServiceInfo.class, xComponent);
186 if (sInfo == null)
188 return "";
190 else if (sInfo.supportsService("com.sun.star.sheet.SpreadsheetDocument"))
192 return "scalc";
194 else if (sInfo.supportsService("com.sun.star.text.TextDocument"))
196 return "swriter";
198 else if (sInfo.supportsService("com.sun.star.drawing.DrawingDocument"))
200 return "sdraw";
202 else if (sInfo.supportsService("com.sun.star.presentation.PresentationDocument"))
204 return "simpress";
206 else if (sInfo.supportsService("com.sun.star.formula.FormulaProperties"))
208 return "smath";
210 else
212 return null;
217 * Opens a new document of a given kind
218 * with arguments
219 * @return the XComponent Interface of the document
220 * @param kind the kind of document to load.<br>
221 * possible:
222 * <ul>
223 * <li>swriter</li>
224 * <li>scalc</li>
225 * <li>sdaw</li>
226 * <li>smath</li>
227 * </ul>
228 * @param Args arguments which passed to the document to load
229 * @param xMSF the MultiServiceFactory
231 public static XComponent openNewDoc(XMultiServiceFactory xMSF, String kind,
232 PropertyValue[] Args)
234 XComponent oDoc = null;
238 oDoc = getCLoader(xMSF).loadComponentFromURL("private:factory/" + kind,
239 "_blank", 0, Args);
241 catch (com.sun.star.uno.Exception e)
243 throw new IllegalArgumentException("Document could not be opened");
246 return oDoc;
247 } //finish openNewDoc
250 * loads a document of from a given url
251 * with arguments
252 * @return the XComponent Interface of the document
253 * @param url the URL of the document to load.
254 * @param Args arguments which passed to the document to load
255 * @param xMSF the MultiServiceFactory
257 public static XComponent loadDoc(XMultiServiceFactory xMSF, String url,
258 PropertyValue[] Args)
260 XComponent oDoc = null;
261 if (Args == null)
263 Args = new PropertyValue[0];
267 oDoc = getCLoader(xMSF).loadComponentFromURL(url, "_blank", 0, Args);
269 catch (com.sun.star.uno.Exception e)
271 throw new IllegalArgumentException("Document could not be loaded");
274 bringWindowToFront(oDoc);
275 return oDoc;
276 } //finish openNewDoc
279 * closes a given document
280 * @param DocumentToClose the document to close
282 public static void closeDoc(XInterface DocumentToClose)
284 if (DocumentToClose == null)
286 return;
289 String kd = System.getProperty("KeepDocument");
290 if (kd != null)
292 System.out.println("The property 'KeepDocument' is set and so the document won't be disposed");
293 return;
295 XModifiable modified = UnoRuntime.queryInterface(XModifiable.class, DocumentToClose);
296 XCloseable closer = UnoRuntime.queryInterface(XCloseable.class, DocumentToClose);
300 if (modified != null)
302 modified.setModified(false);
304 closer.close(true);
306 catch (com.sun.star.util.CloseVetoException e)
308 // e.printStackTrace();
309 System.out.println("Couldn't close document");
311 catch (com.sun.star.lang.DisposedException e)
313 // e.printStackTrace();
314 System.out.println("Couldn't close document");
316 catch (java.lang.NullPointerException e)
318 // e.printStackTrace();
319 System.out.println("Couldn't close document");
321 catch (com.sun.star.beans.PropertyVetoException e)
323 // e.printStackTrace();
324 System.out.println("Couldn't close document");
329 * Creates a floating XWindow with the size of X=500 Y=100 width=400 height=600
330 * @param xMSF the MultiServiceFactory
331 * @throws lib.StatusException if it is not possible to create a floating window a lib.StatusException was thrown
332 * @return a floating XWindow
334 public static XWindowPeer createFloatingWindow(XMultiServiceFactory xMSF)
335 throws StatusException
337 return createFloatingWindow(xMSF, 500, 100, 400, 600);
341 * Creates a floating XWindow on the given position and size.
342 * @return a floating XWindow
343 * @param X the X-Postion of the floating XWindow
344 * @param Y the Y-Postion of the floating XWindow
345 * @param width the width of the floating XWindow
346 * @param height the height of the floating XWindow
347 * @param xMSF the MultiServiceFactory
348 * @throws lib.StatusException if it is not possible to create a floating window a lib.StatusException was thrown
350 public static XWindowPeer createFloatingWindow(XMultiServiceFactory xMSF, int X, int Y, int width, int height)
351 throws StatusException
354 XInterface oObj = null;
358 oObj = (XInterface) xMSF.createInstance("com.sun.star.awt.Toolkit");
360 catch (com.sun.star.uno.Exception e)
362 throw new StatusException("Couldn't get toolkit", e);
365 XToolkit tk = UnoRuntime.queryInterface(
366 XToolkit.class, oObj);
368 WindowDescriptor descriptor = new com.sun.star.awt.WindowDescriptor();
370 descriptor.Type = com.sun.star.awt.WindowClass.TOP;
371 descriptor.WindowServiceName = "modelessdialog";
372 descriptor.ParentIndex = -1;
374 Rectangle bounds = new com.sun.star.awt.Rectangle();
375 bounds.X = X;
376 bounds.Y = Y;
377 bounds.Width = width;
378 bounds.Height = height;
380 descriptor.Bounds = bounds;
381 descriptor.WindowAttributes = (com.sun.star.awt.WindowAttribute.BORDER +
382 com.sun.star.awt.WindowAttribute.MOVEABLE +
383 com.sun.star.awt.WindowAttribute.SIZEABLE +
384 com.sun.star.awt.WindowAttribute.CLOSEABLE +
385 com.sun.star.awt.VclWindowPeerAttribute.CLIPCHILDREN);
387 XWindowPeer xWindow = null;
391 xWindow = tk.createWindow(descriptor);
393 catch (com.sun.star.lang.IllegalArgumentException e)
395 throw new StatusException("Could not create window", e);
398 return xWindow;
403 * zoom to have a view over the hole page
404 * @param xDoc the document to zoom
406 public static void zoomToEntirePage(XInterface xDoc)
410 XModel xMod = UnoRuntime.queryInterface(XModel.class, xDoc);
411 XInterface oCont = xMod.getCurrentController();
412 XViewSettingsSupplier oVSSupp = UnoRuntime.queryInterface(XViewSettingsSupplier.class, oCont);
414 XInterface oViewSettings = oVSSupp.getViewSettings();
415 XPropertySet oViewProp = UnoRuntime.queryInterface(XPropertySet.class, oViewSettings);
416 oViewProp.setPropertyValue("ZoomType",
417 new Short(com.sun.star.view.DocumentZoomType.ENTIRE_PAGE));
419 utils.shortWait(5000);
421 catch (Exception e)
423 System.out.println("Could not zoom to entire page: " + e.toString());
429 * This function docks the Stylist onto the right side of the window.</p>
430 * Note:<P>
431 * Since the svt.viewoptions cache the view configuration at start up
432 * the chage of the docking will be effective at a restart.
433 * @param xMSF the XMultiServiceFactory
435 public static void dockStylist(XMultiServiceFactory xMSF)
437 // prepare Window-Settings
440 ConfigHelper aConfig = new ConfigHelper(xMSF,
441 "org.openoffice.Office.Views", false);
443 aConfig.getOrInsertGroup("Windows", "5539");
445 aConfig.updateGroupProperty(
446 "Windows", "5539", "WindowState", "952,180,244,349;1;0,0,0,0;");
448 aConfig.insertOrUpdateExtensibleGroupProperty(
449 "Windows", "5539", "UserData", "Data", "V2,V,0,AL:(5,16,0/0/244/349,244;610)");
451 // Is node "SplitWindow2" available? If not, instert it.
452 aConfig.getOrInsertGroup("Windows", "SplitWindow2");
454 aConfig.insertOrUpdateExtensibleGroupProperty(
455 "Windows", "SplitWindow2", "UserData", "UserItem", "V1,2,1,0,5539");
457 aConfig.flush();
458 aConfig = null;
461 catch (com.sun.star.uno.Exception e)
463 e.printStackTrace();
468 * Due to typo deprecated
469 * @param xModel
470 * @deprecated
472 @Deprecated
473 public static void bringWindowToFromt(XModel xModel)
475 bringWindowToFront(xModel);
479 * This function brings a document to the front.<P>
480 * NOTE: it is not possible to change the window order of your Window-Manager!!
481 * Only the order of Office documents are changeable.
482 * @param xModel the XModel of the document to bring to top
484 public static void bringWindowToFront(XModel xModel)
486 // System.out.println("DEBUG: bring to front xModel");
488 XTopWindow xTopWindow =
489 UnoRuntime.queryInterface(
490 XTopWindow.class,
491 xModel.getCurrentController().getFrame().getContainerWindow());
493 xTopWindow.toFront();
496 public static void bringWindowToFront(XComponent xComponent)
498 // System.out.println("DEBUG: bring to front xComponent");
499 XModel xModel = UnoRuntime.queryInterface(XModel.class, xComponent);
500 if (xModel != null)
502 bringWindowToFront(xModel);