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