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 
24 package util;
25 
26 // access the implementations via names
27 import com.sun.star.lang.XMultiServiceFactory;
28 import com.sun.star.uno.UnoRuntime;
29 
30 import com.sun.star.beans.PropertyValue;
31 import com.sun.star.lang.XComponent;
32 import com.sun.star.drawing.XDrawPages;
33 import com.sun.star.drawing.XDrawPagesSupplier;
34 import com.sun.star.drawing.XDrawPage;
35 import com.sun.star.drawing.XShapes;
36 import com.sun.star.drawing.XShape;
37 
38 
39 import util.DesktopTools;
40 import util.InstCreator;
41 import util.ShapeDsc;
42 
43 import com.sun.star.uno.AnyConverter;
44 import com.sun.star.uno.Type;
45 
46 /**
47  * contains helper methods for draw documents
48  */
49 
50 
51 public class DrawTools {
52 
53     /**
54      * Opens a new draw document
55      * with arguments
56      * @param xMSF the MultiServiceFactory
57      * @return the XComponent Interface of the document
58     */
59 
createDrawDoc( XMultiServiceFactory xMSF )60     public static XComponent createDrawDoc( XMultiServiceFactory xMSF ) {
61         PropertyValue[] Args = new PropertyValue [0];
62         XComponent DrawDoc = DesktopTools.openNewDoc(  xMSF, "sdraw", Args );
63         return DrawDoc;
64     } // finish createDrawDoc
65 
66     /**
67      * gets the XDrawPages container of a draw document
68      *
69      * @param aDoc the draw document
70      * @return the XDrawpages container of the document
71     */
72 
getDrawPages( XComponent aDoc )73     public static XDrawPages getDrawPages ( XComponent aDoc ) {
74         XDrawPages oDPn = null;
75         try {
76             XDrawPagesSupplier oDPS = (XDrawPagesSupplier)
77             UnoRuntime.queryInterface(XDrawPagesSupplier.class,aDoc);
78 
79             oDPn = oDPS.getDrawPages();
80         } catch ( Exception e ) {
81             throw new IllegalArgumentException( "Couldn't get drawpages" );
82         }
83         return oDPn;
84     } // finish getDrawPages
85 
86     /**
87      * gets the specified XDrawPage of a draw document
88      *
89      * @param aDoc the draw document
90      * @param nr the index of the DrawPage
91      * @return the XDrawpage with index nr of the document
92     */
93 
getDrawPage( XComponent aDoc, int nr )94     public static XDrawPage getDrawPage ( XComponent aDoc, int nr ) {
95         XDrawPage oDP = null;
96         try {
97             oDP = (XDrawPage) AnyConverter.toObject(
98                 new Type(XDrawPage.class),getDrawPages( aDoc ).getByIndex( nr ));
99         } catch ( Exception e ) {
100             throw new IllegalArgumentException( "Couldn't get drawpage" );
101         }
102         return oDP;
103     }
104 
105     /**
106      * gets the XShapes container of a draw page
107      *
108      * @param oDP the draw page
109      * @return the XDrawShape container of the drawpage
110     */
111 
getShapes( XDrawPage oDP )112     public static XShapes getShapes ( XDrawPage oDP ) {
113         return (XShapes) UnoRuntime.queryInterface(XShapes.class,oDP);
114     }
115 
116     /**
117      * creates a XShape
118      *
119      * @param oDoc the document
120      * @param height the height of the shape
121      * @param width the width of the shape
122      * @param x the x-position of the shape
123      * @param y the y-position of the shape
124      * @param kind the kind of the shape ('Ellipse', 'Line' or 'Rectangle')
125      * @return the created XShape
126     */
127 
createShape( XComponent oDoc, int height, int width, int x, int y, String kind )128     public XShape createShape( XComponent oDoc, int height, int width, int x,
129                                                      int y, String kind ) {
130         //possible values for kind are 'Ellipse', 'Line' and 'Rectangle'
131 
132         ShapeDsc sDsc = new ShapeDsc( height, width, x, y, kind );
133         InstCreator instCreate = new InstCreator( oDoc, sDsc );
134         XShape oShape = (XShape)instCreate.getInstance();
135 
136         return oShape;
137     }
138 
139     /**
140      * creates a XShape and adds it to the documents
141      * first drawpage
142      * @param oDoc the document
143      * @param height the height of the shape
144      * @param width the width of the shape
145      * @param x the x-position of the shape
146      * @param y the y-position of the shape
147      * @param kind the kind of the shape ('Ellipse', 'Line' or 'Rectangle')
148      * @return the created XShape
149     */
150 
addShape( XComponent oDoc, int height, int width, int x, int y, String kind )151     public void addShape(  XComponent oDoc, int height, int width, int x,
152                                                          int y, String kind ) {
153 
154         getShapes(getDrawPage(oDoc,0)).add(createShape( oDoc, height, width, x,
155                                                                     y, kind ) );
156     }
157 
158 }
159