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