Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / qadevOOo / runner / util / DesktopTools.java
blobc78e450ae7219a864065fe5b996c24f354b84048
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.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;
53 /**
54 * contains helper methods for the Desktop
56 public class DesktopTools
59 /**
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));
70 return oCLoader;
73 /**
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)
81 XDesktop xDesktop;
83 try
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);
93 return xDesktop;
96 /**
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
155 * @return possible:
156 * <ul>
157 * <li>swriter</li>
158 * <li>scalc</li>
159 * <li>sdraw</li>
160 * <li>smath</li>
161 * </ul>
162 * or <CODE>null</CODE>
164 private static String getDocumentType(XComponent xComponent)
166 XServiceInfo sInfo = UnoRuntime.queryInterface(
167 XServiceInfo.class, xComponent);
169 if (sInfo == null)
171 return "";
173 else if (sInfo.supportsService("com.sun.star.sheet.SpreadsheetDocument"))
175 return "scalc";
177 else if (sInfo.supportsService("com.sun.star.text.TextDocument"))
179 return "swriter";
181 else if (sInfo.supportsService("com.sun.star.drawing.DrawingDocument"))
183 return "sdraw";
185 else if (sInfo.supportsService("com.sun.star.presentation.PresentationDocument"))
187 return "simpress";
189 else if (sInfo.supportsService("com.sun.star.formula.FormulaProperties"))
191 return "smath";
193 else
195 return null;
200 * Opens a new document of a given kind
201 * with arguments
202 * @return the XComponent Interface of the document
203 * @param kind the kind of document to load.<br>
204 * possible:
205 * <ul>
206 * <li>swriter</li>
207 * <li>scalc</li>
208 * <li>sdaw</li>
209 * <li>smath</li>
210 * </ul>
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,
222 "_blank", 0, Args);
224 catch (com.sun.star.uno.Exception e)
226 throw new IllegalArgumentException("Document could not be opened", e);
229 return oDoc;
233 * loads a document of from a given url
234 * with arguments
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;
244 if (Args == 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);
258 return 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;
271 try {
272 final InputStream inputFile = new BufferedInputStream(
273 new FileInputStream(filePath));
274 try {
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());
282 } finally {
283 inputFile.close();
285 } catch (java.io.IOException e) {
286 e.printStackTrace();
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);
303 return oDoc;
307 * closes a given document
308 * @param DocumentToClose the document to close
310 public static void closeDoc(XInterface DocumentToClose)
312 if (DocumentToClose == null)
314 return;
317 String kd = System.getProperty("KeepDocument");
318 if (kd != null)
320 System.out.println("The property 'KeepDocument' is set and so the document won't be disposed");
321 return;
323 XModifiable modified = UnoRuntime.queryInterface(XModifiable.class, DocumentToClose);
324 XCloseable closer = UnoRuntime.queryInterface(XCloseable.class, DocumentToClose);
328 if (modified != null)
330 modified.setModified(false);
332 closer.close(true);
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 (com.sun.star.beans.PropertyVetoException e)
344 System.out.println("Couldn't close document");
349 * Creates a floating XWindow with the size of X=500 Y=100 width=400 height=600
350 * @param xMSF the MultiServiceFactory
351 * @throws lib.StatusException if it is not possible to create a floating window a lib.StatusException was thrown
352 * @return a floating XWindow
354 public static XWindowPeer createFloatingWindow(XMultiServiceFactory xMSF)
355 throws StatusException
357 return createFloatingWindow(xMSF, 500, 100, 400, 600);
361 * Creates a floating XWindow on the given position and size.
362 * @return a floating XWindow
363 * @param X the X-Position of the floating XWindow
364 * @param Y the Y-Position of the floating XWindow
365 * @param width the width of the floating XWindow
366 * @param height the height of the floating XWindow
367 * @param xMSF the MultiServiceFactory
368 * @throws lib.StatusException if it is not possible to create a floating window a lib.StatusException was thrown
370 public static XWindowPeer createFloatingWindow(XMultiServiceFactory xMSF, int X, int Y, int width, int height)
371 throws StatusException
374 XInterface oObj = null;
378 oObj = (XInterface) xMSF.createInstance("com.sun.star.awt.Toolkit");
380 catch (com.sun.star.uno.Exception e)
382 throw new StatusException("Couldn't get toolkit", e);
385 XToolkit tk = UnoRuntime.queryInterface(
386 XToolkit.class, oObj);
388 WindowDescriptor descriptor = new com.sun.star.awt.WindowDescriptor();
390 descriptor.Type = com.sun.star.awt.WindowClass.TOP;
391 descriptor.WindowServiceName = "modelessdialog";
392 descriptor.ParentIndex = -1;
394 Rectangle bounds = new com.sun.star.awt.Rectangle();
395 bounds.X = X;
396 bounds.Y = Y;
397 bounds.Width = width;
398 bounds.Height = height;
400 descriptor.Bounds = bounds;
401 descriptor.WindowAttributes = (com.sun.star.awt.WindowAttribute.BORDER +
402 com.sun.star.awt.WindowAttribute.MOVEABLE +
403 com.sun.star.awt.WindowAttribute.SIZEABLE +
404 com.sun.star.awt.WindowAttribute.CLOSEABLE +
405 com.sun.star.awt.VclWindowPeerAttribute.CLIPCHILDREN);
407 XWindowPeer xWindow = null;
411 xWindow = tk.createWindow(descriptor);
413 catch (com.sun.star.lang.IllegalArgumentException e)
415 throw new StatusException("Could not create window", e);
418 return xWindow;
423 * zoom to have a view over the whole page
424 * @param xDoc the document to zoom
426 public static void zoomToEntirePage(XMultiServiceFactory xMSF, XInterface xDoc)
430 XModel xMod = UnoRuntime.queryInterface(XModel.class, xDoc);
431 XInterface oCont = xMod.getCurrentController();
432 XViewSettingsSupplier oVSSupp = UnoRuntime.queryInterface(XViewSettingsSupplier.class, oCont);
434 XInterface oViewSettings = oVSSupp.getViewSettings();
435 XPropertySet oViewProp = UnoRuntime.queryInterface(XPropertySet.class, oViewSettings);
436 oViewProp.setPropertyValue("ZoomType",
437 Short.valueOf(com.sun.star.view.DocumentZoomType.ENTIRE_PAGE));
439 util.utils.waitForEventIdle(xMSF);
441 catch (Exception e)
443 System.out.println("Could not zoom to entire page: " + e.toString());
449 * This function docks the Navigator onto the right side of the window.</p>
450 * Note:<P>
451 * Since the svt.viewoptions cache the view configuration at start up
452 * the change of the docking will be effective at a restart.
453 * @param xMSF the XMultiServiceFactory
455 public static void dockNavigator(XMultiServiceFactory xMSF)
457 // prepare Window settings
460 ConfigHelper aConfig = new ConfigHelper(xMSF,
461 "org.openoffice.Office.Views", false);
463 aConfig.getOrInsertGroup("Windows", "10366");
465 aConfig.updateGroupProperty(
466 "Windows", "10366", "WindowState", "952,180,244,349;1;0,0,0,0;");
468 aConfig.insertOrUpdateExtensibleGroupProperty(
469 "Windows", "10366", "UserData", "Data", "V2,V,0,AL:(5,16,0/0/244/349,244;610)");
471 // Is node "SplitWindow2" available? If not, insert it.
472 aConfig.getOrInsertGroup("Windows", "SplitWindow2");
474 aConfig.insertOrUpdateExtensibleGroupProperty(
475 "Windows", "SplitWindow2", "UserData", "UserItem", "V1,2,1,0,10366");
477 aConfig.flush();
478 aConfig = null;
481 catch (com.sun.star.uno.Exception e)
483 e.printStackTrace();
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 XTopWindow xTopWindow =
497 UnoRuntime.queryInterface(
498 XTopWindow.class,
499 xModel.getCurrentController().getFrame().getContainerWindow());
501 xTopWindow.toFront();
504 public static void bringWindowToFront(XComponent xComponent)
506 XModel xModel = UnoRuntime.queryInterface(XModel.class, xComponent);
507 if (xModel != null)
509 bringWindowToFront(xModel);