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