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