1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ScChartsObj.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 import java
.io
.PrintWriter
;
35 import lib
.StatusException
;
37 import lib
.TestEnvironment
;
38 import lib
.TestParameters
;
39 import util
.SOfficeFactory
;
41 import com
.sun
.star
.awt
.Rectangle
;
42 import com
.sun
.star
.container
.XIndexAccess
;
43 import com
.sun
.star
.lang
.XComponent
;
44 import com
.sun
.star
.lang
.XMultiServiceFactory
;
45 import com
.sun
.star
.sheet
.XCellRangeAddressable
;
46 import com
.sun
.star
.sheet
.XSpreadsheet
;
47 import com
.sun
.star
.sheet
.XSpreadsheetDocument
;
48 import com
.sun
.star
.sheet
.XSpreadsheets
;
49 import com
.sun
.star
.table
.CellRangeAddress
;
50 import com
.sun
.star
.table
.XCell
;
51 import com
.sun
.star
.table
.XCellRange
;
52 import com
.sun
.star
.table
.XTableCharts
;
53 import com
.sun
.star
.table
.XTableChartsSupplier
;
54 import com
.sun
.star
.uno
.AnyConverter
;
55 import com
.sun
.star
.uno
.Type
;
56 import com
.sun
.star
.uno
.UnoRuntime
;
59 * Test for object which is represented by service
60 * <code>com.sun.star.table.TableCharts</code>. <p>
61 * Object implements the following interfaces :
63 * <li> <code>com::sun::star::container::XNameAccess</code></li>
64 * <li> <code>com::sun::star::container::XIndexAccess</code></li>
65 * <li> <code>com::sun::star::container::XElementAccess</code></li>
66 * <li> <code>com::sun::star::table::XTableCharts</code></li>
68 * @see com.sun.star.table.TableCharts
69 * @see com.sun.star.container.XNameAccess
70 * @see com.sun.star.container.XIndexAccess
71 * @see com.sun.star.container.XElementAccess
72 * @see com.sun.star.table.XTableCharts
73 * @see ifc.container._XNameAccess
74 * @see ifc.container._XIndexAccess
75 * @see ifc.container._XElementAccess
76 * @see ifc.table._XTableCharts
78 public class ScChartsObj
extends TestCase
{
79 static XSpreadsheetDocument xSheetDoc
= null;
82 * Creates Spreadsheet document.
84 protected void initialize( TestParameters tParam
, PrintWriter log
) {
85 // get a soffice factory object
86 SOfficeFactory SOF
= SOfficeFactory
.getFactory( (XMultiServiceFactory
)tParam
.getMSF());
89 log
.println( "creating a sheetdocument" );
90 xSheetDoc
= SOF
.createCalcDoc(null);;
91 } catch (com
.sun
.star
.uno
.Exception e
) {
92 // Some exception occures.FAILED
93 e
.printStackTrace( log
);
94 throw new StatusException( "Couldn't create document", e
);
99 * Disposes Spreadsheet document.
101 protected void cleanup( TestParameters tParam
, PrintWriter log
) {
102 log
.println( " disposing xSheetDoc " );
103 XComponent oComp
= (XComponent
)
104 UnoRuntime
.queryInterface(XComponent
.class, xSheetDoc
);
105 util
.DesktopTools
.closeDoc(oComp
);
109 * Creating a Testenvironment for the interfaces to be tested.
110 * Retrieves a collection of spreadsheets from a document
111 * and takes one of them. Inserts some values into the cells of the some cell
112 * range address. Obtains the collection of the charts using the interface
113 * <code>XTableChartsSupplier</code>. Creates and adds the chart that using
114 * the data from the cells of this cell range address. Collection of
115 * the charts is the instance of the service
116 * <code>com.sun.star.table.TableCharts</code>.
117 * Object relations created :
119 * <li> <code>'RECT'</code> for
120 * {@link ifc.table._XTableCharts}(of type <code>Rectangle</code>
121 * the position of the chart)</li>
122 * <li> <code>'ADDR'</code> for
123 * {@link ifc.table._XTableCharts}(of type
124 * <code>com.sun.star.table.CellRangeAddress[]</code> data source ranges
125 * for chart creating)</li>
127 * @see com.sun.star.container.XNamed
128 * @see com.sun.star.table.XTableChartsSupplier
130 protected synchronized TestEnvironment
createTestEnvironment(TestParameters Param
, PrintWriter log
) {
132 XSpreadsheet oSheet
=null;
135 log
.println("Getting spreadsheet") ;
136 XSpreadsheets oSheets
= xSheetDoc
.getSheets() ;
137 XIndexAccess oIndexSheets
= (XIndexAccess
)
138 UnoRuntime
.queryInterface(XIndexAccess
.class, oSheets
);
139 oSheet
= (XSpreadsheet
) AnyConverter
.toObject(
140 new Type(XSpreadsheet
.class),oIndexSheets
.getByIndex(0));
141 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
142 log
.println("Couldn't get Sheet ");
143 e
.printStackTrace(log
);
144 throw new StatusException("Couldn't get sheet", e
);
145 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
146 log
.println("Couldn't get Sheet ");
147 e
.printStackTrace(log
);
148 throw new StatusException("Couldn't get sheet", e
);
149 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
150 log
.println("Couldn't get Sheet ");
151 e
.printStackTrace(log
);
152 throw new StatusException("Couldn't get sheet", e
);
155 log
.println("Creating the Header") ;
157 insertIntoCell(1,0,"JAN",oSheet
,"");
158 insertIntoCell(2,0,"FEB",oSheet
,"");
159 insertIntoCell(3,0,"MAR",oSheet
,"");
160 insertIntoCell(4,0,"APR",oSheet
,"");
161 insertIntoCell(5,0,"MAI",oSheet
,"");
162 insertIntoCell(6,0,"JUN",oSheet
,"");
163 insertIntoCell(7,0,"JUL",oSheet
,"");
164 insertIntoCell(8,0,"AUG",oSheet
,"");
165 insertIntoCell(9,0,"SEP",oSheet
,"");
166 insertIntoCell(10,0,"OCT",oSheet
,"");
167 insertIntoCell(11,0,"NOV",oSheet
,"");
168 insertIntoCell(12,0,"DEC",oSheet
,"");
169 insertIntoCell(13,0,"SUM",oSheet
,"");
171 log
.println("Fill the lines");
173 insertIntoCell(0,1,"Smith",oSheet
,"");
174 insertIntoCell(1,1,"42",oSheet
,"V");
175 insertIntoCell(2,1,"58.9",oSheet
,"V");
176 insertIntoCell(3,1,"-66.5",oSheet
,"V");
177 insertIntoCell(4,1,"43.4",oSheet
,"V");
178 insertIntoCell(5,1,"44.5",oSheet
,"V");
179 insertIntoCell(6,1,"45.3",oSheet
,"V");
180 insertIntoCell(7,1,"-67.3",oSheet
,"V");
181 insertIntoCell(8,1,"30.5",oSheet
,"V");
182 insertIntoCell(9,1,"23.2",oSheet
,"V");
183 insertIntoCell(10,1,"-97.3",oSheet
,"V");
184 insertIntoCell(11,1,"22.4",oSheet
,"V");
185 insertIntoCell(12,1,"23.5",oSheet
,"V");
186 insertIntoCell(13,1,"=SUM(B2:M2)",oSheet
,"");
188 insertIntoCell(0,2,"Jones",oSheet
,"");
189 insertIntoCell(1,2,"21",oSheet
,"V");
190 insertIntoCell(2,2,"40.9",oSheet
,"V");
191 insertIntoCell(3,2,"-57.5",oSheet
,"V");
192 insertIntoCell(4,2,"-23.4",oSheet
,"V");
193 insertIntoCell(5,2,"34.5",oSheet
,"V");
194 insertIntoCell(6,2,"59.3",oSheet
,"V");
195 insertIntoCell(7,2,"27.3",oSheet
,"V");
196 insertIntoCell(8,2,"-38.5",oSheet
,"V");
197 insertIntoCell(9,2,"43.2",oSheet
,"V");
198 insertIntoCell(10,2,"57.3",oSheet
,"V");
199 insertIntoCell(11,2,"25.4",oSheet
,"V");
200 insertIntoCell(12,2,"28.5",oSheet
,"V");
201 insertIntoCell(13,2,"=SUM(B3:M3)",oSheet
,"");
203 insertIntoCell(0,3,"Brown",oSheet
,"");
204 insertIntoCell(1,3,"31.45",oSheet
,"V");
205 insertIntoCell(2,3,"-20.9",oSheet
,"V");
206 insertIntoCell(3,3,"-117.5",oSheet
,"V");
207 insertIntoCell(4,3,"23.4",oSheet
,"V");
208 insertIntoCell(5,3,"-114.5",oSheet
,"V");
209 insertIntoCell(6,3,"115.3",oSheet
,"V");
210 insertIntoCell(7,3,"-171.3",oSheet
,"V");
211 insertIntoCell(8,3,"89.5",oSheet
,"V");
212 insertIntoCell(9,3,"41.2",oSheet
,"V");
213 insertIntoCell(10,3,"71.3",oSheet
,"V");
214 insertIntoCell(11,3,"25.4",oSheet
,"V");
215 insertIntoCell(12,3,"38.5",oSheet
,"V");
216 insertIntoCell(13,3,"=SUM(A4:L4)",oSheet
,"");
219 Rectangle oRect
= new Rectangle(500, 3000, 25000, 11000);
221 XCellRange oRange
= (XCellRange
)
222 UnoRuntime
.queryInterface(XCellRange
.class, oSheet
);
223 XCellRange myRange
= oRange
.getCellRangeByName("A1:N4");
224 XCellRangeAddressable oRangeAddr
= (XCellRangeAddressable
)
225 UnoRuntime
.queryInterface(XCellRangeAddressable
.class, myRange
);
226 CellRangeAddress myAddr
= oRangeAddr
.getRangeAddress();
228 CellRangeAddress
[] oAddr
= new CellRangeAddress
[1];
230 XTableChartsSupplier oSupp
= (XTableChartsSupplier
)
231 UnoRuntime
.queryInterface(XTableChartsSupplier
.class, oSheet
);
234 log
.println("Insert Chart");
235 XTableCharts oCharts
= oSupp
.getCharts();
236 oCharts
.addNewByName("ScChartObj", oRect
, oAddr
, true, true);
238 log
.println("creating a new environment for object");
239 TestEnvironment tEnv
= new TestEnvironment(oCharts
);
241 tEnv
.addObjRelation("RECT", oRect
);
242 tEnv
.addObjRelation("ADDR", oAddr
);
248 * Inserts a value or a formula in the cell of the spreasheet.
249 * @param CellX is the column index of the cell
250 * @param CellY is the row index of the cell
251 * @param theValue string representation of the value
252 * @param TT1 specify the spreadsheet, the interface
253 * <code>com.sun.star.sheet.XSpreadsheet</code>
254 * @param flag if it's equal to <code>'V'</code> then the method inserts
255 * a double-value in the cell else it inserts a formula in the cell
257 public static void insertIntoCell(
258 int CellX
, int CellY
, String theValue
, XSpreadsheet TT1
, String flag
) {
263 oCell
= TT1
.getCellByPosition(CellX
, CellY
);
264 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException ex
) {
265 System
.out
.println("Could not get Cell");
268 if (flag
.equals("V")) {
269 oCell
.setValue(new Float(theValue
).floatValue());
271 oCell
.setFormula(theValue
);
274 } // end of insertIntoCell