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