xref: /aoo41x/main/odk/examples/java/Drawing/SDraw.java (revision 34dd1e25)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 //***************************************************************************
25 // comment: Step 1: bootstrap UNO and get the remote component context
26 //          Step 2: open an empty text document
27 //          Step 3: get the drawpage an insert some shapes
28 //***************************************************************************
29 
30 
31 import java.lang.Math;
32 
33 import com.sun.star.uno.UnoRuntime;
34 
35 public class SDraw  {
36 
37 
main(String args[])38     public static void main(String args[]) {
39 
40         //oooooooooooooooooooooooooooStep 1oooooooooooooooooooooooooooooooooooooooo
41         // bootstrap UNO and get the remote component context. The context can
42         // be used to get the service manager
43         //*************************************************************************
44         com.sun.star.uno.XComponentContext xContext = null;
45 
46         try {
47             // get the remote office component context
48             xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
49             System.out.println("Connected to a running office ...");
50         }
51         catch( Exception e) {
52             e.printStackTrace(System.err);
53             System.exit(1);
54         }
55 
56         com.sun.star.lang.XComponent xDrawDoc = null;
57         com.sun.star.drawing.XDrawPage xDrawPage = null;
58 
59         //oooooooooooooooooooooooooooStep 2oooooooooooooooooooooooooooooooooooooooo
60         // open an empty document. In this case it's a draw document.
61         // For this purpose an instance of com.sun.star.frame.Desktop
62         // is created. It's interface XDesktop provides the XComponentLoader,
63         // which is used to open the document via loadComponentFromURL
64         //*************************************************************************
65 
66         //Open document
67         //Draw
68         System.out.println("Opening an empty Draw document ...");
69         xDrawDoc = openDraw(xContext);
70 
71         //oooooooooooooooooooooooooooStep 3oooooooooooooooooooooooooooooooooooooooo
72         // get the drawpage an insert some shapes.
73         // the documents DrawPageSupplier supplies the DrawPage vi IndexAccess
74         // To add a shape get the MultiServiceFaktory of the document, create an
75         // instance of the ShapeType and add it to the Shapes-container
76         // provided by the drawpage
77         //*************************************************************************
78 
79 
80         // get the drawpage of drawing here
81         try {
82             System.out.println( "getting Drawpage" );
83             com.sun.star.drawing.XDrawPagesSupplier xDPS =
84                 (com.sun.star.drawing.XDrawPagesSupplier)UnoRuntime.queryInterface(
85                     com.sun.star.drawing.XDrawPagesSupplier.class, xDrawDoc);
86             com.sun.star.drawing.XDrawPages xDPn = xDPS.getDrawPages();
87             com.sun.star.container.XIndexAccess xDPi =
88                 (com.sun.star.container.XIndexAccess)UnoRuntime.queryInterface(
89                     com.sun.star.container.XIndexAccess.class, xDPn);
90             xDrawPage = (com.sun.star.drawing.XDrawPage)UnoRuntime.queryInterface(
91                 com.sun.star.drawing.XDrawPage.class, xDPi.getByIndex(0));
92         } catch ( Exception e ) {
93             System.err.println( "Couldn't create document"+ e );
94             e.printStackTrace(System.err);
95         }
96 
97         createSequence(xDrawDoc, xDrawPage);
98 
99         //put something on the drawpage
100         System.out.println( "inserting some Shapes" );
101         com.sun.star.drawing.XShapes xShapes = (com.sun.star.drawing.XShapes)
102             UnoRuntime.queryInterface(
103                 com.sun.star.drawing.XShapes.class, xDrawPage);
104         xShapes.add(createShape(xDrawDoc,2000,1500,1000,1000,"Line",0));
105         xShapes.add(createShape(xDrawDoc,3000,4500,15000,1000,"Ellipse",16711680));
106         xShapes.add(createShape(xDrawDoc,5000,3500,7500,5000,"Rectangle",6710932));
107 
108         //*************************************************************************
109         System.out.println("done");
110         System.exit(0);
111     }
112 
openDraw( com.sun.star.uno.XComponentContext xContext)113     public static com.sun.star.lang.XComponent openDraw(
114         com.sun.star.uno.XComponentContext xContext)
115     {
116         com.sun.star.frame.XComponentLoader xCLoader;
117         com.sun.star.text.XTextDocument xDoc = null;
118         com.sun.star.lang.XComponent xComp = null;
119 
120         try {
121             // get the remote office service manager
122             com.sun.star.lang.XMultiComponentFactory xMCF =
123                 xContext.getServiceManager();
124 
125             Object oDesktop = xMCF.createInstanceWithContext(
126                                         "com.sun.star.frame.Desktop", xContext);
127 
128             xCLoader = (com.sun.star.frame.XComponentLoader)
129                 UnoRuntime.queryInterface(com.sun.star.frame.XComponentLoader.class,
130                                           oDesktop);
131             com.sun.star.beans.PropertyValue szEmptyArgs[] =
132                 new com.sun.star.beans.PropertyValue[0];
133             String strDoc = "private:factory/sdraw";
134             xComp = xCLoader.loadComponentFromURL(strDoc, "_blank", 0, szEmptyArgs);
135 
136         } catch(Exception e){
137             System.err.println(" Exception " + e);
138             e.printStackTrace(System.err);
139         }
140 
141         return xComp;
142     }
143 
createShape( com.sun.star.lang.XComponent xDocComp, int height, int width, int x, int y, String kind, int col)144     public static com.sun.star.drawing.XShape createShape(
145         com.sun.star.lang.XComponent xDocComp, int height, int width, int x,
146         int y, String kind, int col)
147     {
148         //possible values for kind are 'Ellipse', 'Line' and 'Rectangle'
149         com.sun.star.awt.Size size = new com.sun.star.awt.Size();
150         com.sun.star.awt.Point position = new com.sun.star.awt.Point();
151         com.sun.star.drawing.XShape xShape = null;
152 
153         //get MSF
154         com.sun.star.lang.XMultiServiceFactory xDocMSF =
155             (com.sun.star.lang.XMultiServiceFactory) UnoRuntime.queryInterface(
156                 com.sun.star.lang.XMultiServiceFactory.class, xDocComp );
157 
158         try {
159             Object oInt = xDocMSF.createInstance("com.sun.star.drawing."
160                                                  +kind + "Shape");
161             xShape = (com.sun.star.drawing.XShape)UnoRuntime.queryInterface(
162                 com.sun.star.drawing.XShape.class, oInt);
163             size.Height = height;
164             size.Width = width;
165             position.X = x;
166             position.Y = y;
167             xShape.setSize(size);
168             xShape.setPosition(position);
169 
170         } catch ( Exception e ) {
171             System.err.println( "Couldn't create instance "+ e );
172             e.printStackTrace(System.err);
173         }
174 
175         com.sun.star.beans.XPropertySet xSPS = (com.sun.star.beans.XPropertySet)
176             UnoRuntime.queryInterface(
177                 com.sun.star.beans.XPropertySet.class, xShape);
178 
179         try {
180             xSPS.setPropertyValue("FillColor", new Integer(col));
181         } catch (Exception e) {
182             System.err.println("Can't change colors " + e);
183             e.printStackTrace(System.err);
184         }
185 
186         return xShape;
187     }
188 
createSequence( com.sun.star.lang.XComponent xDocComp, com.sun.star.drawing.XDrawPage xDP)189     public static com.sun.star.drawing.XShape createSequence(
190         com.sun.star.lang.XComponent xDocComp, com.sun.star.drawing.XDrawPage xDP)
191     {
192         com.sun.star.awt.Size size = new com.sun.star.awt.Size();
193         com.sun.star.awt.Point position = new com.sun.star.awt.Point();
194         com.sun.star.drawing.XShape xShape = null;
195         com.sun.star.drawing.XShapes xShapes = (com.sun.star.drawing.XShapes)
196             UnoRuntime.queryInterface(com.sun.star.drawing.XShapes.class, xDP);
197         int height = 3000;
198         int width = 3500;
199         int x = 1900;
200         int y = 20000;
201         Object oInt = null;
202         int r = 40;
203         int g = 0;
204         int b = 80;
205 
206         //get MSF
207         com.sun.star.lang.XMultiServiceFactory xDocMSF =
208             (com.sun.star.lang.XMultiServiceFactory)UnoRuntime.queryInterface(
209                 com.sun.star.lang.XMultiServiceFactory.class, xDocComp );
210 
211         for (int i=0; i<370; i=i+25) {
212             try{
213                 oInt = xDocMSF.createInstance("com.sun.star.drawing.EllipseShape");
214                 xShape = (com.sun.star.drawing.XShape)UnoRuntime.queryInterface(
215                     com.sun.star.drawing.XShape.class, oInt);
216                 size.Height = height;
217                 size.Width = width;
218                 position.X = (x+(i*40));
219                 position.Y =
220                     (new Float(y+(Math.sin((i*Math.PI)/180))*5000)).intValue();
221                 xShape.setSize(size);
222                 xShape.setPosition(position);
223 
224             } catch ( Exception e ) {
225                 // Some exception occures.FAILED
226                 System.err.println( "Couldn't get Shape "+ e );
227                 e.printStackTrace(System.err);
228             }
229 
230             b=b+8;
231 
232             com.sun.star.beans.XPropertySet xSPS = (com.sun.star.beans.XPropertySet)
233                 UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class,
234                                           xShape);
235 
236             try {
237                 xSPS.setPropertyValue("FillColor", new Integer(getCol(r,g,b)));
238                 xSPS.setPropertyValue("Shadow", new Boolean(true));
239             } catch (Exception e) {
240                 System.err.println("Can't change colors " + e);
241                 e.printStackTrace(System.err);
242             }
243             xShapes.add(xShape);
244         }
245 
246         com.sun.star.drawing.XShapeGrouper xSGrouper =
247             (com.sun.star.drawing.XShapeGrouper)UnoRuntime.queryInterface(
248                 com.sun.star.drawing.XShapeGrouper.class, xDP);
249 
250         xShape = xSGrouper.group(xShapes);
251 
252         return xShape;
253     }
254 
getCol(int r, int g, int b)255     public static int getCol(int r, int g, int b) {
256         return r*65536+g*256+b;
257     }
258 }
259 
260 
261 
262