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