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