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.beans.PropertyAttribute;
30 import com.sun.star.beans.XPropertySet;
31 import com.sun.star.container.XIndexAccess;
32 import com.sun.star.container.XNameAccess;
33 import com.sun.star.container.XNameContainer;
34 import com.sun.star.lang.XMultiServiceFactory;
35 import com.sun.star.style.XStyle;
36 import com.sun.star.style.XStyleFamiliesSupplier;
37 import com.sun.star.text.XText;
38 import com.sun.star.text.XTextCursor;
39 import com.sun.star.text.XTextDocument;
40 import com.sun.star.uno.UnoRuntime;
41 import com.sun.star.uno.XInterface;
42 import java.io.PrintWriter;
43 import lib.Status;
44 import lib.StatusException;
45 import lib.TestCase;
46 import lib.TestEnvironment;
47 import lib.TestParameters;
48 import util.DesktopTools;
49 import util.SOfficeFactory;
50 import util.utils;
51 
52 /**
53  * Test for object which is represented by service
54  * <code>com.sun.star.style.ConditionalParagraphStyle</code>. <p>
55  * @see com.sun.star.style.ConditionalParagraphStyle
56  */
57 public class ConditionalParagraphStyle extends TestCase {
58 
59     XTextDocument xTextDoc;
60     SOfficeFactory SOF = null;
61 
62     /**
63     * Creates text document.
64     */
65     protected void initialize( TestParameters tParam, PrintWriter log ) {
66         SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
67         try {
68             log.println( "creating a textdocument" );
69             xTextDoc = SOF.createTextDoc( null );
70         } catch ( com.sun.star.uno.Exception e ) {
71             e.printStackTrace( log );
72             throw new StatusException( "Couldn't create document", e );
73         }
74     }
75 
76     /**
77     * Disposes text document.
78     */
79     protected void cleanup( TestParameters tParam, PrintWriter log ) {
80         log.println( "    disposing xTextDoc " );
81         DesktopTools.closeDoc(xTextDoc);
82     }
83 
84     /**
85      * Create a new instance of the conditional
86      * paragraph style, and insert it into the document.
87      * @see com.sun.star.style.ConditionalParagraphStyle
88      * @param tParam The test paramters
89      * @param log The log writer.
90      */
91     protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
92         TestEnvironment tEnv = null;
93         XNameAccess oSFNA = null;
94         XStyle oStyle = null;
95         XStyle oMyStyle = null;
96 
97         log.println("creating a test environment");
98 
99         try {
100             log.println("getting style");
101             XStyleFamiliesSupplier oSFS = (XStyleFamiliesSupplier)
102                 UnoRuntime.queryInterface(XStyleFamiliesSupplier.class,
103                 xTextDoc);
104             XNameAccess oSF = oSFS.getStyleFamilies();
105             oSFNA = (XNameAccess) UnoRuntime.queryInterface(
106                         XNameAccess.class,oSF.getByName("ParagraphStyles"));
107             XIndexAccess oSFIA = (XIndexAccess)
108                 UnoRuntime.queryInterface(XIndexAccess.class, oSFNA);
109             oStyle = (XStyle) UnoRuntime.queryInterface(
110                         XStyle.class,oSFIA.getByIndex(1));
111         } catch ( com.sun.star.lang.WrappedTargetException e ) {
112             log.println("Error: exception occured.");
113             e.printStackTrace(log);
114             throw new StatusException( "Couldn't create environment ", e );
115         } catch ( com.sun.star.lang.IndexOutOfBoundsException e ) {
116             log.println("Error: exception occured.");
117             e.printStackTrace(log);
118             throw new StatusException( "Couldn't create environment ", e );
119         } catch ( com.sun.star.container.NoSuchElementException e ) {
120             log.println("Error: exception occured.");
121             e.printStackTrace(log);
122             throw new StatusException( "Couldn't create environment ", e );
123         }
124 
125         try {
126             log.print("Creating a user-defined style... ");
127             XMultiServiceFactory oMSF = (XMultiServiceFactory)
128                 UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDoc);
129             XInterface oInt = (XInterface)
130                 oMSF.createInstance("com.sun.star.style.ConditionalParagraphStyle");
131             oMyStyle = (XStyle) UnoRuntime.queryInterface(XStyle.class, oInt);
132         } catch ( com.sun.star.uno.Exception e ) {
133             log.println("Error: exception occured.");
134             e.printStackTrace(log);
135             throw new StatusException( "Couldn't create environment ", e );
136         }
137 
138 
139         if (oMyStyle == null) {
140             log.println("FAILED");
141             throw new StatusException(Status.failed("Couldn't create an own style "));
142         }
143         else {
144             log.println("OK");
145         }
146 
147         XNameContainer oSFNC = (XNameContainer)
148             UnoRuntime.queryInterface(XNameContainer.class, oSFNA);
149 
150         try {
151             if ( oSFNC.hasByName("My Style") )
152                 oSFNC.removeByName("My Style");
153             oSFNC.insertByName("My Style", oMyStyle);
154         } catch ( com.sun.star.lang.WrappedTargetException e ) {
155             e.printStackTrace(log);
156             throw new StatusException( "Couldn't create environment ", e );
157         } catch     ( com.sun.star.lang.IllegalArgumentException e ) {
158             e.printStackTrace(log);
159             throw new StatusException( "Couldn't create environment ", e );
160         } catch ( com.sun.star.container.NoSuchElementException e ) {
161             e.printStackTrace(log);
162             throw new StatusException( "Couldn't create environment ", e );
163         } catch ( com.sun.star.container.ElementExistException e ) {
164             e.printStackTrace(log);
165             throw new StatusException( "Couldn't create environment ", e );
166         }
167 
168         XText oText = xTextDoc.getText();
169         XTextCursor oCursor = oText.createTextCursor();
170         XPropertySet xProp = (XPropertySet)
171             UnoRuntime.queryInterface(XPropertySet.class, oCursor);
172         try {
173             xProp.setPropertyValue("ParaStyleName", oMyStyle.getName());
174         } catch ( com.sun.star.lang.WrappedTargetException e ) {
175             e.printStackTrace( log );
176             throw new StatusException( "Couldn't create environment ", e );
177         } catch ( com.sun.star.lang.IllegalArgumentException e ) {
178             e.printStackTrace( log );
179             throw new StatusException( "Couldn't create environment ", e );
180         } catch ( com.sun.star.beans.PropertyVetoException e ) {
181             e.printStackTrace( log );
182             throw new StatusException( "Couldn't create environment ", e );
183         } catch ( com.sun.star.beans.UnknownPropertyException e ) {
184             e.printStackTrace( log );
185             throw new StatusException( "Couldn't create environment ", e );
186         }
187 
188         log.println("creating a new environment for object");
189         tEnv = new TestEnvironment(oMyStyle);
190         tEnv.addObjRelation("PoolStyle", oStyle);
191 
192         XPropertySet xStyleProp = (XPropertySet)
193             UnoRuntime.queryInterface(XPropertySet.class, oMyStyle);
194         short exclude = PropertyAttribute.MAYBEVOID + PropertyAttribute.READONLY;
195         String[] names = utils.getFilteredPropertyNames(xStyleProp, (short)0, exclude);
196         tEnv.addObjRelation("PropertyNames", names);
197 
198         return tEnv;
199     }
200 
201 }
202