1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package util;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir // access the implementations via names
27cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
28cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
29cdf0e10cSrcweir 
30cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
31cdf0e10cSrcweir import com.sun.star.lang.XComponent;
32cdf0e10cSrcweir import com.sun.star.drawing.XDrawPages;
33cdf0e10cSrcweir import com.sun.star.drawing.XDrawPagesSupplier;
34cdf0e10cSrcweir import com.sun.star.drawing.XDrawPage;
35cdf0e10cSrcweir import com.sun.star.drawing.XShapes;
36cdf0e10cSrcweir import com.sun.star.drawing.XShape;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir 
39cdf0e10cSrcweir import util.DesktopTools;
40cdf0e10cSrcweir import util.InstCreator;
41cdf0e10cSrcweir import util.ShapeDsc;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
44cdf0e10cSrcweir import com.sun.star.uno.Type;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir /**
47cdf0e10cSrcweir  * contains helper methods for draw documents
48cdf0e10cSrcweir  */
49cdf0e10cSrcweir 
50cdf0e10cSrcweir 
51cdf0e10cSrcweir public class DrawTools {
52cdf0e10cSrcweir 
53cdf0e10cSrcweir     /**
54cdf0e10cSrcweir      * Opens a new draw document
55cdf0e10cSrcweir      * with arguments
56cdf0e10cSrcweir      * @param xMSF the MultiServiceFactory
57cdf0e10cSrcweir      * @return the XComponent Interface of the document
58cdf0e10cSrcweir     */
59cdf0e10cSrcweir 
createDrawDoc( XMultiServiceFactory xMSF )60cdf0e10cSrcweir     public static XComponent createDrawDoc( XMultiServiceFactory xMSF ) {
61cdf0e10cSrcweir         PropertyValue[] Args = new PropertyValue [0];
62cdf0e10cSrcweir         XComponent DrawDoc = DesktopTools.openNewDoc(  xMSF, "sdraw", Args );
63cdf0e10cSrcweir         return DrawDoc;
64cdf0e10cSrcweir     } // finish createDrawDoc
65cdf0e10cSrcweir 
66cdf0e10cSrcweir     /**
67cdf0e10cSrcweir      * gets the XDrawPages container of a draw document
68cdf0e10cSrcweir      *
69cdf0e10cSrcweir      * @param aDoc the draw document
70cdf0e10cSrcweir      * @return the XDrawpages container of the document
71cdf0e10cSrcweir     */
72cdf0e10cSrcweir 
getDrawPages( XComponent aDoc )73cdf0e10cSrcweir     public static XDrawPages getDrawPages ( XComponent aDoc ) {
74cdf0e10cSrcweir         XDrawPages oDPn = null;
75cdf0e10cSrcweir         try {
76cdf0e10cSrcweir             XDrawPagesSupplier oDPS = (XDrawPagesSupplier)
77cdf0e10cSrcweir             UnoRuntime.queryInterface(XDrawPagesSupplier.class,aDoc);
78cdf0e10cSrcweir 
79cdf0e10cSrcweir             oDPn = oDPS.getDrawPages();
80cdf0e10cSrcweir         } catch ( Exception e ) {
81cdf0e10cSrcweir             throw new IllegalArgumentException( "Couldn't get drawpages" );
82cdf0e10cSrcweir         }
83cdf0e10cSrcweir         return oDPn;
84cdf0e10cSrcweir     } // finish getDrawPages
85cdf0e10cSrcweir 
86cdf0e10cSrcweir     /**
87cdf0e10cSrcweir      * gets the specified XDrawPage of a draw document
88cdf0e10cSrcweir      *
89cdf0e10cSrcweir      * @param aDoc the draw document
90cdf0e10cSrcweir      * @param nr the index of the DrawPage
91cdf0e10cSrcweir      * @return the XDrawpage with index nr of the document
92cdf0e10cSrcweir     */
93cdf0e10cSrcweir 
getDrawPage( XComponent aDoc, int nr )94cdf0e10cSrcweir     public static XDrawPage getDrawPage ( XComponent aDoc, int nr ) {
95cdf0e10cSrcweir         XDrawPage oDP = null;
96cdf0e10cSrcweir         try {
97cdf0e10cSrcweir             oDP = (XDrawPage) AnyConverter.toObject(
98cdf0e10cSrcweir                 new Type(XDrawPage.class),getDrawPages( aDoc ).getByIndex( nr ));
99cdf0e10cSrcweir         } catch ( Exception e ) {
100cdf0e10cSrcweir             throw new IllegalArgumentException( "Couldn't get drawpage" );
101cdf0e10cSrcweir         }
102cdf0e10cSrcweir         return oDP;
103cdf0e10cSrcweir     }
104cdf0e10cSrcweir 
105cdf0e10cSrcweir     /**
106cdf0e10cSrcweir      * gets the XShapes container of a draw page
107cdf0e10cSrcweir      *
108cdf0e10cSrcweir      * @param oDP the draw page
109cdf0e10cSrcweir      * @return the XDrawShape container of the drawpage
110cdf0e10cSrcweir     */
111cdf0e10cSrcweir 
getShapes( XDrawPage oDP )112cdf0e10cSrcweir     public static XShapes getShapes ( XDrawPage oDP ) {
113cdf0e10cSrcweir         return (XShapes) UnoRuntime.queryInterface(XShapes.class,oDP);
114cdf0e10cSrcweir     }
115cdf0e10cSrcweir 
116cdf0e10cSrcweir     /**
117cdf0e10cSrcweir      * creates a XShape
118cdf0e10cSrcweir      *
119cdf0e10cSrcweir      * @param oDoc the document
120cdf0e10cSrcweir      * @param height the height of the shape
121cdf0e10cSrcweir      * @param width the width of the shape
122cdf0e10cSrcweir      * @param x the x-position of the shape
123cdf0e10cSrcweir      * @param y the y-position of the shape
124cdf0e10cSrcweir      * @param kind the kind of the shape ('Ellipse', 'Line' or 'Rectangle')
125cdf0e10cSrcweir      * @return the created XShape
126cdf0e10cSrcweir     */
127cdf0e10cSrcweir 
createShape( XComponent oDoc, int height, int width, int x, int y, String kind )128cdf0e10cSrcweir     public XShape createShape( XComponent oDoc, int height, int width, int x,
129cdf0e10cSrcweir                                                      int y, String kind ) {
130cdf0e10cSrcweir         //possible values for kind are 'Ellipse', 'Line' and 'Rectangle'
131cdf0e10cSrcweir 
132cdf0e10cSrcweir         ShapeDsc sDsc = new ShapeDsc( height, width, x, y, kind );
133cdf0e10cSrcweir         InstCreator instCreate = new InstCreator( oDoc, sDsc );
134cdf0e10cSrcweir         XShape oShape = (XShape)instCreate.getInstance();
135cdf0e10cSrcweir 
136cdf0e10cSrcweir         return oShape;
137cdf0e10cSrcweir     }
138cdf0e10cSrcweir 
139cdf0e10cSrcweir     /**
140cdf0e10cSrcweir      * creates a XShape and adds it to the documents
141cdf0e10cSrcweir      * first drawpage
142cdf0e10cSrcweir      * @param oDoc the document
143cdf0e10cSrcweir      * @param height the height of the shape
144cdf0e10cSrcweir      * @param width the width of the shape
145cdf0e10cSrcweir      * @param x the x-position of the shape
146cdf0e10cSrcweir      * @param y the y-position of the shape
147cdf0e10cSrcweir      * @param kind the kind of the shape ('Ellipse', 'Line' or 'Rectangle')
148cdf0e10cSrcweir      * @return the created XShape
149cdf0e10cSrcweir     */
150cdf0e10cSrcweir 
addShape( XComponent oDoc, int height, int width, int x, int y, String kind )151cdf0e10cSrcweir     public void addShape(  XComponent oDoc, int height, int width, int x,
152cdf0e10cSrcweir                                                          int y, String kind ) {
153cdf0e10cSrcweir 
154cdf0e10cSrcweir         getShapes(getDrawPage(oDoc,0)).add(createShape( oDoc, height, width, x,
155cdf0e10cSrcweir                                                                     y, kind ) );
156cdf0e10cSrcweir     }
157cdf0e10cSrcweir 
158cdf0e10cSrcweir }
159