1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir package mod._sw;
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir import com.sun.star.container.XIndexAccess;
30*cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
31*cdf0e10cSrcweir import com.sun.star.container.XNamed;
32*cdf0e10cSrcweir import com.sun.star.frame.XController;
33*cdf0e10cSrcweir import com.sun.star.frame.XModel;
34*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
35*cdf0e10cSrcweir import com.sun.star.text.ControlCharacter;
36*cdf0e10cSrcweir import com.sun.star.text.XText;
37*cdf0e10cSrcweir import com.sun.star.text.XTextContent;
38*cdf0e10cSrcweir import com.sun.star.text.XTextCursor;
39*cdf0e10cSrcweir import com.sun.star.text.XTextDocument;
40*cdf0e10cSrcweir import com.sun.star.text.XTextFrame;
41*cdf0e10cSrcweir import com.sun.star.text.XTextSection;
42*cdf0e10cSrcweir import com.sun.star.text.XTextSectionsSupplier;
43*cdf0e10cSrcweir import com.sun.star.text.XTextTable;
44*cdf0e10cSrcweir import com.sun.star.text.XWordCursor;
45*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
46*cdf0e10cSrcweir import com.sun.star.uno.XInterface;
47*cdf0e10cSrcweir import com.sun.star.view.XSelectionSupplier;
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir import java.io.PrintWriter;
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir import lib.StatusException;
52*cdf0e10cSrcweir import lib.TestCase;
53*cdf0e10cSrcweir import lib.TestEnvironment;
54*cdf0e10cSrcweir import lib.TestParameters;
55*cdf0e10cSrcweir import util.DesktopTools;
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir import util.SOfficeFactory;
58*cdf0e10cSrcweir import util.WriterTools;
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir public class SwXTextDocument extends TestCase {
62*cdf0e10cSrcweir     XTextDocument xTextDoc;
63*cdf0e10cSrcweir     XTextDocument xSecondTextDoc;
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir     protected void initialize(TestParameters tParam, PrintWriter log) {
66*cdf0e10cSrcweir     }
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir     protected void cleanup(TestParameters tParam, PrintWriter log) {
69*cdf0e10cSrcweir         log.println("    disposing xTextDoc ");
70*cdf0e10cSrcweir         DesktopTools.closeDoc(xSecondTextDoc);
71*cdf0e10cSrcweir         DesktopTools.closeDoc(xTextDoc);
72*cdf0e10cSrcweir     }
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir     /**
75*cdf0e10cSrcweir      *    creating a Testenvironment for the interfaces to be tested
76*cdf0e10cSrcweir      */
77*cdf0e10cSrcweir     public synchronized TestEnvironment createTestEnvironment(TestParameters Param,
78*cdf0e10cSrcweir                                                               PrintWriter log)
79*cdf0e10cSrcweir         throws StatusException {
80*cdf0e10cSrcweir         TestEnvironment tEnv = null;
81*cdf0e10cSrcweir         XTextTable the_table = null;
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir         try {
84*cdf0e10cSrcweir             log.println("creating a textdocument");
85*cdf0e10cSrcweir             xTextDoc = WriterTools.createTextDoc(
86*cdf0e10cSrcweir                                (XMultiServiceFactory) Param.getMSF());
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir             XText oText = xTextDoc.getText();
89*cdf0e10cSrcweir             XTextCursor oCursor = oText.createTextCursor();
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir             log.println("inserting some lines");
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir             try {
94*cdf0e10cSrcweir                 for (int i = 0; i < 5; i++) {
95*cdf0e10cSrcweir                     oText.insertString(oCursor, "Paragraph Number: " + i,
96*cdf0e10cSrcweir                                        false);
97*cdf0e10cSrcweir                     oText.insertString(oCursor,
98*cdf0e10cSrcweir                                        " The quick brown fox jumps over the lazy Dog: SwXParagraph",
99*cdf0e10cSrcweir                                        false);
100*cdf0e10cSrcweir                     oText.insertControlCharacter(oCursor,
101*cdf0e10cSrcweir                                                  ControlCharacter.PARAGRAPH_BREAK,
102*cdf0e10cSrcweir                                                  false);
103*cdf0e10cSrcweir                     oText.insertString(oCursor,
104*cdf0e10cSrcweir                                        "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG: SwXParagraph",
105*cdf0e10cSrcweir                                        false);
106*cdf0e10cSrcweir                     oText.insertControlCharacter(oCursor,
107*cdf0e10cSrcweir                                                  ControlCharacter.PARAGRAPH_BREAK,
108*cdf0e10cSrcweir                                                  false);
109*cdf0e10cSrcweir                     oText.insertControlCharacter(oCursor,
110*cdf0e10cSrcweir                                                  ControlCharacter.LINE_BREAK,
111*cdf0e10cSrcweir                                                  false);
112*cdf0e10cSrcweir                 }
113*cdf0e10cSrcweir                             for (int i = 0; i < 11; i++) {
114*cdf0e10cSrcweir                 oText.insertString(oCursor, "xTextDoc ", false);
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir             }
117*cdf0e10cSrcweir             } catch (com.sun.star.lang.IllegalArgumentException e) {
118*cdf0e10cSrcweir                 e.printStackTrace(log);
119*cdf0e10cSrcweir                 throw new StatusException("Couldn't insert lines", e);
120*cdf0e10cSrcweir             }
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir             //insert two sections parent and child
123*cdf0e10cSrcweir             XMultiServiceFactory oDocMSF = (XMultiServiceFactory) UnoRuntime.queryInterface(
124*cdf0e10cSrcweir                                                    XMultiServiceFactory.class,
125*cdf0e10cSrcweir                                                    xTextDoc);
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir             XInterface oTS;
128*cdf0e10cSrcweir             XTextSection xTS;
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir             XTextSectionsSupplier oTSSupp = (XTextSectionsSupplier) UnoRuntime.queryInterface(
131*cdf0e10cSrcweir                                                     XTextSectionsSupplier.class,
132*cdf0e10cSrcweir                                                     xTextDoc);
133*cdf0e10cSrcweir             XNameAccess oTSSuppName = oTSSupp.getTextSections();
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir             oTS = (XInterface) oDocMSF.createInstance(
136*cdf0e10cSrcweir                           "com.sun.star.text.TextSection");
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir             XTextContent oTSC = (XTextContent) UnoRuntime.queryInterface(
139*cdf0e10cSrcweir                                         XTextContent.class, oTS);
140*cdf0e10cSrcweir             oText.insertTextContent(oCursor, oTSC, false);
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir             XWordCursor oWordC = (XWordCursor) UnoRuntime.queryInterface(
143*cdf0e10cSrcweir                                          XWordCursor.class, oCursor);
144*cdf0e10cSrcweir             oCursor.setString("End of TextSection");
145*cdf0e10cSrcweir             oCursor.gotoStart(false);
146*cdf0e10cSrcweir             oCursor.setString("Start of TextSection ");
147*cdf0e10cSrcweir             oWordC.gotoEndOfWord(false);
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir             XInterface oTS2 = (XInterface) oDocMSF.createInstance(
150*cdf0e10cSrcweir                                       "com.sun.star.text.TextSection");
151*cdf0e10cSrcweir             oTSC = (XTextContent) UnoRuntime.queryInterface(XTextContent.class,
152*cdf0e10cSrcweir                                                             oTS2);
153*cdf0e10cSrcweir             oText.insertTextContent(oCursor, oTSC, false);
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir             XIndexAccess oTSSuppIndex = (XIndexAccess) UnoRuntime.queryInterface(
156*cdf0e10cSrcweir                                                 XIndexAccess.class,
157*cdf0e10cSrcweir                                                 oTSSuppName);
158*cdf0e10cSrcweir             log.println(
159*cdf0e10cSrcweir                     "getting a TextSection with the XTextSectionSupplier()");
160*cdf0e10cSrcweir             xTS = (XTextSection) UnoRuntime.queryInterface(XTextSection.class,
161*cdf0e10cSrcweir                                                            oTSSuppIndex.getByIndex(0));
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir             XNamed xTSName = (XNamed) UnoRuntime.queryInterface(XNamed.class,
164*cdf0e10cSrcweir                                                                 xTS);
165*cdf0e10cSrcweir             xTSName.setName("SwXTextSection");
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir             log.println("    adding TextTable");
168*cdf0e10cSrcweir             the_table = SOfficeFactory.createTextTable(xTextDoc, 6, 4);
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir             XNamed the_name = (XNamed) UnoRuntime.queryInterface(XNamed.class,
171*cdf0e10cSrcweir                                                                  the_table);
172*cdf0e10cSrcweir             the_name.setName("SwXTextDocument");
173*cdf0e10cSrcweir             SOfficeFactory.insertTextContent(xTextDoc,
174*cdf0e10cSrcweir                                              (XTextContent) the_table);
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir             log.println("    adding ReferenceMark");
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir             XInterface aMark = (XInterface) oDocMSF.createInstance(
179*cdf0e10cSrcweir                                        "com.sun.star.text.ReferenceMark");
180*cdf0e10cSrcweir             the_name = (XNamed) UnoRuntime.queryInterface(XNamed.class, aMark);
181*cdf0e10cSrcweir             the_name.setName("SwXTextDocument");
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir             XTextContent oTC = (XTextContent) UnoRuntime.queryInterface(
184*cdf0e10cSrcweir                                        XTextContent.class, aMark);
185*cdf0e10cSrcweir             SOfficeFactory.insertTextContent(xTextDoc, (XTextContent) oTC);
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir             log.println("    adding TextGraphic");
188*cdf0e10cSrcweir             WriterTools.insertTextGraphic(xTextDoc, oDocMSF, 5200, 4200, 4400,
189*cdf0e10cSrcweir                                           4000, "space-metal.jpg",
190*cdf0e10cSrcweir                                           "SwXTextDocument");
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir             log.println("    adding EndNote");
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir             XInterface aEndNote = (XInterface) oDocMSF.createInstance(
195*cdf0e10cSrcweir                                           "com.sun.star.text.Endnote");
196*cdf0e10cSrcweir             oTC = (XTextContent) UnoRuntime.queryInterface(XTextContent.class,
197*cdf0e10cSrcweir                                                            aEndNote);
198*cdf0e10cSrcweir             SOfficeFactory.insertTextContent(xTextDoc, (XTextContent) oTC);
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir             log.println("    adding Bookmark");
201*cdf0e10cSrcweir             SOfficeFactory.insertTextContent(xTextDoc,
202*cdf0e10cSrcweir                                              (XTextContent) SOfficeFactory.createBookmark(
203*cdf0e10cSrcweir                                                      xTextDoc));
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir             log.println("    adding DocumentIndex");
206*cdf0e10cSrcweir             oTC = SOfficeFactory.createIndex(xTextDoc,
207*cdf0e10cSrcweir                                              "com.sun.star.text.DocumentIndex");
208*cdf0e10cSrcweir             SOfficeFactory.insertTextContent(xTextDoc, (XTextContent) oTC);
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir             log.println("    adding TextFrame");
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir             XTextFrame frame = SOfficeFactory.createTextFrame(xTextDoc, 500,
213*cdf0e10cSrcweir                                                               500);
214*cdf0e10cSrcweir             oTC = (XTextContent) UnoRuntime.queryInterface(XTextContent.class,
215*cdf0e10cSrcweir                                                            frame);
216*cdf0e10cSrcweir             SOfficeFactory.insertTextContent(xTextDoc, (XTextContent) oTC);
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir             log.println("creating a second textdocument");
219*cdf0e10cSrcweir             xSecondTextDoc = WriterTools.createTextDoc(
220*cdf0e10cSrcweir                                      (XMultiServiceFactory) Param.getMSF());
221*cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
222*cdf0e10cSrcweir             // Some exception occures.FAILED
223*cdf0e10cSrcweir             e.printStackTrace(log);
224*cdf0e10cSrcweir             throw new StatusException("Couldn't create document", e);
225*cdf0e10cSrcweir         }
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir         if (xTextDoc != null) {
228*cdf0e10cSrcweir             log.println("Creating instance...");
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir             XText oText = xTextDoc.getText();
231*cdf0e10cSrcweir             XTextCursor oTextCursor = oText.createTextCursor();
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir             for (int i = 0; i < 11; i++) {
234*cdf0e10cSrcweir                 oText.insertString(oTextCursor, "xTextDoc ", false);
235*cdf0e10cSrcweir             }
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir             tEnv = new TestEnvironment(xTextDoc);
238*cdf0e10cSrcweir         } else {
239*cdf0e10cSrcweir             log.println("Failed to create instance.");
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir             return tEnv;
242*cdf0e10cSrcweir         }
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir         XModel model1 = (XModel) UnoRuntime.queryInterface(XModel.class,
245*cdf0e10cSrcweir                                                            xTextDoc);
246*cdf0e10cSrcweir         XModel model2 = (XModel) UnoRuntime.queryInterface(XModel.class,
247*cdf0e10cSrcweir                                                            xSecondTextDoc);
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir         XController cont1 = model1.getCurrentController();
250*cdf0e10cSrcweir         XController cont2 = model2.getCurrentController();
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir         cont1.getFrame().setName("cont1");
253*cdf0e10cSrcweir         cont2.getFrame().setName("cont2");
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir         XSelectionSupplier sel = (XSelectionSupplier) UnoRuntime.queryInterface(
256*cdf0e10cSrcweir                                          XSelectionSupplier.class, cont1);
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir         log.println("Adding SelectionSupplier and Shape to select for XModel");
259*cdf0e10cSrcweir         tEnv.addObjRelation("SELSUPP", sel);
260*cdf0e10cSrcweir         tEnv.addObjRelation("TOSELECT", the_table);
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir         log.println("adding Controller as ObjRelation for XModel");
263*cdf0e10cSrcweir         tEnv.addObjRelation("CONT1", cont1);
264*cdf0e10cSrcweir         tEnv.addObjRelation("CONT2", cont2);
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir         return tEnv;
267*cdf0e10cSrcweir     }
268*cdf0e10cSrcweir } // finish class SwXTextDocument
269