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.awt.Rectangle;
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.XSpreadsheet;
44 import com.sun.star.sheet.XSpreadsheetDocument;
45 import com.sun.star.sheet.XSpreadsheets;
46 import com.sun.star.table.CellRangeAddress;
47 import com.sun.star.table.XCell;
48 import com.sun.star.table.XCellRange;
49 import com.sun.star.table.XTableCharts;
50 import com.sun.star.table.XTableChartsSupplier;
51 import com.sun.star.uno.AnyConverter;
52 import com.sun.star.uno.Type;
53 import com.sun.star.uno.UnoRuntime;
54 
55 /**
56 * Test for object which is represented by service
57 * <code>com.sun.star.table.TableCharts</code>. <p>
58 * Object implements the following interfaces :
59 * <ul>
60 *  <li> <code>com::sun::star::container::XNameAccess</code></li>
61 *  <li> <code>com::sun::star::container::XIndexAccess</code></li>
62 *  <li> <code>com::sun::star::container::XElementAccess</code></li>
63 *  <li> <code>com::sun::star::table::XTableCharts</code></li>
64 * </ul>
65 * @see com.sun.star.table.TableCharts
66 * @see com.sun.star.container.XNameAccess
67 * @see com.sun.star.container.XIndexAccess
68 * @see com.sun.star.container.XElementAccess
69 * @see com.sun.star.table.XTableCharts
70 * @see ifc.container._XNameAccess
71 * @see ifc.container._XIndexAccess
72 * @see ifc.container._XElementAccess
73 * @see ifc.table._XTableCharts
74 */
75 public class ScChartsObj extends TestCase {
76     static XSpreadsheetDocument xSheetDoc = null;
77 
78     /**
79     * Creates Spreadsheet document.
80     */
81     protected void initialize( TestParameters tParam, PrintWriter log ) {
82         // get a soffice factory object
83         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF());
84 
85         try {
86             log.println( "creating a sheetdocument" );
87             xSheetDoc = SOF.createCalcDoc(null);;
88         } catch (com.sun.star.uno.Exception e) {
89             // Some exception occures.FAILED
90             e.printStackTrace( log );
91             throw new StatusException( "Couldn't create document", e );
92         }
93     }
94 
95     /**
96     * Disposes Spreadsheet document.
97     */
98     protected void cleanup( TestParameters tParam, PrintWriter log ) {
99         log.println( "    disposing xSheetDoc " );
100         XComponent oComp = (XComponent)
101             UnoRuntime.queryInterface(XComponent.class, xSheetDoc);
102         util.DesktopTools.closeDoc(oComp);
103     }
104 
105     /**
106     * Creating a Testenvironment for the interfaces to be tested.
107     * Retrieves a collection of spreadsheets from a document
108     * and takes one of them. Inserts some values into the cells of the some cell
109     * range address. Obtains the collection of the charts using the interface
110     * <code>XTableChartsSupplier</code>. Creates and adds the chart that using
111     * the data from the cells of this cell range address. Collection of
112     * the charts is the instance of the service
113     * <code>com.sun.star.table.TableCharts</code>.
114     * Object relations created :
115     * <ul>
116     *  <li> <code>'RECT'</code> for
117     *      {@link ifc.table._XTableCharts}(of type <code>Rectangle</code>
118     *       the position of the chart)</li>
119     *  <li> <code>'ADDR'</code> for
120     *      {@link ifc.table._XTableCharts}(of type
121     *      <code>com.sun.star.table.CellRangeAddress[]</code> data source ranges
122     *      for chart creating)</li>
123     * </ul>
124     * @see com.sun.star.container.XNamed
125     * @see com.sun.star.table.XTableChartsSupplier
126     */
127     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
128 
129         XSpreadsheet oSheet=null;
130 
131         try {
132             log.println("Getting spreadsheet") ;
133             XSpreadsheets oSheets = xSheetDoc.getSheets() ;
134             XIndexAccess oIndexSheets = (XIndexAccess)
135                         UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
136             oSheet = (XSpreadsheet) AnyConverter.toObject(
137                     new Type(XSpreadsheet.class),oIndexSheets.getByIndex(0));
138         } catch (com.sun.star.lang.WrappedTargetException e) {
139             log.println("Couldn't get Sheet ");
140             e.printStackTrace(log);
141             throw new StatusException("Couldn't get sheet", e);
142         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
143             log.println("Couldn't get Sheet ");
144             e.printStackTrace(log);
145             throw new StatusException("Couldn't get sheet", e);
146         } catch (com.sun.star.lang.IllegalArgumentException e) {
147             log.println("Couldn't get Sheet ");
148             e.printStackTrace(log);
149             throw new StatusException("Couldn't get sheet", e);
150         }
151 
152         log.println("Creating the Header") ;
153 
154         insertIntoCell(1,0,"JAN",oSheet,"");
155         insertIntoCell(2,0,"FEB",oSheet,"");
156         insertIntoCell(3,0,"MAR",oSheet,"");
157         insertIntoCell(4,0,"APR",oSheet,"");
158         insertIntoCell(5,0,"MAI",oSheet,"");
159         insertIntoCell(6,0,"JUN",oSheet,"");
160         insertIntoCell(7,0,"JUL",oSheet,"");
161         insertIntoCell(8,0,"AUG",oSheet,"");
162         insertIntoCell(9,0,"SEP",oSheet,"");
163         insertIntoCell(10,0,"OCT",oSheet,"");
164         insertIntoCell(11,0,"NOV",oSheet,"");
165         insertIntoCell(12,0,"DEC",oSheet,"");
166         insertIntoCell(13,0,"SUM",oSheet,"");
167 
168         log.println("Fill the lines");
169 
170         insertIntoCell(0,1,"Smith",oSheet,"");
171         insertIntoCell(1,1,"42",oSheet,"V");
172         insertIntoCell(2,1,"58.9",oSheet,"V");
173         insertIntoCell(3,1,"-66.5",oSheet,"V");
174         insertIntoCell(4,1,"43.4",oSheet,"V");
175         insertIntoCell(5,1,"44.5",oSheet,"V");
176         insertIntoCell(6,1,"45.3",oSheet,"V");
177         insertIntoCell(7,1,"-67.3",oSheet,"V");
178         insertIntoCell(8,1,"30.5",oSheet,"V");
179         insertIntoCell(9,1,"23.2",oSheet,"V");
180         insertIntoCell(10,1,"-97.3",oSheet,"V");
181         insertIntoCell(11,1,"22.4",oSheet,"V");
182         insertIntoCell(12,1,"23.5",oSheet,"V");
183         insertIntoCell(13,1,"=SUM(B2:M2)",oSheet,"");
184 
185         insertIntoCell(0,2,"Jones",oSheet,"");
186         insertIntoCell(1,2,"21",oSheet,"V");
187         insertIntoCell(2,2,"40.9",oSheet,"V");
188         insertIntoCell(3,2,"-57.5",oSheet,"V");
189         insertIntoCell(4,2,"-23.4",oSheet,"V");
190         insertIntoCell(5,2,"34.5",oSheet,"V");
191         insertIntoCell(6,2,"59.3",oSheet,"V");
192         insertIntoCell(7,2,"27.3",oSheet,"V");
193         insertIntoCell(8,2,"-38.5",oSheet,"V");
194         insertIntoCell(9,2,"43.2",oSheet,"V");
195         insertIntoCell(10,2,"57.3",oSheet,"V");
196         insertIntoCell(11,2,"25.4",oSheet,"V");
197         insertIntoCell(12,2,"28.5",oSheet,"V");
198         insertIntoCell(13,2,"=SUM(B3:M3)",oSheet,"");
199 
200         insertIntoCell(0,3,"Brown",oSheet,"");
201         insertIntoCell(1,3,"31.45",oSheet,"V");
202         insertIntoCell(2,3,"-20.9",oSheet,"V");
203         insertIntoCell(3,3,"-117.5",oSheet,"V");
204         insertIntoCell(4,3,"23.4",oSheet,"V");
205         insertIntoCell(5,3,"-114.5",oSheet,"V");
206         insertIntoCell(6,3,"115.3",oSheet,"V");
207         insertIntoCell(7,3,"-171.3",oSheet,"V");
208         insertIntoCell(8,3,"89.5",oSheet,"V");
209         insertIntoCell(9,3,"41.2",oSheet,"V");
210         insertIntoCell(10,3,"71.3",oSheet,"V");
211         insertIntoCell(11,3,"25.4",oSheet,"V");
212         insertIntoCell(12,3,"38.5",oSheet,"V");
213         insertIntoCell(13,3,"=SUM(A4:L4)",oSheet,"");
214 
215         // insert a chart
216         Rectangle oRect = new Rectangle(500, 3000, 25000, 11000);
217 
218         XCellRange oRange = (XCellRange)
219             UnoRuntime.queryInterface(XCellRange.class, oSheet);
220         XCellRange myRange = oRange.getCellRangeByName("A1:N4");
221         XCellRangeAddressable oRangeAddr = (XCellRangeAddressable)
222             UnoRuntime.queryInterface(XCellRangeAddressable.class, myRange);
223         CellRangeAddress myAddr = oRangeAddr.getRangeAddress();
224 
225         CellRangeAddress[] oAddr = new CellRangeAddress[1];
226         oAddr[0] = myAddr;
227         XTableChartsSupplier oSupp = (XTableChartsSupplier)
228             UnoRuntime.queryInterface(XTableChartsSupplier.class, oSheet);
229 
230 
231         log.println("Insert Chart");
232         XTableCharts oCharts = oSupp.getCharts();
233         oCharts.addNewByName("ScChartObj", oRect, oAddr, true, true);
234 
235         log.println("creating a new environment for object");
236         TestEnvironment tEnv = new TestEnvironment(oCharts);
237 
238         tEnv.addObjRelation("RECT", oRect);
239         tEnv.addObjRelation("ADDR", oAddr);
240 
241         return tEnv;
242     }
243 
244     /**
245     * Inserts a value or a formula in the cell of the spreasheet.
246     * @param CellX is the column index of the cell
247     * @param CellY is the row index of the cell
248     * @param theValue string representation of the value
249     * @param TT1 specify the spreadsheet, the interface
250     * <code>com.sun.star.sheet.XSpreadsheet</code>
251     * @param flag if it's equal to <code>'V'</code> then the method inserts
252     * a double-value in the cell else it inserts a formula in the cell
253     */
254     public static void insertIntoCell(
255         int CellX, int CellY, String theValue, XSpreadsheet TT1, String flag) {
256 
257         XCell oCell = null;
258 
259         try {
260             oCell = TT1.getCellByPosition(CellX, CellY);
261         } catch (com.sun.star.lang.IndexOutOfBoundsException ex) {
262             System.out.println("Could not get Cell");
263         }
264 
265         if (flag.equals("V")) {
266             oCell.setValue(new Float(theValue).floatValue());
267         } else {
268             oCell.setFormula(theValue);
269         }
270 
271     } // end of insertIntoCell
272 }
273 
274