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