1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package util;
29 
30 // access the implementations via names
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.uno.UnoRuntime;
33 
34 import com.sun.star.beans.PropertyValue;
35 import com.sun.star.lang.XComponent;
36 import com.sun.star.drawing.XDrawPages;
37 import com.sun.star.drawing.XDrawPagesSupplier;
38 import com.sun.star.drawing.XDrawPage;
39 import com.sun.star.drawing.XShapes;
40 import com.sun.star.drawing.XShape;
41 
42 
43 import util.DesktopTools;
44 import util.InstCreator;
45 import util.ShapeDsc;
46 
47 import com.sun.star.uno.AnyConverter;
48 import com.sun.star.uno.Type;
49 
50 /**
51  * contains helper methods for draw documents
52  */
53 
54 
55 public class DrawTools {
56 
57     /**
58      * Opens a new draw document
59      * with arguments
60      * @param xMSF the MultiServiceFactory
61      * @return the XComponent Interface of the document
62     */
63 
64     public static XComponent createDrawDoc( XMultiServiceFactory xMSF ) {
65         PropertyValue[] Args = new PropertyValue [0];
66         XComponent DrawDoc = DesktopTools.openNewDoc(  xMSF, "sdraw", Args );
67         return DrawDoc;
68     } // finish createDrawDoc
69 
70     /**
71      * gets the XDrawPages container of a draw document
72      *
73      * @param aDoc the draw document
74      * @return the XDrawpages container of the document
75     */
76 
77     public static XDrawPages getDrawPages ( XComponent aDoc ) {
78         XDrawPages oDPn = null;
79         try {
80             XDrawPagesSupplier oDPS = (XDrawPagesSupplier)
81             UnoRuntime.queryInterface(XDrawPagesSupplier.class,aDoc);
82 
83             oDPn = oDPS.getDrawPages();
84         } catch ( Exception e ) {
85             throw new IllegalArgumentException( "Couldn't get drawpages" );
86         }
87         return oDPn;
88     } // finish getDrawPages
89 
90     /**
91      * gets the specified XDrawPage of a draw document
92      *
93      * @param aDoc the draw document
94      * @param nr the index of the DrawPage
95      * @return the XDrawpage with index nr of the document
96     */
97 
98     public static XDrawPage getDrawPage ( XComponent aDoc, int nr ) {
99         XDrawPage oDP = null;
100         try {
101             oDP = (XDrawPage) AnyConverter.toObject(
102                 new Type(XDrawPage.class),getDrawPages( aDoc ).getByIndex( nr ));
103         } catch ( Exception e ) {
104             throw new IllegalArgumentException( "Couldn't get drawpage" );
105         }
106         return oDP;
107     }
108 
109     /**
110      * gets the XShapes container of a draw page
111      *
112      * @param oDP the draw page
113      * @return the XDrawShape container of the drawpage
114     */
115 
116     public static XShapes getShapes ( XDrawPage oDP ) {
117         return (XShapes) UnoRuntime.queryInterface(XShapes.class,oDP);
118     }
119 
120     /**
121      * creates a XShape
122      *
123      * @param oDoc the document
124      * @param height the height of the shape
125      * @param width the width of the shape
126      * @param x the x-position of the shape
127      * @param y the y-position of the shape
128      * @param kind the kind of the shape ('Ellipse', 'Line' or 'Rectangle')
129      * @return the created XShape
130     */
131 
132     public XShape createShape( XComponent oDoc, int height, int width, int x,
133                                                      int y, String kind ) {
134         //possible values for kind are 'Ellipse', 'Line' and 'Rectangle'
135 
136         ShapeDsc sDsc = new ShapeDsc( height, width, x, y, kind );
137         InstCreator instCreate = new InstCreator( oDoc, sDsc );
138         XShape oShape = (XShape)instCreate.getInstance();
139 
140         return oShape;
141     }
142 
143     /**
144      * creates a XShape and adds it to the documents
145      * first drawpage
146      * @param oDoc the document
147      * @param height the height of the shape
148      * @param width the width of the shape
149      * @param x the x-position of the shape
150      * @param y the y-position of the shape
151      * @param kind the kind of the shape ('Ellipse', 'Line' or 'Rectangle')
152      * @return the created XShape
153     */
154 
155     public void addShape(  XComponent oDoc, int height, int width, int x,
156                                                          int y, String kind ) {
157 
158         getShapes(getDrawPage(oDoc,0)).add(createShape( oDoc, height, width, x,
159                                                                     y, kind ) );
160     }
161 
162 }
163