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