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