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 package ifc.sheet;
24 
25 import com.sun.star.awt.Point;
26 import com.sun.star.sheet.TableOperationMode;
27 import com.sun.star.sheet.XCellAddressable;
28 import com.sun.star.sheet.XCellRangeAddressable;
29 import com.sun.star.sheet.XMultipleOperation;
30 import com.sun.star.sheet.XSpreadsheet;
31 import com.sun.star.table.XCell;
32 import com.sun.star.table.XCellRange;
33 import com.sun.star.uno.UnoRuntime;
34 
35 import lib.MultiMethodTest;
36 import lib.Status;
37 import lib.StatusException;
38 
39 
40 public class _XMultipleOperation extends MultiMethodTest {
41     public XMultipleOperation oObj = null;
42     protected XSpreadsheet oSheet = null;
43     boolean both = true;
44 
before()45     protected void before() {
46         oSheet = (XSpreadsheet) tEnv.getObjRelation("SHEET");
47 
48         if (oSheet == null) {
49             throw new StatusException(Status.failed(
50                                               "Object relation oSheet is missing"));
51         }
52 
53         if (UnoRuntime.queryInterface(XSpreadsheet.class, tEnv.getTestObject()) != null) {
54             log.println("We have a sheet and won't do TableOperationMode.BOTH");
55             both = false;
56         }
57     }
58 
_setTableOperation()59     public void _setTableOperation() {
60         boolean res = true;
61         XCellRange cellRange = oSheet.getCellRangeByName("$A$17:$A$17");
62         XCellRangeAddressable CRA = (XCellRangeAddressable) UnoRuntime.queryInterface(
63                                             XCellRangeAddressable.class,
64                                             cellRange);
65         XCell cell = null;
66         XCell cell2 = null;
67 
68         try {
69             cell = oSheet.getCellByPosition(0, 16);
70             cell.setFormula("=a15+a16");
71             cell = oSheet.getCellByPosition(0, 14);
72             cell2 = oSheet.getCellByPosition(0, 15);
73         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
74             log.println("Exception while getting Cell " + e.getMessage());
75         }
76 
77         XCellAddressable CA = (XCellAddressable) UnoRuntime.queryInterface(
78                                       XCellAddressable.class, cell);
79         XCellAddressable CA2 = (XCellAddressable) UnoRuntime.queryInterface(
80                                        XCellAddressable.class, cell2);
81         Point[] cellCoords = new Point[3];
82         double[] cellValues = new double[3];
83 
84         log.println("filling cells");
85         fillCells();
86         log.println("setting TableOperation with parameter ROW");
87         oObj.setTableOperation(CRA.getRangeAddress(), TableOperationMode.ROW,
88                                CA.getCellAddress(), CA2.getCellAddress());
89         log.println("checking values");
90         cellCoords = new Point[] {
91             new Point(1, 1), new Point(2, 1), new Point(3, 1)
92         };
93         cellValues = new double[] { 5, 10, 15 };
94         res &= checkValues(cellCoords, cellValues);
95 
96         log.println("filling cells");
97         fillCells();
98         log.println("setting TableOperation with parameter COLUMN");
99         oObj.setTableOperation(CRA.getRangeAddress(),
100                                TableOperationMode.COLUMN, CA.getCellAddress(),
101                                CA2.getCellAddress());
102         log.println("checking values");
103         cellCoords = new Point[] {
104             new Point(1, 1), new Point(1, 2), new Point(1, 3)
105         };
106         cellValues = new double[] { 12, 24, 36 };
107         res &= checkValues(cellCoords, cellValues);
108 
109         if (both) {
110             log.println("filling cells");
111             fillCells();
112             log.println("setting TableOperation with parameter BOTH");
113             oObj.setTableOperation(CRA.getRangeAddress(),
114                                    TableOperationMode.BOTH,
115                                    CA.getCellAddress(), CA2.getCellAddress());
116             log.println("checking values");
117             cellCoords = new Point[] {
118                 new Point(1, 1), new Point(2, 2), new Point(3, 3)
119             };
120             cellValues = new double[] { 17, 34, 51 };
121             res &= checkValues(cellCoords, cellValues);
122         }
123 
124         tRes.tested("setTableOperation()", res);
125     }
126 
fillCells()127     protected void fillCells() {
128         XCell cell = null;
129 
130         try {
131             for (int k = 1; k < 5; k++) {
132                 cell = oSheet.getCellByPosition(0, k);
133                 cell.setValue(k * 12);
134                 cell = oSheet.getCellByPosition(k, 0);
135                 cell.setValue(k * 5);
136             }
137         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
138             log.println("Exception while filling Cells " + e.getMessage());
139         }
140     }
141 
checkValues(Point[] cellCoords, double[] cellValues)142     protected boolean checkValues(Point[] cellCoords, double[] cellValues) {
143         boolean res = true;
144 
145         for (int i = 0; i < cellValues.length; i++) {
146             try {
147                 boolean locres = oSheet.getCellByPosition(cellCoords[i].X,
148                                                           cellCoords[i].Y)
149                                        .getValue() == cellValues[i];
150                 res &= locres;
151 
152                 if (!locres) {
153                     log.println("Result differs for cell (" +
154                                 cellCoords[i].X + "," + cellCoords[i].Y +
155                                 ")");
156                     log.println("Expected: " + cellValues[i]);
157                     log.println("Getting: " +
158                                 oSheet.getCellByPosition(cellCoords[i].X,
159                                                          cellCoords[i].Y)
160                                       .getValue());
161                 }
162             } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
163                 log.println("Exception while checking Values " +
164                             e.getMessage());
165                 res &= false;
166             }
167         }
168 
169         return res;
170     }
171 
172     /**
173      * Restores initial component text.
174      */
after()175     protected void after() {
176         disposeEnvironment();
177     }
178 }