1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package util;
28 
29 import com.sun.star.beans.PropertyValue;
30 import com.sun.star.beans.XPropertySet;
31 import com.sun.star.container.XNamed;
32 import com.sun.star.drawing.XDrawPage;
33 import com.sun.star.drawing.XDrawPageSupplier;
34 import com.sun.star.lang.XComponent;
35 import com.sun.star.lang.XMultiServiceFactory;
36 import com.sun.star.text.XText;
37 import com.sun.star.text.XTextContent;
38 import com.sun.star.text.XTextCursor;
39 import com.sun.star.text.XTextDocument;
40 import com.sun.star.uno.UnoRuntime;
41 
42 // access the implementations via names
43 import com.sun.star.uno.XInterface;
44 
45 import util.DesktopTools;
46 
47 
48 public class WriterTools {
49     public static XTextDocument createTextDoc(XMultiServiceFactory xMSF) {
50         PropertyValue[] Args = new PropertyValue[0];
51         XComponent comp = DesktopTools.openNewDoc(xMSF, "swriter", Args);
52         XTextDocument WriterDoc = (XTextDocument) UnoRuntime.queryInterface(
53                                           XTextDocument.class, comp);
54 
55         return WriterDoc;
56     } // finish createTextDoc
57 
58     public static XTextDocument loadTextDoc(XMultiServiceFactory xMSF,
59                                             String url) {
60         PropertyValue[] Args = new PropertyValue[0];
61         XTextDocument WriterDoc = loadTextDoc(xMSF, url, Args);
62 
63         return WriterDoc;
64     } // finish createTextDoc
65 
66     public static XTextDocument loadTextDoc(XMultiServiceFactory xMSF,
67                                             String url, PropertyValue[] Args) {
68         XComponent comp = DesktopTools.loadDoc(xMSF, url, Args);
69         XTextDocument WriterDoc = (XTextDocument) UnoRuntime.queryInterface(
70                                           XTextDocument.class, comp);
71 
72         return WriterDoc;
73     } // finish createTextDoc
74 
75     public static XDrawPage getDrawPage(XTextDocument aDoc) {
76         XDrawPage oDP = null;
77 
78         try {
79             XDrawPageSupplier oDPS = (XDrawPageSupplier) UnoRuntime.queryInterface(
80                                              XDrawPageSupplier.class, aDoc);
81             oDP = (XDrawPage) oDPS.getDrawPage();
82         } catch (Exception e) {
83             throw new IllegalArgumentException("Couldn't get drawpage");
84         }
85 
86         return oDP;
87     }
88 
89     public static void insertTextGraphic(XTextDocument aDoc,
90                                          XMultiServiceFactory xMSF, int hpos,
91                                          int vpos, int width, int height,
92                                          String pic, String name) {
93         try {
94             Object oGObject = (XInterface) xMSF.createInstance(
95                                       "com.sun.star.text.GraphicObject");
96 
97             XText the_text = aDoc.getText();
98             XTextCursor the_cursor = the_text.createTextCursor();
99             XTextContent the_content = (XTextContent) UnoRuntime.queryInterface(
100                                                XTextContent.class, oGObject);
101             the_text.insertTextContent(the_cursor, the_content, true);
102 
103             XPropertySet oProps = (XPropertySet) UnoRuntime.queryInterface(
104                                           XPropertySet.class, oGObject);
105 
106             String fullURL = util.utils.getFullTestURL(pic);
107             oProps.setPropertyValue("GraphicURL", fullURL);
108             oProps.setPropertyValue("HoriOrientPosition", new Integer(hpos));
109             oProps.setPropertyValue("VertOrientPosition", new Integer(vpos));
110             oProps.setPropertyValue("Width", new Integer(width));
111             oProps.setPropertyValue("Height", new Integer(height));
112 
113             XNamed the_name = (XNamed) UnoRuntime.queryInterface(XNamed.class,
114                                                                  oGObject);
115             the_name.setName(name);
116         } catch (Exception ex) {
117             System.out.println("Exception while insertin TextGraphic");
118             ex.printStackTrace();
119         }
120     }
121 }