tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / qadevOOo / runner / util / SOfficeFactory.java
blob43667d15d44cecde8230fb71bf7e1305d5b0f6fc
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 package util;
20 import java.util.HashMap;
21 // access the implementations via names
22 import com.sun.star.uno.XInterface;
23 import com.sun.star.lang.XMultiServiceFactory;
25 import com.sun.star.uno.UnoRuntime;
26 // staroffice interfaces to provide desktop and componentloader
27 // and components i.e. spreadsheets, writerdocs etc.
28 import com.sun.star.frame.XDesktop;
29 import com.sun.star.frame.XComponentLoader;
30 import com.sun.star.lang.XComponent;
31 // name - value pair
32 import com.sun.star.beans.PropertyValue;
33 import com.sun.star.beans.PropertyState;
35 // additional classes required for testcase
36 import com.sun.star.sheet.*;
37 import com.sun.star.text.*;
38 import com.sun.star.container.*;
39 import com.sun.star.chart.*;
40 import com.sun.star.drawing.*;
42 public class SOfficeFactory {
44 private static HashMap<String, SOfficeFactory> lookup = new HashMap<String, SOfficeFactory>(10);
45 private final XComponentLoader oCLoader;
47 private SOfficeFactory(XMultiServiceFactory xMSF) {
48 // get XInterface of Desktop service
49 Object oInterface;
50 try {
51 oInterface = xMSF.createInstance("com.sun.star.frame.Desktop");
52 } catch (com.sun.star.uno.Exception e) {
53 throw new IllegalArgumentException("Desktop Service not available", e);
56 // query the desktop interface and then it's componentloader
57 XDesktop oDesktop = UnoRuntime.queryInterface(
58 XDesktop.class, oInterface);
60 oCLoader = UnoRuntime.queryInterface(
61 XComponentLoader.class, oDesktop);
64 public static SOfficeFactory getFactory(XMultiServiceFactory xMSF) {
66 SOfficeFactory soFactory = lookup.get(Integer.toString(xMSF.hashCode()));
68 if (soFactory == null) {
69 soFactory = new SOfficeFactory(xMSF);
70 lookup.put(Integer.toString(xMSF.hashCode()), soFactory);
73 return soFactory;
76 // *********************************************************
77 // Document creation. The documents needed are created here.
78 // *********************************************************
79 /**
80 * method which opens a new TextDocument
82 * @see XTextDocument
84 public XTextDocument createTextDoc(String frameName)
85 throws com.sun.star.uno.Exception {
87 XComponent oDoc = openDoc("swriter", frameName);
89 if (oDoc != null) {
90 DesktopTools.bringWindowToFront(oDoc);
91 return UnoRuntime.queryInterface(XTextDocument.class, oDoc);
92 } else {
93 return null;
96 } // finished createTextDoc
101 * method which opens a new SpreadsheetDocument
103 * @see XSpreadsheetDocument
105 public XSpreadsheetDocument createCalcDoc(String frameName)
106 throws com.sun.star.uno.Exception {
108 XComponent oDoc = openDoc("scalc", frameName);
110 if (oDoc != null) {
111 DesktopTools.bringWindowToFront(oDoc);
112 return UnoRuntime.queryInterface(XSpreadsheetDocument.class, oDoc);
113 } else {
114 return null;
116 } // finished createCalcDoc
121 * method which opens a new DrawDocument
123 public XComponent createDrawDoc(String frameName)
124 throws com.sun.star.uno.Exception {
126 return openDoc("sdraw", frameName);
127 } // finished createDrawDoc
132 * method which opens a new ImpressDocument
134 public XComponent createImpressDoc(String frameName)
135 throws com.sun.star.uno.Exception {
137 return openDoc("simpress", frameName);
138 } // finished createImpressDoc
143 * method which opens a new MathDocument
145 public XComponent createMathDoc(String frameName)
146 throws com.sun.star.uno.Exception {
148 return openDoc("smath", frameName);
149 } // finished createMathDoc
154 * method which opens a new ChartDocument
156 * @see XChartDocument
158 public XChartDocument createChartDoc()
159 throws com.sun.star.uno.Exception {
161 XComponent oDoc = loadDocument("private:factory/schart");
163 if (oDoc != null) {
164 DesktopTools.bringWindowToFront(oDoc);
165 XChartDocument xChartDoc = UnoRuntime.queryInterface(XChartDocument.class, oDoc);
166 // Create a default chart which many chart tests rely on.
167 com.sun.star.chart2.XChartDocument xCD2 =
168 UnoRuntime.queryInterface(com.sun.star.chart2.XChartDocument.class, oDoc);
169 xCD2.createDefaultChart();
170 return xChartDoc;
171 } else {
172 return null;
175 } // finished createChartDoc
178 * creates a simple TextTable defaulted to 2 rows and 2 columns
180 public static XTextTable createTextTable(XTextDocument xTextDoc)
183 TableDsc tDsc = new TableDsc();
184 InstCreator instCreate = new InstCreator(xTextDoc, tDsc);
186 XTextTable oTable = (XTextTable) instCreate.getInstance();
187 return oTable;
191 * creates a TextTable with a specified count of rows and columns
193 public static XTextTable createTextTable(XTextDocument xTextDoc,
194 int rows, int columns)
197 TableDsc tDsc = new TableDsc(rows, columns);
198 InstCreator instCreate = new InstCreator(xTextDoc, tDsc);
200 XTextTable oTable = (XTextTable) instCreate.getInstance();
201 return oTable;
207 * creates a simple TextFrame
208 * ... to be continued
210 public static XTextFrame createTextFrame(XTextDocument xTextDoc,
211 int height, int width) {
213 FrameDsc tDsc = new FrameDsc(height, width);
214 InstCreator instCreate = new InstCreator(xTextDoc, tDsc);
216 XTextFrame oFrame = (XTextFrame) instCreate.getInstance();
217 return oFrame;
222 public static void insertTextContent(XTextDocument xTextDoc,
223 XTextContent xCont)
224 throws com.sun.star.lang.IllegalArgumentException {
225 XText xText = xTextDoc.getText();
226 XText oText = UnoRuntime.queryInterface(
227 XText.class, xText);
229 XTextCursor oCursor = oText.createTextCursor();
230 oText.insertTextContent(oCursor, xCont, false);
233 public static com.sun.star.table.XCell getFirstTableCell(
234 XTextContent oTable) {
236 String CellNames[] = ((XTextTable) oTable).getCellNames();
238 com.sun.star.table.XCell oCell = ((XTextTable) oTable).getCellByName(
239 CellNames[0]);
240 return oCell;
245 * the method createBookmark
247 public static XTextContent createBookmark(XTextDocument xTextDoc)
250 BookmarkDsc tDsc = new BookmarkDsc();
251 InstCreator instCreate = new InstCreator(xTextDoc, tDsc);
253 XTextContent oBookmark = (XTextContent) instCreate.getInstance();
254 return oBookmark;
256 } /// finish createBookmark
263 * the method create Index
265 public static XTextContent createIndex(XTextDocument xTextDoc, String kind)
266 throws com.sun.star.uno.Exception {
268 XMultiServiceFactory oDocMSF = UnoRuntime.queryInterface(XMultiServiceFactory.class,
269 xTextDoc);
271 Object oInt = oDocMSF.createInstance(kind);
273 XTextContent xTC = UnoRuntime.queryInterface(XDocumentIndex.class, oInt);
275 return xTC;
279 public static XSpreadsheet createSpreadsheet(XSpreadsheetDocument oDoc)
280 throws com.sun.star.uno.Exception {
282 XMultiServiceFactory oDocMSF = UnoRuntime.queryInterface(XMultiServiceFactory.class, oDoc);
284 Object oInt = oDocMSF.createInstance(
285 "com.sun.star.sheet.Spreadsheet");
287 XSpreadsheet oSpreadsheet = UnoRuntime.queryInterface(XSpreadsheet.class, oInt);
289 return oSpreadsheet;
292 public static XIndexAccess getTableCollection(XTextDocument oDoc) {
294 XTextTablesSupplier oTTS = UnoRuntime.queryInterface(XTextTablesSupplier.class, oDoc);
296 XNameAccess oNA = oTTS.getTextTables();
297 XIndexAccess oIA = UnoRuntime.queryInterface(XIndexAccess.class, oNA);
299 return oIA;
304 public XShape createShape(XComponent oDoc, int height, int width, int x, int y, String kind) {
305 //possible values for kind are 'Ellipse', 'Line' and 'Rectangle'
307 ShapeDsc sDsc = new ShapeDsc(height, width, x, y, kind);
308 InstCreator instCreate = new InstCreator(oDoc, sDsc);
310 XShape oShape = (XShape) instCreate.getInstance();
312 return oShape;
317 * creates a Diagram as specified in kind
319 public XDiagram createDiagram(XComponent oDoc, String kind) {
320 XInterface oInterface = null;
321 XDiagram oDiagram = null;
323 //get LineDiagram
324 XMultiServiceFactory oDocMSF = UnoRuntime.queryInterface(XMultiServiceFactory.class, oDoc);
326 try {
327 oInterface = (XInterface) oDocMSF.createInstance("com.sun.star.chart." + kind);
328 oDiagram = UnoRuntime.queryInterface(XDiagram.class, oInterface);
329 } catch (Exception e) {
330 // Some exception occurs.FAILED
331 System.out.println("Couldn't create " + kind + "-Diagram " + e);
333 return oDiagram;
337 * creates a control instance as specified in kind
339 public XInterface createControl(XComponent oDoc, String kind) {
341 XInterface oControl = null;
343 XMultiServiceFactory oDocMSF = UnoRuntime.queryInterface(XMultiServiceFactory.class, oDoc);
345 try {
346 oControl = (XInterface) oDocMSF.createInstance("com.sun.star.form.component." + kind);
347 } catch (Exception e) {
348 // Some exception occurs.FAILED
349 System.out.println("Couldn't create instance " + kind + ": " + e);
351 return oControl;
355 * create an Instance as specified in kind
357 public Object createInstance(XComponent oDoc, String kind) {
359 Object oInstance = null;
361 XMultiServiceFactory oDocMSF = UnoRuntime.queryInterface(XMultiServiceFactory.class, oDoc);
363 try {
364 oInstance = oDocMSF.createInstance(kind);
365 } catch (Exception e) {
366 // Some exception occurs.FAILED
367 System.out.println("Couldn't create instance " + kind + ": " + e);
369 return oInstance;
374 public XComponent loadDocument(String fileName)
375 throws com.sun.star.lang.IllegalArgumentException,
376 com.sun.star.io.IOException,
377 com.sun.star.uno.Exception {
379 // that noargs thing for load attributes
380 PropertyValue[] szEmptyArgs = new PropertyValue[0];
381 String frameName = "_blank";
383 XComponent oDoc = oCLoader.loadComponentFromURL(
384 fileName, frameName, 0, szEmptyArgs);
386 if (oDoc == null) {
387 return null;
389 DesktopTools.bringWindowToFront(oDoc);
390 return oDoc;
393 public XComponent loadDocument(String fileName, PropertyValue[] Args)
394 throws com.sun.star.lang.IllegalArgumentException,
395 com.sun.star.io.IOException,
396 com.sun.star.uno.Exception {
398 // that noargs thing for load attributes
399 String frameName = "_blank";
401 XComponent oDoc = oCLoader.loadComponentFromURL(
402 fileName, frameName, 0, Args);
404 if (oDoc == null) {
405 return null;
407 DesktopTools.bringWindowToFront(oDoc);
409 return oDoc;
412 public XComponent openDoc(String kind, String frameName)
413 throws com.sun.star.lang.IllegalArgumentException,
414 com.sun.star.io.IOException,
415 com.sun.star.uno.Exception {
417 // that noargs thing for load attributes
418 PropertyValue[] Args = null;
419 if (kind.equals("simpress")) {
420 Args = new PropertyValue[1];
421 PropertyValue Arg = new PropertyValue();
422 Arg.Name = "OpenFlags";
423 Arg.Value = "S";
424 Arg.Handle = -1;
425 Arg.State = PropertyState.DEFAULT_VALUE;
426 Args[0] = Arg;
427 } else {
428 Args = new PropertyValue[0];
431 if (frameName == null) {
432 frameName = "_blank";
434 // load a blank a doc
435 XComponent oDoc = oCLoader.loadComponentFromURL("private:factory/" + kind, frameName, 40, Args);
436 DesktopTools.bringWindowToFront(oDoc);
438 return oDoc;
440 } // finished openDoc