1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package ifc.sheet;
28 
29 import com.sun.star.awt.Point;
30 import com.sun.star.sheet.TableOperationMode;
31 import com.sun.star.sheet.XCellAddressable;
32 import com.sun.star.sheet.XCellRangeAddressable;
33 import com.sun.star.sheet.XMultipleOperation;
34 import com.sun.star.sheet.XSpreadsheet;
35 import com.sun.star.table.XCell;
36 import com.sun.star.table.XCellRange;
37 import com.sun.star.uno.UnoRuntime;
38 
39 import lib.MultiMethodTest;
40 import lib.Status;
41 import lib.StatusException;
42 
43 
44 public class _XMultipleOperation extends MultiMethodTest {
45     public XMultipleOperation oObj = null;
46     protected XSpreadsheet oSheet = null;
47     boolean both = true;
48 
49     protected void before() {
50         oSheet = (XSpreadsheet) tEnv.getObjRelation("SHEET");
51 
52         if (oSheet == null) {
53             throw new StatusException(Status.failed(
54                                               "Object relation oSheet is missing"));
55         }
56 
57         if (UnoRuntime.queryInterface(XSpreadsheet.class, tEnv.getTestObject()) != null) {
58             log.println("We have a sheet and won't do TableOperationMode.BOTH");
59             both = false;
60         }
61     }
62 
63     public void _setTableOperation() {
64         boolean res = true;
65         XCellRange cellRange = oSheet.getCellRangeByName("$A$17:$A$17");
66         XCellRangeAddressable CRA = (XCellRangeAddressable) UnoRuntime.queryInterface(
67                                             XCellRangeAddressable.class,
68                                             cellRange);
69         XCell cell = null;
70         XCell cell2 = null;
71 
72         try {
73             cell = oSheet.getCellByPosition(0, 16);
74             cell.setFormula("=a15+a16");
75             cell = oSheet.getCellByPosition(0, 14);
76             cell2 = oSheet.getCellByPosition(0, 15);
77         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
78             log.println("Exception while getting Cell " + e.getMessage());
79         }
80 
81         XCellAddressable CA = (XCellAddressable) UnoRuntime.queryInterface(
82                                       XCellAddressable.class, cell);
83         XCellAddressable CA2 = (XCellAddressable) UnoRuntime.queryInterface(
84                                        XCellAddressable.class, cell2);
85         Point[] cellCoords = new Point[3];
86         double[] cellValues = new double[3];
87 
88         log.println("filling cells");
89         fillCells();
90         log.println("setting TableOperation with parameter ROW");
91         oObj.setTableOperation(CRA.getRangeAddress(), TableOperationMode.ROW,
92                                CA.getCellAddress(), CA2.getCellAddress());
93         log.println("checking values");
94         cellCoords = new Point[] {
95             new Point(1, 1), new Point(2, 1), new Point(3, 1)
96         };
97         cellValues = new double[] { 5, 10, 15 };
98         res &= checkValues(cellCoords, cellValues);
99 
100         log.println("filling cells");
101         fillCells();
102         log.println("setting TableOperation with parameter COLUMN");
103         oObj.setTableOperation(CRA.getRangeAddress(),
104                                TableOperationMode.COLUMN, CA.getCellAddress(),
105                                CA2.getCellAddress());
106         log.println("checking values");
107         cellCoords = new Point[] {
108             new Point(1, 1), new Point(1, 2), new Point(1, 3)
109         };
110         cellValues = new double[] { 12, 24, 36 };
111         res &= checkValues(cellCoords, cellValues);
112 
113         if (both) {
114             log.println("filling cells");
115             fillCells();
116             log.println("setting TableOperation with parameter BOTH");
117             oObj.setTableOperation(CRA.getRangeAddress(),
118                                    TableOperationMode.BOTH,
119                                    CA.getCellAddress(), CA2.getCellAddress());
120             log.println("checking values");
121             cellCoords = new Point[] {
122                 new Point(1, 1), new Point(2, 2), new Point(3, 3)
123             };
124             cellValues = new double[] { 17, 34, 51 };
125             res &= checkValues(cellCoords, cellValues);
126         }
127 
128         tRes.tested("setTableOperation()", res);
129     }
130 
131     protected void fillCells() {
132         XCell cell = null;
133 
134         try {
135             for (int k = 1; k < 5; k++) {
136                 cell = oSheet.getCellByPosition(0, k);
137                 cell.setValue(k * 12);
138                 cell = oSheet.getCellByPosition(k, 0);
139                 cell.setValue(k * 5);
140             }
141         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
142             log.println("Exception while filling Cells " + e.getMessage());
143         }
144     }
145 
146     protected boolean checkValues(Point[] cellCoords, double[] cellValues) {
147         boolean res = true;
148 
149         for (int i = 0; i < cellValues.length; i++) {
150             try {
151                 boolean locres = oSheet.getCellByPosition(cellCoords[i].X,
152                                                           cellCoords[i].Y)
153                                        .getValue() == cellValues[i];
154                 res &= locres;
155 
156                 if (!locres) {
157                     log.println("Result differs for cell (" +
158                                 cellCoords[i].X + "," + cellCoords[i].Y +
159                                 ")");
160                     log.println("Expected: " + cellValues[i]);
161                     log.println("Getting: " +
162                                 oSheet.getCellByPosition(cellCoords[i].X,
163                                                          cellCoords[i].Y)
164                                       .getValue());
165                 }
166             } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
167                 log.println("Exception while checking Values " +
168                             e.getMessage());
169                 res &= false;
170             }
171         }
172 
173         return res;
174     }
175 
176     /**
177      * Restores initial component text.
178      */
179     protected void after() {
180         disposeEnvironment();
181     }
182 }