tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / qadevOOo / runner / util / WriterTools.java
blobcdff65557a72f1e6596136ded4cefa4b9f6246ea
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 com.sun.star.beans.PropertyValue;
21 import com.sun.star.beans.XPropertySet;
23 import com.sun.star.container.XNamed;
25 import com.sun.star.drawing.XDrawPage;
26 import com.sun.star.drawing.XDrawPageSupplier;
28 import com.sun.star.lang.XComponent;
29 import com.sun.star.lang.XMultiServiceFactory;
31 import com.sun.star.text.XText;
32 import com.sun.star.text.XTextContent;
33 import com.sun.star.text.XTextCursor;
34 import com.sun.star.text.XTextDocument;
36 import com.sun.star.graphic.XGraphic;
37 import com.sun.star.graphic.XGraphicProvider;
39 import com.sun.star.uno.UnoRuntime;
40 import com.sun.star.uno.XComponentContext;
42 public class WriterTools {
43 public static XTextDocument createTextDoc(XMultiServiceFactory xMSF) {
44 PropertyValue[] Args = new PropertyValue[0];
45 XComponent comp = DesktopTools.openNewDoc(xMSF, "swriter", Args);
46 XTextDocument WriterDoc = UnoRuntime.queryInterface(
47 XTextDocument.class, comp);
49 return WriterDoc;
50 } // finish createTextDoc
52 public static XTextDocument loadTextDoc(XMultiServiceFactory xMSF,
53 String url) {
54 PropertyValue[] Args = new PropertyValue[0];
55 XTextDocument WriterDoc = loadTextDoc(xMSF, url, Args);
57 return WriterDoc;
58 } // finish createTextDoc
60 private static XTextDocument loadTextDoc(XMultiServiceFactory xMSF,
61 String url, PropertyValue[] Args) {
62 XComponent comp = DesktopTools.loadDoc(xMSF, url, Args);
63 XTextDocument WriterDoc = UnoRuntime.queryInterface(
64 XTextDocument.class, comp);
66 return WriterDoc;
67 } // finish createTextDoc
69 public static XDrawPage getDrawPage(XTextDocument aDoc) {
70 XDrawPage oDP = null;
72 try {
73 XDrawPageSupplier oDPS = UnoRuntime.queryInterface(
74 XDrawPageSupplier.class, aDoc);
75 oDP = oDPS.getDrawPage();
76 } catch (Exception e) {
77 throw new IllegalArgumentException("Couldn't get drawpage", e);
80 return oDP;
83 public static void insertTextGraphic(XTextDocument aDoc,
84 XMultiServiceFactory xMSF, XComponentContext xContext, int hpos,
85 int vpos, int width, int height,
86 String pic, String name) {
87 try {
88 Object oGObject = xMSF.createInstance(
89 "com.sun.star.text.GraphicObject");
91 XGraphicProvider xGraphicProvider = UnoRuntime.queryInterface(
92 XGraphicProvider.class,
93 xContext.getServiceManager().createInstanceWithContext(
94 "com.sun.star.graphic.GraphicProvider", xContext));
96 String fullURL = util.utils.getFullTestURL(pic);
98 PropertyValue[] aMediaProps = new PropertyValue[] { new PropertyValue() };
99 aMediaProps[0].Name = "URL";
100 aMediaProps[0].Value = fullURL;
102 XGraphic xGraphic = UnoRuntime.queryInterface(XGraphic.class,
103 xGraphicProvider.queryGraphic(aMediaProps));
105 XText the_text = aDoc.getText();
106 XTextCursor the_cursor = the_text.createTextCursor();
107 XTextContent the_content = UnoRuntime.queryInterface(
108 XTextContent.class, oGObject);
109 the_text.insertTextContent(the_cursor, the_content, true);
111 XPropertySet oProps = UnoRuntime.queryInterface(
112 XPropertySet.class, oGObject);
114 oProps.setPropertyValue("Graphic", xGraphic);
115 oProps.setPropertyValue("HoriOrientPosition", Integer.valueOf(hpos));
116 oProps.setPropertyValue("VertOrientPosition", Integer.valueOf(vpos));
117 oProps.setPropertyValue("Width", Integer.valueOf(width));
118 oProps.setPropertyValue("Height", Integer.valueOf(height));
120 XNamed the_name = UnoRuntime.queryInterface(XNamed.class,
121 oGObject);
122 the_name.setName(name);
123 } catch (Exception ex) {
124 System.out.println("Exception while inserting TextGraphic");
125 ex.printStackTrace();