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