1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
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
;
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
;
43 import com
.sun
.star
.beans
.PropertyValue
;
44 import com
.sun
.star
.beans
.PropertyState
;
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
.*;
54 public class SOfficeFactory
{
56 private static Hashtable lookup
= new Hashtable(10);
57 protected XComponentLoader oCLoader
;
59 private SOfficeFactory(XMultiServiceFactory xMSF
) {
60 // get XInterface of Desktop service
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");
69 // query the desktop interface and then it's componentloader
70 XDesktop oDesktop
= (XDesktop
) UnoRuntime
.queryInterface(
71 XDesktop
.class, oInterface
);
73 oCLoader
= (XComponentLoader
) UnoRuntime
.queryInterface(
74 XComponentLoader
.class, oDesktop
);
77 public static SOfficeFactory
getFactory(XMultiServiceFactory xMSF
) {
79 SOfficeFactory soFactory
= (SOfficeFactory
) lookup
.get(new Integer(xMSF
.hashCode()).toString());
81 if (soFactory
== null) {
82 soFactory
= new SOfficeFactory(xMSF
);
83 lookup
.put(new Integer(xMSF
.hashCode()).toString(), soFactory
);
89 // *********************************************************
90 // Document creation. The documents needed are created here.
91 // *********************************************************
93 * method which opens a new TextDocument
97 public XTextDocument
createTextDoc(String frameName
)
98 throws com
.sun
.star
.uno
.Exception
{
100 XComponent oDoc
= openDoc("swriter", frameName
);
103 DesktopTools
.bringWindowToFront(oDoc
);
104 return (XTextDocument
) UnoRuntime
.queryInterface(XTextDocument
.class, oDoc
);
109 } // finished createTextDoc
112 * method which opens a new TextDocument
116 public XTextDocument
createTextDoc(String frameName
, PropertyValue
[] mediaDescriptor
)
117 throws com
.sun
.star
.uno
.Exception
{
119 XComponent oDoc
= openDoc("swriter", frameName
, mediaDescriptor
);
122 DesktopTools
.bringWindowToFront(oDoc
);
123 return (XTextDocument
) UnoRuntime
.queryInterface(XTextDocument
.class, oDoc
);
127 } // finished createTextDoc
130 * method which opens a new SpreadsheetDocument
132 * @see XSpreadsheetDocument
134 public XSpreadsheetDocument
createCalcDoc(String frameName
)
135 throws com
.sun
.star
.uno
.Exception
{
137 XComponent oDoc
= openDoc("scalc", frameName
);
140 DesktopTools
.bringWindowToFront(oDoc
);
141 return (XSpreadsheetDocument
) UnoRuntime
.queryInterface(XSpreadsheetDocument
.class, oDoc
);
145 } // finished createCalcDoc
148 * method which opens a new SpreadsheetDocument
150 * @see XSpreadsheetDocument
152 public XSpreadsheetDocument
createCalcDoc(String frameName
, PropertyValue
[] mediaDescriptor
)
153 throws com
.sun
.star
.uno
.Exception
{
155 XComponent oDoc
= openDoc("scalc", frameName
, mediaDescriptor
);
158 DesktopTools
.bringWindowToFront(oDoc
);
159 return (XSpreadsheetDocument
) UnoRuntime
.queryInterface(XSpreadsheetDocument
.class, oDoc
);
163 } // finished createCalcDoc
166 * method which opens a new DrawDocument
168 public XComponent
createDrawDoc(String frameName
)
169 throws com
.sun
.star
.uno
.Exception
{
171 return openDoc("sdraw", frameName
);
172 } // finished createDrawDoc
175 * method which opens a new ImpressDocument
178 * method which opens a new DrawDocument
180 public XComponent
createDrawDoc(String frameName
, PropertyValue
[] mediaDescriptor
)
181 throws com
.sun
.star
.uno
.Exception
{
183 return openDoc("sdraw", frameName
, mediaDescriptor
);
184 } // finished createDrawDoc
187 * method which opens a new ImpressDocument
189 public XComponent
createImpressDoc(String frameName
)
190 throws com
.sun
.star
.uno
.Exception
{
192 return openDoc("simpress", frameName
);
193 } // finished createImpressDoc
196 * method which opens a new ImpressDocument
198 public XComponent
createImpressDoc(String frameName
, PropertyValue
[] mediaDescriptor
)
199 throws com
.sun
.star
.uno
.Exception
{
201 return openDoc("simpress", frameName
, mediaDescriptor
);
202 } // finished createImpressDoc
205 * method which opens a new MathDocument
207 public XComponent
createMathDoc(String frameName
)
208 throws com
.sun
.star
.uno
.Exception
{
210 return openDoc("smath", frameName
);
211 } // finished createMathDoc
214 * method which opens a new MathDocument
216 public XComponent
createMathDoc(String frameName
, PropertyValue
[] mediaDescriptor
)
217 throws com
.sun
.star
.uno
.Exception
{
219 return openDoc("smath", frameName
, mediaDescriptor
);
220 } // finished createMathDoc
223 * method which opens a new ChartDocument
225 * @see XChartDocument
227 public XChartDocument
createChartDoc(String frameName
)
228 throws com
.sun
.star
.uno
.Exception
{
230 // XComponent oDoc = loadDocument(
231 // util.utils.getFullTestURL("emptyChart.sds"));
233 XComponent oDoc
= loadDocument("private:factory/schart");
236 DesktopTools
.bringWindowToFront(oDoc
);
237 return (XChartDocument
) UnoRuntime
.queryInterface(XChartDocument
.class, oDoc
);
242 } // finished createChartDoc
245 * creates a simple TextTable defaultet to 2 rows and 2 columns
247 public static XTextTable
createTextTable(XTextDocument xTextDoc
)
248 throws com
.sun
.star
.uno
.Exception
{
250 TableDsc tDsc
= new TableDsc();
251 InstCreator instCreate
= new InstCreator(xTextDoc
, tDsc
);
253 XTextTable oTable
= (XTextTable
) instCreate
.getInstance();
258 * creates a TextTable with a specified count of rows and columns
260 public static XTextTable
createTextTable(XTextDocument xTextDoc
,
261 int rows
, int columns
)
262 throws com
.sun
.star
.uno
.Exception
{
264 TableDsc tDsc
= new TableDsc(rows
, columns
);
265 InstCreator instCreate
= new InstCreator(xTextDoc
, tDsc
);
267 XTextTable oTable
= (XTextTable
) instCreate
.getInstance();
272 * creates a simple TextFrame
273 * ... to be continued
275 public static XTextFrame
createTextFrame(XTextDocument xTextDoc
)
276 throws com
.sun
.star
.uno
.Exception
{
278 FrameDsc tDsc
= new FrameDsc();
279 InstCreator instCreate
= new InstCreator(xTextDoc
, tDsc
);
281 XTextFrame oFrame
= (XTextFrame
) instCreate
.getInstance();
286 * creates a simple TextFrame
287 * ... to be continued
289 public static XTextFrame
createTextFrame(XTextDocument xTextDoc
,
290 int height
, int width
) {
292 FrameDsc tDsc
= new FrameDsc(height
, width
);
293 InstCreator instCreate
= new InstCreator(xTextDoc
, tDsc
);
295 XTextFrame oFrame
= (XTextFrame
) instCreate
.getInstance();
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(
305 XTextCursor oCursor
= oText
.createTextCursor();
306 oText
.insertString(oCursor
, cString
, false);
309 public static void insertTextContent(XTextDocument xTextDoc
,
311 throws com
.sun
.star
.lang
.IllegalArgumentException
{
312 XText xText
= xTextDoc
.getText();
313 XText oText
= (XText
) UnoRuntime
.queryInterface(
316 XTextCursor oCursor
= oText
.createTextCursor();
317 oText
.insertTextContent(oCursor
, xCont
, false);
320 public static com
.sun
.star
.table
.XCell
getFirstTableCell(
321 XTextContent oTable
) {
323 String CellNames
[] = ((XTextTable
) oTable
).getCellNames();
325 com
.sun
.star
.table
.XCell oCell
= ((XTextTable
) oTable
).getCellByName(
332 * the method createBookmark
334 public static XTextContent
createBookmark(XTextDocument xTextDoc
)
335 throws com
.sun
.star
.uno
.Exception
{
337 BookmarkDsc tDsc
= new BookmarkDsc();
338 InstCreator instCreate
= new InstCreator(xTextDoc
, tDsc
);
340 XTextContent oBookmark
= (XTextContent
) instCreate
.getInstance();
343 } /// finish createBookmark
346 * the method createReferenceMark
348 public static XTextContent
createReferenceMark(XTextDocument xTextDoc
)
349 throws com
.sun
.star
.uno
.Exception
{
351 ReferenceMarkDsc tDsc
= new ReferenceMarkDsc();
352 InstCreator instCreate
= new InstCreator(xTextDoc
, tDsc
);
354 XTextContent oReferenceMark
= (XTextContent
) instCreate
.getInstance();
355 return oReferenceMark
;
357 } /// finish createReferenceMark
360 * the method createFootnote
362 public static XTextContent
createFootnote(XTextDocument xTextDoc
)
363 throws com
.sun
.star
.uno
.Exception
{
365 FootnoteDsc tDsc
= new FootnoteDsc();
366 InstCreator instCreate
= new InstCreator(xTextDoc
, tDsc
);
368 XTextContent oFootnote
= (XTextContent
) instCreate
.getInstance();
371 } /// finish createFootnote
374 * the method create Index
376 public static XTextContent
createIndex(XTextDocument xTextDoc
, String kind
)
377 throws com
.sun
.star
.uno
.Exception
{
379 XMultiServiceFactory oDocMSF
= (XMultiServiceFactory
) UnoRuntime
.queryInterface(XMultiServiceFactory
.class,
382 Object oInt
= oDocMSF
.createInstance(kind
);
384 XTextContent xTC
= (XTextContent
) UnoRuntime
.queryInterface(XDocumentIndex
.class, oInt
);
390 public static XSpreadsheet
createSpreadsheet(XSpreadsheetDocument oDoc
)
391 throws com
.sun
.star
.uno
.Exception
{
393 XMultiServiceFactory oDocMSF
= (XMultiServiceFactory
) UnoRuntime
.queryInterface(XMultiServiceFactory
.class, oDoc
);
395 Object oInt
= oDocMSF
.createInstance(
396 "com.sun.star.sheet.Spreadsheet");
398 XSpreadsheet oSpreadsheet
= (XSpreadsheet
) UnoRuntime
.queryInterface(XSpreadsheet
.class, oInt
);
403 public static XIndexAccess
getTableCollection(XTextDocument oDoc
) {
405 XTextTablesSupplier oTTS
= (XTextTablesSupplier
) UnoRuntime
.queryInterface(XTextTablesSupplier
.class, oDoc
);
407 XNameAccess oNA
= oTTS
.getTextTables();
408 XIndexAccess oIA
= (XIndexAccess
) UnoRuntime
.queryInterface(XIndexAccess
.class, oNA
);
413 public static String
getUniqueName(XInterface oInterface
, String prefix
) {
414 XNameAccess oNameAccess
= (XNameAccess
) UnoRuntime
.queryInterface(XNameAccess
.class, oInterface
);
415 if (oNameAccess
== null) {
419 for (i
= 0; oNameAccess
.hasByName(prefix
+ i
); i
++) {
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'
428 ShapeDsc sDsc
= new ShapeDsc(height
, width
, x
, y
, kind
);
429 InstCreator instCreate
= new InstCreator(oDoc
, sDsc
);
431 XShape oShape
= (XShape
) instCreate
.getInstance();
438 * creates a Diagram wich specified in kind(String)
440 public XDiagram
createDiagram(XComponent oDoc
, String kind
) {
441 XInterface oInterface
= null;
442 XDiagram oDiagram
= null;
445 XMultiServiceFactory oDocMSF
= (XMultiServiceFactory
) UnoRuntime
.queryInterface(XMultiServiceFactory
.class, oDoc
);
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
);
458 // create a Control-Instance which specified in kind(String)
460 public XInterface
createControl(XComponent oDoc
, String kind
) {
462 XInterface oControl
= null;
464 XMultiServiceFactory oDocMSF
= (XMultiServiceFactory
) UnoRuntime
.queryInterface(XMultiServiceFactory
.class, oDoc
);
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
);
476 // create an Instance which is specified in kind(String)
478 public Object
createInstance(XComponent oDoc
, String kind
) {
480 Object oInstance
= null;
482 XMultiServiceFactory oDocMSF
= (XMultiServiceFactory
) UnoRuntime
.queryInterface(XMultiServiceFactory
.class, oDoc
);
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
);
493 public XControlShape
createControlShape(XComponent oDoc
, int height
, int width
, int x
, int y
, String kind
) {
495 Size size
= new Size();
496 Point position
= new Point();
497 XControlShape oCShape
= null;
498 XControlModel aControl
= null;
501 XMultiServiceFactory oDocMSF
= (XMultiServiceFactory
) UnoRuntime
.queryInterface(XMultiServiceFactory
.class, oDoc
);
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
;
512 oCShape
.setSize(size
);
513 oCShape
.setPosition(position
);
516 } catch (Exception e
) {
517 // Some exception occures.FAILED
518 System
.out
.println("Couldn't create instance " + e
);
522 oCShape
.setControl(aControl
);
523 } catch (Exception e
) {
524 // Some exception occures.FAILED
525 System
.out
.println("Couldn't get Control " + e
);
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
{
538 // that noargs thing for load attributes
539 PropertyValue
[] szEmptyArgs
= new PropertyValue
[0];
540 String frameName
= "_blank";
542 XComponent oDoc
= oCLoader
.loadComponentFromURL(
543 fileName
, frameName
, 0, szEmptyArgs
);
548 DesktopTools
.bringWindowToFront(oDoc
);
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
{
557 // that noargs thing for load attributes
558 String frameName
= "_blank";
560 XComponent oDoc
= oCLoader
.loadComponentFromURL(
561 fileName
, frameName
, 0, Args
);
566 DesktopTools
.bringWindowToFront(oDoc
);
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
{
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";
584 Arg
.State
= PropertyState
.DEFAULT_VALUE
;
587 Args
= new PropertyValue
[0];
590 if (frameName
== null) {
591 frameName
= "_blank";
593 // load a blank a doc
594 XComponent oDoc
= oCLoader
.loadComponentFromURL("private:factory/" + kind
, frameName
, 40, Args
);
595 DesktopTools
.bringWindowToFront(oDoc
);
599 } // finished openDoc
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
{
606 if (frameName
== null) {
607 frameName
= "_blank";
609 // load a blank a doc
610 XComponent oDoc
= oCLoader
.loadComponentFromURL(
611 "private:factory/" + kind
, frameName
, 40, mediaDescriptor
);
612 DesktopTools
.bringWindowToFront(oDoc
);
616 } // finished openDoc
618 // query for XServiceInfo
619 public Object
queryXServiceInfo(Object oObj
) {
621 XServiceInfo oInfo
= (XServiceInfo
) UnoRuntime
.queryInterface(
622 XServiceInfo
.class, oObj
);
623 System
.out
.println("!!!! XServiceInfo n.a. !!!! ");
625 System
.out
.println("Object is empty!!!! ");
628 } // finish queryXServiceInfo