update dev300-m58
[ooovba.git] / qadevOOo / runner / util / SOfficeFactory.java
blobfc9fc4a0d3f4efa9491717698cce4d222d5a771d
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
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 ************************************************************************/
30 package util;
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;
45 // name - value pair
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
64 Object oInterface;
65 try {
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);
89 return soFactory;
92 // *********************************************************
93 // Document creation. The documents needed are created here.
94 // *********************************************************
95 /**
96 * method which opens a new TextDocument
98 * @see XTextDocument
100 public XTextDocument createTextDoc(String frameName)
101 throws com.sun.star.uno.Exception {
103 XComponent oDoc = openDoc("swriter", frameName);
105 if (oDoc != null) {
106 DesktopTools.bringWindowToFront(oDoc);
107 return (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, oDoc);
108 } else {
109 return null;
112 } // finished createTextDoc
115 * method which opens a new TextDocument
117 * @see XTextDocument
119 public XTextDocument createTextDoc(String frameName, PropertyValue[] mediaDescriptor)
120 throws com.sun.star.uno.Exception {
122 XComponent oDoc = openDoc("swriter", frameName, mediaDescriptor);
124 if (oDoc != null) {
125 DesktopTools.bringWindowToFront(oDoc);
126 return (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, oDoc);
127 } else {
128 return null;
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);
142 if (oDoc != null) {
143 DesktopTools.bringWindowToFront(oDoc);
144 return (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, oDoc);
145 } else {
146 return null;
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);
160 if (oDoc != null) {
161 DesktopTools.bringWindowToFront(oDoc);
162 return (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, oDoc);
163 } else {
164 return null;
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");
238 if (oDoc != null) {
239 DesktopTools.bringWindowToFront(oDoc);
240 return (XChartDocument) UnoRuntime.queryInterface(XChartDocument.class, oDoc);
241 } else {
242 return null;
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();
257 return oTable;
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();
271 return oTable;
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();
285 return oFrame;
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();
299 return oFrame;
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(
306 XText.class, xText);
308 XTextCursor oCursor = oText.createTextCursor();
309 oText.insertString(oCursor, cString, false);
312 public static void insertTextContent(XTextDocument xTextDoc,
313 XTextContent xCont)
314 throws com.sun.star.lang.IllegalArgumentException {
315 XText xText = xTextDoc.getText();
316 XText oText = (XText) UnoRuntime.queryInterface(
317 XText.class, xText);
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(
329 CellNames[0]);
330 return oCell;
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();
344 return oBookmark;
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();
372 return oFootnote;
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,
383 xTextDoc);
385 Object oInt = oDocMSF.createInstance(kind);
387 XTextContent xTC = (XTextContent) UnoRuntime.queryInterface(XDocumentIndex.class, oInt);
389 return xTC;
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);
403 return oSpreadsheet;
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);
413 return oIA;
416 public static String getUniqueName(XInterface oInterface, String prefix) {
417 XNameAccess oNameAccess = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, oInterface);
418 if (oNameAccess == null) {
419 return null;
421 int i;
422 for (i = 0; oNameAccess.hasByName(prefix + i); i++) {
425 return prefix + 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();
436 return oShape;
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;
447 //get LineDiagram
448 XMultiServiceFactory oDocMSF = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, oDoc);
450 try {
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);
457 return oDiagram;
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);
469 try {
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);
475 return oControl;
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);
487 try {
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);
493 return oInstance;
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;
503 //get MSF
504 XMultiServiceFactory oDocMSF = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, oDoc);
506 try {
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;
512 size.Width = width;
513 position.X = x;
514 position.Y = y;
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);
524 try {
525 oCShape.setControl(aControl);
526 } catch (Exception e) {
527 // Some exception occures.FAILED
528 System.out.println("Couldn't get Control " + e);
532 return oCShape;
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);
548 if (oDoc == null) {
549 return null;
551 DesktopTools.bringWindowToFront(oDoc);
552 return 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);
566 if (oDoc == null) {
567 return null;
569 DesktopTools.bringWindowToFront(oDoc);
571 return 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";
585 Arg.Value = "S";
586 Arg.Handle = -1;
587 Arg.State = PropertyState.DEFAULT_VALUE;
588 Args[0] = Arg;
589 } else {
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);
600 return 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);
617 return oDoc;
619 } // finished openDoc
621 // query for XServiceInfo
622 public Object queryXServiceInfo(Object oObj) {
623 if (oObj != null) {
624 XServiceInfo oInfo = (XServiceInfo) UnoRuntime.queryInterface(
625 XServiceInfo.class, oObj);
626 System.out.println("!!!! XServiceInfo n.a. !!!! ");
627 } else {
628 System.out.println("Object is empty!!!! ");
630 return null;
631 } // finish queryXServiceInfo