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