1 /*
2  * This file is part of the LibreOffice project.
3  *
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/.
7  *
8  * This file incorporates work covered by the following license notice:
9  *
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 .
17  */
18 
19 package util;
20 
21 // access the implementations via names
22 import com.sun.star.lang.XMultiServiceFactory;
23 import com.sun.star.uno.UnoRuntime;
24 
25 import com.sun.star.beans.PropertyValue;
26 import com.sun.star.lang.XComponent;
27 import com.sun.star.drawing.XDrawPages;
28 import com.sun.star.drawing.XDrawPagesSupplier;
29 import com.sun.star.drawing.XDrawPage;
30 import com.sun.star.drawing.XShapes;
31 import com.sun.star.uno.AnyConverter;
32 import com.sun.star.uno.Type;
33 
34 /**
35  * contains helper methods for draw documents
36  */
37 
38 
39 public class DrawTools {
40 
41     /**
42      * Opens a new draw document
43      * with arguments
44      * @param xMSF the MultiServiceFactory
45      * @return the XComponent Interface of the document
46     */
47 
createDrawDoc( XMultiServiceFactory xMSF )48     public static XComponent createDrawDoc( XMultiServiceFactory xMSF ) {
49         PropertyValue[] Args = new PropertyValue [0];
50         XComponent DrawDoc = DesktopTools.openNewDoc(  xMSF, "sdraw", Args );
51         return DrawDoc;
52     } // finish createDrawDoc
53 
54     /**
55      * gets the XDrawPages container of a draw document
56      *
57      * @param aDoc the draw document
58      * @return the XDrawpages container of the document
59     */
60 
getDrawPages( XComponent aDoc )61     public static XDrawPages getDrawPages ( XComponent aDoc ) {
62         XDrawPages oDPn = null;
63         try {
64             XDrawPagesSupplier oDPS = UnoRuntime.queryInterface(XDrawPagesSupplier.class,aDoc);
65 
66             oDPn = oDPS.getDrawPages();
67         } catch ( Exception e ) {
68             throw new IllegalArgumentException( "Couldn't get drawpages", e );
69         }
70         return oDPn;
71     } // finish getDrawPages
72 
73     /**
74      * gets the specified XDrawPage of a draw document
75      *
76      * @param aDoc the draw document
77      * @param nr the index of the DrawPage
78      * @return the XDrawpage with index nr of the document
79     */
80 
getDrawPage( XComponent aDoc, int nr )81     public static XDrawPage getDrawPage ( XComponent aDoc, int nr ) {
82         XDrawPage oDP = null;
83         try {
84             oDP = (XDrawPage) AnyConverter.toObject(
85                 new Type(XDrawPage.class),getDrawPages( aDoc ).getByIndex( nr ));
86         } catch ( Exception e ) {
87             throw new IllegalArgumentException( "Couldn't get drawpage", e );
88         }
89         return oDP;
90     }
91 
92     /**
93      * gets the XShapes container of a draw page
94      *
95      * @param oDP the draw page
96      * @return the XDrawShape container of the drawpage
97     */
98 
getShapes( XDrawPage oDP )99     public static XShapes getShapes ( XDrawPage oDP ) {
100         return UnoRuntime.queryInterface(XShapes.class,oDP);
101     }
102 
103 }
104