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