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