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 
28 package mod._sw;
29 
30 import java.io.PrintWriter;
31 
32 import lib.StatusException;
33 import lib.TestCase;
34 import lib.TestEnvironment;
35 import lib.TestParameters;
36 import util.InstCreator;
37 import util.SOfficeFactory;
38 import util.TableDsc;
39 
40 import com.sun.star.container.XIndexAccess;
41 import com.sun.star.container.XNameAccess;
42 import com.sun.star.container.XNamed;
43 import com.sun.star.lang.XComponent;
44 import com.sun.star.lang.XMultiServiceFactory;
45 import com.sun.star.text.XText;
46 import com.sun.star.text.XTextColumns;
47 import com.sun.star.text.XTextContent;
48 import com.sun.star.text.XTextCursor;
49 import com.sun.star.text.XTextDocument;
50 import com.sun.star.text.XTextSection;
51 import com.sun.star.text.XTextSectionsSupplier;
52 import com.sun.star.text.XWordCursor;
53 import com.sun.star.uno.AnyConverter;
54 import com.sun.star.uno.Type;
55 import com.sun.star.uno.UnoRuntime;
56 import com.sun.star.uno.XInterface;
57 
58 /**
59  *
60  * initial description
61  * @see com.sun.star.text.XText
62  *
63  */
64 
65 public class SwXTextSection extends TestCase {
66     XTextDocument xTextDoc;
67 
68     protected void initialize( TestParameters tParam, PrintWriter log ) {
69     SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
70 
71     try {
72         log.println( "creating a textdocument" );
73         xTextDoc = SOF.createTextDoc( null );
74     } catch ( com.sun.star.uno.Exception e ) {
75         // Some exception occures.FAILED
76         e.printStackTrace( log );
77         throw new StatusException( "Couldn't create document", e );
78     }
79     }
80 
81     protected void cleanup( TestParameters tParam, PrintWriter log ) {
82     log.println( "    disposing xTextDoc " );
83     util.DesktopTools.closeDoc(xTextDoc);
84     }
85 
86     /**
87      *    creating a Testenvironment for the interfaces to be tested
88      */
89     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
90 
91         XInterface oObj = null;
92         XInterface oTS = null;
93         XTextSection xTS = null;
94         XText oText = null;
95         XTextColumns TC = null;
96         Object instance = null;
97 
98         log.println( "creating a test environment" );
99 
100         oText = xTextDoc.getText();
101             XTextCursor oCursor = oText.createTextCursor();
102 
103         XMultiServiceFactory oDocMSF = (XMultiServiceFactory)
104             UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDoc);
105 
106         try {
107             XTextSectionsSupplier oTSSupp = (XTextSectionsSupplier)
108             UnoRuntime.queryInterface
109                     ( XTextSectionsSupplier.class, xTextDoc );
110             XNameAccess oTSSuppName = oTSSupp.getTextSections();
111 
112             //cleanup if necessary
113             if (oTSSuppName.hasByName("SwXTextSection")) {
114                 XTextSection old = (XTextSection) AnyConverter.toObject(
115                     new Type(XTextSection.class),
116                         oTSSuppName.getByName("SwXTextSection"));
117                 XComponent oldC = (XComponent)
118                         UnoRuntime.queryInterface(XComponent.class,old);
119                 oldC.dispose();
120                 oText.setString("");
121             }
122 
123             //insert two sections parent and child
124             oTS = (XInterface) oDocMSF.createInstance
125                     ("com.sun.star.text.TextSection");
126             instance = oDocMSF.createInstance("com.sun.star.text.TextSection");
127             XTextContent oTSC = (XTextContent)
128                     UnoRuntime.queryInterface(XTextContent.class, oTS);
129             oText.insertTextContent(oCursor, oTSC, false);
130             XWordCursor oWordC = (XWordCursor)
131                 UnoRuntime.queryInterface(XWordCursor.class, oCursor);
132             oCursor.setString("End of TextSection");
133             oCursor.gotoStart(false);
134             oCursor.setString("Start of TextSection ");
135             oWordC.gotoEndOfWord(false);
136             XInterface oTS2 = (XInterface) oDocMSF.createInstance
137                 ("com.sun.star.text.TextSection");
138             oTSC = (XTextContent)UnoRuntime.queryInterface(XTextContent.class, oTS2);
139             oText.insertTextContent(oCursor, oTSC, false);
140 
141             XIndexAccess oTSSuppIndex = (XIndexAccess)
142             UnoRuntime.queryInterface(XIndexAccess.class, oTSSuppName);
143 
144             log.println( "getting a TextSection with the XTextSectionSupplier()" );
145             xTS = (XTextSection) AnyConverter.toObject(
146                         new Type(XTextSection.class),oTSSuppIndex.getByIndex(0));
147             XNamed xTSName = (XNamed)
148                     UnoRuntime.queryInterface( XNamed.class, xTS);
149             xTSName.setName("SwXTextSection");
150         }
151         catch(Exception e){
152             System.out.println("Couldn't get Textsection " + e);
153         }
154 
155         oObj = xTS;
156 
157         log.println( "creating a new environment for TextSection object" );
158         TestEnvironment tEnv = new TestEnvironment( oObj );
159 
160         log.println( "adding InstDescriptor object" );
161         TableDsc tDsc = new TableDsc( 6, 4 );
162 
163         log.println( "adding InstCreator object" );
164         tEnv.addObjRelation( "XTEXTINFO", new InstCreator( xTextDoc, tDsc ) );
165 
166         tEnv.addObjRelation("TRO",new Boolean(true));
167 
168         try {
169             TC = (XTextColumns) UnoRuntime.queryInterface(XTextColumns.class,
170                     oDocMSF.createInstance("com.sun.star.text.TextColumns"));
171         } catch ( com.sun.star.uno.Exception e ) {
172             e.printStackTrace(log);
173             throw new StatusException
174                     ("Couldn't create instance of service TextColumns", e );
175         }
176         tEnv.addObjRelation("TC",TC);
177 
178         tEnv.addObjRelation("CONTENT", (XTextContent)
179                         UnoRuntime.queryInterface(XTextContent.class,instance));
180         tEnv.addObjRelation("RANGE", xTextDoc.getText().createTextCursor());
181 
182         return tEnv;
183     } // finish method getTestEnvironment
184 
185 }// finish class SwXTextSection
186 
187 
188