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