1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
//ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
// Name : SDraw
//***************************************************************************
//
// comment: Step 1: connect to the office an get the MSF
// Step 2: open an empty text document
// Step 3: get the drawpage an insert some shapes
//
//***************************************************************************
// date : Tue August 22 2000
//
//***************************************************************************
import java.lang.Math;
// base interface
import com.sun.star.uno.XInterface;
// access the implementations via names
import com.sun.star.comp.servicemanager.ServiceManager;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.connection.XConnector;
import com.sun.star.connection.XConnection;
import com.sun.star.bridge.XUnoUrlResolver;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XInterface;
import com.sun.star.uno.XNamingService;
import com.sun.star.uno.XComponentContext;
// staroffice interfaces to provide desktop and componentloader
// and components i.e. spreadsheets, writerdocs etc.
import com.sun.star.frame.XDesktop;
import com.sun.star.frame.XComponentLoader;
// additional classes required
import com.sun.star.sheet.*;
import com.sun.star.container.*;
import com.sun.star.table.*;
import com.sun.star.beans.*;
import com.sun.star.style.*;
import com.sun.star.lang.*;
import com.sun.star.drawing.*;
import com.sun.star.text.XText;
import com.sun.star.awt.*;
public class SDraw {
public static void main(String args[]) {
//oooooooooooooooooooooooooooStep 1oooooooooooooooooooooooooooooooooooooooooo
// connect to the office an get the MultiServiceFactory
// this is necessary to create instances of Services
//***************************************************************************
String sConnectionString = "uno:socket,host=localhost,port=8100;urp;StarOffice.NamingService";
// It is possible to use a different connection string, passed as argument
if ( args.length == 1 ) {
sConnectionString = args[0];
}
XMultiServiceFactory xMSF = null;
XComponent xDrawDoc = null;
XDrawPage oObj = null;
XShape oShape = null;
XShape oShape1 = null;
XShapes oShapes = null;
// create connection(s) and get multiservicefactory
// create connection(s) and get multiservicefactory
System.out.println( "getting MultiServiceFactory" );
try {
xMSF = connect( sConnectionString );
} catch( Exception Ex ) {
System.out.println( "Couldn't get MSF"+ Ex );
}
//***************************************************************************
//oooooooooooooooooooooooooooStep 2oooooooooooooooooooooooooooooooooooooooooo
// open an empty document. In this case it's a draw document.
// For this purpose an instance of com.sun.star.frame.Desktop
// is created. It's interface XDesktop provides the XComponentLoader,
// which is used to open the document via loadComponentFromURL
//***************************************************************************
//Open document
//Draw
System.out.println("Opening an empty Draw document");
xDrawDoc = openDraw(xMSF);
//***************************************************************************
//oooooooooooooooooooooooooooStep 3oooooooooooooooooooooooooooooooooooooooooo
// get the drawpage an insert some shapes.
// the documents DrawPageSupplier supplies the DrawPage vi IndexAccess
// To add a shape get the MultiServiceFaktory of the document, create an
// instance of the ShapeType and add it to the Shapes-container
// provided by the drawpage
//***************************************************************************
// get the drawpage of drawing here
try {
System.out.println( "getting Drawpage" );
XDrawPagesSupplier oDPS = (XDrawPagesSupplier) UnoRuntime.queryInterface(
XDrawPagesSupplier.class,xDrawDoc);
XDrawPages oDPn = oDPS.getDrawPages();
XIndexAccess oDPi = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class,oDPn);
oObj = (XDrawPage) UnoRuntime.queryInterface(XDrawPage.class, oDPi.getByIndex(0));
} catch ( Exception e ) {
System.out.println( "Couldn't create document"+ e );
}
createSequence(xDrawDoc, oObj);
//put something on the drawpage
System.out.println( "inserting some Shapes" );
oShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class,oObj);
oShapes.add(createShape(xDrawDoc,2000,1500,1000,1000,"Line",0));
oShapes.add(createShape(xDrawDoc,3000,4500,15000,1000,"Ellipse",16711680));
oShapes.add(createShape(xDrawDoc,5000,3500,7500,5000,"Rectangle",6710932));
//***************************************************************************
System.out.println("done");
System.exit(0);
} // finish method main
public static XMultiServiceFactory connect( String connectStr )
throws com.sun.star.uno.Exception,
com.sun.star.uno.RuntimeException, Exception {
/* Bootstraps a component context with the jurt base components
registered. Component context to be granted to a component for running.
Arbitrary values can be retrieved from the context. */
XComponentContext xcomponentcontext =
com.sun.star.comp.helper.Bootstrap.createInitialComponentContext( null );
/* Gets the service manager instance to be used (or null). This method has
been added for convenience, because the service manager is a often used
object. */
XMultiComponentFactory xmulticomponentfactory =
xcomponentcontext.getServiceManager();
/* Creates an instance of the component UnoUrlResolver which
supports the services specified by the factory. */
Object objectUrlResolver =
xmulticomponentfactory.createInstanceWithContext(
"com.sun.star.bridge.UnoUrlResolver", xcomponentcontext );
// Create a new url resolver
XUnoUrlResolver xurlresolver = ( XUnoUrlResolver )
UnoRuntime.queryInterface( XUnoUrlResolver.class,
objectUrlResolver );
Object rInitialObject = xurlresolver.resolve( connectStr );
XNamingService rName = (XNamingService)UnoRuntime.queryInterface(
XNamingService.class, rInitialObject );
XMultiServiceFactory xMSF = null;
if( rName != null ) {
System.err.println( "got the remote naming service !" );
Object rXsmgr = rName.getRegisteredObject("StarOffice.ServiceManager" );
xMSF = (XMultiServiceFactory)
UnoRuntime.queryInterface( XMultiServiceFactory.class, rXsmgr );
}
return ( xMSF );
}
public static XComponent openDraw(XMultiServiceFactory oMSF) {
//define variables
XInterface oInterface;
XDesktop oDesktop;
XComponentLoader oCLoader;
XComponent aDoc = null;
try {
oInterface = (XInterface) oMSF.createInstance( "com.sun.star.frame.Desktop" );
oDesktop = ( XDesktop ) UnoRuntime.queryInterface( XDesktop.class, oInterface );
oCLoader = ( XComponentLoader ) UnoRuntime.queryInterface( XComponentLoader.class, oDesktop );
PropertyValue [] szEmptyArgs = new PropertyValue [0];
String doc = "private:factory/sdraw";
aDoc = oCLoader.loadComponentFromURL(doc, "_blank", 0, szEmptyArgs );
} // end of try
catch(Exception e){
System.out.println(" Exception " + e);
} // end of catch
return aDoc;
}//end of openDraw
public static XShape createShape(XComponent oDoc, int height, int width, int x, int y, String kind, int col) {
//possible values for kind are 'Ellipse', 'Line' and 'Rectangle'
Size size = new Size();
Point position = new Point();
XShape oShape = null;
//get MSF
XMultiServiceFactory oDocMSF = (XMultiServiceFactory) UnoRuntime.queryInterface( XMultiServiceFactory.class, oDoc );
try{
Object oInt = oDocMSF.createInstance("com.sun.star.drawing."+kind+"Shape");
oShape = (XShape)UnoRuntime.queryInterface( XShape.class, oInt );
size.Height = height;
size.Width = width;
position.X = x;
position.Y = y;
oShape.setSize(size);
oShape.setPosition(position);
} catch ( Exception e ) {
// Some exception occures.FAILED
System.out.println( "Couldn't create instance "+ e );
}
XPropertySet oSPS = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class, oShape);
try {
oSPS.setPropertyValue("FillColor",new Integer(col));
} catch (Exception e) {
System.out.println("Can't change colors " + e);
}
return oShape;
}
public static XShape createSequence(XComponent oDoc, XDrawPage oDP) {
Size size = new Size();
Point position = new Point();
XShape oShape = null;
XShapes oShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class,oDP);
int height = 3000;
int width = 3500;
int x = 1900;
int y = 20000;
Object oInt = null;
int r = 40;
int g = 0;
int b = 80;
//get MSF
XMultiServiceFactory oDocMSF = (XMultiServiceFactory) UnoRuntime.queryInterface(
XMultiServiceFactory.class, oDoc );
for (int i=0; i<370; i=i+25) {
try{
oInt = oDocMSF.createInstance("com.sun.star.drawing.EllipseShape");
oShape = (XShape)UnoRuntime.queryInterface( XShape.class, oInt );
size.Height = height;
size.Width = width;
position.X = (x+(i*40));
position.Y = (new Float(y+(Math.sin((i*Math.PI)/180))*5000)).intValue();
oShape.setSize(size);
oShape.setPosition(position);
} catch ( Exception e ) {
// Some exception occures.FAILED
System.out.println( "Couldn't get Shape "+ e );
}
b=b+8;
XPropertySet oSPS = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class, oShape);
try {
oSPS.setPropertyValue("FillColor",new Integer(getCol(r,g,b)));
oSPS.setPropertyValue("Shadow",new Boolean(true));
} catch (Exception e) {
System.out.println("Can't change colors " + e);
}
oShapes.add(oShape);
}
XShapeGrouper gr = (XShapeGrouper)UnoRuntime.queryInterface( XShapeGrouper.class, oDP );
oShape = gr.group(oShapes);
return oShape;
}
public static int getCol(int r, int g, int b) {
return r*65536+g*256+b;
}
} // finish class SDraw
|