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 32 import lib.StatusException; 33 import lib.TestCase; 34 import lib.TestEnvironment; 35 import lib.TestParameters; 36 import util.SOfficeFactory; 37 38 import com.sun.star.container.XEnumerationAccess; 39 import com.sun.star.container.XIndexAccess; 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.uno.AnyConverter; 47 import com.sun.star.uno.Type; 48 import com.sun.star.uno.UnoRuntime; 49 import com.sun.star.uno.XInterface; 50 51 public class ScIndexEnumeration_SheetCellRangesEnumeration extends TestCase { 52 static XSpreadsheetDocument xSheetDoc = null; 53 54 /** 55 * Creates Spreadsheet document. 56 */ 57 protected void initialize( TestParameters tParam, PrintWriter log ) { 58 59 // get a soffice factory object 60 SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF()); 61 62 try { 63 log.println( "creating a sheetdocument" ); 64 xSheetDoc = SOF.createCalcDoc(null);; 65 } catch (com.sun.star.uno.Exception e) { 66 // Some exception occures.FAILED 67 e.printStackTrace( log ); 68 throw new StatusException( "Couldn't create document", e ); 69 } 70 } 71 72 /** 73 * Disposes Spreadsheet document. 74 */ 75 protected void cleanup( TestParameters tParam, PrintWriter log ) { 76 log.println( " disposing xSheetDoc " ); 77 XComponent oComp = 78 (XComponent) UnoRuntime.queryInterface (XComponent.class, xSheetDoc); 79 util.DesktopTools.closeDoc(oComp); 80 } 81 82 protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) { 83 84 XInterface oObj = null; 85 Object oRange = null ; 86 87 // creation of testobject here 88 // first we write what we are intend to do to log file 89 log.println( "Creating a test environment" ); 90 91 // get a soffice factory object 92 SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)Param.getMSF() ); 93 94 log.println("Getting test object "); 95 96 XComponent oComp = (XComponent) 97 UnoRuntime.queryInterface (XComponent.class, xSheetDoc); 98 99 oObj = (XInterface) 100 SOF.createInstance(oComp, "com.sun.star.sheet.SheetCellRanges"); 101 102 XSpreadsheets oSheets = xSheetDoc.getSheets() ; 103 XIndexAccess oIndSheets = (XIndexAccess) 104 UnoRuntime.queryInterface (XIndexAccess.class, oSheets); 105 XSpreadsheet oSheet = null; 106 try { 107 oSheet = (XSpreadsheet) AnyConverter.toObject( 108 new Type(XSpreadsheet.class),oIndSheets.getByIndex(0)); 109 XNameContainer oRanges = (XNameContainer) 110 UnoRuntime.queryInterface(XNameContainer.class, oObj); 111 112 oRange = oSheet.getCellRangeByName("C1:D4"); 113 oRanges.insertByName("Range1", oRange); 114 oRange = oSheet.getCellRangeByName("E2:F5"); 115 oRanges.insertByName("Range2", oRange); 116 oRange = oSheet.getCellRangeByName("G2:H3"); 117 oRanges.insertByName("Range3", oRange); 118 oRange = oSheet.getCellRangeByName("I7:J8"); 119 oRanges.insertByName("Range4", oRange); 120 } catch(com.sun.star.lang.WrappedTargetException e) { 121 e.printStackTrace(log); 122 throw new StatusException("Couldn't create test object", e); 123 } catch(com.sun.star.lang.IndexOutOfBoundsException e) { 124 e.printStackTrace(log); 125 throw new StatusException("Couldn't create test object", e); 126 } catch(com.sun.star.container.ElementExistException e) { 127 e.printStackTrace(log); 128 throw new StatusException("Couldn't create test object", e); 129 } catch(com.sun.star.lang.IllegalArgumentException e) { 130 e.printStackTrace(log); 131 throw new StatusException("Couldn't create test object", e); 132 } 133 134 log.println("filling some cells"); 135 try { 136 for (int i = 0; i < 10; i++) { 137 for (int j = 0; j < 5; j++) { 138 oSheet.getCellByPosition(i, j).setFormula("a"); 139 } 140 } 141 for (int i = 0; i < 10; i++) { 142 for (int j = 5; j < 10; j++) { 143 oSheet.getCellByPosition(i, j).setValue(i + j); 144 } 145 } 146 } catch (com.sun.star.lang.IndexOutOfBoundsException e) { 147 e.printStackTrace(log); 148 throw new StatusException ( 149 "Exception occurred while filling cells", e); 150 } 151 152 XEnumerationAccess ea = (XEnumerationAccess) 153 UnoRuntime.queryInterface(XEnumerationAccess.class,oObj); 154 155 oObj = ea.createEnumeration(); 156 157 log.println("ImplementationName: "+util.utils.getImplName(oObj)); 158 // creating test environment 159 TestEnvironment tEnv = new TestEnvironment( oObj ); 160 161 tEnv.addObjRelation("ENUM",ea); 162 163 return tEnv ; 164 } 165 166 } 167 168