summaryrefslogtreecommitdiff
path: root/qadevOOo/runner/util/DesktopTools.java
blob: c78e450ae7219a864065fe5b996c24f354b84048 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */
package util;

import helper.ConfigHelper;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;

import lib.StatusException;

import com.sun.star.awt.Rectangle;
import com.sun.star.awt.WindowDescriptor;
import com.sun.star.awt.XToolkit;
import com.sun.star.awt.XTopWindow;
import com.sun.star.awt.XWindowPeer;
import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.XPropertySet;
import com.sun.star.container.XEnumeration;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.frame.XDesktop;
import com.sun.star.frame.XFrame;
import com.sun.star.frame.XModel;
import com.sun.star.io.XInputStream;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.lang.XServiceInfo;
import com.sun.star.lib.uno.adapter.ByteArrayToXInputStreamAdapter;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XInterface;
import com.sun.star.util.XCloseable;
import com.sun.star.util.XModifiable;
import com.sun.star.view.XViewSettingsSupplier;

/**
 * contains helper methods for the Desktop
 */
public class DesktopTools
{

    /**
     * Queries the XComponentLoader
     *
     * @param xMSF the MultiServiceFactory
     * @return the gained XComponentLoader
     */
    private static XComponentLoader getCLoader(XMultiServiceFactory xMSF)
    {
        XComponentLoader oCLoader = UnoRuntime.queryInterface(
                XComponentLoader.class, createDesktop(xMSF));

        return oCLoader;
    }

    /**
     * Creates an Instance of the Desktop service
     *
     * @param xMSF the MultiServiceFactory
     * @return the gained XDesktop object
     */
    public static XDesktop createDesktop(XMultiServiceFactory xMSF)
    {
        XDesktop xDesktop;

        try
        {
            xDesktop = UnoRuntime.queryInterface(
                XDesktop.class, xMSF.createInstance("com.sun.star.comp.framework.Desktop"));
        }
        catch (com.sun.star.uno.Exception e)
        {
            throw new IllegalArgumentException("Desktop Service not available", e);
        }

        return xDesktop;
    }

    /**
     * returns a XEnumeration containing all components containing on the desktop
     * @param xMSF the XMultiServiceFactory
     * @return XEnumeration of all components on the desktop
     */
    public static XEnumeration getAllComponents(XMultiServiceFactory xMSF)
    {
        return createDesktop(xMSF).getComponents().createEnumeration();
    }



    /**
     * returns the current component on the desktop
     * @param xMSF the XMultiServiceFactory
     * @return XComponent of the current component on the desktop
     */
    public static XFrame getCurrentFrame(XMultiServiceFactory xMSF)
    {
        return createDesktop(xMSF).getCurrentFrame();
    }

    /**
     * returns an object array of all open documents
     * @param xMSF the MultiServiceFactory
     * @return returns an Array of document kinds like ["swriter"]
     */
    public static Object[] getAllOpenDocuments(XMultiServiceFactory xMSF)
    {
        ArrayList<XComponent> components = new ArrayList<XComponent>();

        XEnumeration allComp = getAllComponents(xMSF);

        while (allComp.hasMoreElements())
        {
            try
            {
                XComponent xComponent = UnoRuntime.queryInterface(
                        XComponent.class, allComp.nextElement());

                if (getDocumentType(xComponent) != null)
                {
                    components.add(xComponent);
                }

            }
            catch (com.sun.star.container.NoSuchElementException e)
            {
            }
            catch (com.sun.star.lang.WrappedTargetException e)
            {
            }
        }
        return components.toArray();
    }

    /**
     * Returns the document type for the given XComponent of a document
     * @param xComponent the document to query for its type
     * @return possible:
     * <ul>
     * <li>swriter</li>
     * <li>scalc</li>
     * <li>sdraw</li>
     * <li>smath</li>
     * </ul>
     * or <CODE>null</CODE>
     */
    private static String getDocumentType(XComponent xComponent)
    {
        XServiceInfo sInfo = UnoRuntime.queryInterface(
                XServiceInfo.class, xComponent);

        if (sInfo == null)
        {
            return "";
        }
        else if (sInfo.supportsService("com.sun.star.sheet.SpreadsheetDocument"))
        {
            return "scalc";
        }
        else if (sInfo.supportsService("com.sun.star.text.TextDocument"))
        {
            return "swriter";
        }
        else if (sInfo.supportsService("com.sun.star.drawing.DrawingDocument"))
        {
            return "sdraw";
        }
        else if (sInfo.supportsService("com.sun.star.presentation.PresentationDocument"))
        {
            return "simpress";
        }
        else if (sInfo.supportsService("com.sun.star.formula.FormulaProperties"))
        {
            return "smath";
        }
        else
        {
            return null;
        }
    }

    /**
     * Opens a new document of a given kind
     * with arguments
     * @return the XComponent Interface of the document
     * @param kind the kind of document to load.<br>
     * possible:
     * <ul>
     * <li>swriter</li>
     * <li>scalc</li>
     * <li>sdaw</li>
     * <li>smath</li>
     * </ul>
     * @param Args arguments which passed to the document to load
     * @param xMSF the MultiServiceFactory
     */
    public static XComponent openNewDoc(XMultiServiceFactory xMSF, String kind,
            PropertyValue[] Args)
    {
        XComponent oDoc = null;

        try
        {
            oDoc = getCLoader(xMSF).loadComponentFromURL("private:factory/" + kind,
                    "_blank", 0, Args);
        }
        catch (com.sun.star.uno.Exception e)
        {
            throw new IllegalArgumentException("Document could not be opened", e);
        }

        return oDoc;
    }

    /**
     * loads a document of from a given url
     * with arguments
     * @return the XComponent Interface of the document
     * @param url the URL of the document to load.
     * @param Args arguments which passed to the document to load
     * @param xMSF the MultiServiceFactory
     */
    public static XComponent loadDoc(XMultiServiceFactory xMSF, String url,
            PropertyValue[] Args)
    {
        XComponent oDoc = null;
        if (Args == null)
        {
            Args = new PropertyValue[0];
        }
        try
        {
            oDoc = getCLoader(xMSF).loadComponentFromURL(url, "_blank", 0, Args);
        }
        catch (com.sun.star.uno.Exception e)
        {
            throw new IllegalArgumentException("Document could not be loaded", e);
        }

        bringWindowToFront(oDoc);
        return oDoc;
    }

    /**
     * loads a document of from a given path using an input stream
     *
     * @param xMSF the MultiServiceFactory
     * @param filePath the path of the document to load.
     * @return the XComponent Interface of the document
     */
    public static XComponent loadDocUsingStream(XMultiServiceFactory xMSF, String filePath)
    {
        XInputStream inputStream = null;
        try {
            final InputStream inputFile = new BufferedInputStream(
                    new FileInputStream(filePath));
            try {
                final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                final byte[] byteBuffer = new byte[4096];
                int byteBufferLength = 0;
                while ((byteBufferLength = inputFile.read(byteBuffer)) > 0)
                    bytes.write(byteBuffer, 0, byteBufferLength);
                inputStream = new ByteArrayToXInputStreamAdapter(
                    bytes.toByteArray());
            } finally {
                inputFile.close();
            }
        } catch (java.io.IOException e) {
            e.printStackTrace();
        }

        PropertyValue[] loadProps = new PropertyValue[1];
        loadProps[0] = new PropertyValue();
        loadProps[0].Name = "InputStream";
        loadProps[0].Value = inputStream;

        XComponent oDoc = null;
        try
        {
            oDoc = getCLoader(xMSF).loadComponentFromURL("private:stream", "_blank", 0, loadProps);
        }
        catch (com.sun.star.uno.Exception e)
        {
            throw new IllegalArgumentException("Document could not be loaded", e);
        }
        return oDoc;
    }

    /**
     * closes a given document
     * @param DocumentToClose the document to close
     */
    public static void closeDoc(XInterface DocumentToClose)
    {
        if (DocumentToClose == null)
        {
            return;
        }

        String kd = System.getProperty("KeepDocument");
        if (kd != null)
        {
            System.out.println("The property 'KeepDocument' is set and so the document won't be disposed");
            return;
        }
        XModifiable modified = UnoRuntime.queryInterface(XModifiable.class, DocumentToClose);
        XCloseable closer = UnoRuntime.queryInterface(XCloseable.class, DocumentToClose);

        try
        {
            if (modified != null)
            {
                modified.setModified(false);
            }
            closer.close(true);
        }
        catch (com.sun.star.util.CloseVetoException e)
        {
            System.out.println("Couldn't close document");
        }
        catch (com.sun.star.lang.DisposedException e)
        {
            System.out.println("Couldn't close document");
        }
        catch (com.sun.star.beans.PropertyVetoException e)
        {
            System.out.println("Couldn't close document");
        }
    }

    /**
     * Creates a floating XWindow with the size of X=500 Y=100 width=400 height=600
     * @param xMSF the MultiServiceFactory
     * @throws lib.StatusException if it is not possible to create a floating window a lib.StatusException was thrown
     * @return a floating XWindow
     */
    public static XWindowPeer createFloatingWindow(XMultiServiceFactory xMSF)
            throws StatusException
    {
        return createFloatingWindow(xMSF, 500, 100, 400, 600);
    }

    /**
     * Creates a floating XWindow on the given position and size.
     * @return a floating XWindow
     * @param X the X-Position of the floating XWindow
     * @param Y the Y-Position of the floating XWindow
     * @param width the width of the floating XWindow
     * @param height the height of the floating XWindow
     * @param xMSF the MultiServiceFactory
     * @throws lib.StatusException if it is not possible to create a floating window a lib.StatusException was thrown
     */
    public static XWindowPeer createFloatingWindow(XMultiServiceFactory xMSF, int X, int Y, int width, int height)
            throws StatusException
    {

        XInterface oObj = null;

        try
        {
            oObj = (XInterface) xMSF.createInstance("com.sun.star.awt.Toolkit");
        }
        catch (com.sun.star.uno.Exception e)
        {
            throw new StatusException("Couldn't get toolkit", e);
        }

        XToolkit tk = UnoRuntime.queryInterface(
                XToolkit.class, oObj);

        WindowDescriptor descriptor = new com.sun.star.awt.WindowDescriptor();

        descriptor.Type = com.sun.star.awt.WindowClass.TOP;
        descriptor.WindowServiceName = "modelessdialog";
        descriptor.ParentIndex = -1;

        Rectangle bounds = new com.sun.star.awt.Rectangle();
        bounds.X = X;
        bounds.Y = Y;
        bounds.Width = width;
        bounds.Height = height;

        descriptor.Bounds = bounds;
        descriptor.WindowAttributes = (com.sun.star.awt.WindowAttribute.BORDER +
                com.sun.star.awt.WindowAttribute.MOVEABLE +
                com.sun.star.awt.WindowAttribute.SIZEABLE +
                com.sun.star.awt.WindowAttribute.CLOSEABLE +
                com.sun.star.awt.VclWindowPeerAttribute.CLIPCHILDREN);

        XWindowPeer xWindow = null;

        try
        {
            xWindow = tk.createWindow(descriptor);
        }
        catch (com.sun.star.lang.IllegalArgumentException e)
        {
            throw new StatusException("Could not create window", e);
        }

        return xWindow;

    }

    /**
     * zoom to have a view over the whole page
     * @param xDoc the document to zoom
     */
    public static void zoomToEntirePage(XMultiServiceFactory xMSF, XInterface xDoc)
    {
        try
        {
            XModel xMod = UnoRuntime.queryInterface(XModel.class, xDoc);
            XInterface oCont = xMod.getCurrentController();
            XViewSettingsSupplier oVSSupp = UnoRuntime.queryInterface(XViewSettingsSupplier.class, oCont);

            XInterface oViewSettings = oVSSupp.getViewSettings();
            XPropertySet oViewProp = UnoRuntime.queryInterface(XPropertySet.class, oViewSettings);
            oViewProp.setPropertyValue("ZoomType",
                    Short.valueOf(com.sun.star.view.DocumentZoomType.ENTIRE_PAGE));

            util.utils.waitForEventIdle(xMSF);
        }
        catch (Exception e)
        {
            System.out.println("Could not zoom to entire page: " + e.toString());
        }

    }

    /**
     * This function docks the Navigator onto the right side of the window.</p>
     * Note:<P>
     * Since the svt.viewoptions cache the view configuration at start up
     * the change of the docking will be effective at a restart.
     * @param xMSF the XMultiServiceFactory
     */
    public static void dockNavigator(XMultiServiceFactory xMSF)
    {
        // prepare Window settings
        try
        {
            ConfigHelper aConfig = new ConfigHelper(xMSF,
                    "org.openoffice.Office.Views", false);

            aConfig.getOrInsertGroup("Windows", "10366");

            aConfig.updateGroupProperty(
                    "Windows", "10366", "WindowState", "952,180,244,349;1;0,0,0,0;");

            aConfig.insertOrUpdateExtensibleGroupProperty(
                    "Windows", "10366", "UserData", "Data", "V2,V,0,AL:(5,16,0/0/244/349,244;610)");

            // Is node "SplitWindow2" available? If not, insert it.
            aConfig.getOrInsertGroup("Windows", "SplitWindow2");

            aConfig.insertOrUpdateExtensibleGroupProperty(
                    "Windows", "SplitWindow2", "UserData", "UserItem", "V1,2,1,0,10366");

            aConfig.flush();
            aConfig = null;

        }
        catch (com.sun.star.uno.Exception e)
        {
            e.printStackTrace();
        }
    }


    /**
     * This function brings a document to the front.<P>
     * NOTE: it is not possible to change the window order of your Window-Manager!!
     * Only the order of Office documents are changeable.
     * @param xModel the XModel of the document to bring to top
     */
    public static void bringWindowToFront(XModel xModel)
    {
        XTopWindow xTopWindow =
                UnoRuntime.queryInterface(
        XTopWindow.class,
        xModel.getCurrentController().getFrame().getContainerWindow());

        xTopWindow.toFront();
    }

    public static void bringWindowToFront(XComponent xComponent)
    {
        XModel xModel = UnoRuntime.queryInterface(XModel.class, xComponent);
        if (xModel != null)
        {
            bringWindowToFront(xModel);
        }
    }
}