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