update emoji autocorrect entries from po-files
[LibreOffice.git] / odk / examples / java / Drawing / SDraw.java
blob73a05022147a6ceddd9e552ea0c2ef96a27d96c3
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 *************************************************************************/
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
42 import com.sun.star.uno.UnoRuntime;
44 public class SDraw {
47 public static void main(String args[]) {
49 //oooooooooooooooooooooooooooStep 1oooooooooooooooooooooooooooooooooooooooo
50 // bootstrap UNO and get the remote component context. The context can
51 // be used to get the service manager
53 com.sun.star.uno.XComponentContext xContext = null;
55 try {
56 // get the remote office component context
57 xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
58 System.out.println("Connected to a running office ...");
60 catch( Exception e) {
61 e.printStackTrace(System.err);
62 System.exit(1);
65 com.sun.star.lang.XComponent xDrawDoc = null;
66 com.sun.star.drawing.XDrawPage xDrawPage = null;
68 //oooooooooooooooooooooooooooStep 2oooooooooooooooooooooooooooooooooooooooo
69 // open an empty document. In this case it's a draw document.
70 // For this purpose an instance of com.sun.star.frame.Desktop
71 // is created. It's interface XDesktop provides the XComponentLoader,
72 // which is used to open the document via loadComponentFromURL
75 //Open document
76 //Draw
77 System.out.println("Opening an empty Draw document ...");
78 xDrawDoc = openDraw(xContext);
80 //oooooooooooooooooooooooooooStep 3oooooooooooooooooooooooooooooooooooooooo
81 // get the drawpage an insert some shapes.
82 // the documents DrawPageSupplier supplies the DrawPage vi IndexAccess
83 // To add a shape get the MultiServiceFaktory of the document, create an
84 // instance of the ShapeType and add it to the Shapes-container
85 // provided by the drawpage
89 // get the drawpage of drawing here
90 try {
91 System.out.println( "getting Drawpage" );
92 com.sun.star.drawing.XDrawPagesSupplier xDPS =
93 UnoRuntime.queryInterface(
94 com.sun.star.drawing.XDrawPagesSupplier.class, xDrawDoc);
95 com.sun.star.drawing.XDrawPages xDPn = xDPS.getDrawPages();
96 com.sun.star.container.XIndexAccess xDPi =
97 UnoRuntime.queryInterface(
98 com.sun.star.container.XIndexAccess.class, xDPn);
99 xDrawPage = UnoRuntime.queryInterface(
100 com.sun.star.drawing.XDrawPage.class, xDPi.getByIndex(0));
101 } catch ( Exception e ) {
102 System.err.println( "Couldn't create document"+ e );
103 e.printStackTrace(System.err);
106 createSequence(xDrawDoc, xDrawPage);
108 //put something on the drawpage
109 System.out.println( "inserting some Shapes" );
110 com.sun.star.drawing.XShapes xShapes = UnoRuntime.queryInterface(
111 com.sun.star.drawing.XShapes.class, xDrawPage);
112 xShapes.add(createShape(xDrawDoc,2000,1500,1000,1000,"Line",0));
113 xShapes.add(createShape(xDrawDoc,3000,4500,15000,1000,"Ellipse",16711680));
114 xShapes.add(createShape(xDrawDoc,5000,3500,7500,5000,"Rectangle",6710932));
117 System.out.println("done");
118 System.exit(0);
121 public static com.sun.star.lang.XComponent openDraw(
122 com.sun.star.uno.XComponentContext xContext)
124 com.sun.star.frame.XComponentLoader xCLoader;
125 com.sun.star.lang.XComponent xComp = null;
127 try {
128 // get the remote office service manager
129 com.sun.star.lang.XMultiComponentFactory xMCF =
130 xContext.getServiceManager();
132 Object oDesktop = xMCF.createInstanceWithContext(
133 "com.sun.star.frame.Desktop", xContext);
135 xCLoader = UnoRuntime.queryInterface(com.sun.star.frame.XComponentLoader.class,
136 oDesktop);
137 com.sun.star.beans.PropertyValue szEmptyArgs[] =
138 new com.sun.star.beans.PropertyValue[0];
139 String strDoc = "private:factory/sdraw";
140 xComp = xCLoader.loadComponentFromURL(strDoc, "_blank", 0, szEmptyArgs);
142 } catch(Exception e){
143 System.err.println(" Exception " + e);
144 e.printStackTrace(System.err);
147 return xComp;
150 public static com.sun.star.drawing.XShape createShape(
151 com.sun.star.lang.XComponent xDocComp, int height, int width, int x,
152 int y, String kind, int col)
154 //possible values for kind are 'Ellipse', 'Line' and 'Rectangle'
155 com.sun.star.awt.Size size = new com.sun.star.awt.Size();
156 com.sun.star.awt.Point position = new com.sun.star.awt.Point();
157 com.sun.star.drawing.XShape xShape = null;
159 //get MSF
160 com.sun.star.lang.XMultiServiceFactory xDocMSF =
161 UnoRuntime.queryInterface(
162 com.sun.star.lang.XMultiServiceFactory.class, xDocComp );
164 try {
165 Object oInt = xDocMSF.createInstance("com.sun.star.drawing."
166 +kind + "Shape");
167 xShape = UnoRuntime.queryInterface(
168 com.sun.star.drawing.XShape.class, oInt);
169 size.Height = height;
170 size.Width = width;
171 position.X = x;
172 position.Y = y;
173 xShape.setSize(size);
174 xShape.setPosition(position);
176 } catch ( Exception e ) {
177 System.err.println( "Couldn't create instance "+ e );
178 e.printStackTrace(System.err);
181 com.sun.star.beans.XPropertySet xSPS = UnoRuntime.queryInterface(
182 com.sun.star.beans.XPropertySet.class, xShape);
184 try {
185 xSPS.setPropertyValue("FillColor", Integer.valueOf(col));
186 } catch (Exception e) {
187 System.err.println("Can't change colors " + e);
188 e.printStackTrace(System.err);
191 return xShape;
194 public static com.sun.star.drawing.XShape createSequence(
195 com.sun.star.lang.XComponent xDocComp, com.sun.star.drawing.XDrawPage xDP)
197 com.sun.star.awt.Size size = new com.sun.star.awt.Size();
198 com.sun.star.awt.Point position = new com.sun.star.awt.Point();
199 com.sun.star.drawing.XShape xShape = null;
200 com.sun.star.drawing.XShapes xShapes = UnoRuntime.queryInterface(com.sun.star.drawing.XShapes.class, xDP);
201 int height = 3000;
202 int width = 3500;
203 int x = 1900;
204 int y = 20000;
205 Object oInt = null;
206 int r = 40;
207 int g = 0;
208 int b = 80;
210 //get MSF
211 com.sun.star.lang.XMultiServiceFactory xDocMSF =
212 UnoRuntime.queryInterface(
213 com.sun.star.lang.XMultiServiceFactory.class, xDocComp );
215 for (int i=0; i<370; i=i+25) {
216 try{
217 oInt = xDocMSF.createInstance("com.sun.star.drawing.EllipseShape");
218 xShape = UnoRuntime.queryInterface(
219 com.sun.star.drawing.XShape.class, oInt);
220 size.Height = height;
221 size.Width = width;
222 position.X = (x+(i*40));
223 position.Y =
224 (new Float(y+(Math.sin((i*Math.PI)/180))*5000)).intValue();
225 xShape.setSize(size);
226 xShape.setPosition(position);
228 } catch ( Exception e ) {
229 // Some exception occurs.FAILED
230 System.err.println( "Couldn't get Shape "+ e );
231 e.printStackTrace(System.err);
234 b=b+8;
236 com.sun.star.beans.XPropertySet xSPS = UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class,
237 xShape);
239 try {
240 xSPS.setPropertyValue("FillColor", Integer.valueOf(getCol(r,g,b)));
241 xSPS.setPropertyValue("Shadow", Boolean.TRUE);
242 } catch (Exception e) {
243 System.err.println("Can't change colors " + e);
244 e.printStackTrace(System.err);
246 xShapes.add(xShape);
249 com.sun.star.drawing.XShapeGrouper xSGrouper =
250 UnoRuntime.queryInterface(
251 com.sun.star.drawing.XShapeGrouper.class, xDP);
253 xShape = xSGrouper.group(xShapes);
255 return xShape;
258 public static int getCol(int r, int g, int b) {
259 return r*65536+g*256+b;