1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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 *************************************************************************/
35 //***************************************************************************
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
39 //***************************************************************************
42 import java
.lang
.Math
;
44 import com
.sun
.star
.uno
.UnoRuntime
;
49 public static void main(String args
[]) {
51 //oooooooooooooooooooooooooooStep 1oooooooooooooooooooooooooooooooooooooooo
52 // bootstrap UNO and get the remote component context. The context can
53 // be used to get the service manager
54 //*************************************************************************
55 com
.sun
.star
.uno
.XComponentContext xContext
= null;
58 // get the remote office component context
59 xContext
= com
.sun
.star
.comp
.helper
.Bootstrap
.bootstrap();
60 System
.out
.println("Connected to a running office ...");
63 e
.printStackTrace(System
.err
);
67 com
.sun
.star
.lang
.XComponent xDrawDoc
= null;
68 com
.sun
.star
.drawing
.XDrawPage xDrawPage
= null;
70 //oooooooooooooooooooooooooooStep 2oooooooooooooooooooooooooooooooooooooooo
71 // open an empty document. In this case it's a draw document.
72 // For this purpose an instance of com.sun.star.frame.Desktop
73 // is created. It's interface XDesktop provides the XComponentLoader,
74 // which is used to open the document via loadComponentFromURL
75 //*************************************************************************
79 System
.out
.println("Opening an empty Draw document ...");
80 xDrawDoc
= openDraw(xContext
);
82 //oooooooooooooooooooooooooooStep 3oooooooooooooooooooooooooooooooooooooooo
83 // get the drawpage an insert some shapes.
84 // the documents DrawPageSupplier supplies the DrawPage vi IndexAccess
85 // To add a shape get the MultiServiceFaktory of the document, create an
86 // instance of the ShapeType and add it to the Shapes-container
87 // provided by the drawpage
88 //*************************************************************************
91 // get the drawpage of drawing here
93 System
.out
.println( "getting Drawpage" );
94 com
.sun
.star
.drawing
.XDrawPagesSupplier xDPS
=
95 UnoRuntime
.queryInterface(
96 com
.sun
.star
.drawing
.XDrawPagesSupplier
.class, xDrawDoc
);
97 com
.sun
.star
.drawing
.XDrawPages xDPn
= xDPS
.getDrawPages();
98 com
.sun
.star
.container
.XIndexAccess xDPi
=
99 UnoRuntime
.queryInterface(
100 com
.sun
.star
.container
.XIndexAccess
.class, xDPn
);
101 xDrawPage
= UnoRuntime
.queryInterface(
102 com
.sun
.star
.drawing
.XDrawPage
.class, xDPi
.getByIndex(0));
103 } catch ( Exception e
) {
104 System
.err
.println( "Couldn't create document"+ e
);
105 e
.printStackTrace(System
.err
);
108 createSequence(xDrawDoc
, xDrawPage
);
110 //put something on the drawpage
111 System
.out
.println( "inserting some Shapes" );
112 com
.sun
.star
.drawing
.XShapes xShapes
= UnoRuntime
.queryInterface(
113 com
.sun
.star
.drawing
.XShapes
.class, xDrawPage
);
114 xShapes
.add(createShape(xDrawDoc
,2000,1500,1000,1000,"Line",0));
115 xShapes
.add(createShape(xDrawDoc
,3000,4500,15000,1000,"Ellipse",16711680));
116 xShapes
.add(createShape(xDrawDoc
,5000,3500,7500,5000,"Rectangle",6710932));
118 //*************************************************************************
119 System
.out
.println("done");
123 public static com
.sun
.star
.lang
.XComponent
openDraw(
124 com
.sun
.star
.uno
.XComponentContext xContext
)
126 com
.sun
.star
.frame
.XComponentLoader xCLoader
;
127 com
.sun
.star
.text
.XTextDocument xDoc
= null;
128 com
.sun
.star
.lang
.XComponent xComp
= null;
131 // get the remote office service manager
132 com
.sun
.star
.lang
.XMultiComponentFactory xMCF
=
133 xContext
.getServiceManager();
135 Object oDesktop
= xMCF
.createInstanceWithContext(
136 "com.sun.star.frame.Desktop", xContext
);
138 xCLoader
= UnoRuntime
.queryInterface(com
.sun
.star
.frame
.XComponentLoader
.class,
140 com
.sun
.star
.beans
.PropertyValue szEmptyArgs
[] =
141 new com
.sun
.star
.beans
.PropertyValue
[0];
142 String strDoc
= "private:factory/sdraw";
143 xComp
= xCLoader
.loadComponentFromURL(strDoc
, "_blank", 0, szEmptyArgs
);
145 } catch(Exception e
){
146 System
.err
.println(" Exception " + e
);
147 e
.printStackTrace(System
.err
);
153 public static com
.sun
.star
.drawing
.XShape
createShape(
154 com
.sun
.star
.lang
.XComponent xDocComp
, int height
, int width
, int x
,
155 int y
, String kind
, int col
)
157 //possible values for kind are 'Ellipse', 'Line' and 'Rectangle'
158 com
.sun
.star
.awt
.Size size
= new com
.sun
.star
.awt
.Size();
159 com
.sun
.star
.awt
.Point position
= new com
.sun
.star
.awt
.Point();
160 com
.sun
.star
.drawing
.XShape xShape
= null;
163 com
.sun
.star
.lang
.XMultiServiceFactory xDocMSF
=
164 UnoRuntime
.queryInterface(
165 com
.sun
.star
.lang
.XMultiServiceFactory
.class, xDocComp
);
168 Object oInt
= xDocMSF
.createInstance("com.sun.star.drawing."
170 xShape
= UnoRuntime
.queryInterface(
171 com
.sun
.star
.drawing
.XShape
.class, oInt
);
172 size
.Height
= height
;
176 xShape
.setSize(size
);
177 xShape
.setPosition(position
);
179 } catch ( Exception e
) {
180 System
.err
.println( "Couldn't create instance "+ e
);
181 e
.printStackTrace(System
.err
);
184 com
.sun
.star
.beans
.XPropertySet xSPS
= UnoRuntime
.queryInterface(
185 com
.sun
.star
.beans
.XPropertySet
.class, xShape
);
188 xSPS
.setPropertyValue("FillColor", new Integer(col
));
189 } catch (Exception e
) {
190 System
.err
.println("Can't change colors " + e
);
191 e
.printStackTrace(System
.err
);
197 public static com
.sun
.star
.drawing
.XShape
createSequence(
198 com
.sun
.star
.lang
.XComponent xDocComp
, com
.sun
.star
.drawing
.XDrawPage xDP
)
200 com
.sun
.star
.awt
.Size size
= new com
.sun
.star
.awt
.Size();
201 com
.sun
.star
.awt
.Point position
= new com
.sun
.star
.awt
.Point();
202 com
.sun
.star
.drawing
.XShape xShape
= null;
203 com
.sun
.star
.drawing
.XShapes xShapes
= UnoRuntime
.queryInterface(com
.sun
.star
.drawing
.XShapes
.class, xDP
);
214 com
.sun
.star
.lang
.XMultiServiceFactory xDocMSF
=
215 UnoRuntime
.queryInterface(
216 com
.sun
.star
.lang
.XMultiServiceFactory
.class, xDocComp
);
218 for (int i
=0; i
<370; i
=i
+25) {
220 oInt
= xDocMSF
.createInstance("com.sun.star.drawing.EllipseShape");
221 xShape
= UnoRuntime
.queryInterface(
222 com
.sun
.star
.drawing
.XShape
.class, oInt
);
223 size
.Height
= height
;
225 position
.X
= (x
+(i
*40));
227 (new Float(y
+(Math
.sin((i
*Math
.PI
)/180))*5000)).intValue();
228 xShape
.setSize(size
);
229 xShape
.setPosition(position
);
231 } catch ( Exception e
) {
232 // Some exception occures.FAILED
233 System
.err
.println( "Couldn't get Shape "+ e
);
234 e
.printStackTrace(System
.err
);
239 com
.sun
.star
.beans
.XPropertySet xSPS
= UnoRuntime
.queryInterface(com
.sun
.star
.beans
.XPropertySet
.class,
243 xSPS
.setPropertyValue("FillColor", new Integer(getCol(r
,g
,b
)));
244 xSPS
.setPropertyValue("Shadow", new Boolean(true));
245 } catch (Exception e
) {
246 System
.err
.println("Can't change colors " + e
);
247 e
.printStackTrace(System
.err
);
252 com
.sun
.star
.drawing
.XShapeGrouper xSGrouper
=
253 UnoRuntime
.queryInterface(
254 com
.sun
.star
.drawing
.XShapeGrouper
.class, xDP
);
256 xShape
= xSGrouper
.group(xShapes
);
261 public static int getCol(int r
, int g
, int b
) {
262 return r
*65536+g
*256+b
;