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.container.XIndexAccess;
26 import com.sun.star.lang.XComponent;
27 import com.sun.star.lang.XMultiServiceFactory;
28 import com.sun.star.sheet.XSheetAnnotation;
29 import com.sun.star.sheet.XSheetAnnotationAnchor;
30 import com.sun.star.sheet.XSheetAnnotationShapeSupplier;
31 import com.sun.star.sheet.XSpreadsheet;
32 import com.sun.star.sheet.XSpreadsheetDocument;
33 import com.sun.star.sheet.XSpreadsheets;
34 import com.sun.star.table.CellAddress;
35 import com.sun.star.table.XCell;
36 import com.sun.star.table.XCellRange;
37 import com.sun.star.text.XSimpleText;
38 import com.sun.star.uno.AnyConverter;
39 import com.sun.star.uno.Type;
40 import com.sun.star.uno.UnoRuntime;
41 import com.sun.star.uno.XInterface;
42 
43 import lib.StatusException;
44 import lib.TestCase;
45 import lib.TestEnvironment;
46 import lib.TestParameters;
47 
48 import util.SOfficeFactory;
49 import util.utils;
50 
51 import java.io.PrintWriter;
52 import util.DefaultDsc;
53 import util.InstCreator;
54 
55 
56 /**
57 * Test for object which represents some text annotation
58 * anchored to some cell in spreadsheet (implement
59 * <code>com.sun.star.sheet.CellAnnotation</code>).<p>
60 * Object implements the following interfaces :
61 * <ul>
62 *  <li> <code>com::sun::star::text::XSimpleText</code></li>
63 *  <li> <code>com::sun::star::text::XTextRange</code></li>
64 *  <li> <code>com::sun::star::sheet::XSheetAnnotation</code></li>
65 * </ul>
66 * This object test <b> is NOT </b> designed to be run in several
67 * threads concurently.
68 * @see com.sun.star.sheet.CellAnnotation
69 * @see com.sun.star.text.XSimpleText
70 * @see com.sun.star.text.XTextRange
71 * @see com.sun.star.sheet.XSheetAnnotation
72 * @see ifc.text._XSimpleText
73 * @see ifc.text._XTextRange
74 * @see ifc.sheet._XSheetAnnotation
75 */
76 public class ScAnnotationShapeObj extends TestCase {
77     static XSpreadsheetDocument xSheetDoc = null;
78 
79     /**
80     * Creates a spreadsheet document.
81     */
initialize(TestParameters tParam, PrintWriter log)82     protected void initialize(TestParameters tParam, PrintWriter log) {
83         SOfficeFactory SOF =
84             SOfficeFactory.getFactory((XMultiServiceFactory) tParam
85                 .getMSF());
86 
87         try {
88             log.println("creating a Spreadsheet document");
89             log.println("Loading: "+utils.getFullTestURL(
90                             "ScAnnotationShapeObj.sxc"));
91             xSheetDoc =
92                 (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class,
93                     SOF.loadDocument(utils.getFullTestURL(
94                             "ScAnnotationShapeObj.sxc")));
95         } catch (com.sun.star.uno.Exception e) {
96             // Some exception occured.FAILED
97             e.printStackTrace(log);
98             throw new StatusException("Couldn't create document", e);
99         }
100     }
101 
102     /**
103     * Disposes a spreadsheet document.
104     */
cleanup(TestParameters tParam, PrintWriter log)105     protected void cleanup(TestParameters tParam, PrintWriter log) {
106         log.println("    disposing xSheetDoc ");
107 
108         XComponent oComp =
109             (XComponent) UnoRuntime.queryInterface(XComponent.class,
110                 xSheetDoc);
111         util.DesktopTools.closeDoc(oComp);
112     }
113 
114     /**
115     * Creating a Testenvironment for the interfaces to be tested.
116     * Retrieves a collection of spreadsheets from a document,
117     * and takes one them. Then a single cell is retrieved, and
118     * using its <code>com.sun.star.sheet.XSheetAnnotationAnchor</code>
119     * interface an annotation is got.
120     * Object relations created :
121     * <ul>
122     *  <li> <code>'CELLPOS'</code> for
123     *      {@link ifc.sheet._XSheetAnnotation} (of <code>
124     *       com.sun.star.table.CellAddress</code> type) which
125     *      contains the annotation cell address.</li>
126     * </ul>
127     */
createTestEnvironment( TestParameters Param, PrintWriter log)128     public synchronized TestEnvironment createTestEnvironment(
129         TestParameters Param, PrintWriter log) throws StatusException {
130         XInterface oObj = null;
131 
132         // creation of testobject here
133         // first we write what we are intend to do to log file
134         log.println("Creating a test environment");
135 
136         CellAddress cellPos = new CellAddress((short) 0, 1, 2);
137 
138         log.println("Getting test object ");
139 
140         XSpreadsheetDocument xArea =
141             (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class,
142                 xSheetDoc);
143         XSpreadsheets oSheets = (XSpreadsheets) xArea.getSheets();
144 
145         XIndexAccess XAccess =
146             (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class,
147                 oSheets);
148         XCell oCell = null;
149 
150         try {
151             XSpreadsheet oSheet =
152                 (XSpreadsheet) AnyConverter.toObject(new Type(
153                         XSpreadsheet.class),
154                     XAccess.getByIndex(cellPos.Sheet));
155             XCellRange oCRange =
156                 (XCellRange) UnoRuntime.queryInterface(XCellRange.class,
157                     oSheet);
158             oCell =
159                 oCRange.getCellByPosition(cellPos.Column, cellPos.Row);
160         } catch (com.sun.star.lang.WrappedTargetException e) {
161             e.printStackTrace(log);
162             throw new StatusException("Error getting test object from spreadsheet document",
163                 e);
164         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
165             e.printStackTrace(log);
166             throw new StatusException("Error getting test object from spreadsheet document",
167                 e);
168         } catch (com.sun.star.lang.IllegalArgumentException e) {
169             e.printStackTrace(log);
170             throw new StatusException("Error getting test object from spreadsheet document",
171                 e);
172         }
173 
174         XSheetAnnotationAnchor oAnnoA =
175             (XSheetAnnotationAnchor) UnoRuntime.queryInterface(XSheetAnnotationAnchor.class,
176                 oCell);
177         XSheetAnnotation oAnno = oAnnoA.getAnnotation();
178 
179         XSimpleText xAnnoText =
180             (XSimpleText) UnoRuntime.queryInterface(XSimpleText.class,
181                 oAnno);
182         xAnnoText.setString("ScAnnotationShapeObj");
183 
184         XSheetAnnotationShapeSupplier xSheetAnnotationShapeSupplier =
185             (XSheetAnnotationShapeSupplier) UnoRuntime.queryInterface(XSheetAnnotationShapeSupplier.class,
186                 oAnno);
187 
188         oObj = xSheetAnnotationShapeSupplier.getAnnotationShape();
189 
190         log.println("ImplementationName: "
191             + util.utils.getImplName(oObj));
192 
193         TestEnvironment tEnv = new TestEnvironment(oObj);
194 
195         //adding ObjRelation for RotationDescriptor
196         tEnv.addObjRelation("NoShear", Boolean.TRUE);
197 
198         //adding ObjRelation for XText
199         DefaultDsc tDsc = new DefaultDsc("com.sun.star.text.XTextContent",
200                                     "com.sun.star.text.TextField.DateTime");
201 
202         log.println( "adding InstCreator object" );
203         tEnv.addObjRelation(
204             "XTEXTINFO", new InstCreator( xSheetDoc, tDsc ) );
205 
206         return tEnv;
207     }
208 }
209  // finish class ScAnnotationShapeObj
210