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.Property;
26 import com.sun.star.beans.PropertyAttribute;
27 import com.sun.star.beans.XPropertySet;
28 import com.sun.star.container.XIndexAccess;
29 import com.sun.star.container.XNameAccess;
30 import com.sun.star.container.XNameContainer;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.style.XStyle;
33 import com.sun.star.style.XStyleFamiliesSupplier;
34 import com.sun.star.text.XText;
35 import com.sun.star.text.XTextCursor;
36 import com.sun.star.text.XTextDocument;
37 import com.sun.star.uno.UnoRuntime;
38 import com.sun.star.uno.XInterface;
39 import java.io.PrintWriter;
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.PageStyle</code>. <p>
51  * @see com.sun.star.style.PageStyle
52  */
53 public class PageStyle 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("PageStyles"));    // get the page style
96             XIndexAccess oSFIA = (XIndexAccess)
97                 UnoRuntime.queryInterface(XIndexAccess.class, oSFNA);
98             oStyle = (XStyle) UnoRuntime.queryInterface(
99                         XStyle.class,oSFIA.getByIndex(0));
100             log.println("Chosen pool style: "+oStyle.getName());
101 
102         } catch ( com.sun.star.lang.WrappedTargetException e ) {
103             log.println("Error: exception occurred.");
104             e.printStackTrace(log);
105             throw new StatusException( "Couldn't create environment ", e );
106         } catch ( com.sun.star.lang.IndexOutOfBoundsException e ) {
107             log.println("Error: exception occurred.");
108             e.printStackTrace(log);
109             throw new StatusException( "Couldn't create environment ", e );
110         } catch ( com.sun.star.container.NoSuchElementException e ) {
111             log.println("Error: exception occurred.");
112             e.printStackTrace(log);
113             throw new StatusException( "Couldn't create environment ", e );
114         }
115 
116         try {
117             log.print("Creating a user-defined style... ");
118             XMultiServiceFactory oMSF = (XMultiServiceFactory)
119                 UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDoc);
120             XInterface oInt = (XInterface)
121                 oMSF.createInstance("com.sun.star.style.PageStyle");
122 //                oMSF.createInstanceWithArguments("com.sun.star.style.PageStyle",new Object[]{oStyle});
123             oMyStyle = (XStyle) UnoRuntime.queryInterface(XStyle.class, oInt);
124         } catch ( com.sun.star.uno.Exception e ) {
125             log.println("Error: exception occurred.");
126             e.printStackTrace(log);
127             throw new StatusException( "Couldn't create environment ", e );
128         }
129 
130 
131         if (oMyStyle == null)
132             log.println("FAILED");
133         else
134             log.println("OK");
135 
136         XNameContainer oSFNC = (XNameContainer)
137             UnoRuntime.queryInterface(XNameContainer.class, oSFNA);
138 
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         Property[] props = xProp.getPropertySetInfo().getProperties();
163         for (int i=0; i<props.length; i++)
164             System.out.println("# Property: " + props[i].Name + "    val: " + props[i].Type.toString() + "   attr: " + props[i].Attributes);
165         try {
166             xProp.setPropertyValue("PageDescName", oMyStyle.getName());
167         } catch ( com.sun.star.lang.WrappedTargetException e ) {
168             e.printStackTrace( log );
169             throw new StatusException( "Couldn't create environment ", e );
170         } catch ( com.sun.star.lang.IllegalArgumentException e ) {
171             e.printStackTrace( log );
172             throw new StatusException( "Couldn't create environment ", e );
173         } catch ( com.sun.star.beans.PropertyVetoException e ) {
174             e.printStackTrace( log );
175             throw new StatusException( "Couldn't create environment ", e );
176         } catch ( com.sun.star.beans.UnknownPropertyException e ) {
177             e.printStackTrace( log );
178             throw new StatusException( "Couldn't create environment ", e );
179         }
180 
181 //        oMyStyle = oStyle;
182         log.println("creating a new environment for object");
183         tEnv = new TestEnvironment(oMyStyle);
184         tEnv.addObjRelation("PoolStyle", oStyle);
185 
186         tEnv.addObjRelation("FollowStyle", "Envelope");
187         XPropertySet xStyleProp = (XPropertySet)
188             UnoRuntime.queryInterface(XPropertySet.class, oMyStyle);
189 
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