update dev300-m58
[ooovba.git] / qadevOOo / runner / util / DesktopTools.java
blob3507a8a086e2e1571521f88d3170476493ddd9a5
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: DesktopTools.java,v $
10 * $Revision: 1.10.8.2 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 package util;
32 import com.sun.star.awt.Rectangle;
33 import com.sun.star.awt.WindowDescriptor;
34 import com.sun.star.awt.XToolkit;
35 import com.sun.star.awt.XWindowPeer;
36 import com.sun.star.awt.XTopWindow;
37 import com.sun.star.beans.PropertyValue;
38 import com.sun.star.beans.XPropertySet;
39 import com.sun.star.container.XEnumeration;
40 import com.sun.star.container.XNameReplace;
41 import com.sun.star.frame.XComponentLoader;
42 import com.sun.star.frame.XDesktop;
43 import com.sun.star.frame.XFrame;
44 import com.sun.star.frame.XModel;
45 import com.sun.star.lang.XComponent;
46 import com.sun.star.lang.XMultiServiceFactory;
47 import com.sun.star.lang.XServiceInfo;
48 import com.sun.star.uno.UnoRuntime;
50 // access the implementations via names
51 import com.sun.star.uno.XInterface;
52 import com.sun.star.util.XCloseable;
53 import com.sun.star.util.XModifiable;
54 import com.sun.star.view.XViewSettingsSupplier;
55 import helper.ConfigHelper;
56 import java.util.Vector;
57 import lib.StatusException;
59 /**
60 * contains helper methods for the Desktop
62 public class DesktopTools
65 /**
66 * Queries the XComponentLoader
68 * @param xMSF the MultiServiceFactory
69 * @return the gained XComponentLoader
71 public static XComponentLoader getCLoader(XMultiServiceFactory xMSF)
73 XDesktop oDesktop = (XDesktop) UnoRuntime.queryInterface(
74 XDesktop.class, createDesktop(xMSF));
76 XComponentLoader oCLoader = (XComponentLoader) UnoRuntime.queryInterface(
77 XComponentLoader.class, oDesktop);
79 return oCLoader;
80 } // finish getCLoader
82 /**
83 * Creates an Instance of the Desktop service
85 * @param xMSF the MultiServiceFactory
86 * @return the gained Object
88 public static Object createDesktop(XMultiServiceFactory xMSF)
90 Object oInterface;
92 try
94 oInterface = xMSF.createInstance("com.sun.star.comp.framework.Desktop");
96 catch (com.sun.star.uno.Exception e)
98 throw new IllegalArgumentException("Desktop Service not available");
101 return oInterface;
102 } //finish createDesktop
105 * returns a XEnumeration containing all components containing on the desktop
106 * @param xMSF the XMultiServiceFactory
107 * @return XEnumeration of all components on the desktop
109 public static XEnumeration getAllComponents(XMultiServiceFactory xMSF)
111 XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(
112 XDesktop.class, createDesktop(xMSF));
113 return xDesktop.getComponents().createEnumeration();
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 XComponent getCurrentComponent(XMultiServiceFactory xMSF)
123 XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(
124 XDesktop.class, createDesktop(xMSF));
125 return xDesktop.getCurrentComponent();
129 * returns the current component on the desktop
130 * @param xMSF the XMultiServiceFactory
131 * @return XComponent of the current component on the desktop
133 public static XFrame getCurrentFrame(XMultiServiceFactory xMSF)
135 XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(
136 XDesktop.class, createDesktop(xMSF));
137 return xDesktop.getCurrentFrame();
141 * returns an object arrary of all open documents
142 * @param xMSF the MultiServiceFactory
143 * @return returns an Array of document kinds like ["swriter"]
146 * returns an array of all open documents
147 * @param xMSF the XMultiSerivceFactory
148 * @return returns an array of all open documents
150 public static Object[] getAllOpenDocuments(XMultiServiceFactory xMSF)
152 Vector components = new Vector();
153 XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(
154 XDesktop.class, createDesktop(xMSF));
156 XEnumeration allComp = getAllComponents(xMSF);
158 while (allComp.hasMoreElements())
162 XComponent xComponent = (XComponent) UnoRuntime.queryInterface(
163 XComponent.class, allComp.nextElement());
165 if (getDocumentType(xComponent) != null)
167 components.add(xComponent);
171 catch (com.sun.star.container.NoSuchElementException e)
174 catch (com.sun.star.lang.WrappedTargetException e)
178 return components.toArray();
182 * Returns the document type for the given XComponent of an document
183 * @param xComponent the document to query for its type
184 * @return possible:
185 * <ul>
186 * <li>swriter</li>
187 * <li>scalc</li>
188 * <li>sdraw</li>
189 * <li>smath</li>
190 * </ul>
191 * or <CODE>null</CODE>
193 public static String getDocumentType(XComponent xComponent)
195 XServiceInfo sInfo = (XServiceInfo) UnoRuntime.queryInterface(
196 XServiceInfo.class, xComponent);
198 if (sInfo == null)
200 return "";
202 else if (sInfo.supportsService("com.sun.star.sheet.SpreadsheetDocument"))
204 return "scalc";
206 else if (sInfo.supportsService("com.sun.star.text.TextDocument"))
208 return "swriter";
210 else if (sInfo.supportsService("com.sun.star.drawing.DrawingDocument"))
212 return "sdraw";
214 else if (sInfo.supportsService("com.sun.star.presentation.PresentationDocument"))
216 return "simpress";
218 else if (sInfo.supportsService("com.sun.star.formula.FormulaProperties"))
220 return "smath";
222 else
224 return null;
229 * Opens a new document of a given kind
230 * with arguments
231 * @return the XComponent Interface of the document
232 * @param kind the kind of document to load.<br>
233 * possible:
234 * <ul>
235 * <li>swriter</li>
236 * <li>scalc</li>
237 * <li>sdaw</li>
238 * <li>smath</li>
239 * </ul>
240 * @param Args arguments which passed to the document to load
241 * @param xMSF the MultiServiceFactory
243 public static XComponent openNewDoc(XMultiServiceFactory xMSF, String kind,
244 PropertyValue[] Args)
246 XComponent oDoc = null;
250 oDoc = getCLoader(xMSF).loadComponentFromURL("private:factory/" + kind,
251 "_blank", 0, Args);
253 catch (com.sun.star.uno.Exception e)
255 throw new IllegalArgumentException("Document could not be opened");
258 return oDoc;
259 } //finish openNewDoc
262 * loads a document of from a given url
263 * with arguments
264 * @return the XComponent Interface of the document
265 * @param url the URL of the document to load.
266 * @param Args arguments which passed to the document to load
267 * @param xMSF the MultiServiceFactory
269 public static XComponent loadDoc(XMultiServiceFactory xMSF, String url,
270 PropertyValue[] Args)
272 XComponent oDoc = null;
273 if (Args == null)
275 Args = new PropertyValue[0];
279 oDoc = getCLoader(xMSF).loadComponentFromURL(url, "_blank", 0, Args);
281 catch (com.sun.star.uno.Exception e)
283 throw new IllegalArgumentException("Document could not be loaded");
286 bringWindowToFront(oDoc);
287 return oDoc;
288 } //finish openNewDoc
291 * closes a given document
292 * @param DocumentToClose the document to close
294 public static void closeDoc(XInterface DocumentToClose)
296 if (DocumentToClose == null)
298 return;
301 String kd = System.getProperty("KeepDocument");
302 if (kd != null)
304 System.out.println("The property 'KeepDocument' is set and so the document won't be disposed");
305 return;
307 XModifiable modified = (XModifiable) UnoRuntime.queryInterface(XModifiable.class, DocumentToClose);
308 XCloseable closer = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, DocumentToClose);
312 if (modified != null)
314 modified.setModified(false);
316 closer.close(true);
318 catch (com.sun.star.util.CloseVetoException e)
320 // e.printStackTrace();
321 System.out.println("Couldn't close document");
323 catch (com.sun.star.lang.DisposedException e)
325 // e.printStackTrace();
326 System.out.println("Couldn't close document");
328 catch (java.lang.NullPointerException e)
330 // e.printStackTrace();
331 System.out.println("Couldn't close document");
333 catch (com.sun.star.beans.PropertyVetoException e)
335 // e.printStackTrace();
336 System.out.println("Couldn't close document");
341 * Creates a floating XWindow with the size of X=500 Y=100 width=400 height=600
342 * @param xMSF the MultiServiceFactory
343 * @throws lib.StatusException if it is not possible to create a floating window a lib.StatusException was thrown
344 * @return a floating XWindow
346 public static XWindowPeer createFloatingWindow(XMultiServiceFactory xMSF)
347 throws StatusException
349 return createFloatingWindow(xMSF, 500, 100, 400, 600);
353 * Creates a floating XWindow on the given position and size.
354 * @return a floating XWindow
355 * @param X the X-Postion of the floating XWindow
356 * @param Y the Y-Postion of the floating XWindow
357 * @param width the width of the floating XWindow
358 * @param height the height of the floating XWindow
359 * @param xMSF the MultiServiceFactory
360 * @throws lib.StatusException if it is not possible to create a floating window a lib.StatusException was thrown
362 public static XWindowPeer createFloatingWindow(XMultiServiceFactory xMSF, int X, int Y, int width, int height)
363 throws StatusException
366 XInterface oObj = null;
370 oObj = (XInterface) xMSF.createInstance("com.sun.star.awt.Toolkit");
372 catch (com.sun.star.uno.Exception e)
374 throw new StatusException("Couldn't get toolkit", e);
377 XToolkit tk = (XToolkit) UnoRuntime.queryInterface(
378 XToolkit.class, oObj);
380 WindowDescriptor descriptor = new com.sun.star.awt.WindowDescriptor();
382 descriptor.Type = com.sun.star.awt.WindowClass.TOP;
383 descriptor.WindowServiceName = "modelessdialog";
384 descriptor.ParentIndex = -1;
386 Rectangle bounds = new com.sun.star.awt.Rectangle();
387 bounds.X = X;
388 bounds.Y = Y;
389 bounds.Width = width;
390 bounds.Height = height;
392 descriptor.Bounds = bounds;
393 descriptor.WindowAttributes = (com.sun.star.awt.WindowAttribute.BORDER +
394 com.sun.star.awt.WindowAttribute.MOVEABLE +
395 com.sun.star.awt.WindowAttribute.SIZEABLE +
396 com.sun.star.awt.WindowAttribute.CLOSEABLE +
397 com.sun.star.awt.VclWindowPeerAttribute.CLIPCHILDREN);
399 XWindowPeer xWindow = null;
403 xWindow = tk.createWindow(descriptor);
405 catch (com.sun.star.lang.IllegalArgumentException e)
407 throw new StatusException("Could not create window", e);
410 return xWindow;
415 * zoom to have a view over the hole page
416 * @param xDoc the document to zoom
418 public static void zoomToEntirePage(XInterface xDoc)
422 XModel xMod = (XModel) UnoRuntime.queryInterface(XModel.class, xDoc);
423 XInterface oCont = xMod.getCurrentController();
424 XViewSettingsSupplier oVSSupp = (XViewSettingsSupplier) UnoRuntime.queryInterface(XViewSettingsSupplier.class, oCont);
426 XInterface oViewSettings = oVSSupp.getViewSettings();
427 XPropertySet oViewProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, oViewSettings);
428 oViewProp.setPropertyValue("ZoomType",
429 new Short(com.sun.star.view.DocumentZoomType.ENTIRE_PAGE));
431 utils.shortWait(5000);
433 catch (Exception e)
435 System.out.println("Could not zoom to entire page: " + e.toString());
441 * This function docks the Stylist onto the right side of the window.</p>
442 * Note:<P>
443 * Since the svt.viewoptions cache the view configuration at start up
444 * the chage of the docking will be effective at a restart.
445 * @param xMSF the XMultiServiceFactory
447 public static void dockStylist(XMultiServiceFactory xMSF)
449 // prepare Window-Settings
452 ConfigHelper aConfig = new ConfigHelper(xMSF,
453 "org.openoffice.Office.Views", false);
455 // Is node "5539" (slot-id for navigator) available? If not, insert it
456 XNameReplace x5539 = aConfig.getOrInsertGroup("Windows", "5539");
458 aConfig.updateGroupProperty(
459 "Windows", "5539", "WindowState", "952,180,244,349;1;0,0,0,0;");
461 aConfig.insertOrUpdateExtensibleGroupProperty(
462 "Windows", "5539", "UserData", "Data", "V2,V,0,AL:(5,16,0/0/244/349,244;610)");
464 // Is node "SplitWindow2" available? If not, instert it.
465 aConfig.getOrInsertGroup("Windows", "SplitWindow2");
467 aConfig.insertOrUpdateExtensibleGroupProperty(
468 "Windows", "SplitWindow2", "UserData", "UserItem", "V1,2,1,0,5539");
470 aConfig.flush();
471 aConfig = null;
474 catch (com.sun.star.uno.Exception e)
476 e.printStackTrace();
481 * Due to typo deprecated
482 * @param xModel
483 * @deprecated
485 @Deprecated
486 public static void bringWindowToFromt(XModel xModel)
488 bringWindowToFront(xModel);
491 /**
492 * This function brings a document to the front.<P>
493 * NOTE: it is not possible to change the window order of your Window-Manager!!
494 * Only the order of Office documents are changeable.
495 * @param xModel the XModel of the document to bring to top
497 public static void bringWindowToFront(XModel xModel)
499 // System.out.println("DEBUG: bring to front xModel");
501 XTopWindow xTopWindow =
502 (XTopWindow) UnoRuntime.queryInterface(
503 XTopWindow.class,
504 xModel.getCurrentController().getFrame().getContainerWindow());
506 xTopWindow.toFront();
509 public static void bringWindowToFront(XComponent xComponent)
511 // System.out.println("DEBUG: bring to front xComponent");
512 XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class, xComponent);
513 if (xModel != null)
515 bringWindowToFront(xModel);