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 
24 package mod._sc;
25 
26 import java.io.PrintWriter;
27 
28 import lib.StatusException;
29 import lib.TestCase;
30 import lib.TestEnvironment;
31 import lib.TestParameters;
32 import util.SOfficeFactory;
33 
34 import com.sun.star.container.XEnumerationAccess;
35 import com.sun.star.container.XIndexAccess;
36 import com.sun.star.lang.XComponent;
37 import com.sun.star.lang.XMultiServiceFactory;
38 import com.sun.star.sheet.XSheetAnnotations;
39 import com.sun.star.sheet.XSheetAnnotationsSupplier;
40 import com.sun.star.sheet.XSpreadsheet;
41 import com.sun.star.sheet.XSpreadsheetDocument;
42 import com.sun.star.sheet.XSpreadsheets;
43 import com.sun.star.uno.AnyConverter;
44 import com.sun.star.uno.Type;
45 import com.sun.star.uno.UnoRuntime;
46 import com.sun.star.uno.XInterface;
47 
48 
49 public class ScIndexEnumeration_CellAnnotationsEnumeration extends TestCase {
50     static XSpreadsheetDocument xSheetDoc = null;
51 
52     /**
53     * Creates Spreadsheet document.
54     */
55     protected void initialize( TestParameters tParam, PrintWriter log ) {
56         // get a soffice factory object
57         SOfficeFactory SOF = SOfficeFactory.getFactory( (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     */
72     protected void cleanup( TestParameters tParam, PrintWriter log ) {
73         log.println( "    disposing xSheetDoc " );
74         XComponent oComp = (XComponent)
75             UnoRuntime.queryInterface (XComponent.class, xSheetDoc);
76         util.DesktopTools.closeDoc(oComp);
77     }
78 
79     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
80 
81         XInterface oObj = null;
82 
83         log.println("getting sheets");
84         XSpreadsheets xSpreadsheets = (XSpreadsheets)xSheetDoc.getSheets();
85 
86         log.println("getting a sheet");
87         XSpreadsheet oSheet = null;
88         XIndexAccess oIndexAccess = (XIndexAccess)
89             UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
90         try {
91             oSheet = (XSpreadsheet) AnyConverter.toObject(
92                     new Type(XSpreadsheet.class),oIndexAccess.getByIndex(0));
93         } catch (com.sun.star.lang.WrappedTargetException e) {
94             e.printStackTrace(log);
95             throw new StatusException( "Couldn't get a spreadsheet", e);
96         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
97             e.printStackTrace(log);
98             throw new StatusException( "Couldn't get a spreadsheet", e);
99         } catch (com.sun.star.lang.IllegalArgumentException e) {
100             e.printStackTrace(log);
101             throw new StatusException( "Couldn't get a spreadsheet", e);
102         }
103 
104         log.println("filling some cells");
105         try {
106             oSheet.getCellByPosition(5, 5).setValue(15);
107             oSheet.getCellByPosition(1, 4).setValue(10);
108             oSheet.getCellByPosition(2, 0).setValue(-5.15);
109         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
110             e.printStackTrace(log);
111             throw new StatusException(
112                 "Exception occurred while filling cells", e);
113         }
114 
115         XSheetAnnotationsSupplier sas = (XSheetAnnotationsSupplier)
116             UnoRuntime.queryInterface(XSheetAnnotationsSupplier.class, oSheet);
117 
118         XSheetAnnotations anno = sas.getAnnotations();
119         XEnumerationAccess ea = (XEnumerationAccess)
120                     UnoRuntime.queryInterface(XEnumerationAccess.class, anno);
121 
122         oObj = ea.createEnumeration();
123 
124         log.println("ImplementationName: "+util.utils.getImplName(oObj));
125 
126         log.println("creating a new environment for object");
127         TestEnvironment tEnv = new TestEnvironment(oObj);
128 
129         tEnv.addObjRelation("ENUM",ea);
130 
131         return tEnv;
132     }
133 }
134 
135