xref: /AOO41X/main/odk/examples/java/Drawing/SDraw.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  *  The Contents of this file are made available subject to the terms of
4*cdf0e10cSrcweir  *  the BSD license.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir  *  All rights reserved.
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  *  Redistribution and use in source and binary forms, with or without
10*cdf0e10cSrcweir  *  modification, are permitted provided that the following conditions
11*cdf0e10cSrcweir  *  are met:
12*cdf0e10cSrcweir  *  1. Redistributions of source code must retain the above copyright
13*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer.
14*cdf0e10cSrcweir  *  2. Redistributions in binary form must reproduce the above copyright
15*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer in the
16*cdf0e10cSrcweir  *     documentation and/or other materials provided with the distribution.
17*cdf0e10cSrcweir  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18*cdf0e10cSrcweir  *     contributors may be used to endorse or promote products derived
19*cdf0e10cSrcweir  *     from this software without specific prior written permission.
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*cdf0e10cSrcweir  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*cdf0e10cSrcweir  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*cdf0e10cSrcweir  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25*cdf0e10cSrcweir  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*cdf0e10cSrcweir  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*cdf0e10cSrcweir  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28*cdf0e10cSrcweir  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29*cdf0e10cSrcweir  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30*cdf0e10cSrcweir  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31*cdf0e10cSrcweir  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*cdf0e10cSrcweir  *
33*cdf0e10cSrcweir  *************************************************************************/
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir //***************************************************************************
36*cdf0e10cSrcweir // comment: Step 1: bootstrap UNO and get the remote component context
37*cdf0e10cSrcweir //          Step 2: open an empty text document
38*cdf0e10cSrcweir //          Step 3: get the drawpage an insert some shapes
39*cdf0e10cSrcweir //***************************************************************************
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir import java.lang.Math;
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir public class SDraw  {
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir     public static void main(String args[]) {
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir         //oooooooooooooooooooooooooooStep 1oooooooooooooooooooooooooooooooooooooooo
52*cdf0e10cSrcweir         // bootstrap UNO and get the remote component context. The context can
53*cdf0e10cSrcweir         // be used to get the service manager
54*cdf0e10cSrcweir         //*************************************************************************
55*cdf0e10cSrcweir         com.sun.star.uno.XComponentContext xContext = null;
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir         try {
58*cdf0e10cSrcweir             // get the remote office component context
59*cdf0e10cSrcweir             xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
60*cdf0e10cSrcweir             System.out.println("Connected to a running office ...");
61*cdf0e10cSrcweir         }
62*cdf0e10cSrcweir         catch( Exception e) {
63*cdf0e10cSrcweir             e.printStackTrace(System.err);
64*cdf0e10cSrcweir             System.exit(1);
65*cdf0e10cSrcweir         }
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir         com.sun.star.lang.XComponent xDrawDoc = null;
68*cdf0e10cSrcweir         com.sun.star.drawing.XDrawPage xDrawPage = null;
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir         //oooooooooooooooooooooooooooStep 2oooooooooooooooooooooooooooooooooooooooo
71*cdf0e10cSrcweir         // open an empty document. In this case it's a draw document.
72*cdf0e10cSrcweir         // For this purpose an instance of com.sun.star.frame.Desktop
73*cdf0e10cSrcweir         // is created. It's interface XDesktop provides the XComponentLoader,
74*cdf0e10cSrcweir         // which is used to open the document via loadComponentFromURL
75*cdf0e10cSrcweir         //*************************************************************************
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir         //Open document
78*cdf0e10cSrcweir         //Draw
79*cdf0e10cSrcweir         System.out.println("Opening an empty Draw document ...");
80*cdf0e10cSrcweir         xDrawDoc = openDraw(xContext);
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir         //oooooooooooooooooooooooooooStep 3oooooooooooooooooooooooooooooooooooooooo
83*cdf0e10cSrcweir         // get the drawpage an insert some shapes.
84*cdf0e10cSrcweir         // the documents DrawPageSupplier supplies the DrawPage vi IndexAccess
85*cdf0e10cSrcweir         // To add a shape get the MultiServiceFaktory of the document, create an
86*cdf0e10cSrcweir         // instance of the ShapeType and add it to the Shapes-container
87*cdf0e10cSrcweir         // provided by the drawpage
88*cdf0e10cSrcweir         //*************************************************************************
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir         // get the drawpage of drawing here
92*cdf0e10cSrcweir         try {
93*cdf0e10cSrcweir             System.out.println( "getting Drawpage" );
94*cdf0e10cSrcweir             com.sun.star.drawing.XDrawPagesSupplier xDPS =
95*cdf0e10cSrcweir                 (com.sun.star.drawing.XDrawPagesSupplier)UnoRuntime.queryInterface(
96*cdf0e10cSrcweir                     com.sun.star.drawing.XDrawPagesSupplier.class, xDrawDoc);
97*cdf0e10cSrcweir             com.sun.star.drawing.XDrawPages xDPn = xDPS.getDrawPages();
98*cdf0e10cSrcweir             com.sun.star.container.XIndexAccess xDPi =
99*cdf0e10cSrcweir                 (com.sun.star.container.XIndexAccess)UnoRuntime.queryInterface(
100*cdf0e10cSrcweir                     com.sun.star.container.XIndexAccess.class, xDPn);
101*cdf0e10cSrcweir             xDrawPage = (com.sun.star.drawing.XDrawPage)UnoRuntime.queryInterface(
102*cdf0e10cSrcweir                 com.sun.star.drawing.XDrawPage.class, xDPi.getByIndex(0));
103*cdf0e10cSrcweir         } catch ( Exception e ) {
104*cdf0e10cSrcweir             System.err.println( "Couldn't create document"+ e );
105*cdf0e10cSrcweir             e.printStackTrace(System.err);
106*cdf0e10cSrcweir         }
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir         createSequence(xDrawDoc, xDrawPage);
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir         //put something on the drawpage
111*cdf0e10cSrcweir         System.out.println( "inserting some Shapes" );
112*cdf0e10cSrcweir         com.sun.star.drawing.XShapes xShapes = (com.sun.star.drawing.XShapes)
113*cdf0e10cSrcweir             UnoRuntime.queryInterface(
114*cdf0e10cSrcweir                 com.sun.star.drawing.XShapes.class, xDrawPage);
115*cdf0e10cSrcweir         xShapes.add(createShape(xDrawDoc,2000,1500,1000,1000,"Line",0));
116*cdf0e10cSrcweir         xShapes.add(createShape(xDrawDoc,3000,4500,15000,1000,"Ellipse",16711680));
117*cdf0e10cSrcweir         xShapes.add(createShape(xDrawDoc,5000,3500,7500,5000,"Rectangle",6710932));
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir         //*************************************************************************
120*cdf0e10cSrcweir         System.out.println("done");
121*cdf0e10cSrcweir         System.exit(0);
122*cdf0e10cSrcweir     }
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir     public static com.sun.star.lang.XComponent openDraw(
125*cdf0e10cSrcweir         com.sun.star.uno.XComponentContext xContext)
126*cdf0e10cSrcweir     {
127*cdf0e10cSrcweir         com.sun.star.frame.XComponentLoader xCLoader;
128*cdf0e10cSrcweir         com.sun.star.text.XTextDocument xDoc = null;
129*cdf0e10cSrcweir         com.sun.star.lang.XComponent xComp = null;
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir         try {
132*cdf0e10cSrcweir             // get the remote office service manager
133*cdf0e10cSrcweir             com.sun.star.lang.XMultiComponentFactory xMCF =
134*cdf0e10cSrcweir                 xContext.getServiceManager();
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir             Object oDesktop = xMCF.createInstanceWithContext(
137*cdf0e10cSrcweir                                         "com.sun.star.frame.Desktop", xContext);
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir             xCLoader = (com.sun.star.frame.XComponentLoader)
140*cdf0e10cSrcweir                 UnoRuntime.queryInterface(com.sun.star.frame.XComponentLoader.class,
141*cdf0e10cSrcweir                                           oDesktop);
142*cdf0e10cSrcweir             com.sun.star.beans.PropertyValue szEmptyArgs[] =
143*cdf0e10cSrcweir                 new com.sun.star.beans.PropertyValue[0];
144*cdf0e10cSrcweir             String strDoc = "private:factory/sdraw";
145*cdf0e10cSrcweir             xComp = xCLoader.loadComponentFromURL(strDoc, "_blank", 0, szEmptyArgs);
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir         } catch(Exception e){
148*cdf0e10cSrcweir             System.err.println(" Exception " + e);
149*cdf0e10cSrcweir             e.printStackTrace(System.err);
150*cdf0e10cSrcweir         }
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir         return xComp;
153*cdf0e10cSrcweir     }
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir     public static com.sun.star.drawing.XShape createShape(
156*cdf0e10cSrcweir         com.sun.star.lang.XComponent xDocComp, int height, int width, int x,
157*cdf0e10cSrcweir         int y, String kind, int col)
158*cdf0e10cSrcweir     {
159*cdf0e10cSrcweir         //possible values for kind are 'Ellipse', 'Line' and 'Rectangle'
160*cdf0e10cSrcweir         com.sun.star.awt.Size size = new com.sun.star.awt.Size();
161*cdf0e10cSrcweir         com.sun.star.awt.Point position = new com.sun.star.awt.Point();
162*cdf0e10cSrcweir         com.sun.star.drawing.XShape xShape = null;
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir         //get MSF
165*cdf0e10cSrcweir         com.sun.star.lang.XMultiServiceFactory xDocMSF =
166*cdf0e10cSrcweir             (com.sun.star.lang.XMultiServiceFactory) UnoRuntime.queryInterface(
167*cdf0e10cSrcweir                 com.sun.star.lang.XMultiServiceFactory.class, xDocComp );
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir         try {
170*cdf0e10cSrcweir             Object oInt = xDocMSF.createInstance("com.sun.star.drawing."
171*cdf0e10cSrcweir                                                  +kind + "Shape");
172*cdf0e10cSrcweir             xShape = (com.sun.star.drawing.XShape)UnoRuntime.queryInterface(
173*cdf0e10cSrcweir                 com.sun.star.drawing.XShape.class, oInt);
174*cdf0e10cSrcweir             size.Height = height;
175*cdf0e10cSrcweir             size.Width = width;
176*cdf0e10cSrcweir             position.X = x;
177*cdf0e10cSrcweir             position.Y = y;
178*cdf0e10cSrcweir             xShape.setSize(size);
179*cdf0e10cSrcweir             xShape.setPosition(position);
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir         } catch ( Exception e ) {
182*cdf0e10cSrcweir             System.err.println( "Couldn't create instance "+ e );
183*cdf0e10cSrcweir             e.printStackTrace(System.err);
184*cdf0e10cSrcweir         }
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir         com.sun.star.beans.XPropertySet xSPS = (com.sun.star.beans.XPropertySet)
187*cdf0e10cSrcweir             UnoRuntime.queryInterface(
188*cdf0e10cSrcweir                 com.sun.star.beans.XPropertySet.class, xShape);
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir         try {
191*cdf0e10cSrcweir             xSPS.setPropertyValue("FillColor", new Integer(col));
192*cdf0e10cSrcweir         } catch (Exception e) {
193*cdf0e10cSrcweir             System.err.println("Can't change colors " + e);
194*cdf0e10cSrcweir             e.printStackTrace(System.err);
195*cdf0e10cSrcweir         }
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir         return xShape;
198*cdf0e10cSrcweir     }
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir     public static com.sun.star.drawing.XShape createSequence(
201*cdf0e10cSrcweir         com.sun.star.lang.XComponent xDocComp, com.sun.star.drawing.XDrawPage xDP)
202*cdf0e10cSrcweir     {
203*cdf0e10cSrcweir         com.sun.star.awt.Size size = new com.sun.star.awt.Size();
204*cdf0e10cSrcweir         com.sun.star.awt.Point position = new com.sun.star.awt.Point();
205*cdf0e10cSrcweir         com.sun.star.drawing.XShape xShape = null;
206*cdf0e10cSrcweir         com.sun.star.drawing.XShapes xShapes = (com.sun.star.drawing.XShapes)
207*cdf0e10cSrcweir             UnoRuntime.queryInterface(com.sun.star.drawing.XShapes.class, xDP);
208*cdf0e10cSrcweir         int height = 3000;
209*cdf0e10cSrcweir         int width = 3500;
210*cdf0e10cSrcweir         int x = 1900;
211*cdf0e10cSrcweir         int y = 20000;
212*cdf0e10cSrcweir         Object oInt = null;
213*cdf0e10cSrcweir         int r = 40;
214*cdf0e10cSrcweir         int g = 0;
215*cdf0e10cSrcweir         int b = 80;
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir         //get MSF
218*cdf0e10cSrcweir         com.sun.star.lang.XMultiServiceFactory xDocMSF =
219*cdf0e10cSrcweir             (com.sun.star.lang.XMultiServiceFactory)UnoRuntime.queryInterface(
220*cdf0e10cSrcweir                 com.sun.star.lang.XMultiServiceFactory.class, xDocComp );
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir         for (int i=0; i<370; i=i+25) {
223*cdf0e10cSrcweir             try{
224*cdf0e10cSrcweir                 oInt = xDocMSF.createInstance("com.sun.star.drawing.EllipseShape");
225*cdf0e10cSrcweir                 xShape = (com.sun.star.drawing.XShape)UnoRuntime.queryInterface(
226*cdf0e10cSrcweir                     com.sun.star.drawing.XShape.class, oInt);
227*cdf0e10cSrcweir                 size.Height = height;
228*cdf0e10cSrcweir                 size.Width = width;
229*cdf0e10cSrcweir                 position.X = (x+(i*40));
230*cdf0e10cSrcweir                 position.Y =
231*cdf0e10cSrcweir                     (new Float(y+(Math.sin((i*Math.PI)/180))*5000)).intValue();
232*cdf0e10cSrcweir                 xShape.setSize(size);
233*cdf0e10cSrcweir                 xShape.setPosition(position);
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir             } catch ( Exception e ) {
236*cdf0e10cSrcweir                 // Some exception occures.FAILED
237*cdf0e10cSrcweir                 System.err.println( "Couldn't get Shape "+ e );
238*cdf0e10cSrcweir                 e.printStackTrace(System.err);
239*cdf0e10cSrcweir             }
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir             b=b+8;
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir             com.sun.star.beans.XPropertySet xSPS = (com.sun.star.beans.XPropertySet)
244*cdf0e10cSrcweir                 UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class,
245*cdf0e10cSrcweir                                           xShape);
246*cdf0e10cSrcweir 
247*cdf0e10cSrcweir             try {
248*cdf0e10cSrcweir                 xSPS.setPropertyValue("FillColor", new Integer(getCol(r,g,b)));
249*cdf0e10cSrcweir                 xSPS.setPropertyValue("Shadow", new Boolean(true));
250*cdf0e10cSrcweir             } catch (Exception e) {
251*cdf0e10cSrcweir                 System.err.println("Can't change colors " + e);
252*cdf0e10cSrcweir                 e.printStackTrace(System.err);
253*cdf0e10cSrcweir             }
254*cdf0e10cSrcweir             xShapes.add(xShape);
255*cdf0e10cSrcweir         }
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir         com.sun.star.drawing.XShapeGrouper xSGrouper =
258*cdf0e10cSrcweir             (com.sun.star.drawing.XShapeGrouper)UnoRuntime.queryInterface(
259*cdf0e10cSrcweir                 com.sun.star.drawing.XShapeGrouper.class, xDP);
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir         xShape = xSGrouper.group(xShapes);
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir         return xShape;
264*cdf0e10cSrcweir     }
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir     public static int getCol(int r, int g, int b) {
267*cdf0e10cSrcweir         return r*65536+g*256+b;
268*cdf0e10cSrcweir     }
269*cdf0e10cSrcweir }
270*cdf0e10cSrcweir 
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir 
273