1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package util;
24 
25 import com.sun.star.beans.PropertyValue;
26 import com.sun.star.beans.XPropertySet;
27 import com.sun.star.container.XNamed;
28 import com.sun.star.drawing.XDrawPage;
29 import com.sun.star.drawing.XDrawPageSupplier;
30 import com.sun.star.lang.XComponent;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.text.XText;
33 import com.sun.star.text.XTextContent;
34 import com.sun.star.text.XTextCursor;
35 import com.sun.star.text.XTextDocument;
36 import com.sun.star.uno.UnoRuntime;
37 
38 // access the implementations via names
39 import com.sun.star.uno.XInterface;
40 
41 import util.DesktopTools;
42 
43 
44 public class WriterTools {
createTextDoc(XMultiServiceFactory xMSF)45     public static XTextDocument createTextDoc(XMultiServiceFactory xMSF) {
46         PropertyValue[] Args = new PropertyValue[0];
47         XComponent comp = DesktopTools.openNewDoc(xMSF, "swriter", Args);
48         XTextDocument WriterDoc = (XTextDocument) UnoRuntime.queryInterface(
49                                           XTextDocument.class, comp);
50 
51         return WriterDoc;
52     } // finish createTextDoc
53 
loadTextDoc(XMultiServiceFactory xMSF, String url)54     public static XTextDocument loadTextDoc(XMultiServiceFactory xMSF,
55                                             String url) {
56         PropertyValue[] Args = new PropertyValue[0];
57         XTextDocument WriterDoc = loadTextDoc(xMSF, url, Args);
58 
59         return WriterDoc;
60     } // finish createTextDoc
61 
loadTextDoc(XMultiServiceFactory xMSF, String url, PropertyValue[] Args)62     public static XTextDocument loadTextDoc(XMultiServiceFactory xMSF,
63                                             String url, PropertyValue[] Args) {
64         XComponent comp = DesktopTools.loadDoc(xMSF, url, Args);
65         XTextDocument WriterDoc = (XTextDocument) UnoRuntime.queryInterface(
66                                           XTextDocument.class, comp);
67 
68         return WriterDoc;
69     } // finish createTextDoc
70 
getDrawPage(XTextDocument aDoc)71     public static XDrawPage getDrawPage(XTextDocument aDoc) {
72         XDrawPage oDP = null;
73 
74         try {
75             XDrawPageSupplier oDPS = (XDrawPageSupplier) UnoRuntime.queryInterface(
76                                              XDrawPageSupplier.class, aDoc);
77             oDP = (XDrawPage) oDPS.getDrawPage();
78         } catch (Exception e) {
79             throw new IllegalArgumentException("Couldn't get drawpage");
80         }
81 
82         return oDP;
83     }
84 
insertTextGraphic(XTextDocument aDoc, XMultiServiceFactory xMSF, int hpos, int vpos, int width, int height, String pic, String name)85     public static void insertTextGraphic(XTextDocument aDoc,
86                                          XMultiServiceFactory xMSF, int hpos,
87                                          int vpos, int width, int height,
88                                          String pic, String name) {
89         try {
90             Object oGObject = (XInterface) xMSF.createInstance(
91                                       "com.sun.star.text.GraphicObject");
92 
93             XText the_text = aDoc.getText();
94             XTextCursor the_cursor = the_text.createTextCursor();
95             XTextContent the_content = (XTextContent) UnoRuntime.queryInterface(
96                                                XTextContent.class, oGObject);
97             the_text.insertTextContent(the_cursor, the_content, true);
98 
99             XPropertySet oProps = (XPropertySet) UnoRuntime.queryInterface(
100                                           XPropertySet.class, oGObject);
101 
102             String fullURL = util.utils.getFullTestURL(pic);
103             oProps.setPropertyValue("GraphicURL", fullURL);
104             oProps.setPropertyValue("HoriOrientPosition", new Integer(hpos));
105             oProps.setPropertyValue("VertOrientPosition", new Integer(vpos));
106             oProps.setPropertyValue("Width", new Integer(width));
107             oProps.setPropertyValue("Height", new Integer(height));
108 
109             XNamed the_name = (XNamed) UnoRuntime.queryInterface(XNamed.class,
110                                                                  oGObject);
111             the_name.setName(name);
112         } catch (Exception ex) {
113             System.out.println("Exception while insertin TextGraphic");
114             ex.printStackTrace();
115         }
116     }
117 }