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 mod._sw;
28 
29 import com.sun.star.container.XIndexAccess;
30 import com.sun.star.container.XNameAccess;
31 import com.sun.star.container.XNamed;
32 import com.sun.star.frame.XController;
33 import com.sun.star.frame.XModel;
34 import com.sun.star.lang.XMultiServiceFactory;
35 import com.sun.star.text.ControlCharacter;
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.text.XTextFrame;
41 import com.sun.star.text.XTextSection;
42 import com.sun.star.text.XTextSectionsSupplier;
43 import com.sun.star.text.XTextTable;
44 import com.sun.star.text.XWordCursor;
45 import com.sun.star.uno.UnoRuntime;
46 import com.sun.star.uno.XInterface;
47 import com.sun.star.view.XSelectionSupplier;
48 
49 import java.io.PrintWriter;
50 
51 import lib.StatusException;
52 import lib.TestCase;
53 import lib.TestEnvironment;
54 import lib.TestParameters;
55 import util.DesktopTools;
56 
57 import util.SOfficeFactory;
58 import util.WriterTools;
59 
60 
61 public class SwXTextDocument extends TestCase {
62     XTextDocument xTextDoc;
63     XTextDocument xSecondTextDoc;
64 
65     protected void initialize(TestParameters tParam, PrintWriter log) {
66     }
67 
68     protected void cleanup(TestParameters tParam, PrintWriter log) {
69         log.println("    disposing xTextDoc ");
70         DesktopTools.closeDoc(xSecondTextDoc);
71         DesktopTools.closeDoc(xTextDoc);
72     }
73 
74     /**
75      *    creating a Testenvironment for the interfaces to be tested
76      */
77     public synchronized TestEnvironment createTestEnvironment(TestParameters Param,
78                                                               PrintWriter log)
79         throws StatusException {
80         TestEnvironment tEnv = null;
81         XTextTable the_table = null;
82 
83         try {
84             log.println("creating a textdocument");
85             xTextDoc = WriterTools.createTextDoc(
86                                (XMultiServiceFactory) Param.getMSF());
87 
88             XText oText = xTextDoc.getText();
89             XTextCursor oCursor = oText.createTextCursor();
90 
91             log.println("inserting some lines");
92 
93             try {
94                 for (int i = 0; i < 5; i++) {
95                     oText.insertString(oCursor, "Paragraph Number: " + i,
96                                        false);
97                     oText.insertString(oCursor,
98                                        " The quick brown fox jumps over the lazy Dog: SwXParagraph",
99                                        false);
100                     oText.insertControlCharacter(oCursor,
101                                                  ControlCharacter.PARAGRAPH_BREAK,
102                                                  false);
103                     oText.insertString(oCursor,
104                                        "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG: SwXParagraph",
105                                        false);
106                     oText.insertControlCharacter(oCursor,
107                                                  ControlCharacter.PARAGRAPH_BREAK,
108                                                  false);
109                     oText.insertControlCharacter(oCursor,
110                                                  ControlCharacter.LINE_BREAK,
111                                                  false);
112                 }
113                             for (int i = 0; i < 11; i++) {
114                 oText.insertString(oCursor, "xTextDoc ", false);
115 
116             }
117             } catch (com.sun.star.lang.IllegalArgumentException e) {
118                 e.printStackTrace(log);
119                 throw new StatusException("Couldn't insert lines", e);
120             }
121 
122             //insert two sections parent and child
123             XMultiServiceFactory oDocMSF = (XMultiServiceFactory) UnoRuntime.queryInterface(
124                                                    XMultiServiceFactory.class,
125                                                    xTextDoc);
126 
127             XInterface oTS;
128             XTextSection xTS;
129 
130             XTextSectionsSupplier oTSSupp = (XTextSectionsSupplier) UnoRuntime.queryInterface(
131                                                     XTextSectionsSupplier.class,
132                                                     xTextDoc);
133             XNameAccess oTSSuppName = oTSSupp.getTextSections();
134 
135             oTS = (XInterface) oDocMSF.createInstance(
136                           "com.sun.star.text.TextSection");
137 
138             XTextContent oTSC = (XTextContent) UnoRuntime.queryInterface(
139                                         XTextContent.class, oTS);
140             oText.insertTextContent(oCursor, oTSC, false);
141 
142             XWordCursor oWordC = (XWordCursor) UnoRuntime.queryInterface(
143                                          XWordCursor.class, oCursor);
144             oCursor.setString("End of TextSection");
145             oCursor.gotoStart(false);
146             oCursor.setString("Start of TextSection ");
147             oWordC.gotoEndOfWord(false);
148 
149             XInterface oTS2 = (XInterface) oDocMSF.createInstance(
150                                       "com.sun.star.text.TextSection");
151             oTSC = (XTextContent) UnoRuntime.queryInterface(XTextContent.class,
152                                                             oTS2);
153             oText.insertTextContent(oCursor, oTSC, false);
154 
155             XIndexAccess oTSSuppIndex = (XIndexAccess) UnoRuntime.queryInterface(
156                                                 XIndexAccess.class,
157                                                 oTSSuppName);
158             log.println(
159                     "getting a TextSection with the XTextSectionSupplier()");
160             xTS = (XTextSection) UnoRuntime.queryInterface(XTextSection.class,
161                                                            oTSSuppIndex.getByIndex(0));
162 
163             XNamed xTSName = (XNamed) UnoRuntime.queryInterface(XNamed.class,
164                                                                 xTS);
165             xTSName.setName("SwXTextSection");
166 
167             log.println("    adding TextTable");
168             the_table = SOfficeFactory.createTextTable(xTextDoc, 6, 4);
169 
170             XNamed the_name = (XNamed) UnoRuntime.queryInterface(XNamed.class,
171                                                                  the_table);
172             the_name.setName("SwXTextDocument");
173             SOfficeFactory.insertTextContent(xTextDoc,
174                                              (XTextContent) the_table);
175 
176             log.println("    adding ReferenceMark");
177 
178             XInterface aMark = (XInterface) oDocMSF.createInstance(
179                                        "com.sun.star.text.ReferenceMark");
180             the_name = (XNamed) UnoRuntime.queryInterface(XNamed.class, aMark);
181             the_name.setName("SwXTextDocument");
182 
183             XTextContent oTC = (XTextContent) UnoRuntime.queryInterface(
184                                        XTextContent.class, aMark);
185             SOfficeFactory.insertTextContent(xTextDoc, (XTextContent) oTC);
186 
187             log.println("    adding TextGraphic");
188             WriterTools.insertTextGraphic(xTextDoc, oDocMSF, 5200, 4200, 4400,
189                                           4000, "space-metal.jpg",
190                                           "SwXTextDocument");
191 
192             log.println("    adding EndNote");
193 
194             XInterface aEndNote = (XInterface) oDocMSF.createInstance(
195                                           "com.sun.star.text.Endnote");
196             oTC = (XTextContent) UnoRuntime.queryInterface(XTextContent.class,
197                                                            aEndNote);
198             SOfficeFactory.insertTextContent(xTextDoc, (XTextContent) oTC);
199 
200             log.println("    adding Bookmark");
201             SOfficeFactory.insertTextContent(xTextDoc,
202                                              (XTextContent) SOfficeFactory.createBookmark(
203                                                      xTextDoc));
204 
205             log.println("    adding DocumentIndex");
206             oTC = SOfficeFactory.createIndex(xTextDoc,
207                                              "com.sun.star.text.DocumentIndex");
208             SOfficeFactory.insertTextContent(xTextDoc, (XTextContent) oTC);
209 
210             log.println("    adding TextFrame");
211 
212             XTextFrame frame = SOfficeFactory.createTextFrame(xTextDoc, 500,
213                                                               500);
214             oTC = (XTextContent) UnoRuntime.queryInterface(XTextContent.class,
215                                                            frame);
216             SOfficeFactory.insertTextContent(xTextDoc, (XTextContent) oTC);
217 
218             log.println("creating a second textdocument");
219             xSecondTextDoc = WriterTools.createTextDoc(
220                                      (XMultiServiceFactory) Param.getMSF());
221         } catch (com.sun.star.uno.Exception e) {
222             // Some exception occures.FAILED
223             e.printStackTrace(log);
224             throw new StatusException("Couldn't create document", e);
225         }
226 
227         if (xTextDoc != null) {
228             log.println("Creating instance...");
229 
230             XText oText = xTextDoc.getText();
231             XTextCursor oTextCursor = oText.createTextCursor();
232 
233             for (int i = 0; i < 11; i++) {
234                 oText.insertString(oTextCursor, "xTextDoc ", false);
235             }
236 
237             tEnv = new TestEnvironment(xTextDoc);
238         } else {
239             log.println("Failed to create instance.");
240 
241             return tEnv;
242         }
243 
244         XModel model1 = (XModel) UnoRuntime.queryInterface(XModel.class,
245                                                            xTextDoc);
246         XModel model2 = (XModel) UnoRuntime.queryInterface(XModel.class,
247                                                            xSecondTextDoc);
248 
249         XController cont1 = model1.getCurrentController();
250         XController cont2 = model2.getCurrentController();
251 
252         cont1.getFrame().setName("cont1");
253         cont2.getFrame().setName("cont2");
254 
255         XSelectionSupplier sel = (XSelectionSupplier) UnoRuntime.queryInterface(
256                                          XSelectionSupplier.class, cont1);
257 
258         log.println("Adding SelectionSupplier and Shape to select for XModel");
259         tEnv.addObjRelation("SELSUPP", sel);
260         tEnv.addObjRelation("TOSELECT", the_table);
261 
262         log.println("adding Controller as ObjRelation for XModel");
263         tEnv.addObjRelation("CONT1", cont1);
264         tEnv.addObjRelation("CONT2", cont2);
265 
266         return tEnv;
267     }
268 } // finish class SwXTextDocument
269