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 *************************************************************************/
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
;
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;
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 ...");
61 e
.printStackTrace(System
.err
);
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
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
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");
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;
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,
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
);
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;
160 com
.sun
.star
.lang
.XMultiServiceFactory xDocMSF
=
161 UnoRuntime
.queryInterface(
162 com
.sun
.star
.lang
.XMultiServiceFactory
.class, xDocComp
);
165 Object oInt
= xDocMSF
.createInstance("com.sun.star.drawing."
167 xShape
= UnoRuntime
.queryInterface(
168 com
.sun
.star
.drawing
.XShape
.class, oInt
);
169 size
.Height
= height
;
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
);
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
);
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
);
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) {
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
;
222 position
.X
= (x
+(i
*40));
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
);
236 com
.sun
.star
.beans
.XPropertySet xSPS
= UnoRuntime
.queryInterface(com
.sun
.star
.beans
.XPropertySet
.class,
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
);
249 com
.sun
.star
.drawing
.XShapeGrouper xSGrouper
=
250 UnoRuntime
.queryInterface(
251 com
.sun
.star
.drawing
.XShapeGrouper
.class, xDP
);
253 xShape
= xSGrouper
.group(xShapes
);
258 public static int getCol(int r
, int g
, int b
) {
259 return r
*65536+g
*256+b
;