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