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._sch;
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.chart.XChartDataArray;
40 import com.sun.star.chart.XChartDocument;
41 import com.sun.star.chart.XDiagram;
42 import com.sun.star.container.XIndexAccess;
43 import com.sun.star.container.XNameAccess;
44 import com.sun.star.document.XEmbeddedObjectSupplier;
45 import com.sun.star.lang.XMultiServiceFactory;
46 import com.sun.star.sheet.XCellRangeAddressable;
47 import com.sun.star.sheet.XSpreadsheet;
48 import com.sun.star.sheet.XSpreadsheetDocument;
49 import com.sun.star.sheet.XSpreadsheets;
50 import com.sun.star.table.CellRangeAddress;
51 import com.sun.star.table.XCell;
52 import com.sun.star.table.XCellRange;
53 import com.sun.star.table.XTableChart;
54 import com.sun.star.table.XTableCharts;
55 import com.sun.star.table.XTableChartsSupplier;
56 import com.sun.star.uno.AnyConverter;
57 import com.sun.star.uno.Type;
58 import com.sun.star.uno.UnoRuntime;
59 import com.sun.star.uno.XInterface;
60 
61 /**
62 * Test for object which is represented by the following services:
63 * <ul>
64 *   <li> <code>com.sun.star.chart.Dim3DDiagram</code> </li>
65 *   <li> <code>com.sun.star.chart.StockDiagram</code> </li>
66 *   <li> <code>com.sun.star.chart.LineDiagram</code> </li>
67 *   <li> <code>com.sun.star.chart.BarDiagram</code> </li>
68 *   <li> <code>com.sun.star.chart.StackableDiagram</code> </li>
69 * </ul>
70 *  <p>
71 * Object implements the following interfaces :
72 * <ul>
73 *  <li> <code>com::sun::star::chart::XDiagram</code></li>
74 *  <li> <code>com::sun::star::chart::ChartAxisXSupplier</code></li>
75 *  <li> <code>com::sun::star::chart::Dim3DDiagram</code></li>
76 *  <li> <code>com::sun::star::chart::StockDiagram</code></li>
77 *  <li> <code>com::sun::star::chart::ChartAxisZSupplier</code></li>
78 *  <li> <code>com::sun::star::chart::XTwoAxisXSupplier</code></li>
79 *  <li> <code>com::sun::star::chart::LineDiagram</code></li>
80 *  <li> <code>com::sun::star::chart::BarDiagram</code></li>
81 *  <li> <code>com::sun::star::chart::XAxisYSupplier</code></li>
82 *  <li> <code>com::sun::star::chart::Diagram</code></li>
83 *  <li> <code>com::sun::star::chart::X3DDisplay</code></li>
84 *  <li> <code>com::sun::star::chart::ChartTwoAxisYSupplier</code></li>
85 *  <li> <code>com::sun::star::chart::StackableDiagram</code></li>
86 *  <li> <code>com::sun::star::chart::ChartAxisYSupplier</code></li>
87 *  <li> <code>com::sun::star::chart::XAxisXSupplier</code></li>
88 *  <li> <code>com::sun::star::chart::ChartTwoAxisXSupplier</code></li>
89 *  <li> <code>com::sun::star::drawing::XShape</code></li>
90 *  <li> <code>com::sun::star::chart::XTwoAxisYSupplier</code></li>
91 *  <li> <code>com::sun::star::chart::ChartStatistics</code></li>
92 *  <li> <code>com::sun::star::beans::XPropertySet</code></li>
93 *  <li> <code>com::sun::star::drawing::XShapeDescriptor</code></li>
94 *  <li> <code>com::sun::star::chart::XAxisZSupplier</code></li>
95 *  <li> <code>com::sun::star::chart::XStatisticDisplay</code></li>
96 * </ul>
97 * @see com.sun.star.chart.XDiagram
98 * @see com.sun.star.chart.ChartAxisXSupplier
99 * @see com.sun.star.chart.Dim3DDiagram
100 * @see com.sun.star.chart.StockDiagram
101 * @see com.sun.star.chart.ChartAxisZSupplier
102 * @see com.sun.star.chart.XTwoAxisXSupplier
103 * @see com.sun.star.chart.LineDiagram
104 * @see com.sun.star.chart.BarDiagram
105 * @see com.sun.star.chart.XAxisYSupplier
106 * @see com.sun.star.chart.Diagram
107 * @see com.sun.star.chart.X3DDisplay
108 * @see com.sun.star.chart.ChartTwoAxisYSupplier
109 * @see com.sun.star.chart.StackableDiagram
110 * @see com.sun.star.chart.ChartAxisYSupplier
111 * @see com.sun.star.chart.XAxisXSupplier
112 * @see com.sun.star.chart.ChartTwoAxisXSupplier
113 * @see com.sun.star.drawing.XShape
114 * @see com.sun.star.chart.XTwoAxisYSupplier
115 * @see com.sun.star.chart.ChartStatistics
116 * @see com.sun.star.beans.XPropertySet
117 * @see com.sun.star.drawing.XShapeDescriptor
118 * @see com.sun.star.chart.XAxisZSupplier
119 * @see com.sun.star.chart.XStatisticDisplay
120 * @see ifc.chart._XDiagram
121 * @see ifc.chart._ChartAxisXSupplier
122 * @see ifc.chart._Dim3DDiagram
123 * @see ifc.chart._StockDiagram
124 * @see ifc.chart._ChartAxisZSupplier
125 * @see ifc.chart._XTwoAxisXSupplier
126 * @see ifc.chart._LineDiagram
127 * @see ifc.chart._BarDiagram
128 * @see ifc.chart._XAxisYSupplier
129 * @see ifc.chart._Diagram
130 * @see ifc.chart._X3DDisplay
131 * @see ifc.chart._ChartTwoAxisYSupplier
132 * @see ifc.chart._StackableDiagram
133 * @see ifc.chart._ChartAxisYSupplier
134 * @see ifc.chart._XAxisXSupplier
135 * @see ifc.chart._ChartTwoAxisXSupplier
136 * @see ifc.drawing._XShape
137 * @see ifc.chart._XTwoAxisYSupplier
138 * @see ifc.chart._ChartStatistics
139 * @see ifc.beans._XPropertySet
140 * @see ifc.drawing._XShapeDescriptor
141 * @see ifc.chart._XAxisZSupplier
142 * @see ifc.chart._XStatisticDisplay
143 */
144 public class ChXDiagram extends TestCase {
145     XSpreadsheetDocument xSheetDoc = null;
146 
147     /**
148     * Creates Spreadsheet document.
149     */
150     protected void initialize( TestParameters tParam, PrintWriter log ) {
151         // get a soffice factory object
152         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF());
153 
154         try {
155             log.println( "creating a sheetdocument" );
156             xSheetDoc = SOF.createCalcDoc(null);;
157         } catch (com.sun.star.uno.Exception e) {
158             // Some exception occures.FAILED
159             e.printStackTrace( log );
160             throw new StatusException( "Couldn't create document", e );
161         }
162     }
163 
164     /**
165     * Disposes Spreadsheet document.
166     */
167     protected void cleanup( TestParameters tParam, PrintWriter log ) {
168         log.println( "    closing xSheetDoc " );
169         util.DesktopTools.closeDoc(xSheetDoc);
170     }
171 
172     /**
173     * Creating a Testenvironment for the interfaces to be tested.
174     * Retrieves a collection of spreadsheets from a document
175     * and takes one of them. Inserts some values into the cells of the some cell
176     * range address. Adds and retrieves the chart that using the data from
177     * the cells of this cell range address. Obtains the chart document which is
178     * embedded into the retrieved chart using the interface
179     * <code>XEmbeddedObjectSupplier</code>. Retrieves the diagram from
180     * the obtained chart document. The retrieved diagram is the instance of
181     * the service <code>com.sun.star.chart.Diagram</code>.
182     * Obtains the data source of the chart from the chart document.
183     * Creates a stock-diagram, a bar-diagram, a XY-diagram and line-diagram
184     * that are the instances of the following services:
185     * <ul>
186     *   <li> <code>com.sun.star.chart.StockDiagram</code> </li>
187     *   <li> <code>com.sun.star.chart.BarDiagram</code> </li>
188     *   <li> <code>com.sun.star.chart.LineDiagram</code> </li>
189     *   <li> <code>com.sun.star.chart.StackableDiagram</code> </li>
190     * </ul>.
191     * Object relations created :
192     * <ul>
193     *  <li> <code>'CHARTDOC'</code> for
194     *      {@link ifc.chart._Dim3DDiagram}, {@link ifc.chart._StockDiagram},
195     *      {@link ifc.chart._ChartAxisZSupplier}, {@link _LineDiagram},
196     *      {@link ifc.chart._BarDiagram}, {@link ifc.chart._Diagram},
197     *      {@link ifc.chart._ChartTwoAxisYSupplier},
198     *      {@link ifc.chart._StackableDiagram}, {@link ifc.chart._Diagram},
199     *      {@link ifc.chart._ChartAxisYSupplier},
200     *      {@link ifc.chart._ChartTwoAxisXSupplier},
201     *      {@link ifc.chart._ChartStatistics} (the obtained chart document)</li>
202     *  <li> <code>'ROWAMOUNT', 'COLAMOUNT'</code> for
203     *      {@link ifc.chart._XDiagram}(the number of chart columns and
204     *       the number of chart rows) </li>
205     *  <li> <code>'STOCK'</code> for
206     *      {@link ifc.chart._StockDiagram}(the created stock-diagram) </li>
207     *  <li> <code>'BAR'</code> for
208     *      {@link ifc.chart._BarDiagram}, {@link ifc.chart._ChartAxisZSupplier},
209     *      {@link ifc.chart._ChartTwoAxisXSupplier},
210     *      {@link ifc.chart._ChartTwoAxisYSupplier}(the created bar-diagram)</li>
211     *  <li> <code>'LINE'</code> for
212     *      {@link ifc.chart._LineDiagram}(the created XY-diagram) </li>
213     *  <li> <code>'STACK'</code> for
214     *      {@link ifc.chart._StackableDiagram}(the created Line-diagram) </li>
215     * </ul>
216     * @see com.sun.star.document.XEmbeddedObjectSupplier
217     * @see com.sun.star.chart.Diagram
218     * @see com.sun.star.chart.StockDiagram
219     */
220     protected synchronized TestEnvironment createTestEnvironment
221             (TestParameters Param, PrintWriter log) {
222 
223         XSpreadsheet oSheet=null;
224         XChartDocument xChartDoc=null;
225         XDiagram oObj = null;
226 
227         System.out.println("Getting spreadsheet") ;
228         XSpreadsheets oSheets = xSheetDoc.getSheets() ;
229         XIndexAccess oIndexSheets = (XIndexAccess)
230             UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
231         try {
232             oSheet = (XSpreadsheet) AnyConverter.toObject(
233                     new Type(XSpreadsheet.class),oIndexSheets.getByIndex(0));
234         } catch(com.sun.star.lang.WrappedTargetException e) {
235             e.printStackTrace(log);
236             throw new StatusException("Couldn't get sheet", e);
237         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
238             e.printStackTrace(log);
239             throw new StatusException("Couldn't get sheet", e);
240         } catch(com.sun.star.lang.IllegalArgumentException e) {
241             e.printStackTrace(log);
242             throw new StatusException("Couldn't get sheet", e);
243         }
244 
245         log.println("Creating the Header") ;
246 
247         insertIntoCell(1,0,"JAN",oSheet,"");
248         insertIntoCell(2,0,"FEB",oSheet,"");
249         insertIntoCell(3,0,"MAR",oSheet,"");
250         insertIntoCell(4,0,"APR",oSheet,"");
251         insertIntoCell(5,0,"MAI",oSheet,"");
252         insertIntoCell(6,0,"JUN",oSheet,"");
253         insertIntoCell(7,0,"JUL",oSheet,"");
254         insertIntoCell(8,0,"AUG",oSheet,"");
255         insertIntoCell(9,0,"SEP",oSheet,"");
256         insertIntoCell(10,0,"OCT",oSheet,"");
257         insertIntoCell(11,0,"NOV",oSheet,"");
258         insertIntoCell(12,0,"DEC",oSheet,"");
259         insertIntoCell(13,0,"SUM",oSheet,"");
260 
261         log.println("Fill the lines");
262 
263         insertIntoCell(0,1,"Smith",oSheet,"");
264         insertIntoCell(1,1,"42",oSheet,"V");
265         insertIntoCell(2,1,"58.9",oSheet,"V");
266         insertIntoCell(3,1,"-66.5",oSheet,"V");
267         insertIntoCell(4,1,"43.4",oSheet,"V");
268         insertIntoCell(5,1,"44.5",oSheet,"V");
269         insertIntoCell(6,1,"45.3",oSheet,"V");
270         insertIntoCell(7,1,"-67.3",oSheet,"V");
271         insertIntoCell(8,1,"30.5",oSheet,"V");
272         insertIntoCell(9,1,"23.2",oSheet,"V");
273         insertIntoCell(10,1,"-97.3",oSheet,"V");
274         insertIntoCell(11,1,"22.4",oSheet,"V");
275         insertIntoCell(12,1,"23.5",oSheet,"V");
276         insertIntoCell(13,1,"=SUM(B2:M2)",oSheet,"");
277 
278         insertIntoCell(0,2,"Jones",oSheet,"");
279         insertIntoCell(1,2,"21",oSheet,"V");
280         insertIntoCell(2,2,"40.9",oSheet,"V");
281         insertIntoCell(3,2,"-57.5",oSheet,"V");
282         insertIntoCell(4,2,"-23.4",oSheet,"V");
283         insertIntoCell(5,2,"34.5",oSheet,"V");
284         insertIntoCell(6,2,"59.3",oSheet,"V");
285         insertIntoCell(7,2,"27.3",oSheet,"V");
286         insertIntoCell(8,2,"-38.5",oSheet,"V");
287         insertIntoCell(9,2,"43.2",oSheet,"V");
288         insertIntoCell(10,2,"57.3",oSheet,"V");
289         insertIntoCell(11,2,"25.4",oSheet,"V");
290         insertIntoCell(12,2,"28.5",oSheet,"V");
291         insertIntoCell(13,2,"=SUM(B3:M3)",oSheet,"");
292 
293         insertIntoCell(0,3,"Brown",oSheet,"");
294         insertIntoCell(1,3,"31.45",oSheet,"V");
295         insertIntoCell(2,3,"-20.9",oSheet,"V");
296         insertIntoCell(3,3,"-117.5",oSheet,"V");
297         insertIntoCell(4,3,"23.4",oSheet,"V");
298         insertIntoCell(5,3,"-114.5",oSheet,"V");
299         insertIntoCell(6,3,"115.3",oSheet,"V");
300         insertIntoCell(7,3,"-171.3",oSheet,"V");
301         insertIntoCell(8,3,"89.5",oSheet,"V");
302         insertIntoCell(9,3,"41.2",oSheet,"V");
303         insertIntoCell(10,3,"71.3",oSheet,"V");
304         insertIntoCell(11,3,"25.4",oSheet,"V");
305         insertIntoCell(12,3,"38.5",oSheet,"V");
306         insertIntoCell(13,3,"=SUM(A4:L4)",oSheet,"");
307 
308         // insert a chart
309         Rectangle oRect = new Rectangle(500, 3000, 25000, 11000);
310 
311         XCellRange oRange = (XCellRange)
312             UnoRuntime.queryInterface(XCellRange.class, oSheet);
313         XCellRange myRange = oRange.getCellRangeByName("A1:N4");
314         XCellRangeAddressable oRangeAddr = (XCellRangeAddressable)
315             UnoRuntime.queryInterface(XCellRangeAddressable.class, myRange);
316         CellRangeAddress myAddr = oRangeAddr.getRangeAddress();
317 
318         CellRangeAddress[] oAddr = new CellRangeAddress[1];
319         oAddr[0] = myAddr;
320         XTableChartsSupplier oSupp = (XTableChartsSupplier)
321             UnoRuntime.queryInterface(XTableChartsSupplier.class, oSheet);
322 
323         log.println("Insert Chart");
324         XTableCharts oCharts = oSupp.getCharts();
325 
326 
327         if (!oCharts.hasByName("ChXDiagram")) {
328             oCharts.addNewByName("ChXDiagram", oRect, oAddr, true, true);
329         }
330 
331         // get the TableChart
332         XTableChart oChart = null;
333         try {
334             oChart = (XTableChart) AnyConverter.toObject(
335                 new Type(XTableChart.class),((XNameAccess)
336                     UnoRuntime.queryInterface(
337                         XNameAccess.class, oCharts)).getByName("ChXDiagram"));
338         } catch (com.sun.star.lang.WrappedTargetException e) {
339             e.printStackTrace(log);
340             throw new StatusException("Couldn't get TableChart", e);
341         } catch (com.sun.star.container.NoSuchElementException e) {
342             e.printStackTrace(log);
343             throw new StatusException("Couldn't get TableChart", e);
344         }  catch (com.sun.star.lang.IllegalArgumentException e) {
345             e.printStackTrace(log);
346             throw new StatusException("Couldn't get TableChart", e);
347         }
348 
349         XEmbeddedObjectSupplier oEOS = (XEmbeddedObjectSupplier)
350             UnoRuntime.queryInterface(XEmbeddedObjectSupplier.class, oChart);
351         XInterface oInt = oEOS.getEmbeddedObject();
352         xChartDoc = (XChartDocument)
353              UnoRuntime.queryInterface(XChartDocument.class,oInt);
354         oObj = (XDiagram) xChartDoc.getDiagram();
355 
356         log.println( "creating a new environment for chartdocument object" );
357         TestEnvironment tEnv = new TestEnvironment( oObj );
358 
359         log.println( "adding ChartDocument as mod relation to environment" );
360         tEnv.addObjRelation("CHARTDOC", xChartDoc);
361 
362         XChartDataArray da = (XChartDataArray)
363             UnoRuntime.queryInterface(XChartDataArray.class, xChartDoc.getData());
364         int cols = da.getColumnDescriptions().length;
365         int rows = da.getRowDescriptions().length;
366 
367         tEnv.addObjRelation("ROWAMOUNT", new Integer(rows));
368         tEnv.addObjRelation("COLAMOUNT", new Integer(cols));
369 
370         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)Param.getMSF());
371         Object stock = SOF.createDiagram(xChartDoc,"StockDiagram");
372         tEnv.addObjRelation("STOCK",stock);
373 
374         Object bar = SOF.createDiagram(xChartDoc,"BarDiagram");
375         tEnv.addObjRelation("BAR",bar);
376 
377         Object line = SOF.createDiagram(xChartDoc,"XYDiagram");
378         tEnv.addObjRelation("LINE",line);
379 
380         Object stack = SOF.createDiagram(xChartDoc,"LineDiagram");
381         tEnv.addObjRelation("STACK",stack);
382 
383         return tEnv;
384     } // finish method getTestEnvironment
385 
386     /**
387     * Inserts a value or a formula in the cell of the spreasheet.
388     * @param CellX is the column index of the cell
389     * @param CellY is the row index of the cell
390     * @param theValue string representation of the value
391     * @param TT1 specify the spreadsheet, the interface
392     * <code>com.sun.star.sheet.XSpreadsheet</code>
393     * @param flag if it's equal to <code>'V'</code> then the method inserts
394     * a double-value in the cell else it inserts a formula in the cell
395     */
396     public static void insertIntoCell(
397         int CellX, int CellY, String theValue, XSpreadsheet TT1, String flag) {
398 
399         XCell oCell = null;
400 
401         try {
402             oCell = TT1.getCellByPosition(CellX, CellY);
403         } catch (com.sun.star.lang.IndexOutOfBoundsException ex) {
404             System.out.println("Could not get Cell");
405         }
406         if (flag.equals("V")) {oCell.setValue((new Float(theValue)).floatValue());}
407                    else {oCell.setFormula(theValue);}
408 
409     } // end of insertIntoCell
410 
411 
412 }    // finish class ChXDiagram
413 
414