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