1 /*************************************************************************
3 * $RCSfile: SDraw.java,v $
7 * last change: $Author: rt $ $Date: 2005-01-31 17:10:06 $
9 * The Contents of this file are made available subject to the terms of
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 //***************************************************************************
42 // comment: Step 1: bootstrap UNO and get the remote component context
43 // Step 2: open an empty text document
44 // Step 3: get the drawpage an insert some shapes
45 //***************************************************************************
48 import java
.lang
.Math
;
50 import com
.sun
.star
.uno
.UnoRuntime
;
55 public static void main(String args
[]) {
57 //oooooooooooooooooooooooooooStep 1oooooooooooooooooooooooooooooooooooooooo
58 // bootstrap UNO and get the remote component context. The context can
59 // be used to get the service manager
60 //*************************************************************************
61 com
.sun
.star
.uno
.XComponentContext xContext
= null;
64 // get the remote office component context
65 xContext
= com
.sun
.star
.comp
.helper
.Bootstrap
.bootstrap();
66 System
.out
.println("Connected to a running office ...");
69 e
.printStackTrace(System
.err
);
73 com
.sun
.star
.lang
.XComponent xDrawDoc
= null;
74 com
.sun
.star
.drawing
.XDrawPage xDrawPage
= null;
76 //oooooooooooooooooooooooooooStep 2oooooooooooooooooooooooooooooooooooooooo
77 // open an empty document. In this case it's a draw document.
78 // For this purpose an instance of com.sun.star.frame.Desktop
79 // is created. It's interface XDesktop provides the XComponentLoader,
80 // which is used to open the document via loadComponentFromURL
81 //*************************************************************************
85 System
.out
.println("Opening an empty Draw document ...");
86 xDrawDoc
= openDraw(xContext
);
88 //oooooooooooooooooooooooooooStep 3oooooooooooooooooooooooooooooooooooooooo
89 // get the drawpage an insert some shapes.
90 // the documents DrawPageSupplier supplies the DrawPage vi IndexAccess
91 // To add a shape get the MultiServiceFaktory of the document, create an
92 // instance of the ShapeType and add it to the Shapes-container
93 // provided by the drawpage
94 //*************************************************************************
97 // get the drawpage of drawing here
99 System
.out
.println( "getting Drawpage" );
100 com
.sun
.star
.drawing
.XDrawPagesSupplier xDPS
=
101 (com
.sun
.star
.drawing
.XDrawPagesSupplier
)UnoRuntime
.queryInterface(
102 com
.sun
.star
.drawing
.XDrawPagesSupplier
.class, xDrawDoc
);
103 com
.sun
.star
.drawing
.XDrawPages xDPn
= xDPS
.getDrawPages();
104 com
.sun
.star
.container
.XIndexAccess xDPi
=
105 (com
.sun
.star
.container
.XIndexAccess
)UnoRuntime
.queryInterface(
106 com
.sun
.star
.container
.XIndexAccess
.class, xDPn
);
107 xDrawPage
= (com
.sun
.star
.drawing
.XDrawPage
)UnoRuntime
.queryInterface(
108 com
.sun
.star
.drawing
.XDrawPage
.class, xDPi
.getByIndex(0));
109 } catch ( Exception e
) {
110 System
.err
.println( "Couldn't create document"+ e
);
111 e
.printStackTrace(System
.err
);
114 createSequence(xDrawDoc
, xDrawPage
);
116 //put something on the drawpage
117 System
.out
.println( "inserting some Shapes" );
118 com
.sun
.star
.drawing
.XShapes xShapes
= (com
.sun
.star
.drawing
.XShapes
)
119 UnoRuntime
.queryInterface(
120 com
.sun
.star
.drawing
.XShapes
.class, xDrawPage
);
121 xShapes
.add(createShape(xDrawDoc
,2000,1500,1000,1000,"Line",0));
122 xShapes
.add(createShape(xDrawDoc
,3000,4500,15000,1000,"Ellipse",16711680));
123 xShapes
.add(createShape(xDrawDoc
,5000,3500,7500,5000,"Rectangle",6710932));
125 //*************************************************************************
126 System
.out
.println("done");
130 public static com
.sun
.star
.lang
.XComponent
openDraw(
131 com
.sun
.star
.uno
.XComponentContext xContext
)
133 com
.sun
.star
.frame
.XComponentLoader xCLoader
;
134 com
.sun
.star
.text
.XTextDocument xDoc
= null;
135 com
.sun
.star
.lang
.XComponent xComp
= null;
138 // get the remote office service manager
139 com
.sun
.star
.lang
.XMultiComponentFactory xMCF
=
140 xContext
.getServiceManager();
142 Object oDesktop
= xMCF
.createInstanceWithContext(
143 "com.sun.star.frame.Desktop", xContext
);
145 xCLoader
= (com
.sun
.star
.frame
.XComponentLoader
)
146 UnoRuntime
.queryInterface(com
.sun
.star
.frame
.XComponentLoader
.class,
148 com
.sun
.star
.beans
.PropertyValue szEmptyArgs
[] =
149 new com
.sun
.star
.beans
.PropertyValue
[0];
150 String strDoc
= "private:factory/sdraw";
151 xComp
= xCLoader
.loadComponentFromURL(strDoc
, "_blank", 0, szEmptyArgs
);
153 } catch(Exception e
){
154 System
.err
.println(" Exception " + e
);
155 e
.printStackTrace(System
.err
);
161 public static com
.sun
.star
.drawing
.XShape
createShape(
162 com
.sun
.star
.lang
.XComponent xDocComp
, int height
, int width
, int x
,
163 int y
, String kind
, int col
)
165 //possible values for kind are 'Ellipse', 'Line' and 'Rectangle'
166 com
.sun
.star
.awt
.Size size
= new com
.sun
.star
.awt
.Size();
167 com
.sun
.star
.awt
.Point position
= new com
.sun
.star
.awt
.Point();
168 com
.sun
.star
.drawing
.XShape xShape
= null;
171 com
.sun
.star
.lang
.XMultiServiceFactory xDocMSF
=
172 (com
.sun
.star
.lang
.XMultiServiceFactory
) UnoRuntime
.queryInterface(
173 com
.sun
.star
.lang
.XMultiServiceFactory
.class, xDocComp
);
176 Object oInt
= xDocMSF
.createInstance("com.sun.star.drawing."
178 xShape
= (com
.sun
.star
.drawing
.XShape
)UnoRuntime
.queryInterface(
179 com
.sun
.star
.drawing
.XShape
.class, oInt
);
180 size
.Height
= height
;
184 xShape
.setSize(size
);
185 xShape
.setPosition(position
);
187 } catch ( Exception e
) {
188 System
.err
.println( "Couldn't create instance "+ e
);
189 e
.printStackTrace(System
.err
);
192 com
.sun
.star
.beans
.XPropertySet xSPS
= (com
.sun
.star
.beans
.XPropertySet
)
193 UnoRuntime
.queryInterface(
194 com
.sun
.star
.beans
.XPropertySet
.class, xShape
);
197 xSPS
.setPropertyValue("FillColor", new Integer(col
));
198 } catch (Exception e
) {
199 System
.err
.println("Can't change colors " + e
);
200 e
.printStackTrace(System
.err
);
206 public static com
.sun
.star
.drawing
.XShape
createSequence(
207 com
.sun
.star
.lang
.XComponent xDocComp
, com
.sun
.star
.drawing
.XDrawPage xDP
)
209 com
.sun
.star
.awt
.Size size
= new com
.sun
.star
.awt
.Size();
210 com
.sun
.star
.awt
.Point position
= new com
.sun
.star
.awt
.Point();
211 com
.sun
.star
.drawing
.XShape xShape
= null;
212 com
.sun
.star
.drawing
.XShapes xShapes
= (com
.sun
.star
.drawing
.XShapes
)
213 UnoRuntime
.queryInterface(com
.sun
.star
.drawing
.XShapes
.class, xDP
);
224 com
.sun
.star
.lang
.XMultiServiceFactory xDocMSF
=
225 (com
.sun
.star
.lang
.XMultiServiceFactory
)UnoRuntime
.queryInterface(
226 com
.sun
.star
.lang
.XMultiServiceFactory
.class, xDocComp
);
228 for (int i
=0; i
<370; i
=i
+25) {
230 oInt
= xDocMSF
.createInstance("com.sun.star.drawing.EllipseShape");
231 xShape
= (com
.sun
.star
.drawing
.XShape
)UnoRuntime
.queryInterface(
232 com
.sun
.star
.drawing
.XShape
.class, oInt
);
233 size
.Height
= height
;
235 position
.X
= (x
+(i
*40));
237 (new Float(y
+(Math
.sin((i
*Math
.PI
)/180))*5000)).intValue();
238 xShape
.setSize(size
);
239 xShape
.setPosition(position
);
241 } catch ( Exception e
) {
242 // Some exception occures.FAILED
243 System
.err
.println( "Couldn't get Shape "+ e
);
244 e
.printStackTrace(System
.err
);
249 com
.sun
.star
.beans
.XPropertySet xSPS
= (com
.sun
.star
.beans
.XPropertySet
)
250 UnoRuntime
.queryInterface(com
.sun
.star
.beans
.XPropertySet
.class,
254 xSPS
.setPropertyValue("FillColor", new Integer(getCol(r
,g
,b
)));
255 xSPS
.setPropertyValue("Shadow", new Boolean(true));
256 } catch (Exception e
) {
257 System
.err
.println("Can't change colors " + e
);
258 e
.printStackTrace(System
.err
);
263 com
.sun
.star
.drawing
.XShapeGrouper xSGrouper
=
264 (com
.sun
.star
.drawing
.XShapeGrouper
)UnoRuntime
.queryInterface(
265 com
.sun
.star
.drawing
.XShapeGrouper
.class, xDP
);
267 xShape
= xSGrouper
.group(xShapes
);
272 public static int getCol(int r
, int g
, int b
) {
273 return r
*65536+g
*256+b
;