1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: SOfficeFactory.java,v $
10 * $Revision: 1.10.8.2 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 import java
.util
.Hashtable
;
33 // access the implementations via names
34 import com
.sun
.star
.uno
.XInterface
;
35 import com
.sun
.star
.lang
.XMultiServiceFactory
;
37 import com
.sun
.star
.uno
.UnoRuntime
;
38 // staroffice interfaces to provide desktop and componentloader
39 // and components i.e. spreadsheets, writerdocs etc.
40 import com
.sun
.star
.frame
.XDesktop
;
41 import com
.sun
.star
.frame
.XComponentLoader
;
42 import com
.sun
.star
.lang
.XComponent
;
43 import com
.sun
.star
.lang
.XServiceInfo
;
46 import com
.sun
.star
.beans
.PropertyValue
;
47 import com
.sun
.star
.beans
.PropertyState
;
49 // additional classes required for testcase
50 import com
.sun
.star
.sheet
.*;
51 import com
.sun
.star
.text
.*;
52 import com
.sun
.star
.container
.*;
53 import com
.sun
.star
.chart
.*;
54 import com
.sun
.star
.drawing
.*;
55 import com
.sun
.star
.awt
.*;
57 public class SOfficeFactory
{
59 private static Hashtable lookup
= new Hashtable(10);
60 protected XComponentLoader oCLoader
;
62 private SOfficeFactory(XMultiServiceFactory xMSF
) {
63 // get XInterface of Desktop service
66 oInterface
= xMSF
.createInstance("com.sun.star.frame.Desktop");
67 } catch (com
.sun
.star
.uno
.Exception e
) {
68 throw new IllegalArgumentException(
69 "Desktop Service not available");
72 // query the desktop interface and then it's componentloader
73 XDesktop oDesktop
= (XDesktop
) UnoRuntime
.queryInterface(
74 XDesktop
.class, oInterface
);
76 oCLoader
= (XComponentLoader
) UnoRuntime
.queryInterface(
77 XComponentLoader
.class, oDesktop
);
80 public static SOfficeFactory
getFactory(XMultiServiceFactory xMSF
) {
82 SOfficeFactory soFactory
= (SOfficeFactory
) lookup
.get(new Integer(xMSF
.hashCode()).toString());
84 if (soFactory
== null) {
85 soFactory
= new SOfficeFactory(xMSF
);
86 lookup
.put(new Integer(xMSF
.hashCode()).toString(), soFactory
);
92 // *********************************************************
93 // Document creation. The documents needed are created here.
94 // *********************************************************
96 * method which opens a new TextDocument
100 public XTextDocument
createTextDoc(String frameName
)
101 throws com
.sun
.star
.uno
.Exception
{
103 XComponent oDoc
= openDoc("swriter", frameName
);
106 DesktopTools
.bringWindowToFront(oDoc
);
107 return (XTextDocument
) UnoRuntime
.queryInterface(XTextDocument
.class, oDoc
);
112 } // finished createTextDoc
115 * method which opens a new TextDocument
119 public XTextDocument
createTextDoc(String frameName
, PropertyValue
[] mediaDescriptor
)
120 throws com
.sun
.star
.uno
.Exception
{
122 XComponent oDoc
= openDoc("swriter", frameName
, mediaDescriptor
);
125 DesktopTools
.bringWindowToFront(oDoc
);
126 return (XTextDocument
) UnoRuntime
.queryInterface(XTextDocument
.class, oDoc
);
130 } // finished createTextDoc
133 * method which opens a new SpreadsheetDocument
135 * @see XSpreadsheetDocument
137 public XSpreadsheetDocument
createCalcDoc(String frameName
)
138 throws com
.sun
.star
.uno
.Exception
{
140 XComponent oDoc
= openDoc("scalc", frameName
);
143 DesktopTools
.bringWindowToFront(oDoc
);
144 return (XSpreadsheetDocument
) UnoRuntime
.queryInterface(XSpreadsheetDocument
.class, oDoc
);
148 } // finished createCalcDoc
151 * method which opens a new SpreadsheetDocument
153 * @see XSpreadsheetDocument
155 public XSpreadsheetDocument
createCalcDoc(String frameName
, PropertyValue
[] mediaDescriptor
)
156 throws com
.sun
.star
.uno
.Exception
{
158 XComponent oDoc
= openDoc("scalc", frameName
, mediaDescriptor
);
161 DesktopTools
.bringWindowToFront(oDoc
);
162 return (XSpreadsheetDocument
) UnoRuntime
.queryInterface(XSpreadsheetDocument
.class, oDoc
);
166 } // finished createCalcDoc
169 * method which opens a new DrawDocument
171 public XComponent
createDrawDoc(String frameName
)
172 throws com
.sun
.star
.uno
.Exception
{
174 return openDoc("sdraw", frameName
);
175 } // finished createDrawDoc
178 * method which opens a new ImpressDocument
181 * method which opens a new DrawDocument
183 public XComponent
createDrawDoc(String frameName
, PropertyValue
[] mediaDescriptor
)
184 throws com
.sun
.star
.uno
.Exception
{
186 return openDoc("sdraw", frameName
, mediaDescriptor
);
187 } // finished createDrawDoc
190 * method which opens a new ImpressDocument
192 public XComponent
createImpressDoc(String frameName
)
193 throws com
.sun
.star
.uno
.Exception
{
195 return openDoc("simpress", frameName
);
196 } // finished createImpressDoc
199 * method which opens a new ImpressDocument
201 public XComponent
createImpressDoc(String frameName
, PropertyValue
[] mediaDescriptor
)
202 throws com
.sun
.star
.uno
.Exception
{
204 return openDoc("simpress", frameName
, mediaDescriptor
);
205 } // finished createImpressDoc
208 * method which opens a new MathDocument
210 public XComponent
createMathDoc(String frameName
)
211 throws com
.sun
.star
.uno
.Exception
{
213 return openDoc("smath", frameName
);
214 } // finished createMathDoc
217 * method which opens a new MathDocument
219 public XComponent
createMathDoc(String frameName
, PropertyValue
[] mediaDescriptor
)
220 throws com
.sun
.star
.uno
.Exception
{
222 return openDoc("smath", frameName
, mediaDescriptor
);
223 } // finished createMathDoc
226 * method which opens a new ChartDocument
228 * @see XChartDocument
230 public XChartDocument
createChartDoc(String frameName
)
231 throws com
.sun
.star
.uno
.Exception
{
233 // XComponent oDoc = loadDocument(
234 // util.utils.getFullTestURL("emptyChart.sds"));
236 XComponent oDoc
= loadDocument("private:factory/schart");
239 DesktopTools
.bringWindowToFront(oDoc
);
240 return (XChartDocument
) UnoRuntime
.queryInterface(XChartDocument
.class, oDoc
);
245 } // finished createChartDoc
248 * creates a simple TextTable defaultet to 2 rows and 2 columns
250 public static XTextTable
createTextTable(XTextDocument xTextDoc
)
251 throws com
.sun
.star
.uno
.Exception
{
253 TableDsc tDsc
= new TableDsc();
254 InstCreator instCreate
= new InstCreator(xTextDoc
, tDsc
);
256 XTextTable oTable
= (XTextTable
) instCreate
.getInstance();
261 * creates a TextTable with a specified count of rows and columns
263 public static XTextTable
createTextTable(XTextDocument xTextDoc
,
264 int rows
, int columns
)
265 throws com
.sun
.star
.uno
.Exception
{
267 TableDsc tDsc
= new TableDsc(rows
, columns
);
268 InstCreator instCreate
= new InstCreator(xTextDoc
, tDsc
);
270 XTextTable oTable
= (XTextTable
) instCreate
.getInstance();
275 * creates a simple TextFrame
276 * ... to be continued
278 public static XTextFrame
createTextFrame(XTextDocument xTextDoc
)
279 throws com
.sun
.star
.uno
.Exception
{
281 FrameDsc tDsc
= new FrameDsc();
282 InstCreator instCreate
= new InstCreator(xTextDoc
, tDsc
);
284 XTextFrame oFrame
= (XTextFrame
) instCreate
.getInstance();
289 * creates a simple TextFrame
290 * ... to be continued
292 public static XTextFrame
createTextFrame(XTextDocument xTextDoc
,
293 int height
, int width
) {
295 FrameDsc tDsc
= new FrameDsc(height
, width
);
296 InstCreator instCreate
= new InstCreator(xTextDoc
, tDsc
);
298 XTextFrame oFrame
= (XTextFrame
) instCreate
.getInstance();
302 public static void insertString(XTextDocument xTextDoc
, String cString
)
303 throws com
.sun
.star
.uno
.Exception
{
304 XText xText
= xTextDoc
.getText();
305 XText oText
= (XText
) UnoRuntime
.queryInterface(
308 XTextCursor oCursor
= oText
.createTextCursor();
309 oText
.insertString(oCursor
, cString
, false);
312 public static void insertTextContent(XTextDocument xTextDoc
,
314 throws com
.sun
.star
.lang
.IllegalArgumentException
{
315 XText xText
= xTextDoc
.getText();
316 XText oText
= (XText
) UnoRuntime
.queryInterface(
319 XTextCursor oCursor
= oText
.createTextCursor();
320 oText
.insertTextContent(oCursor
, xCont
, false);
323 public static com
.sun
.star
.table
.XCell
getFirstTableCell(
324 XTextContent oTable
) {
326 String CellNames
[] = ((XTextTable
) oTable
).getCellNames();
328 com
.sun
.star
.table
.XCell oCell
= ((XTextTable
) oTable
).getCellByName(
335 * the method createBookmark
337 public static XTextContent
createBookmark(XTextDocument xTextDoc
)
338 throws com
.sun
.star
.uno
.Exception
{
340 BookmarkDsc tDsc
= new BookmarkDsc();
341 InstCreator instCreate
= new InstCreator(xTextDoc
, tDsc
);
343 XTextContent oBookmark
= (XTextContent
) instCreate
.getInstance();
346 } /// finish createBookmark
349 * the method createReferenceMark
351 public static XTextContent
createReferenceMark(XTextDocument xTextDoc
)
352 throws com
.sun
.star
.uno
.Exception
{
354 ReferenceMarkDsc tDsc
= new ReferenceMarkDsc();
355 InstCreator instCreate
= new InstCreator(xTextDoc
, tDsc
);
357 XTextContent oReferenceMark
= (XTextContent
) instCreate
.getInstance();
358 return oReferenceMark
;
360 } /// finish createReferenceMark
363 * the method createFootnote
365 public static XTextContent
createFootnote(XTextDocument xTextDoc
)
366 throws com
.sun
.star
.uno
.Exception
{
368 FootnoteDsc tDsc
= new FootnoteDsc();
369 InstCreator instCreate
= new InstCreator(xTextDoc
, tDsc
);
371 XTextContent oFootnote
= (XTextContent
) instCreate
.getInstance();
374 } /// finish createFootnote
377 * the method create Index
379 public static XTextContent
createIndex(XTextDocument xTextDoc
, String kind
)
380 throws com
.sun
.star
.uno
.Exception
{
382 XMultiServiceFactory oDocMSF
= (XMultiServiceFactory
) UnoRuntime
.queryInterface(XMultiServiceFactory
.class,
385 Object oInt
= oDocMSF
.createInstance(kind
);
387 XTextContent xTC
= (XTextContent
) UnoRuntime
.queryInterface(XDocumentIndex
.class, oInt
);
393 public static XSpreadsheet
createSpreadsheet(XSpreadsheetDocument oDoc
)
394 throws com
.sun
.star
.uno
.Exception
{
396 XMultiServiceFactory oDocMSF
= (XMultiServiceFactory
) UnoRuntime
.queryInterface(XMultiServiceFactory
.class, oDoc
);
398 Object oInt
= oDocMSF
.createInstance(
399 "com.sun.star.sheet.Spreadsheet");
401 XSpreadsheet oSpreadsheet
= (XSpreadsheet
) UnoRuntime
.queryInterface(XSpreadsheet
.class, oInt
);
406 public static XIndexAccess
getTableCollection(XTextDocument oDoc
) {
408 XTextTablesSupplier oTTS
= (XTextTablesSupplier
) UnoRuntime
.queryInterface(XTextTablesSupplier
.class, oDoc
);
410 XNameAccess oNA
= oTTS
.getTextTables();
411 XIndexAccess oIA
= (XIndexAccess
) UnoRuntime
.queryInterface(XIndexAccess
.class, oNA
);
416 public static String
getUniqueName(XInterface oInterface
, String prefix
) {
417 XNameAccess oNameAccess
= (XNameAccess
) UnoRuntime
.queryInterface(XNameAccess
.class, oInterface
);
418 if (oNameAccess
== null) {
422 for (i
= 0; oNameAccess
.hasByName(prefix
+ i
); i
++) {
428 public XShape
createShape(XComponent oDoc
, int height
, int width
, int x
, int y
, String kind
) {
429 //possible values for kind are 'Ellipse', 'Line' and 'Rectangle'
431 ShapeDsc sDsc
= new ShapeDsc(height
, width
, x
, y
, kind
);
432 InstCreator instCreate
= new InstCreator(oDoc
, sDsc
);
434 XShape oShape
= (XShape
) instCreate
.getInstance();
441 * creates a Diagram wich specified in kind(String)
443 public XDiagram
createDiagram(XComponent oDoc
, String kind
) {
444 XInterface oInterface
= null;
445 XDiagram oDiagram
= null;
448 XMultiServiceFactory oDocMSF
= (XMultiServiceFactory
) UnoRuntime
.queryInterface(XMultiServiceFactory
.class, oDoc
);
451 oInterface
= (XInterface
) oDocMSF
.createInstance("com.sun.star.chart." + kind
);
452 oDiagram
= (XDiagram
) UnoRuntime
.queryInterface(XDiagram
.class, oInterface
);
453 } catch (Exception e
) {
454 // Some exception occures.FAILED
455 System
.out
.println("Couldn't create " + kind
+ "-Diagram " + e
);
461 // create a Control-Instance which specified in kind(String)
463 public XInterface
createControl(XComponent oDoc
, String kind
) {
465 XInterface oControl
= null;
467 XMultiServiceFactory oDocMSF
= (XMultiServiceFactory
) UnoRuntime
.queryInterface(XMultiServiceFactory
.class, oDoc
);
470 oControl
= (XInterface
) oDocMSF
.createInstance("com.sun.star.form.component." + kind
);
471 } catch (Exception e
) {
472 // Some exception occures.FAILED
473 System
.out
.println("Couldn't create instance " + kind
+ ": " + e
);
479 // create an Instance which is specified in kind(String)
481 public Object
createInstance(XComponent oDoc
, String kind
) {
483 Object oInstance
= null;
485 XMultiServiceFactory oDocMSF
= (XMultiServiceFactory
) UnoRuntime
.queryInterface(XMultiServiceFactory
.class, oDoc
);
488 oInstance
= (Object
) oDocMSF
.createInstance(kind
);
489 } catch (Exception e
) {
490 // Some exception occures.FAILED
491 System
.out
.println("Couldn't create instance " + kind
+ ": " + e
);
496 public XControlShape
createControlShape(XComponent oDoc
, int height
, int width
, int x
, int y
, String kind
) {
498 Size size
= new Size();
499 Point position
= new Point();
500 XControlShape oCShape
= null;
501 XControlModel aControl
= null;
504 XMultiServiceFactory oDocMSF
= (XMultiServiceFactory
) UnoRuntime
.queryInterface(XMultiServiceFactory
.class, oDoc
);
507 Object oInt
= oDocMSF
.createInstance("com.sun.star.drawing.ControlShape");
508 Object aCon
= oDocMSF
.createInstance("com.sun.star.form.component." + kind
);
509 aControl
= (XControlModel
) UnoRuntime
.queryInterface(XControlModel
.class, aCon
);
510 oCShape
= (XControlShape
) UnoRuntime
.queryInterface(XControlShape
.class, oInt
);
511 size
.Height
= height
;
515 oCShape
.setSize(size
);
516 oCShape
.setPosition(position
);
519 } catch (Exception e
) {
520 // Some exception occures.FAILED
521 System
.out
.println("Couldn't create instance " + e
);
525 oCShape
.setControl(aControl
);
526 } catch (Exception e
) {
527 // Some exception occures.FAILED
528 System
.out
.println("Couldn't get Control " + e
);
536 public XComponent
loadDocument(String fileName
)
537 throws com
.sun
.star
.lang
.IllegalArgumentException
,
538 com
.sun
.star
.io
.IOException
,
539 com
.sun
.star
.uno
.Exception
{
541 // that noargs thing for load attributes
542 PropertyValue
[] szEmptyArgs
= new PropertyValue
[0];
543 String frameName
= "_blank";
545 XComponent oDoc
= oCLoader
.loadComponentFromURL(
546 fileName
, frameName
, 0, szEmptyArgs
);
551 DesktopTools
.bringWindowToFront(oDoc
);
555 public XComponent
loadDocument(String fileName
, PropertyValue
[] Args
)
556 throws com
.sun
.star
.lang
.IllegalArgumentException
,
557 com
.sun
.star
.io
.IOException
,
558 com
.sun
.star
.uno
.Exception
{
560 // that noargs thing for load attributes
561 String frameName
= "_blank";
563 XComponent oDoc
= oCLoader
.loadComponentFromURL(
564 fileName
, frameName
, 0, Args
);
569 DesktopTools
.bringWindowToFront(oDoc
);
574 public XComponent
openDoc(String kind
, String frameName
)
575 throws com
.sun
.star
.lang
.IllegalArgumentException
,
576 com
.sun
.star
.io
.IOException
,
577 com
.sun
.star
.uno
.Exception
{
579 // that noargs thing for load attributes
580 PropertyValue
[] Args
= null;
581 if (kind
.equals("simpress")) {
582 Args
= new PropertyValue
[1];
583 PropertyValue Arg
= new PropertyValue();
584 Arg
.Name
= "OpenFlags";
587 Arg
.State
= PropertyState
.DEFAULT_VALUE
;
590 Args
= new PropertyValue
[0];
593 if (frameName
== null) {
594 frameName
= "_blank";
596 // load a blank a doc
597 XComponent oDoc
= oCLoader
.loadComponentFromURL("private:factory/" + kind
, frameName
, 40, Args
);
598 DesktopTools
.bringWindowToFront(oDoc
);
602 } // finished openDoc
604 public XComponent
openDoc(String kind
, String frameName
, PropertyValue
[] mediaDescriptor
)
605 throws com
.sun
.star
.lang
.IllegalArgumentException
,
606 com
.sun
.star
.io
.IOException
,
607 com
.sun
.star
.uno
.Exception
{
609 if (frameName
== null) {
610 frameName
= "_blank";
612 // load a blank a doc
613 XComponent oDoc
= oCLoader
.loadComponentFromURL(
614 "private:factory/" + kind
, frameName
, 40, mediaDescriptor
);
615 DesktopTools
.bringWindowToFront(oDoc
);
619 } // finished openDoc
621 // query for XServiceInfo
622 public Object
queryXServiceInfo(Object oObj
) {
624 XServiceInfo oInfo
= (XServiceInfo
) UnoRuntime
.queryInterface(
625 XServiceInfo
.class, oObj
);
626 System
.out
.println("!!!! XServiceInfo n.a. !!!! ");
628 System
.out
.println("Object is empty!!!! ");
631 } // finish queryXServiceInfo