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 
28 package mod._sc;
29 
30 import java.io.PrintWriter;
31 
32 import lib.StatusException;
33 import lib.TestCase;
34 import lib.TestEnvironment;
35 import lib.TestParameters;
36 import util.SOfficeFactory;
37 import util.utils;
38 
39 import com.sun.star.beans.PropertyValue;
40 import com.sun.star.beans.XPropertySet;
41 import com.sun.star.container.XEnumerationAccess;
42 import com.sun.star.container.XIndexAccess;
43 import com.sun.star.lang.XComponent;
44 import com.sun.star.lang.XMultiServiceFactory;
45 import com.sun.star.sheet.ConditionOperator;
46 import com.sun.star.sheet.XSheetConditionalEntries;
47 import com.sun.star.sheet.XSpreadsheet;
48 import com.sun.star.sheet.XSpreadsheetDocument;
49 import com.sun.star.sheet.XSpreadsheets;
50 import com.sun.star.table.CellAddress;
51 import com.sun.star.uno.AnyConverter;
52 import com.sun.star.uno.Type;
53 import com.sun.star.uno.UnoRuntime;
54 import com.sun.star.uno.XInterface;
55 
56 public class ScIndexEnumeration_TableConditionalEntryEnumeration extends TestCase {
57     static XSpreadsheetDocument xSpreadsheetDoc = null;
58 
59     /**
60     * Creates Spreadsheet document.
61     */
62     protected void initialize( TestParameters tParam, PrintWriter log ) {
63         // get a soffice factory object
64         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF());
65 
66         try {
67             log.println( "creating a sheetdocument" );
68             xSpreadsheetDoc = SOF.createCalcDoc(null);;
69         } catch (com.sun.star.uno.Exception e) {
70             // Some exception occures.FAILED
71             e.printStackTrace( log );
72             throw new StatusException( "Couldn't create document", e );
73         }
74     }
75 
76     /**
77     * Disposes Spreadsheet document.
78     */
79     protected void cleanup( TestParameters tParam, PrintWriter log ) {
80         log.println( "    disposing xSheetDoc " );
81         XComponent oComp = (XComponent)
82             UnoRuntime.queryInterface (XComponent.class, xSpreadsheetDoc) ;
83         util.DesktopTools.closeDoc(oComp);
84     }
85 
86     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
87 
88         XInterface oObj = null;
89         log.println("getting sheets");
90         XSpreadsheets xSpreadsheets = (XSpreadsheets)xSpreadsheetDoc.getSheets();
91 
92         log.println("getting a sheet");
93         XSpreadsheet oSheet = null;
94         XIndexAccess oIndexAccess = (XIndexAccess)
95             UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
96 
97         try {
98             oSheet = (XSpreadsheet) AnyConverter.toObject(
99                     new Type(XSpreadsheet.class),oIndexAccess.getByIndex(0));
100         } catch (com.sun.star.lang.WrappedTargetException e) {
101             e.printStackTrace(log);
102             throw new StatusException( "Couldn't get a spreadsheet", e);
103         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
104             e.printStackTrace(log);
105             throw new StatusException( "Couldn't get a spreadsheet", e);
106         } catch (com.sun.star.lang.IllegalArgumentException e) {
107             e.printStackTrace(log);
108             throw new StatusException( "Couldn't get a spreadsheet", e);
109         }
110 
111         log.println("filling some cells");
112         try {
113             oSheet.getCellByPosition(5, 5).setValue(15);
114             oSheet.getCellByPosition(1, 4).setValue(10);
115             oSheet.getCellByPosition(2, 0).setValue(-5.15);
116         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
117             log.println("Exception occurred while filling cells");
118             e.printStackTrace(log);
119         }
120 
121         Object CFormat = null;
122         XPropertySet Props = null;
123 
124         try {
125             Props = (XPropertySet)
126                 UnoRuntime.queryInterface(XPropertySet.class, oSheet);
127             CFormat = Props.getPropertyValue("ConditionalFormat");
128             if (utils.isVoid(CFormat)) {
129                 log.println("Property 'ConditionalFormat' is void");
130             }
131         } catch (com.sun.star.lang.WrappedTargetException e){
132             e.printStackTrace(log);
133             throw new StatusException("Couldn't create instance", e);
134         } catch (com.sun.star.beans.UnknownPropertyException e){
135             e.printStackTrace(log);
136             throw new StatusException("Couldn't create instance", e);
137         }
138 
139         try {
140             XSheetConditionalEntries xSCE = (XSheetConditionalEntries)
141                 UnoRuntime.queryInterface(XSheetConditionalEntries.class, CFormat);
142             xSCE.addNew(Conditions());
143             Props.setPropertyValue("ConditionalFormat", xSCE);
144             oObj = xSCE;
145         } catch (com.sun.star.lang.WrappedTargetException e) {
146             e.printStackTrace(log);
147             throw new StatusException(
148                 "Exception occurred while getting Entry", e);
149         } catch (com.sun.star.lang.IllegalArgumentException e) {
150             e.printStackTrace(log);
151             throw new StatusException(
152                 "Exception occurred while getting Entry", e);
153         } catch (com.sun.star.beans.PropertyVetoException e) {
154             e.printStackTrace(log);
155             throw new StatusException(
156                 "Exception occurred while getting Entry", e);
157         } catch (com.sun.star.beans.UnknownPropertyException e) {
158             e.printStackTrace(log);
159             throw new StatusException(
160                 "Exception occurred while getting Entry", e);
161         }
162 
163         log.println("creating a new environment for object");
164         XEnumerationAccess ea = (XEnumerationAccess)
165                     UnoRuntime.queryInterface(XEnumerationAccess.class,oObj);
166 
167         oObj = ea.createEnumeration();
168 
169         log.println("ImplementationName: "+util.utils.getImplName(oObj));
170         // creating test environment
171         TestEnvironment tEnv = new TestEnvironment( oObj );
172 
173         tEnv.addObjRelation("ENUM",ea);
174 
175         return tEnv;
176     } // finish method getTestEnvironment
177 
178     /**
179     * Returns the array of the property values that was filled by condition
180     * values.
181     */
182     protected PropertyValue[] Conditions() {
183         PropertyValue[] con = new PropertyValue[5];
184         CellAddress ca = new CellAddress();
185         ca.Column = 1;
186         ca.Row = 5;
187         ca.Sheet = 0;
188         con[0] = new PropertyValue();
189         con[0].Name = "StyleName";
190         con[0].Value = "Result2";
191         con[1] = new PropertyValue();
192         con[1].Name = "Formula1";
193         con[1].Value = "$Sheet1.$B$5";
194         con[2] = new PropertyValue();
195         con[2].Name = "Formula2";
196         con[2].Value = "";
197         con[3] = new PropertyValue();
198         con[3].Name = "Operator";
199         con[3].Value = ConditionOperator.EQUAL;
200         con[4] = new PropertyValue();
201         con[4].Name = "SourcePosition";
202         con[4].Value = ca;
203         return con;
204     }
205 
206 }    // finish class ScTableConditionalEntry
207 
208