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 mod._sc;
24 
25 import com.sun.star.beans.XPropertySet;
26 import com.sun.star.container.XEnumerationAccess;
27 import com.sun.star.container.XIndexAccess;
28 import com.sun.star.lang.XMultiServiceFactory;
29 import com.sun.star.sheet.XSpreadsheet;
30 import com.sun.star.sheet.XSpreadsheetDocument;
31 import com.sun.star.sheet.XUniqueCellFormatRangesSupplier;
32 import com.sun.star.table.XCellRange;
33 import com.sun.star.uno.UnoRuntime;
34 import com.sun.star.uno.XInterface;
35 
36 import java.awt.Color;
37 
38 import java.io.PrintWriter;
39 
40 import lib.StatusException;
41 import lib.TestCase;
42 import lib.TestEnvironment;
43 import lib.TestParameters;
44 
45 import util.SOfficeFactory;
46 
47 
48 public class ScUniqueCellFormatsEnumeration extends TestCase {
49     static XSpreadsheetDocument xSheetDoc = null;
50     static XSpreadsheet oSheet = null;
51 
52     /**
53     * Creates Spreadsheet document.
54     */
initialize(TestParameters tParam, PrintWriter log)55     protected void initialize(TestParameters tParam, PrintWriter log) {
56         // get a soffice factory object
57         SOfficeFactory SOF = SOfficeFactory.getFactory(
58                                      (XMultiServiceFactory) tParam.getMSF());
59 
60         try {
61             log.println("creating a sheetdocument");
62             xSheetDoc = SOF.createCalcDoc(null);
63         } catch (com.sun.star.uno.Exception e) {
64             // Some exception occures.FAILED
65             e.printStackTrace(log);
66             throw new StatusException("Couldn't create document", e);
67         }
68     }
69 
70     /**
71     * Disposes Spreadsheet document.
72     */
cleanup(TestParameters tParam, PrintWriter log)73     protected void cleanup(TestParameters tParam, PrintWriter log) {
74         //add this lines after synchronisation
75 	//log.println("    disposing xSheetDoc ");
76         //DesktopTools.closeDoc(xSheetDoc);
77     }
78 
createTestEnvironment(TestParameters tParam, PrintWriter log)79     protected TestEnvironment createTestEnvironment(TestParameters tParam,
80                                                     PrintWriter log) {
81         log.println("Getting the first sheet");
82 
83         XIndexAccess xIA = (XIndexAccess) UnoRuntime.queryInterface(
84                                    XIndexAccess.class, xSheetDoc.getSheets());
85 
86         try {
87             oSheet = (XSpreadsheet) UnoRuntime.queryInterface(
88                              XSpreadsheet.class, xIA.getByIndex(0));
89         } catch (com.sun.star.lang.WrappedTargetException e) {
90             e.printStackTrace(log);
91             throw new StatusException("Couldn't get a spreadsheet", e);
92         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
93             e.printStackTrace(log);
94             throw new StatusException("Couldn't get a spreadsheet", e);
95         }
96 
97         changeColor("A1:A5", 0, 255, 0);
98         changeColor("A6:B10", 255, 0, 0);
99         changeColor("B1:B6", 0, 0, 255);
100         changeColor("B7", 0, 255, 0);
101         changeColor("B8:B10", 0, 0, 255);
102         changeColor("C1:C10", 0, 0, 255);
103         changeColor("D1:D10", 0, 255, 0);
104 
105         XUniqueCellFormatRangesSupplier xUCRS = (XUniqueCellFormatRangesSupplier) UnoRuntime.queryInterface(
106                                                         XUniqueCellFormatRangesSupplier.class,
107                                                         oSheet);
108 
109         XEnumerationAccess xEnum = (XEnumerationAccess) UnoRuntime.queryInterface(
110                                            XEnumerationAccess.class,
111                                            xUCRS.getUniqueCellFormatRanges());
112         XInterface oObj = xEnum.createEnumeration();
113         log.println("Implementationname: " + util.utils.getImplName(oObj));
114 
115         TestEnvironment tEnv = new TestEnvironment(oObj);
116 
117         tEnv.addObjRelation("ExpectedCount", new Integer(4));
118 
119         return tEnv;
120     }
121 
changeColor(String RangeName, int r, int g, int b)122     protected void changeColor(String RangeName, int r, int g, int b) {
123         XCellRange xRange = oSheet.getCellRangeByName(RangeName);
124         XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(
125                                             XPropertySet.class, xRange);
126         Color c = new Color(r, g, b);
127         int c2int = 16777216 + c.hashCode();
128 
129         try {
130             xPropertySet.setPropertyValue("CellBackColor", new Integer(c2int));
131         } catch (com.sun.star.beans.UnknownPropertyException e) {
132             log.println("Couldn't change CellFormat");
133         } catch (com.sun.star.beans.PropertyVetoException e) {
134             log.println("Couldn't change CellFormat");
135         } catch (com.sun.star.lang.IllegalArgumentException e) {
136             log.println("Couldn't change CellFormat");
137         } catch (com.sun.star.lang.WrappedTargetException e) {
138             log.println("Couldn't change CellFormat");
139         }
140     }
141 }
142