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.beans.XPropertySet;
35 import com.sun.star.container.XEnumerationAccess;
36 import com.sun.star.lang.XComponent;
37 import com.sun.star.lang.XMultiServiceFactory;
38 import com.sun.star.sheet.XNamedRanges;
39 import com.sun.star.sheet.XSpreadsheetDocument;
40 import com.sun.star.table.CellAddress;
41 import com.sun.star.table.CellRangeAddress;
42 import com.sun.star.uno.UnoRuntime;
43 import com.sun.star.uno.XInterface;
44 
45 public class ScIndexEnumeration_NamedRangesEnumeration extends TestCase {
46     static XSpreadsheetDocument xSheetDoc = null;
47 
48     /**
49     * Creates Spreadsheet document.
50     */
initialize( TestParameters tParam, PrintWriter log )51     protected void initialize( TestParameters tParam, PrintWriter log ) {
52         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
53 
54         try {
55             log.println( "creating a Spreadsheet document" );
56             xSheetDoc = SOF.createCalcDoc(null);
57         } catch ( com.sun.star.uno.Exception e ) {
58             // Some exception occures.FAILED
59             e.printStackTrace( log );
60             throw new StatusException( "Couldn't create document", e );
61         }
62     }
63 
64     /**
65     * Disposes Spreadsheet document.
66     */
cleanup( TestParameters tParam, PrintWriter log )67     protected void cleanup( TestParameters tParam, PrintWriter log ) {
68         log.println( "    disposing xSheetDoc " );
69         XComponent oComp = (XComponent)
70             UnoRuntime.queryInterface (XComponent.class, xSheetDoc) ;
71         util.DesktopTools.closeDoc(oComp);
72     }
73 
createTestEnvironment(TestParameters Param, PrintWriter log)74     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
75 
76         XInterface oObj = null;
77 
78         // creation of testobject here
79         // first we write what we are intend to do to log file
80         log.println( "Creating a test environment" );
81 
82         log.println("Getting test object ");
83 
84         // Getting named ranges.
85         XPropertySet docProps = (XPropertySet)
86             UnoRuntime.queryInterface(XPropertySet.class, xSheetDoc);
87         Object ranges = null;
88         try {
89             ranges = docProps.getPropertyValue("NamedRanges");
90         } catch(com.sun.star.lang.WrappedTargetException e){
91             e.printStackTrace(log);
92             throw new StatusException("Couldn't get NamedRanges", e);
93         } catch(com.sun.star.beans.UnknownPropertyException e){
94             e.printStackTrace(log);
95             throw new StatusException("Couldn't get NamedRanges", e);
96         }
97 
98         XNamedRanges xNamedRanges = (XNamedRanges)
99             UnoRuntime.queryInterface(XNamedRanges.class, ranges);
100 
101         CellRangeAddress DataArea = new CellRangeAddress((short)0, 0, 0, 2, 2);
102         CellAddress base = new CellAddress(DataArea.Sheet,
103                                            DataArea.StartColumn,
104                                            DataArea.StartRow);
105 
106         xNamedRanges.addNewByName("ANamedRange", "A1:B2", base, 0);
107 
108         CellAddress listOutputPosition = new CellAddress((short)0, 1, 1);
109         xNamedRanges.outputList(listOutputPosition);
110 
111         oObj = xNamedRanges;
112 
113         XEnumerationAccess ea = (XEnumerationAccess)
114                     UnoRuntime.queryInterface(XEnumerationAccess.class,oObj);
115 
116         oObj = ea.createEnumeration();
117 
118         log.println("ImplementationName: "+util.utils.getImplName(oObj));
119         // creating test environment
120         TestEnvironment tEnv = new TestEnvironment( oObj );
121 
122         tEnv.addObjRelation("ENUM",ea);
123 
124         return tEnv;
125     }
126 
127 }
128 
129 
130