1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package util;
24 
25 import java.util.Hashtable;
26 // access the implementations via names
27 import com.sun.star.uno.XInterface;
28 import com.sun.star.lang.XMultiServiceFactory;
29 
30 import com.sun.star.uno.UnoRuntime;
31 // staroffice interfaces to provide desktop and componentloader
32 // and components i.e. spreadsheets, writerdocs etc.
33 import com.sun.star.frame.XDesktop;
34 import com.sun.star.frame.XComponentLoader;
35 import com.sun.star.lang.XComponent;
36 import com.sun.star.lang.XServiceInfo;
37 
38 // name - value pair
39 import com.sun.star.beans.PropertyValue;
40 import com.sun.star.beans.PropertyState;
41 
42 // additional classes required for testcase
43 import com.sun.star.sheet.*;
44 import com.sun.star.text.*;
45 import com.sun.star.container.*;
46 import com.sun.star.chart.*;
47 import com.sun.star.drawing.*;
48 import com.sun.star.awt.*;
49 
50 public class SOfficeFactory {
51 
52     private static Hashtable lookup = new Hashtable(10);
53     protected XComponentLoader oCLoader;
54 
SOfficeFactory(XMultiServiceFactory xMSF)55     private SOfficeFactory(XMultiServiceFactory xMSF) {
56         // get XInterface of Desktop service
57         Object oInterface;
58         try {
59             oInterface = xMSF.createInstance("com.sun.star.frame.Desktop");
60         } catch (com.sun.star.uno.Exception e) {
61             throw new IllegalArgumentException(
62                 "Desktop Service not available");
63         }
64 
65         // query the desktop interface and then it's componentloader
66         XDesktop oDesktop = (XDesktop) UnoRuntime.queryInterface(
67             XDesktop.class, oInterface);
68 
69         oCLoader = (XComponentLoader) UnoRuntime.queryInterface(
70             XComponentLoader.class, oDesktop);
71     }
72 
getFactory(XMultiServiceFactory xMSF)73     public static SOfficeFactory getFactory(XMultiServiceFactory xMSF) {
74 
75         SOfficeFactory soFactory = (SOfficeFactory) lookup.get(new Integer(xMSF.hashCode()).toString());
76 
77         if (soFactory == null) {
78             soFactory = new SOfficeFactory(xMSF);
79             lookup.put(new Integer(xMSF.hashCode()).toString(), soFactory);
80         }
81 
82         return soFactory;
83     }
84 
85     // *********************************************************
86     // Document creation. The documents needed are created here.
87     // *********************************************************
88     /**
89      * method which opens a new TextDocument
90      *
91      * @see XTextDocument
92      */
createTextDoc(String frameName)93     public XTextDocument createTextDoc(String frameName)
94         throws com.sun.star.uno.Exception {
95 
96         XComponent oDoc = openDoc("swriter", frameName);
97 
98         if (oDoc != null) {
99             DesktopTools.bringWindowToFront(oDoc);
100             return (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, oDoc);
101         } else {
102             return null;
103         }
104 
105     } // finished createTextDoc
106 
107     /**
108      * method which opens a new TextDocument
109      *
110      * @see XTextDocument
111      */
createTextDoc(String frameName, PropertyValue[] mediaDescriptor)112     public XTextDocument createTextDoc(String frameName, PropertyValue[] mediaDescriptor)
113         throws com.sun.star.uno.Exception {
114 
115         XComponent oDoc = openDoc("swriter", frameName, mediaDescriptor);
116 
117         if (oDoc != null) {
118             DesktopTools.bringWindowToFront(oDoc);
119             return (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, oDoc);
120         } else {
121             return null;
122         }
123     } // finished createTextDoc
124 
125     /**
126      * method which opens a new SpreadsheetDocument
127      *
128      * @see XSpreadsheetDocument
129      */
createCalcDoc(String frameName)130     public XSpreadsheetDocument createCalcDoc(String frameName)
131         throws com.sun.star.uno.Exception {
132 
133         XComponent oDoc = openDoc("scalc", frameName);
134 
135         if (oDoc != null) {
136             DesktopTools.bringWindowToFront(oDoc);
137             return (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, oDoc);
138         } else {
139             return null;
140         }
141     } // finished createCalcDoc
142 
143     /**
144      * method which opens a new SpreadsheetDocument
145      *
146      * @see XSpreadsheetDocument
147      */
createCalcDoc(String frameName, PropertyValue[] mediaDescriptor)148     public XSpreadsheetDocument createCalcDoc(String frameName, PropertyValue[] mediaDescriptor)
149         throws com.sun.star.uno.Exception {
150 
151         XComponent oDoc = openDoc("scalc", frameName, mediaDescriptor);
152 
153         if (oDoc != null) {
154             DesktopTools.bringWindowToFront(oDoc);
155             return (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, oDoc);
156         } else {
157             return null;
158         }
159     } // finished createCalcDoc
160 
161     /**
162      * method which opens a new DrawDocument
163      */
createDrawDoc(String frameName)164     public XComponent createDrawDoc(String frameName)
165         throws com.sun.star.uno.Exception {
166 
167         return openDoc("sdraw", frameName);
168     } // finished createDrawDoc
169 
170     /**
171      * method which opens a new ImpressDocument
172      */
173     /**
174      * method which opens a new DrawDocument
175      */
createDrawDoc(String frameName, PropertyValue[] mediaDescriptor)176     public XComponent createDrawDoc(String frameName, PropertyValue[] mediaDescriptor)
177         throws com.sun.star.uno.Exception {
178 
179         return openDoc("sdraw", frameName, mediaDescriptor);
180     } // finished createDrawDoc
181 
182     /**
183      * method which opens a new ImpressDocument
184      */
createImpressDoc(String frameName)185     public XComponent createImpressDoc(String frameName)
186         throws com.sun.star.uno.Exception {
187 
188         return openDoc("simpress", frameName);
189     } // finished createImpressDoc
190 
191     /**
192      * method which opens a new ImpressDocument
193      */
createImpressDoc(String frameName, PropertyValue[] mediaDescriptor)194     public XComponent createImpressDoc(String frameName, PropertyValue[] mediaDescriptor)
195         throws com.sun.star.uno.Exception {
196 
197         return openDoc("simpress", frameName, mediaDescriptor);
198     } // finished createImpressDoc
199 
200     /**
201      * method which opens a new MathDocument
202      */
createMathDoc(String frameName)203     public XComponent createMathDoc(String frameName)
204         throws com.sun.star.uno.Exception {
205 
206         return openDoc("smath", frameName);
207     } // finished createMathDoc
208 
209     /**
210      * method which opens a new MathDocument
211      */
createMathDoc(String frameName, PropertyValue[] mediaDescriptor)212     public XComponent createMathDoc(String frameName, PropertyValue[] mediaDescriptor)
213         throws com.sun.star.uno.Exception {
214 
215         return openDoc("smath", frameName, mediaDescriptor);
216     } // finished createMathDoc
217 
218     /**
219      * method which opens a new ChartDocument
220      *
221      * @see XChartDocument
222      */
createChartDoc(String frameName)223     public XChartDocument createChartDoc(String frameName)
224         throws com.sun.star.uno.Exception {
225 
226 //        XComponent oDoc = loadDocument(
227 //                            util.utils.getFullTestURL("emptyChart.sds"));
228 
229         XComponent oDoc = loadDocument("private:factory/schart");
230 
231         if (oDoc != null) {
232             DesktopTools.bringWindowToFront(oDoc);
233             return (XChartDocument) UnoRuntime.queryInterface(XChartDocument.class, oDoc);
234         } else {
235             return null;
236         }
237 
238     } // finished createChartDoc
239 
240     /**
241      * creates a simple TextTable defaultet to 2 rows and 2 columns
242      */
createTextTable(XTextDocument xTextDoc)243     public static XTextTable createTextTable(XTextDocument xTextDoc)
244         throws com.sun.star.uno.Exception {
245 
246         TableDsc tDsc = new TableDsc();
247         InstCreator instCreate = new InstCreator(xTextDoc, tDsc);
248 
249         XTextTable oTable = (XTextTable) instCreate.getInstance();
250         return oTable;
251     }
252 
253     /**
254      * creates a TextTable with a specified count of rows and columns
255      */
createTextTable(XTextDocument xTextDoc, int rows, int columns)256     public static XTextTable createTextTable(XTextDocument xTextDoc,
257         int rows, int columns)
258         throws com.sun.star.uno.Exception {
259 
260         TableDsc tDsc = new TableDsc(rows, columns);
261         InstCreator instCreate = new InstCreator(xTextDoc, tDsc);
262 
263         XTextTable oTable = (XTextTable) instCreate.getInstance();
264         return oTable;
265     }
266 
267     /**
268      * creates a simple TextFrame
269      * ... to be continued
270      */
createTextFrame(XTextDocument xTextDoc)271     public static XTextFrame createTextFrame(XTextDocument xTextDoc)
272         throws com.sun.star.uno.Exception {
273 
274         FrameDsc tDsc = new FrameDsc();
275         InstCreator instCreate = new InstCreator(xTextDoc, tDsc);
276 
277         XTextFrame oFrame = (XTextFrame) instCreate.getInstance();
278         return oFrame;
279     }
280 
281     /**
282      * creates a simple TextFrame
283      * ... to be continued
284      */
createTextFrame(XTextDocument xTextDoc, int height, int width)285     public static XTextFrame createTextFrame(XTextDocument xTextDoc,
286         int height, int width) {
287 
288         FrameDsc tDsc = new FrameDsc(height, width);
289         InstCreator instCreate = new InstCreator(xTextDoc, tDsc);
290 
291         XTextFrame oFrame = (XTextFrame) instCreate.getInstance();
292         return oFrame;
293     }
294 
insertString(XTextDocument xTextDoc, String cString)295     public static void insertString(XTextDocument xTextDoc, String cString)
296         throws com.sun.star.uno.Exception {
297         XText xText = xTextDoc.getText();
298         XText oText = (XText) UnoRuntime.queryInterface(
299             XText.class, xText);
300 
301         XTextCursor oCursor = oText.createTextCursor();
302         oText.insertString(oCursor, cString, false);
303     }
304 
insertTextContent(XTextDocument xTextDoc, XTextContent xCont)305     public static void insertTextContent(XTextDocument xTextDoc,
306         XTextContent xCont)
307         throws com.sun.star.lang.IllegalArgumentException {
308         XText xText = xTextDoc.getText();
309         XText oText = (XText) UnoRuntime.queryInterface(
310             XText.class, xText);
311 
312         XTextCursor oCursor = oText.createTextCursor();
313         oText.insertTextContent(oCursor, xCont, false);
314     }
315 
getFirstTableCell( XTextContent oTable)316     public static com.sun.star.table.XCell getFirstTableCell(
317         XTextContent oTable) {
318 
319         String CellNames[] = ((XTextTable) oTable).getCellNames();
320 
321         com.sun.star.table.XCell oCell = ((XTextTable) oTable).getCellByName(
322             CellNames[0]);
323         return oCell;
324 
325     }
326 
327     /**
328      * the method createBookmark
329      */
createBookmark(XTextDocument xTextDoc)330     public static XTextContent createBookmark(XTextDocument xTextDoc)
331         throws com.sun.star.uno.Exception {
332 
333         BookmarkDsc tDsc = new BookmarkDsc();
334         InstCreator instCreate = new InstCreator(xTextDoc, tDsc);
335 
336         XTextContent oBookmark = (XTextContent) instCreate.getInstance();
337         return oBookmark;
338 
339     } /// finish createBookmark
340 
341     /**
342      * the method createReferenceMark
343      */
createReferenceMark(XTextDocument xTextDoc)344     public static XTextContent createReferenceMark(XTextDocument xTextDoc)
345         throws com.sun.star.uno.Exception {
346 
347         ReferenceMarkDsc tDsc = new ReferenceMarkDsc();
348         InstCreator instCreate = new InstCreator(xTextDoc, tDsc);
349 
350         XTextContent oReferenceMark = (XTextContent) instCreate.getInstance();
351         return oReferenceMark;
352 
353     } /// finish createReferenceMark
354 
355     /**
356      * the method createFootnote
357      */
createFootnote(XTextDocument xTextDoc)358     public static XTextContent createFootnote(XTextDocument xTextDoc)
359         throws com.sun.star.uno.Exception {
360 
361         FootnoteDsc tDsc = new FootnoteDsc();
362         InstCreator instCreate = new InstCreator(xTextDoc, tDsc);
363 
364         XTextContent oFootnote = (XTextContent) instCreate.getInstance();
365         return oFootnote;
366 
367     } /// finish createFootnote
368 
369     /**
370      * the method create Index
371      */
createIndex(XTextDocument xTextDoc, String kind)372     public static XTextContent createIndex(XTextDocument xTextDoc, String kind)
373         throws com.sun.star.uno.Exception {
374 
375         XMultiServiceFactory oDocMSF = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class,
376             xTextDoc);
377 
378         Object oInt = oDocMSF.createInstance(kind);
379 
380         XTextContent xTC = (XTextContent) UnoRuntime.queryInterface(XDocumentIndex.class, oInt);
381 
382         return xTC;
383 
384     }
385 
createSpreadsheet(XSpreadsheetDocument oDoc)386     public static XSpreadsheet createSpreadsheet(XSpreadsheetDocument oDoc)
387         throws com.sun.star.uno.Exception {
388 
389         XMultiServiceFactory oDocMSF = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, oDoc);
390 
391         Object oInt = oDocMSF.createInstance(
392             "com.sun.star.sheet.Spreadsheet");
393 
394         XSpreadsheet oSpreadsheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, oInt);
395 
396         return oSpreadsheet;
397     }
398 
getTableCollection(XTextDocument oDoc)399     public static XIndexAccess getTableCollection(XTextDocument oDoc) {
400 
401         XTextTablesSupplier oTTS = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, oDoc);
402 
403         XNameAccess oNA = oTTS.getTextTables();
404         XIndexAccess oIA = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, oNA);
405 
406         return oIA;
407     }
408 
getUniqueName(XInterface oInterface, String prefix)409     public static String getUniqueName(XInterface oInterface, String prefix) {
410         XNameAccess oNameAccess = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, oInterface);
411         if (oNameAccess == null) {
412             return null;
413         }
414         int i;
415         for (i = 0; oNameAccess.hasByName(prefix + i); i++) {
416         }
417         return prefix + i;
418     }
419 
createShape(XComponent oDoc, int height, int width, int x, int y, String kind)420     public XShape createShape(XComponent oDoc, int height, int width, int x, int y, String kind) {
421         //possible values for kind are 'Ellipse', 'Line' and 'Rectangle'
422 
423         ShapeDsc sDsc = new ShapeDsc(height, width, x, y, kind);
424         InstCreator instCreate = new InstCreator(oDoc, sDsc);
425 
426         XShape oShape = (XShape) instCreate.getInstance();
427 
428         return oShape;
429 
430     }
431 
432     /**
433      * creates a Diagram wich specified in kind(String)
434      */
createDiagram(XComponent oDoc, String kind)435     public XDiagram createDiagram(XComponent oDoc, String kind) {
436         XInterface oInterface = null;
437         XDiagram oDiagram = null;
438 
439         //get LineDiagram
440         XMultiServiceFactory oDocMSF = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, oDoc);
441 
442         try {
443             oInterface = (XInterface) oDocMSF.createInstance("com.sun.star.chart." + kind);
444             oDiagram = (XDiagram) UnoRuntime.queryInterface(XDiagram.class, oInterface);
445         } catch (Exception e) {
446             // Some exception occures.FAILED
447             System.out.println("Couldn't create " + kind + "-Diagram " + e);
448         }
449         return oDiagram;
450     }
451 
452     /*
453     // create a Control-Instance which specified in kind(String)
454      */
createControl(XComponent oDoc, String kind)455     public XInterface createControl(XComponent oDoc, String kind) {
456 
457         XInterface oControl = null;
458 
459         XMultiServiceFactory oDocMSF = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, oDoc);
460 
461         try {
462             oControl = (XInterface) oDocMSF.createInstance("com.sun.star.form.component." + kind);
463         } catch (Exception e) {
464             // Some exception occures.FAILED
465             System.out.println("Couldn't create instance " + kind + ": " + e);
466         }
467         return oControl;
468     }
469 
470     /*
471     // create an Instance which is specified in kind(String)
472      */
createInstance(XComponent oDoc, String kind)473     public Object createInstance(XComponent oDoc, String kind) {
474 
475         Object oInstance = null;
476 
477         XMultiServiceFactory oDocMSF = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, oDoc);
478 
479         try {
480             oInstance = (Object) oDocMSF.createInstance(kind);
481         } catch (Exception e) {
482             // Some exception occures.FAILED
483             System.out.println("Couldn't create instance " + kind + ": " + e);
484         }
485         return oInstance;
486     }
487 
createControlShape(XComponent oDoc, int height, int width, int x, int y, String kind)488     public XControlShape createControlShape(XComponent oDoc, int height, int width, int x, int y, String kind) {
489 
490         Size size = new Size();
491         Point position = new Point();
492         XControlShape oCShape = null;
493         XControlModel aControl = null;
494 
495         //get MSF
496         XMultiServiceFactory oDocMSF = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, oDoc);
497 
498         try {
499             Object oInt = oDocMSF.createInstance("com.sun.star.drawing.ControlShape");
500             Object aCon = oDocMSF.createInstance("com.sun.star.form.component." + kind);
501             aControl = (XControlModel) UnoRuntime.queryInterface(XControlModel.class, aCon);
502             oCShape = (XControlShape) UnoRuntime.queryInterface(XControlShape.class, oInt);
503             size.Height = height;
504             size.Width = width;
505             position.X = x;
506             position.Y = y;
507             oCShape.setSize(size);
508             oCShape.setPosition(position);
509 
510 
511         } catch (Exception e) {
512             // Some exception occures.FAILED
513             System.out.println("Couldn't create instance " + e);
514         }
515 
516         try {
517             oCShape.setControl(aControl);
518         } catch (Exception e) {
519             // Some exception occures.FAILED
520             System.out.println("Couldn't get Control " + e);
521         }
522 
523 
524         return oCShape;
525 
526     }
527 
loadDocument(String fileName)528     public XComponent loadDocument(String fileName)
529         throws com.sun.star.lang.IllegalArgumentException,
530         com.sun.star.io.IOException,
531         com.sun.star.uno.Exception {
532 
533         // that noargs thing for load attributes
534         PropertyValue[] szEmptyArgs = new PropertyValue[0];
535         String frameName = "_blank";
536 
537         XComponent oDoc = oCLoader.loadComponentFromURL(
538             fileName, frameName, 0, szEmptyArgs);
539 
540         if (oDoc == null) {
541             return null;
542         }
543         DesktopTools.bringWindowToFront(oDoc);
544         return oDoc;
545     }
546 
loadDocument(String fileName, PropertyValue[] Args)547     public XComponent loadDocument(String fileName, PropertyValue[] Args)
548         throws com.sun.star.lang.IllegalArgumentException,
549         com.sun.star.io.IOException,
550         com.sun.star.uno.Exception {
551 
552         // that noargs thing for load attributes
553         String frameName = "_blank";
554 
555         XComponent oDoc = oCLoader.loadComponentFromURL(
556             fileName, frameName, 0, Args);
557 
558         if (oDoc == null) {
559             return null;
560         }
561         DesktopTools.bringWindowToFront(oDoc);
562 
563         return oDoc;
564     }
565 
openDoc(String kind, String frameName)566     public XComponent openDoc(String kind, String frameName)
567         throws com.sun.star.lang.IllegalArgumentException,
568         com.sun.star.io.IOException,
569         com.sun.star.uno.Exception {
570 
571         // that noargs thing for load attributes
572         PropertyValue[] Args = null;
573         if (kind.equals("simpress")) {
574             Args = new PropertyValue[1];
575             PropertyValue Arg = new PropertyValue();
576             Arg.Name = "OpenFlags";
577             Arg.Value = "S";
578             Arg.Handle = -1;
579             Arg.State = PropertyState.DEFAULT_VALUE;
580             Args[0] = Arg;
581         } else {
582             Args = new PropertyValue[0];
583         }
584 
585         if (frameName == null) {
586             frameName = "_blank";
587         }
588         // load a blank a doc
589         XComponent oDoc = oCLoader.loadComponentFromURL("private:factory/" + kind, frameName, 40, Args);
590         DesktopTools.bringWindowToFront(oDoc);
591 
592         return oDoc;
593 
594     } // finished openDoc
595 
openDoc(String kind, String frameName, PropertyValue[] mediaDescriptor)596     public XComponent openDoc(String kind, String frameName, PropertyValue[] mediaDescriptor)
597         throws com.sun.star.lang.IllegalArgumentException,
598         com.sun.star.io.IOException,
599         com.sun.star.uno.Exception {
600 
601         if (frameName == null) {
602             frameName = "_blank";
603         }
604         // load a blank a doc
605         XComponent oDoc = oCLoader.loadComponentFromURL(
606             "private:factory/" + kind, frameName, 40, mediaDescriptor);
607         DesktopTools.bringWindowToFront(oDoc);
608 
609         return oDoc;
610 
611     } // finished openDoc
612 
613     // query for XServiceInfo
queryXServiceInfo(Object oObj)614     public Object queryXServiceInfo(Object oObj) {
615         if (oObj != null) {
616             XServiceInfo oInfo = (XServiceInfo) UnoRuntime.queryInterface(
617                 XServiceInfo.class, oObj);
618             System.out.println("!!!! XServiceInfo n.a. !!!! ");
619         } else {
620             System.out.println("Object is empty!!!! ");
621         }
622         return null;
623     } // finish queryXServiceInfo
624 }
625