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.GeneralFunction;
43 import com.sun.star.sheet.SubTotalColumn;
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.sheet.XSubTotalCalculatable;
48 import com.sun.star.sheet.XSubTotalDescriptor;
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_SubTotalFieldsEnumeration extends TestCase {
55     public static XSpreadsheetDocument xSpreadsheetDoc;
56 
57     /**
58     * Creates Spreadsheet document.
59     */
60     public void initialize( TestParameters Param, PrintWriter log ) {
61         // creation of the testobject here
62         // first we write what we are intend to do to log file
63         log.println("creating a test environment");
64 
65         // get a soffice factory object
66         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)Param.getMSF());
67 
68         try {
69             log.println("creating a spreadsheetdocument");
70             xSpreadsheetDoc = SOF.createCalcDoc(null);
71         } catch (com.sun.star.uno.Exception e) {
72             e.printStackTrace( log );
73             throw new StatusException( "Couldn't create document ", e );
74         }
75     }
76 
77     /**
78     * Disposes Spreadsheet document.
79     */
80     protected void cleanup( TestParameters tParam, PrintWriter log ) {
81         log.println( "    disposing xSheetDoc " );
82         XComponent oComp = (XComponent)
83             UnoRuntime.queryInterface (XComponent.class, xSpreadsheetDoc) ;
84         util.DesktopTools.closeDoc(oComp);
85     }
86 
87     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
88 
89         log.println("getting sheets");
90         XSpreadsheets xSpreadsheets = (XSpreadsheets)xSpreadsheetDoc.getSheets();
91 
92         log.println("getting a sheet");
93         XSpreadsheet oSheet = null;
94         XIndexAccess oIndexAccess = (XIndexAccess)
95             UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
96         try {
97             oSheet = (XSpreadsheet) AnyConverter.toObject(
98                 new Type(XSpreadsheet.class),oIndexAccess.getByIndex(0));
99         } catch (com.sun.star.lang.WrappedTargetException e) {
100             e.printStackTrace(log);
101             throw new StatusException( "Couldn't get a spreadsheet", e);
102         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
103             e.printStackTrace(log);
104             throw new StatusException( "Couldn't get a spreadsheet", e);
105         } catch (com.sun.star.lang.IllegalArgumentException e) {
106             e.printStackTrace(log);
107             throw new StatusException( "Couldn't get a spreadsheet", e);
108         }
109 
110         XSubTotalCalculatable xSTC = (XSubTotalCalculatable)
111             UnoRuntime.queryInterface(XSubTotalCalculatable.class, oSheet);
112 
113         XSubTotalDescriptor xSTD = xSTC.createSubTotalDescriptor(true);
114 
115         SubTotalColumn[] columns = new SubTotalColumn[1];
116         SubTotalColumn column = new SubTotalColumn();
117         column.Column = 5;
118         column.Function = GeneralFunction.SUM;
119         columns[0] = column;
120         xSTD.addNew(columns, 1);
121 
122         XIndexAccess oDescIndex = (XIndexAccess)
123             UnoRuntime.queryInterface(XIndexAccess.class, xSTD);
124 
125         XInterface oObj = null;
126 
127         XEnumerationAccess ea = (XEnumerationAccess)
128                     UnoRuntime.queryInterface(XEnumerationAccess.class,oDescIndex);
129 
130         oObj = ea.createEnumeration();
131 
132         log.println("ImplementationName: "+util.utils.getImplName(oObj));
133         // creating test environment
134         TestEnvironment tEnv = new TestEnvironment( oObj );
135 
136         tEnv.addObjRelation("ENUM",ea);
137 
138         return tEnv;
139 
140     } // finish method getTestEnvironment
141 }
142