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 
24 package mod._sc;
25 
26 import java.io.PrintWriter;
27 import java.util.Vector;
28 
29 import lib.StatusException;
30 import lib.TestCase;
31 import lib.TestEnvironment;
32 import lib.TestParameters;
33 import util.SOfficeFactory;
34 
35 import com.sun.star.beans.Property;
36 import com.sun.star.beans.PropertyAttribute;
37 import com.sun.star.beans.XPropertySet;
38 import com.sun.star.container.XIndexAccess;
39 import com.sun.star.container.XNameAccess;
40 import com.sun.star.container.XNameContainer;
41 import com.sun.star.lang.XComponent;
42 import com.sun.star.lang.XMultiServiceFactory;
43 import com.sun.star.sheet.XSpreadsheet;
44 import com.sun.star.sheet.XSpreadsheetDocument;
45 import com.sun.star.sheet.XSpreadsheets;
46 import com.sun.star.style.XStyle;
47 import com.sun.star.style.XStyleFamiliesSupplier;
48 import com.sun.star.table.XCell;
49 import com.sun.star.uno.AnyConverter;
50 import com.sun.star.uno.Type;
51 import com.sun.star.uno.UnoRuntime;
52 import com.sun.star.uno.XInterface;
53 
54 /**
55 * Test for object which is represented by service
56 * <code>com.sun.star.style.Style</code>. <p>
57 * Object implements the following interfaces :
58 * <ul>
59 *  <li> <code>com::sun::star::container::XNamed</code></li>
60 *  <li> <code>com::sun::star::style::Style</code></li>
61 *  <li> <code>com::sun::star::style::XStyle</code></li>
62 * </ul>
63 * @see com.sun.star.style.Style
64 * @see com.sun.star.container.XNamed
65 * @see com.sun.star.style.Style
66 * @see com.sun.star.style.XStyle
67 * @see ifc.container._XNamed
68 * @see ifc.style._Style
69 * @see ifc.style._XStyle
70 */
71 public class ScStyleObj extends TestCase {
72     static XSpreadsheetDocument xSpreadsheetDoc = null;
73 
74     /**
75     * Creates Spreadsheet document.
76     */
77     protected void initialize( TestParameters tParam, PrintWriter log ) {
78         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
79 
80         try {
81             log.println( "creating a Spreadsheet document" );
82             xSpreadsheetDoc = SOF.createCalcDoc(null);
83         } catch ( com.sun.star.uno.Exception e ) {
84             // Some exception occures.FAILED
85             e.printStackTrace( log );
86             throw new StatusException( "Couldn't create document", e );
87         }
88     }
89 
90     /**
91     * Disposes Spreadsheet document.
92     */
93     protected void cleanup( TestParameters tParam, PrintWriter log ) {
94         log.println( "    disposing xSheetDoc " );
95         XComponent oComp = (XComponent)
96             UnoRuntime.queryInterface(XComponent.class, xSpreadsheetDoc);
97         util.DesktopTools.closeDoc(oComp);
98     }
99 
100     /**
101     * Creating a Testenvironment for the interfaces to be tested.
102     * Retrieves the collection of style families from the document
103     * using the interface <code>XStyleFamiliesSupplier</code>.
104     * Obtains style family with index 0 from the collection and obtains
105     * style with index 0 from this style family. Creates the instance of the
106     * service <code>com.sun.star.style.CellStyle</code>. Inserts the created
107     * style to the obtained style family. Retrieves a collection of spreadsheets
108     * from the document and takes one of them. Gets a cell from the spreadsheet
109     * and sets the value of property <code>'CellStyle'</code> to the created
110     * style. The created style is the instance of the service
111     * <code>com.sun.star.style.Style</code> also.
112     * Object relations created :
113     * <ul>
114     *  <li> <code>'PoolStyle'</code> for
115     *      {@link ifc.style._XStyle}(the style with index 0 that was obtained
116     *      from the collection)</li>
117     * </ul>
118     * @see com.sun.star.style.CellStyle
119     * @see com.sun.star.style.Style
120     * @see com.sun.star.style.XStyleFamiliesSupplier
121     */
122     protected TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
123 
124 
125         // creation of the testobject here
126         // first we write what we are intend to do to log file
127 
128         log.println("creating a test environment");
129 
130         log.println("getting style");
131         XStyleFamiliesSupplier oStyleFamiliesSupplier = (XStyleFamiliesSupplier)
132             UnoRuntime.queryInterface(
133                 XStyleFamiliesSupplier.class, xSpreadsheetDoc);
134         XNameAccess oStyleFamilies = oStyleFamiliesSupplier.getStyleFamilies();
135         XIndexAccess oStyleFamiliesIndexAccess = (XIndexAccess)
136             UnoRuntime.queryInterface(XIndexAccess.class, oStyleFamilies);
137         XNameAccess oStyleFamilyNameAccess = null;
138         XStyle oStyle = null;
139         try {
140             oStyleFamilyNameAccess = (XNameAccess) AnyConverter.toObject(
141                 new Type(XNameAccess.class),
142                     oStyleFamiliesIndexAccess.getByIndex(0));
143 
144             XIndexAccess oStyleFamilyIndexAccess = (XIndexAccess)
145                 UnoRuntime.queryInterface(XIndexAccess.class,
146                 oStyleFamilyNameAccess);
147             oStyle = (XStyle) AnyConverter.toObject(
148                 new Type(XStyle.class),oStyleFamilyIndexAccess.getByIndex(0));
149         } catch(com.sun.star.lang.WrappedTargetException e) {
150             e.printStackTrace(log);
151             throw new StatusException("Couldn't get by index", e);
152         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
153             e.printStackTrace(log);
154             throw new StatusException("Couldn't get by index", e);
155         } catch(com.sun.star.lang.IllegalArgumentException e) {
156             e.printStackTrace(log);
157             throw new StatusException("Couldn't get by index", e);
158         }
159 
160         log.println("Creating a user-defined style");
161         XMultiServiceFactory oMSF = (XMultiServiceFactory)
162             UnoRuntime.queryInterface(
163                 XMultiServiceFactory.class, xSpreadsheetDoc);
164 
165         XInterface oInt = null;
166         try {
167             oInt = (XInterface)
168                 oMSF.createInstance("com.sun.star.style.CellStyle");
169         } catch(com.sun.star.uno.Exception e) {
170             e.printStackTrace(log);
171             throw new StatusException("Couldn't create instance", e);
172         }
173         XStyle oMyStyle = (XStyle)UnoRuntime.queryInterface(XStyle.class, oInt);
174 
175         XNameContainer oStyleFamilyNameContainer = (XNameContainer)UnoRuntime.
176             queryInterface(XNameContainer.class, oStyleFamilyNameAccess);
177 
178         try {
179             if (oStyleFamilyNameContainer.hasByName("My Style")) {
180                 oStyleFamilyNameContainer.removeByName("My Style");
181             }
182 
183             oStyleFamilyNameContainer.insertByName("My Style", oMyStyle);
184         } catch(com.sun.star.lang.WrappedTargetException e) {
185             e.printStackTrace(log);
186             throw new StatusException("Couldn't create test environment", e);
187         } catch(com.sun.star.container.NoSuchElementException e) {
188             e.printStackTrace(log);
189             throw new StatusException("Couldn't create test environment", e);
190         } catch(com.sun.star.container.ElementExistException e) {
191             e.printStackTrace(log);
192             throw new StatusException("Couldn't create test environment", e);
193         } catch(com.sun.star.lang.IllegalArgumentException e) {
194             e.printStackTrace(log);
195             throw new StatusException("Couldn't create test environment", e);
196         }
197 
198 
199         //using the style
200         log.println("Getting spreadsheet") ;
201         XSpreadsheets oSheets = xSpreadsheetDoc.getSheets() ;
202         XIndexAccess oIndexSheets = (XIndexAccess)
203             UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
204 
205         XCell aCell = null;
206         try {
207             XSpreadsheet oSheet = (XSpreadsheet) AnyConverter.toObject(
208                     new Type(XSpreadsheet.class),oIndexSheets.getByIndex(0));
209             log.println("Getting a cell from sheet") ;
210             aCell = oSheet.getCellByPosition(2,3) ;
211         } catch(com.sun.star.lang.WrappedTargetException e) {
212             e.printStackTrace(log);
213             throw new StatusException("Couldn't get spreadsheet by index", e);
214         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
215             e.printStackTrace(log);
216             throw new StatusException("Couldn't get spreadsheet by index", e);
217         } catch(com.sun.star.lang.IllegalArgumentException e) {
218             e.printStackTrace(log);
219             throw new StatusException("Couldn't get spreadsheet by index", e);
220         }
221 
222         XPropertySet xProp = (XPropertySet)
223             UnoRuntime.queryInterface(XPropertySet.class, aCell);
224 
225         try {
226             xProp.setPropertyValue("CellStyle", oMyStyle.getName());
227         } catch(com.sun.star.lang.WrappedTargetException e) {
228             e.printStackTrace(log);
229             throw new StatusException("Couldn't set property CellStyle", e);
230         } catch(com.sun.star.lang.IllegalArgumentException e) {
231             e.printStackTrace(log);
232             throw new StatusException("Couldn't set property CellStyle", e);
233         } catch(com.sun.star.beans.PropertyVetoException e) {
234             e.printStackTrace(log);
235             throw new StatusException("Couldn't set property CellStyle", e);
236         } catch(com.sun.star.beans.UnknownPropertyException e) {
237             e.printStackTrace(log);
238             throw new StatusException("Couldn't set property CellStyle", e);
239         }
240 
241         log.println("creating a new environment for object");
242         TestEnvironment tEnv = new TestEnvironment(oMyStyle);
243 
244         tEnv.addObjRelation("PoolStyle", oStyle);
245 
246         tEnv.addObjRelation("PropertyNames", getPropertyNames
247             ((XPropertySet) UnoRuntime.queryInterface
248             (XPropertySet.class, oMyStyle)));
249 
250         return tEnv;
251     }
252 
253     public String[] getPropertyNames(XPropertySet props) {
254         Property[] the_props = props.getPropertySetInfo().getProperties();
255         Vector names = new Vector() ;
256 
257         for (int i=0;i<the_props.length;i++) {
258             boolean isWritable =
259                 ((the_props[i].Attributes & PropertyAttribute.READONLY) == 0);
260             if (isWritable) {
261                 names.add(the_props[i].Name);
262             }
263         }
264         return (String[]) names.toArray(new String[names.size()]) ;
265     }
266 }    // finish class ScStyleObj
267