1ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5ef39d40dSAndrew Rist  * distributed with this work for additional information
6ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10ef39d40dSAndrew Rist  *
11ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12ef39d40dSAndrew Rist  *
13ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18ef39d40dSAndrew Rist  * under the License.
19ef39d40dSAndrew Rist  *
20ef39d40dSAndrew Rist  *************************************************************/
21ef39d40dSAndrew Rist 
22ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package mod._sc;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import java.io.PrintWriter;
27cdf0e10cSrcweir 
28cdf0e10cSrcweir import lib.StatusException;
29cdf0e10cSrcweir import lib.TestCase;
30cdf0e10cSrcweir import lib.TestEnvironment;
31cdf0e10cSrcweir import lib.TestParameters;
32cdf0e10cSrcweir import util.SOfficeFactory;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir import com.sun.star.awt.Rectangle;
35cdf0e10cSrcweir import com.sun.star.container.XEnumerationAccess;
36cdf0e10cSrcweir import com.sun.star.container.XIndexAccess;
37cdf0e10cSrcweir import com.sun.star.lang.XComponent;
38cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
39cdf0e10cSrcweir import com.sun.star.sheet.XCellRangeAddressable;
40cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheet;
41cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheetDocument;
42cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheets;
43cdf0e10cSrcweir import com.sun.star.table.CellRangeAddress;
44cdf0e10cSrcweir import com.sun.star.table.XCell;
45cdf0e10cSrcweir import com.sun.star.table.XCellRange;
46cdf0e10cSrcweir import com.sun.star.table.XTableCharts;
47cdf0e10cSrcweir import com.sun.star.table.XTableChartsSupplier;
48cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
49cdf0e10cSrcweir import com.sun.star.uno.Type;
50cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
51cdf0e10cSrcweir import com.sun.star.uno.XInterface;
52cdf0e10cSrcweir 
53cdf0e10cSrcweir public class ScIndexEnumeration_TableChartsEnumeration extends TestCase {
54cdf0e10cSrcweir     static XSpreadsheetDocument xSheetDoc = null;
55cdf0e10cSrcweir 
56cdf0e10cSrcweir     /**
57cdf0e10cSrcweir     * Creates Spreadsheet document.
58cdf0e10cSrcweir     */
initialize( TestParameters tParam, PrintWriter log )59cdf0e10cSrcweir     protected void initialize( TestParameters tParam, PrintWriter log ) {
60cdf0e10cSrcweir         // get a soffice factory object
61cdf0e10cSrcweir         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF());
62cdf0e10cSrcweir 
63cdf0e10cSrcweir         try {
64cdf0e10cSrcweir             log.println( "creating a sheetdocument" );
65*170fb961SPedro Giffuni             xSheetDoc = SOF.createCalcDoc(null);
66cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
67cdf0e10cSrcweir             // Some exception occures.FAILED
68cdf0e10cSrcweir             e.printStackTrace( log );
69cdf0e10cSrcweir             throw new StatusException( "Couldn't create document", e );
70cdf0e10cSrcweir         }
71cdf0e10cSrcweir     }
72cdf0e10cSrcweir 
73cdf0e10cSrcweir     /**
74cdf0e10cSrcweir     * Disposes Spreadsheet document.
75cdf0e10cSrcweir     */
cleanup( TestParameters tParam, PrintWriter log )76cdf0e10cSrcweir     protected void cleanup( TestParameters tParam, PrintWriter log ) {
77cdf0e10cSrcweir         log.println( "    disposing xSheetDoc " );
78cdf0e10cSrcweir         XComponent oComp = (XComponent)
79cdf0e10cSrcweir             UnoRuntime.queryInterface(XComponent.class, xSheetDoc);
80cdf0e10cSrcweir         util.DesktopTools.closeDoc(oComp);
81cdf0e10cSrcweir     }
82cdf0e10cSrcweir 
createTestEnvironment(TestParameters Param, PrintWriter log)83cdf0e10cSrcweir     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
84cdf0e10cSrcweir 
85cdf0e10cSrcweir         XSpreadsheet oSheet=null;
86cdf0e10cSrcweir 
87cdf0e10cSrcweir         try {
88cdf0e10cSrcweir             log.println("Getting spreadsheet") ;
89cdf0e10cSrcweir             XSpreadsheets oSheets = xSheetDoc.getSheets() ;
90cdf0e10cSrcweir             XIndexAccess oIndexSheets = (XIndexAccess)
91cdf0e10cSrcweir                         UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
92cdf0e10cSrcweir             oSheet = (XSpreadsheet) AnyConverter.toObject(
93cdf0e10cSrcweir                     new Type(XSpreadsheet.class),oIndexSheets.getByIndex(0));
94cdf0e10cSrcweir         } catch (com.sun.star.lang.WrappedTargetException e) {
95cdf0e10cSrcweir             log.println("Couldn't get Sheet ");
96cdf0e10cSrcweir             e.printStackTrace(log);
97cdf0e10cSrcweir             throw new StatusException("Couldn't get sheet", e);
98cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
99cdf0e10cSrcweir             log.println("Couldn't get Sheet ");
100cdf0e10cSrcweir             e.printStackTrace(log);
101cdf0e10cSrcweir             throw new StatusException("Couldn't get sheet", e);
102cdf0e10cSrcweir         } catch (com.sun.star.lang.IllegalArgumentException e) {
103cdf0e10cSrcweir             log.println("Couldn't get Sheet ");
104cdf0e10cSrcweir             e.printStackTrace(log);
105cdf0e10cSrcweir             throw new StatusException("Couldn't get sheet", e);
106cdf0e10cSrcweir         }
107cdf0e10cSrcweir 
108cdf0e10cSrcweir         log.println("Creating the Header") ;
109cdf0e10cSrcweir 
110cdf0e10cSrcweir         insertIntoCell(1,0,"JAN",oSheet,"");
111cdf0e10cSrcweir         insertIntoCell(2,0,"FEB",oSheet,"");
112cdf0e10cSrcweir         insertIntoCell(3,0,"MAR",oSheet,"");
113cdf0e10cSrcweir         insertIntoCell(4,0,"APR",oSheet,"");
114cdf0e10cSrcweir         insertIntoCell(5,0,"MAI",oSheet,"");
115cdf0e10cSrcweir         insertIntoCell(6,0,"JUN",oSheet,"");
116cdf0e10cSrcweir         insertIntoCell(7,0,"JUL",oSheet,"");
117cdf0e10cSrcweir         insertIntoCell(8,0,"AUG",oSheet,"");
118cdf0e10cSrcweir         insertIntoCell(9,0,"SEP",oSheet,"");
119cdf0e10cSrcweir         insertIntoCell(10,0,"OCT",oSheet,"");
120cdf0e10cSrcweir         insertIntoCell(11,0,"NOV",oSheet,"");
121cdf0e10cSrcweir         insertIntoCell(12,0,"DEC",oSheet,"");
122cdf0e10cSrcweir         insertIntoCell(13,0,"SUM",oSheet,"");
123cdf0e10cSrcweir 
124cdf0e10cSrcweir         log.println("Fill the lines");
125cdf0e10cSrcweir 
126cdf0e10cSrcweir         insertIntoCell(0,1,"Smith",oSheet,"");
127cdf0e10cSrcweir         insertIntoCell(1,1,"42",oSheet,"V");
128cdf0e10cSrcweir         insertIntoCell(2,1,"58.9",oSheet,"V");
129cdf0e10cSrcweir         insertIntoCell(3,1,"-66.5",oSheet,"V");
130cdf0e10cSrcweir         insertIntoCell(4,1,"43.4",oSheet,"V");
131cdf0e10cSrcweir         insertIntoCell(5,1,"44.5",oSheet,"V");
132cdf0e10cSrcweir         insertIntoCell(6,1,"45.3",oSheet,"V");
133cdf0e10cSrcweir         insertIntoCell(7,1,"-67.3",oSheet,"V");
134cdf0e10cSrcweir         insertIntoCell(8,1,"30.5",oSheet,"V");
135cdf0e10cSrcweir         insertIntoCell(9,1,"23.2",oSheet,"V");
136cdf0e10cSrcweir         insertIntoCell(10,1,"-97.3",oSheet,"V");
137cdf0e10cSrcweir         insertIntoCell(11,1,"22.4",oSheet,"V");
138cdf0e10cSrcweir         insertIntoCell(12,1,"23.5",oSheet,"V");
139cdf0e10cSrcweir         insertIntoCell(13,1,"=SUM(B2:M2)",oSheet,"");
140cdf0e10cSrcweir 
141cdf0e10cSrcweir         insertIntoCell(0,2,"Jones",oSheet,"");
142cdf0e10cSrcweir         insertIntoCell(1,2,"21",oSheet,"V");
143cdf0e10cSrcweir         insertIntoCell(2,2,"40.9",oSheet,"V");
144cdf0e10cSrcweir         insertIntoCell(3,2,"-57.5",oSheet,"V");
145cdf0e10cSrcweir         insertIntoCell(4,2,"-23.4",oSheet,"V");
146cdf0e10cSrcweir         insertIntoCell(5,2,"34.5",oSheet,"V");
147cdf0e10cSrcweir         insertIntoCell(6,2,"59.3",oSheet,"V");
148cdf0e10cSrcweir         insertIntoCell(7,2,"27.3",oSheet,"V");
149cdf0e10cSrcweir         insertIntoCell(8,2,"-38.5",oSheet,"V");
150cdf0e10cSrcweir         insertIntoCell(9,2,"43.2",oSheet,"V");
151cdf0e10cSrcweir         insertIntoCell(10,2,"57.3",oSheet,"V");
152cdf0e10cSrcweir         insertIntoCell(11,2,"25.4",oSheet,"V");
153cdf0e10cSrcweir         insertIntoCell(12,2,"28.5",oSheet,"V");
154cdf0e10cSrcweir         insertIntoCell(13,2,"=SUM(B3:M3)",oSheet,"");
155cdf0e10cSrcweir 
156cdf0e10cSrcweir         insertIntoCell(0,3,"Brown",oSheet,"");
157cdf0e10cSrcweir         insertIntoCell(1,3,"31.45",oSheet,"V");
158cdf0e10cSrcweir         insertIntoCell(2,3,"-20.9",oSheet,"V");
159cdf0e10cSrcweir         insertIntoCell(3,3,"-117.5",oSheet,"V");
160cdf0e10cSrcweir         insertIntoCell(4,3,"23.4",oSheet,"V");
161cdf0e10cSrcweir         insertIntoCell(5,3,"-114.5",oSheet,"V");
162cdf0e10cSrcweir         insertIntoCell(6,3,"115.3",oSheet,"V");
163cdf0e10cSrcweir         insertIntoCell(7,3,"-171.3",oSheet,"V");
164cdf0e10cSrcweir         insertIntoCell(8,3,"89.5",oSheet,"V");
165cdf0e10cSrcweir         insertIntoCell(9,3,"41.2",oSheet,"V");
166cdf0e10cSrcweir         insertIntoCell(10,3,"71.3",oSheet,"V");
167cdf0e10cSrcweir         insertIntoCell(11,3,"25.4",oSheet,"V");
168cdf0e10cSrcweir         insertIntoCell(12,3,"38.5",oSheet,"V");
169cdf0e10cSrcweir         insertIntoCell(13,3,"=SUM(A4:L4)",oSheet,"");
170cdf0e10cSrcweir 
171cdf0e10cSrcweir         // insert a chart
172cdf0e10cSrcweir         Rectangle oRect = new Rectangle(500, 3000, 25000, 11000);
173cdf0e10cSrcweir 
174cdf0e10cSrcweir         XCellRange oRange = (XCellRange)
175cdf0e10cSrcweir             UnoRuntime.queryInterface(XCellRange.class, oSheet);
176cdf0e10cSrcweir         XCellRange myRange = oRange.getCellRangeByName("A1:N4");
177cdf0e10cSrcweir         XCellRangeAddressable oRangeAddr = (XCellRangeAddressable)
178cdf0e10cSrcweir             UnoRuntime.queryInterface(XCellRangeAddressable.class, myRange);
179cdf0e10cSrcweir         CellRangeAddress myAddr = oRangeAddr.getRangeAddress();
180cdf0e10cSrcweir 
181cdf0e10cSrcweir         CellRangeAddress[] oAddr = new CellRangeAddress[1];
182cdf0e10cSrcweir         oAddr[0] = myAddr;
183cdf0e10cSrcweir         XTableChartsSupplier oSupp = (XTableChartsSupplier)
184cdf0e10cSrcweir             UnoRuntime.queryInterface(XTableChartsSupplier.class, oSheet);
185cdf0e10cSrcweir 
186cdf0e10cSrcweir 
187cdf0e10cSrcweir         log.println("Insert Chart");
188cdf0e10cSrcweir         XTableCharts oCharts = oSupp.getCharts();
189cdf0e10cSrcweir         oCharts.addNewByName("ScChartObj", oRect, oAddr, true, true);
190cdf0e10cSrcweir 
191cdf0e10cSrcweir         log.println("creating a new environment for object");
192cdf0e10cSrcweir         XEnumerationAccess ea = (XEnumerationAccess)
193cdf0e10cSrcweir                     UnoRuntime.queryInterface(XEnumerationAccess.class,oCharts);
194cdf0e10cSrcweir 
195cdf0e10cSrcweir         XInterface oObj = ea.createEnumeration();
196cdf0e10cSrcweir 
197cdf0e10cSrcweir         TestEnvironment tEnv = new TestEnvironment(oObj);
198cdf0e10cSrcweir 
199cdf0e10cSrcweir //        tEnv.addObjRelation("RECT", oRect);
200cdf0e10cSrcweir //        tEnv.addObjRelation("ADDR", oAddr);
201cdf0e10cSrcweir 
202cdf0e10cSrcweir         return tEnv;
203cdf0e10cSrcweir     }
204cdf0e10cSrcweir 
205cdf0e10cSrcweir     /**
206cdf0e10cSrcweir     * Inserts a value or a formula in the cell of the spreasheet.
207cdf0e10cSrcweir     * @param CellX is the column index of the cell
208cdf0e10cSrcweir     * @param CellY is the row index of the cell
209cdf0e10cSrcweir     * @param theValue string representation of the value
210cdf0e10cSrcweir     * @param TT1 specify the spreadsheet, the interface
211cdf0e10cSrcweir     * <code>com.sun.star.sheet.XSpreadsheet</code>
212cdf0e10cSrcweir     * @param flag if it's equal to <code>'V'</code> then the method inserts
213cdf0e10cSrcweir     * a double-value in the cell else it inserts a formula in the cell
214cdf0e10cSrcweir     */
insertIntoCell( int CellX, int CellY, String theValue, XSpreadsheet TT1, String flag)215cdf0e10cSrcweir     public static void insertIntoCell(
216cdf0e10cSrcweir         int CellX, int CellY, String theValue, XSpreadsheet TT1, String flag) {
217cdf0e10cSrcweir 
218cdf0e10cSrcweir         XCell oCell = null;
219cdf0e10cSrcweir 
220cdf0e10cSrcweir         try {
221cdf0e10cSrcweir             oCell = TT1.getCellByPosition(CellX, CellY);
222cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException ex) {
223cdf0e10cSrcweir             System.out.println("Could not get Cell");
224cdf0e10cSrcweir         }
225cdf0e10cSrcweir 
226cdf0e10cSrcweir         if (flag.equals("V")) {
227cdf0e10cSrcweir             oCell.setValue(new Float(theValue).floatValue());
228cdf0e10cSrcweir         } else {
229cdf0e10cSrcweir             oCell.setFormula(theValue);
230cdf0e10cSrcweir         }
231cdf0e10cSrcweir 
232cdf0e10cSrcweir     } // end of insertIntoCell
233cdf0e10cSrcweir }
234cdf0e10cSrcweir 
235