1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package util;
24 
25 import com.sun.star.awt.Rectangle;
26 import com.sun.star.awt.WindowDescriptor;
27 import com.sun.star.awt.XToolkit;
28 import com.sun.star.awt.XWindowPeer;
29 import com.sun.star.awt.XTopWindow;
30 import com.sun.star.beans.PropertyValue;
31 import com.sun.star.beans.XPropertySet;
32 import com.sun.star.container.XEnumeration;
33 import com.sun.star.container.XNameReplace;
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 
43 // access the implementations via names
44 import com.sun.star.uno.XInterface;
45 import com.sun.star.util.XCloseable;
46 import com.sun.star.util.XModifiable;
47 import com.sun.star.view.XViewSettingsSupplier;
48 import helper.ConfigHelper;
49 import java.util.Vector;
50 import lib.StatusException;
51 
52 /**
53  * contains helper methods for the Desktop
54  */
55 public class DesktopTools
56 {
57 
58     /**
59      * Queries the XComponentLoader
60      *
61      * @param xMSF the MultiServiceFactory
62      * @return the gained XComponentLoader
63      */
getCLoader(XMultiServiceFactory xMSF)64     public static XComponentLoader getCLoader(XMultiServiceFactory xMSF)
65     {
66         XDesktop oDesktop = (XDesktop) UnoRuntime.queryInterface(
67                 XDesktop.class, createDesktop(xMSF));
68 
69         XComponentLoader oCLoader = (XComponentLoader) UnoRuntime.queryInterface(
70                 XComponentLoader.class, oDesktop);
71 
72         return oCLoader;
73     } // finish getCLoader
74 
75     /**
76      * Creates an Instance of the Desktop service
77      *
78      * @param xMSF the MultiServiceFactory
79      * @return the gained Object
80      */
createDesktop(XMultiServiceFactory xMSF)81     public static Object createDesktop(XMultiServiceFactory xMSF)
82     {
83         Object oInterface;
84 
85         try
86         {
87             oInterface = xMSF.createInstance("com.sun.star.comp.framework.Desktop");
88         }
89         catch (com.sun.star.uno.Exception e)
90         {
91             throw new IllegalArgumentException("Desktop Service not available");
92         }
93 
94         return oInterface;
95     } //finish createDesktop
96 
97     /**
98      * returns a XEnumeration containing all components containing on the desktop
99      * @param xMSF the XMultiServiceFactory
100      * @return XEnumeration of all components on the desktop
101      */
getAllComponents(XMultiServiceFactory xMSF)102     public static XEnumeration getAllComponents(XMultiServiceFactory xMSF)
103     {
104         XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(
105                 XDesktop.class, createDesktop(xMSF));
106         return xDesktop.getComponents().createEnumeration();
107     }
108 
109     /**
110      * returns the current component on the desktop
111      * @param xMSF the XMultiServiceFactory
112      * @return XComponent of the current component on the desktop
113      */
getCurrentComponent(XMultiServiceFactory xMSF)114     public static XComponent getCurrentComponent(XMultiServiceFactory xMSF)
115     {
116         XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(
117                 XDesktop.class, createDesktop(xMSF));
118         return xDesktop.getCurrentComponent();
119     }
120 
121     /**
122      * returns the current component on the desktop
123      * @param xMSF the XMultiServiceFactory
124      * @return XComponent of the current component on the desktop
125      */
getCurrentFrame(XMultiServiceFactory xMSF)126     public static XFrame getCurrentFrame(XMultiServiceFactory xMSF)
127     {
128         XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(
129                 XDesktop.class, createDesktop(xMSF));
130         return xDesktop.getCurrentFrame();
131     }
132 
133     /**
134      * returns an object arrary of all open documents
135      * @param xMSF the MultiServiceFactory
136      * @return returns an Array of document kinds like ["swriter"]
137      */
138     /**
139      * returns an array of all open documents
140      * @param xMSF the XMultiSerivceFactory
141      * @return returns an array of all open documents
142      */
getAllOpenDocuments(XMultiServiceFactory xMSF)143     public static Object[] getAllOpenDocuments(XMultiServiceFactory xMSF)
144     {
145         Vector components = new Vector();
146         XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(
147                 XDesktop.class, createDesktop(xMSF));
148 
149         XEnumeration allComp = getAllComponents(xMSF);
150 
151         while (allComp.hasMoreElements())
152         {
153             try
154             {
155                 XComponent xComponent = (XComponent) UnoRuntime.queryInterface(
156                         XComponent.class, allComp.nextElement());
157 
158                 if (getDocumentType(xComponent) != null)
159                 {
160                     components.add(xComponent);
161                 }
162 
163             }
164             catch (com.sun.star.container.NoSuchElementException e)
165             {
166             }
167             catch (com.sun.star.lang.WrappedTargetException e)
168             {
169             }
170         }
171         return components.toArray();
172     }
173 
174     /**
175      * Returns the document type for the given XComponent of an document
176      * @param xComponent the document to query for its type
177      * @return possible:
178      * <ul>
179      * <li>swriter</li>
180      * <li>scalc</li>
181      * <li>sdraw</li>
182      * <li>smath</li>
183      * </ul>
184      * or <CODE>null</CODE>
185      */
getDocumentType(XComponent xComponent)186     public static String getDocumentType(XComponent xComponent)
187     {
188         XServiceInfo sInfo = (XServiceInfo) UnoRuntime.queryInterface(
189                 XServiceInfo.class, xComponent);
190 
191         if (sInfo == null)
192         {
193             return "";
194         }
195         else if (sInfo.supportsService("com.sun.star.sheet.SpreadsheetDocument"))
196         {
197             return "scalc";
198         }
199         else if (sInfo.supportsService("com.sun.star.text.TextDocument"))
200         {
201             return "swriter";
202         }
203         else if (sInfo.supportsService("com.sun.star.drawing.DrawingDocument"))
204         {
205             return "sdraw";
206         }
207         else if (sInfo.supportsService("com.sun.star.presentation.PresentationDocument"))
208         {
209             return "simpress";
210         }
211         else if (sInfo.supportsService("com.sun.star.formula.FormulaProperties"))
212         {
213             return "smath";
214         }
215         else
216         {
217             return null;
218         }
219     }
220 
221     /**
222      * Opens a new document of a given kind
223      * with arguments
224      * @return the XComponent Interface of the document
225      * @param kind the kind of document to load.<br>
226      * possible:
227      * <ul>
228      * <li>swriter</li>
229      * <li>scalc</li>
230      * <li>sdaw</li>
231      * <li>smath</li>
232      * </ul>
233      * @param Args arguments which passed to the document to load
234      * @param xMSF the MultiServiceFactory
235      */
openNewDoc(XMultiServiceFactory xMSF, String kind, PropertyValue[] Args)236     public static XComponent openNewDoc(XMultiServiceFactory xMSF, String kind,
237             PropertyValue[] Args)
238     {
239         XComponent oDoc = null;
240 
241         try
242         {
243             oDoc = getCLoader(xMSF).loadComponentFromURL("private:factory/" + kind,
244                     "_blank", 0, Args);
245         }
246         catch (com.sun.star.uno.Exception e)
247         {
248             throw new IllegalArgumentException("Document could not be opened");
249         }
250 
251         return oDoc;
252     } //finish openNewDoc
253 
254     /**
255      * loads a document of from a given url
256      * with arguments
257      * @return the XComponent Interface of the document
258      * @param url the URL of the document to load.
259      * @param Args arguments which passed to the document to load
260      * @param xMSF the MultiServiceFactory
261      */
loadDoc(XMultiServiceFactory xMSF, String url, PropertyValue[] Args)262     public static XComponent loadDoc(XMultiServiceFactory xMSF, String url,
263             PropertyValue[] Args)
264     {
265         XComponent oDoc = null;
266         if (Args == null)
267         {
268             Args = new PropertyValue[0];
269         }
270         try
271         {
272             oDoc = getCLoader(xMSF).loadComponentFromURL(url, "_blank", 0, Args);
273         }
274         catch (com.sun.star.uno.Exception e)
275         {
276             throw new IllegalArgumentException("Document could not be loaded");
277         }
278 
279         bringWindowToFront(oDoc);
280         return oDoc;
281     } //finish openNewDoc
282 
283     /**
284      * closes a given document
285      * @param DocumentToClose the document to close
286      */
closeDoc(XInterface DocumentToClose)287     public static void closeDoc(XInterface DocumentToClose)
288     {
289         if (DocumentToClose == null)
290         {
291             return;
292         }
293 
294         String kd = System.getProperty("KeepDocument");
295         if (kd != null)
296         {
297             System.out.println("The property 'KeepDocument' is set and so the document won't be disposed");
298             return;
299         }
300         XModifiable modified = (XModifiable) UnoRuntime.queryInterface(XModifiable.class, DocumentToClose);
301         XCloseable closer = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, DocumentToClose);
302 
303         try
304         {
305             if (modified != null)
306             {
307                 modified.setModified(false);
308             }
309             closer.close(true);
310         }
311         catch (com.sun.star.util.CloseVetoException e)
312         {
313             // e.printStackTrace();
314             System.out.println("Couldn't close document");
315         }
316         catch (com.sun.star.lang.DisposedException e)
317         {
318             // e.printStackTrace();
319             System.out.println("Couldn't close document");
320         }
321         catch (java.lang.NullPointerException e)
322         {
323             // e.printStackTrace();
324             System.out.println("Couldn't close document");
325         }
326         catch (com.sun.star.beans.PropertyVetoException e)
327         {
328             // e.printStackTrace();
329             System.out.println("Couldn't close document");
330         }
331     }
332 
333     /**
334      * Creates a floating XWindow with the size of X=500 Y=100 width=400 height=600
335      * @param xMSF the MultiServiceFactory
336      * @throws lib.StatusException if it is not possible to create a floating window a lib.StatusException was thrown
337      * @return a floating XWindow
338      */
createFloatingWindow(XMultiServiceFactory xMSF)339     public static XWindowPeer createFloatingWindow(XMultiServiceFactory xMSF)
340             throws StatusException
341     {
342         return createFloatingWindow(xMSF, 500, 100, 400, 600);
343     }
344 
345     /**
346      * Creates a floating XWindow on the given position and size.
347      * @return a floating XWindow
348      * @param X the X-Postion of the floating XWindow
349      * @param Y the Y-Postion of the floating XWindow
350      * @param width the width of the floating XWindow
351      * @param height the height of the floating XWindow
352      * @param xMSF the MultiServiceFactory
353      * @throws lib.StatusException if it is not possible to create a floating window a lib.StatusException was thrown
354      */
createFloatingWindow(XMultiServiceFactory xMSF, int X, int Y, int width, int height)355     public static XWindowPeer createFloatingWindow(XMultiServiceFactory xMSF, int X, int Y, int width, int height)
356             throws StatusException
357     {
358 
359         XInterface oObj = null;
360 
361         try
362         {
363             oObj = (XInterface) xMSF.createInstance("com.sun.star.awt.Toolkit");
364         }
365         catch (com.sun.star.uno.Exception e)
366         {
367             throw new StatusException("Couldn't get toolkit", e);
368         }
369 
370         XToolkit tk = (XToolkit) UnoRuntime.queryInterface(
371                 XToolkit.class, oObj);
372 
373         WindowDescriptor descriptor = new com.sun.star.awt.WindowDescriptor();
374 
375         descriptor.Type = com.sun.star.awt.WindowClass.TOP;
376         descriptor.WindowServiceName = "modelessdialog";
377         descriptor.ParentIndex = -1;
378 
379         Rectangle bounds = new com.sun.star.awt.Rectangle();
380         bounds.X = X;
381         bounds.Y = Y;
382         bounds.Width = width;
383         bounds.Height = height;
384 
385         descriptor.Bounds = bounds;
386         descriptor.WindowAttributes = (com.sun.star.awt.WindowAttribute.BORDER +
387                 com.sun.star.awt.WindowAttribute.MOVEABLE +
388                 com.sun.star.awt.WindowAttribute.SIZEABLE +
389                 com.sun.star.awt.WindowAttribute.CLOSEABLE +
390                 com.sun.star.awt.VclWindowPeerAttribute.CLIPCHILDREN);
391 
392         XWindowPeer xWindow = null;
393 
394         try
395         {
396             xWindow = tk.createWindow(descriptor);
397         }
398         catch (com.sun.star.lang.IllegalArgumentException e)
399         {
400             throw new StatusException("Could not create window", e);
401         }
402 
403         return xWindow;
404 
405     }
406 
407     /**
408      * zoom to have a view over the hole page
409      * @param xDoc the document to zoom
410      */
zoomToEntirePage(XInterface xDoc)411     public static void zoomToEntirePage(XInterface xDoc)
412     {
413         try
414         {
415             XModel xMod = (XModel) UnoRuntime.queryInterface(XModel.class, xDoc);
416             XInterface oCont = xMod.getCurrentController();
417             XViewSettingsSupplier oVSSupp = (XViewSettingsSupplier) UnoRuntime.queryInterface(XViewSettingsSupplier.class, oCont);
418 
419             XInterface oViewSettings = oVSSupp.getViewSettings();
420             XPropertySet oViewProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, oViewSettings);
421             oViewProp.setPropertyValue("ZoomType",
422                     new Short(com.sun.star.view.DocumentZoomType.ENTIRE_PAGE));
423 
424             utils.shortWait(5000);
425         }
426         catch (Exception e)
427         {
428             System.out.println("Could not zoom to entire page: " + e.toString());
429         }
430 
431     }
432 
433     /**
434      * This function docks the Stylist onto the right side of the window.</p>
435      * Note:<P>
436      * Since the svt.viewoptions cache the view configuration at start up
437      * the chage of the docking will be effective at a restart.
438      * @param xMSF the XMultiServiceFactory
439      */
dockStylist(XMultiServiceFactory xMSF)440     public static void dockStylist(XMultiServiceFactory xMSF)
441     {
442         // prepare Window-Settings
443         try
444         {
445             ConfigHelper aConfig = new ConfigHelper(xMSF,
446                     "org.openoffice.Office.Views", false);
447 
448             // Is node "5539" (slot-id for navigator) available? If not, insert it
449             XNameReplace x5539 = aConfig.getOrInsertGroup("Windows", "5539");
450 
451             aConfig.updateGroupProperty(
452                     "Windows", "5539", "WindowState", "952,180,244,349;1;0,0,0,0;");
453 
454             aConfig.insertOrUpdateExtensibleGroupProperty(
455                     "Windows", "5539", "UserData", "Data", "V2,V,0,AL:(5,16,0/0/244/349,244;610)");
456 
457             // Is node "SplitWindow2" available? If not, instert it.
458             aConfig.getOrInsertGroup("Windows", "SplitWindow2");
459 
460             aConfig.insertOrUpdateExtensibleGroupProperty(
461                     "Windows", "SplitWindow2", "UserData", "UserItem", "V1,2,1,0,5539");
462 
463             aConfig.flush();
464             aConfig = null;
465 
466         }
467         catch (com.sun.star.uno.Exception e)
468         {
469             e.printStackTrace();
470         }
471     }
472 
473     /**
474      * Due to typo deprecated
475      * @param xModel
476      * @deprecated
477      */
478     @Deprecated
bringWindowToFromt(XModel xModel)479     public static void bringWindowToFromt(XModel xModel)
480     {
481         bringWindowToFront(xModel);
482     }
483 
484     /**
485      * This function brings a document to the front.<P>
486      * NOTE: it is not possible to change the window order of your Window-Manager!!
487      * Only the order of Office documents are changeable.
488      * @param xModel the XModel of the document to bring to top
489      */
bringWindowToFront(XModel xModel)490     public static void bringWindowToFront(XModel xModel)
491     {
492         // System.out.println("DEBUG: bring to front xModel");
493 
494         XTopWindow xTopWindow =
495                 (XTopWindow) UnoRuntime.queryInterface(
496                 XTopWindow.class,
497                 xModel.getCurrentController().getFrame().getContainerWindow());
498 
499         xTopWindow.toFront();
500     }
501 
bringWindowToFront(XComponent xComponent)502     public static void bringWindowToFront(XComponent xComponent)
503     {
504         // System.out.println("DEBUG: bring to front xComponent");
505         XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class, xComponent);
506         if (xModel != null)
507         {
508             bringWindowToFront(xModel);
509         }
510     }
511 }
512