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