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.lang.XComponent;
41 import com.sun.star.lang.XMultiServiceFactory;
42 import com.sun.star.sheet.XCellRangeAddressable;
43 import com.sun.star.sheet.XScenariosSupplier;
44 import com.sun.star.sheet.XSpreadsheet;
45 import com.sun.star.sheet.XSpreadsheetDocument;
46 import com.sun.star.sheet.XSpreadsheets;
47 import com.sun.star.table.CellRangeAddress;
48 import com.sun.star.table.XCellRange;
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 public class ScIndexEnumeration_ScenariosEnumeration extends TestCase {
55     public static XSpreadsheetDocument xSpreadsheetDoc;
56 
57     /**
58     * Creates Spreadsheet document.
59     */
60     public void initialize( TestParameters Param, PrintWriter log ) {
61         // get a soffice factory object
62         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)Param.getMSF());
63 
64         try {
65             log.println("creating a spreadsheetdocument");
66             xSpreadsheetDoc = SOF.createCalcDoc(null);
67         } catch (com.sun.star.uno.Exception e) {
68             e.printStackTrace( log );
69             throw new StatusException( "Couldn't create document ", e );
70         }
71     }
72 
73     /**
74     * Disposes Spreadsheet document.
75     */
76     protected void cleanup( TestParameters tParam, PrintWriter log ) {
77         log.println( "    disposing xSheetDoc " );
78         XComponent oComp = (XComponent)
79             UnoRuntime.queryInterface (XComponent.class, xSpreadsheetDoc) ;
80         util.DesktopTools.closeDoc(oComp);
81     }
82 
83     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
84 
85         log.println("getting sheets");
86         XSpreadsheets xSpreadsheets = (XSpreadsheets)xSpreadsheetDoc.getSheets();
87         log.println("getting a sheet");
88         XSpreadsheet oSheet = null;
89         XIndexAccess oIndexAccess = (XIndexAccess)
90             UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
91         try {
92             oSheet = (XSpreadsheet) AnyConverter.toObject(
93                     new Type(XSpreadsheet.class),oIndexAccess.getByIndex(0));
94         } catch (com.sun.star.lang.WrappedTargetException e) {
95             e.printStackTrace(log);
96             throw new StatusException( "Couldn't get a spreadsheet", e);
97         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
98             e.printStackTrace(log);
99             throw new StatusException( "Couldn't get a spreadsheet", e);
100         } catch (com.sun.star.lang.IllegalArgumentException e) {
101             e.printStackTrace(log);
102             throw new StatusException( "Couldn't get a spreadsheet", e);
103         }
104 
105         log.println("filling some cells");
106         try {
107             oSheet.getCellByPosition(5, 5).setValue(15);
108             oSheet.getCellByPosition(1, 4).setValue(10);
109             oSheet.getCellByPosition(2, 0).setValue(-5.15);
110         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
111             e.printStackTrace(log);
112             throw new StatusException("Couldn't fill some cell", e);
113         }
114 
115         XScenariosSupplier xSupp = (XScenariosSupplier)
116             UnoRuntime.queryInterface(XScenariosSupplier.class, oSheet);
117         XCellRange oRange = (XCellRange)
118             UnoRuntime.queryInterface(XCellRange.class, oSheet);
119         XCellRange myRange = oRange.getCellRangeByName("A1:N4");
120         XCellRangeAddressable oRangeAddr = (XCellRangeAddressable)
121             UnoRuntime.queryInterface(XCellRangeAddressable.class, myRange);
122         CellRangeAddress myAddr = oRangeAddr.getRangeAddress();
123 
124         CellRangeAddress[] oAddr = new CellRangeAddress[1];
125         oAddr[0] = myAddr;
126 
127         xSupp.getScenarios().addNewByName("ScScenarios", oAddr, "Range");
128 
129         XInterface oObj = xSupp.getScenarios();
130 
131         XEnumerationAccess ea = (XEnumerationAccess)
132                     UnoRuntime.queryInterface(XEnumerationAccess.class,oObj);
133 
134         oObj = ea.createEnumeration();
135 
136         log.println("ImplementationName: "+util.utils.getImplName(oObj));
137         // creating test environment
138         TestEnvironment tEnv = new TestEnvironment( oObj );
139 
140         tEnv.addObjRelation("ENUM",ea);
141 
142         return tEnv;
143     }
144 }
145 
146