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