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