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