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: ScChartObj.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
.container
.XNameAccess
;
44 import com
.sun
.star
.lang
.XComponent
;
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
;
61 * Test for object which is represented by service
62 * <code>com.sun.star.table.TableChart</code>. <p>
63 * Object implements the following interfaces :
65 * <li> <code>com::sun::star::container::XNamed</code></li>
66 * <li> <code>com::sun::star::table::XTableChart</code></li>
67 * <li> <code>com::sun::star::document::XEmbeddedObjectSupplier</code></li>
69 * @see com.sun.star.table.TableChart
70 * @see com.sun.star.container.XNamed
71 * @see com.sun.star.table.XTableChart
72 * @see com.sun.star.document.XEmbeddedObjectSupplier
73 * @see ifc.container._XNamed
74 * @see ifc.table._XTableChart
75 * @see ifc.document._XEmbeddedObjectSupplier
77 public class ScChartObj
extends TestCase
{
78 static XSpreadsheetDocument xSheetDoc
= null;
81 * Creates Spreadsheet document.
83 protected void initialize( TestParameters tParam
, PrintWriter log
) {
84 // get a soffice factory object
85 SOfficeFactory SOF
= SOfficeFactory
.getFactory( (XMultiServiceFactory
)tParam
.getMSF());
88 log
.println( "creating a sheetdocument" );
89 xSheetDoc
= SOF
.createCalcDoc(null);;
90 } catch (com
.sun
.star
.uno
.Exception e
) {
91 // Some exception occures.FAILED
92 e
.printStackTrace( log
);
93 throw new StatusException( "Couldn't create document", e
);
98 * Disposes Spreadsheet document.
100 protected void cleanup( TestParameters tParam
, PrintWriter log
) {
101 log
.println( " disposing xSheetDoc " );
102 XComponent oComp
= (XComponent
)
103 UnoRuntime
.queryInterface(XComponent
.class, xSheetDoc
);
104 util
.DesktopTools
.closeDoc(oComp
);
108 * Creating a Testenvironment for the interfaces to be tested.
109 * Retrieves a collection of spreadsheets from a document
110 * and takes one of them. Inserts some values into the cells of the some cell
111 * range address. Adds the chart that using the data from the cells of this
112 * cell range address. New chart is the instance of
113 * the service <code>com.sun.star.table.TableChart</code>.
114 * Object relations created :
116 * <li> <code>'setName'</code> for
117 * {@link ifc.container._XNamed}(the method <code>setName</code> must
118 * not be tested)</li>
120 * @see com.sun.star.container.XNamed
122 protected synchronized TestEnvironment
createTestEnvironment(TestParameters Param
, PrintWriter log
) {
124 XSpreadsheet oSheet
=null;
127 log
.println("Getting spreadsheet") ;
128 XSpreadsheets oSheets
= xSheetDoc
.getSheets() ;
129 XIndexAccess oIndexSheets
= (XIndexAccess
)
130 UnoRuntime
.queryInterface(XIndexAccess
.class, oSheets
);
131 oSheet
= (XSpreadsheet
) AnyConverter
.toObject(
132 new Type(XSpreadsheet
.class),oIndexSheets
.getByIndex(0));
133 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
134 log
.println("Couldn't get Sheet ");
135 e
.printStackTrace(log
);
136 throw new StatusException("Couldn't get sheet", e
);
137 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
138 log
.println("Couldn't get Sheet ");
139 e
.printStackTrace(log
);
140 throw new StatusException("Couldn't get sheet", e
);
141 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
142 log
.println("Couldn't get Sheet ");
143 e
.printStackTrace(log
);
144 throw new StatusException("Couldn't get sheet", e
);
147 log
.println("Creating the Header") ;
149 insertIntoCell(1,0,"JAN",oSheet
,"");
150 insertIntoCell(2,0,"FEB",oSheet
,"");
151 insertIntoCell(3,0,"MAR",oSheet
,"");
152 insertIntoCell(4,0,"APR",oSheet
,"");
153 insertIntoCell(5,0,"MAI",oSheet
,"");
154 insertIntoCell(6,0,"JUN",oSheet
,"");
155 insertIntoCell(7,0,"JUL",oSheet
,"");
156 insertIntoCell(8,0,"AUG",oSheet
,"");
157 insertIntoCell(9,0,"SEP",oSheet
,"");
158 insertIntoCell(10,0,"OCT",oSheet
,"");
159 insertIntoCell(11,0,"NOV",oSheet
,"");
160 insertIntoCell(12,0,"DEC",oSheet
,"");
161 insertIntoCell(13,0,"SUM",oSheet
,"");
163 log
.println("Fill the lines");
165 insertIntoCell(0,1,"Smith",oSheet
,"");
166 insertIntoCell(1,1,"42",oSheet
,"V");
167 insertIntoCell(2,1,"58.9",oSheet
,"V");
168 insertIntoCell(3,1,"-66.5",oSheet
,"V");
169 insertIntoCell(4,1,"43.4",oSheet
,"V");
170 insertIntoCell(5,1,"44.5",oSheet
,"V");
171 insertIntoCell(6,1,"45.3",oSheet
,"V");
172 insertIntoCell(7,1,"-67.3",oSheet
,"V");
173 insertIntoCell(8,1,"30.5",oSheet
,"V");
174 insertIntoCell(9,1,"23.2",oSheet
,"V");
175 insertIntoCell(10,1,"-97.3",oSheet
,"V");
176 insertIntoCell(11,1,"22.4",oSheet
,"V");
177 insertIntoCell(12,1,"23.5",oSheet
,"V");
178 insertIntoCell(13,1,"=SUM(B2:M2)",oSheet
,"");
180 insertIntoCell(0,2,"Jones",oSheet
,"");
181 insertIntoCell(1,2,"21",oSheet
,"V");
182 insertIntoCell(2,2,"40.9",oSheet
,"V");
183 insertIntoCell(3,2,"-57.5",oSheet
,"V");
184 insertIntoCell(4,2,"-23.4",oSheet
,"V");
185 insertIntoCell(5,2,"34.5",oSheet
,"V");
186 insertIntoCell(6,2,"59.3",oSheet
,"V");
187 insertIntoCell(7,2,"27.3",oSheet
,"V");
188 insertIntoCell(8,2,"-38.5",oSheet
,"V");
189 insertIntoCell(9,2,"43.2",oSheet
,"V");
190 insertIntoCell(10,2,"57.3",oSheet
,"V");
191 insertIntoCell(11,2,"25.4",oSheet
,"V");
192 insertIntoCell(12,2,"28.5",oSheet
,"V");
193 insertIntoCell(13,2,"=SUM(B3:M3)",oSheet
,"");
195 insertIntoCell(0,3,"Brown",oSheet
,"");
196 insertIntoCell(1,3,"31.45",oSheet
,"V");
197 insertIntoCell(2,3,"-20.9",oSheet
,"V");
198 insertIntoCell(3,3,"-117.5",oSheet
,"V");
199 insertIntoCell(4,3,"23.4",oSheet
,"V");
200 insertIntoCell(5,3,"-114.5",oSheet
,"V");
201 insertIntoCell(6,3,"115.3",oSheet
,"V");
202 insertIntoCell(7,3,"-171.3",oSheet
,"V");
203 insertIntoCell(8,3,"89.5",oSheet
,"V");
204 insertIntoCell(9,3,"41.2",oSheet
,"V");
205 insertIntoCell(10,3,"71.3",oSheet
,"V");
206 insertIntoCell(11,3,"25.4",oSheet
,"V");
207 insertIntoCell(12,3,"38.5",oSheet
,"V");
208 insertIntoCell(13,3,"=SUM(A4:L4)",oSheet
,"");
212 Rectangle oRect
= new Rectangle(500, 3000, 25000, 11000);
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();
221 CellRangeAddress
[] oAddr
= new CellRangeAddress
[1];
223 XTableChartsSupplier oSupp
= (XTableChartsSupplier
)
224 UnoRuntime
.queryInterface(XTableChartsSupplier
.class, oSheet
);
226 log
.println("Insert Chart");
228 XTableCharts oCharts
= oSupp
.getCharts();
229 oCharts
.addNewByName("ScChartObj", oRect
, oAddr
, true, true);
231 // get the TableChart
232 XTableChart oChart
= null;
234 XNameAccess names
= (XNameAccess
) AnyConverter
.toObject(
235 new Type(XNameAccess
.class),UnoRuntime
.queryInterface(
236 XNameAccess
.class, oCharts
));
238 oChart
= (XTableChart
) AnyConverter
.toObject(
239 new Type(XTableChart
.class),names
.getByName("ScChartObj"));
241 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
242 log
.println("Couldn't get TableChart");
243 e
.printStackTrace(log
);
244 throw new StatusException("Couldn't get TableChart", e
);
245 } catch (com
.sun
.star
.container
.NoSuchElementException e
) {
246 log
.println("Couldn't get TableChart");
247 e
.printStackTrace(log
);
248 throw new StatusException("Couldn't get TableChart", e
);
249 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
250 log
.println("Couldn't get TableChart");
251 e
.printStackTrace(log
);
252 throw new StatusException("Couldn't get TableChart", e
);
255 log
.println("creating a new environment for object");
256 TestEnvironment tEnv
= new TestEnvironment(oChart
);
258 //adding ObjRelation 'setName' for 'XNamed'
259 tEnv
.addObjRelation("setName", new Boolean(true));
265 * Inserts a value or a formula in the cell of the spreasheet.
266 * @param CellX is the column index of the cell
267 * @param CellY is the row index of the cell
268 * @param theValue string representation of the value
269 * @param TT1 specify the spreadsheet, the interface
270 * <code>com.sun.star.sheet.XSpreadsheet</code>
271 * @param flag if it's equal to <code>'V'</code> then the method inserts
272 * a double-value in the cell else it inserts a formula in the cell
274 public static void insertIntoCell(
275 int CellX
, int CellY
, String theValue
, XSpreadsheet TT1
, String flag
)
280 oCell
= TT1
.getCellByPosition(CellX
, CellY
);
281 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException ex
) {
282 System
.out
.println("Could not get Cell");
285 if (flag
.equals("V")) {
286 oCell
.setValue(new Float(theValue
).floatValue());
288 oCell
.setFormula(theValue
);
290 } // end of insertIntoCell