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