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.StatusException;
40 import lib.TestCase;
41 import lib.TestEnvironment;
42 import lib.TestParameters;
43 import util.DesktopTools;
44 import util.SOfficeFactory;
45 import util.utils;
46 
47 
48 /**
49  * Test for object which is represented by service
50  * <code>com.sun.star.style.ParagraphStyle</code>. <p>
51  * @see com.sun.star.style.ParagraphStyle
52  */
53 public class ParagraphStyle 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 
createTestEnvironment(TestParameters tParam, PrintWriter log)80     protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
81         TestEnvironment tEnv = null;
82         XNameAccess oSFNA = null;
83         XStyle oStyle = null;
84         XStyle oMyStyle = null;
85 
86         log.println("creating a test environment");
87 
88         try {
89             log.println("getting style");
90             XStyleFamiliesSupplier oSFS = (XStyleFamiliesSupplier)
91                 UnoRuntime.queryInterface(XStyleFamiliesSupplier.class,
92                 xTextDoc);
93             XNameAccess oSF = oSFS.getStyleFamilies();
94             oSFNA = (XNameAccess) UnoRuntime.queryInterface(
95                             XNameAccess.class,oSF.getByName("ParagraphStyles"));
96             XIndexAccess oSFIA = (XIndexAccess)
97                 UnoRuntime.queryInterface(XIndexAccess.class, oSFNA);
98             String[] els = oSFNA.getElementNames();
99             oStyle = (XStyle) UnoRuntime.queryInterface(
100                            XStyle.class,oSFIA.getByIndex(1));
101         } catch ( com.sun.star.lang.WrappedTargetException e ) {
102             log.println("Error: exception occured.");
103             e.printStackTrace(log);
104             throw new StatusException( "Couldn't create environment ", e );
105         } catch ( com.sun.star.lang.IndexOutOfBoundsException e ) {
106             log.println("Error: exception occured.");
107             e.printStackTrace(log);
108             throw new StatusException( "Couldn't create environment ", e );
109         } catch ( com.sun.star.container.NoSuchElementException e ) {
110             log.println("Error: exception occured.");
111             e.printStackTrace(log);
112             throw new StatusException( "Couldn't create environment ", e );
113         }
114 
115         try {
116             log.print("Creating a user-defined style... ");
117             XMultiServiceFactory oMSF = (XMultiServiceFactory)
118                 UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDoc);
119             XInterface oInt = (XInterface)
120                 oMSF.createInstance("com.sun.star.style.ParagraphStyle");
121             oMyStyle = (XStyle) UnoRuntime.queryInterface(XStyle.class, oInt);
122         } catch ( com.sun.star.uno.Exception e ) {
123             log.println("Error: exception occured.");
124             e.printStackTrace(log);
125             throw new StatusException( "Couldn't create environment ", e );
126         }
127 
128 
129         if (oMyStyle == null)
130             log.println("FAILED");
131         else
132             log.println("OK");
133             XNameContainer oSFNC = (XNameContainer)
134             UnoRuntime.queryInterface(XNameContainer.class, oSFNA);
135 
136         try {
137             if ( oSFNC.hasByName("My Style") )
138                 oSFNC.removeByName("My Style");
139             oSFNC.insertByName("My Style", oMyStyle);
140         } catch ( com.sun.star.lang.WrappedTargetException e ) {
141             e.printStackTrace(log);
142             throw new StatusException( "Couldn't create environment ", e );
143         } catch     ( com.sun.star.lang.IllegalArgumentException e ) {
144             e.printStackTrace(log);
145             throw new StatusException( "Couldn't create environment ", e );
146         } catch ( com.sun.star.container.NoSuchElementException e ) {
147             e.printStackTrace(log);
148             throw new StatusException( "Couldn't create environment ", e );
149         } catch ( com.sun.star.container.ElementExistException e ) {
150             e.printStackTrace(log);
151             throw new StatusException( "Couldn't create environment ", e );
152         }
153 
154         XText oText = xTextDoc.getText();
155         XTextCursor oCursor = oText.createTextCursor();
156         XPropertySet xProp = (XPropertySet)
157             UnoRuntime.queryInterface(XPropertySet.class, oCursor);
158 
159         try {
160             xProp.setPropertyValue("ParaStyleName", oMyStyle.getName());
161         } catch ( com.sun.star.lang.WrappedTargetException e ) {
162             e.printStackTrace( log );
163             throw new StatusException( "Couldn't create environment ", e );
164         } catch ( com.sun.star.lang.IllegalArgumentException e ) {
165             e.printStackTrace( log );
166             throw new StatusException( "Couldn't create environment ", e );
167         } catch ( com.sun.star.beans.PropertyVetoException e ) {
168             e.printStackTrace( log );
169             throw new StatusException( "Couldn't create environment ", e );
170         } catch ( com.sun.star.beans.UnknownPropertyException e ) {
171             e.printStackTrace( log );
172             throw new StatusException( "Couldn't create environment ", e );
173         }
174 
175         log.println("creating a new environment for object");
176         tEnv = new TestEnvironment(oMyStyle);
177         tEnv.addObjRelation("PoolStyle", oStyle);
178 
179         XPropertySet xStyleProp = (XPropertySet)
180             UnoRuntime.queryInterface(XPropertySet.class, oMyStyle);
181         short exclude = PropertyAttribute.MAYBEVOID + PropertyAttribute.READONLY;
182         String[] names = utils.getFilteredPropertyNames(xStyleProp, (short)0, exclude);
183         tEnv.addObjRelation("PropertyNames", names);
184 
185         return tEnv;
186     }
187 
188 }
189