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 com.sun.star.container.NoSuchElementException;
31 import java.io.PrintWriter;
32 
33 import lib.StatusException;
34 import lib.TestCase;
35 import lib.TestEnvironment;
36 import lib.TestParameters;
37 import util.SOfficeFactory;
38 
39 import com.sun.star.container.XIndexAccess;
40 import com.sun.star.container.XNameContainer;
41 import com.sun.star.drawing.XDrawPage;
42 import com.sun.star.drawing.XDrawPages;
43 import com.sun.star.drawing.XDrawPagesSupplier;
44 import com.sun.star.drawing.XShape;
45 import com.sun.star.form.XForm;
46 import com.sun.star.frame.XController;
47 import com.sun.star.frame.XModel;
48 import com.sun.star.lang.WrappedTargetException;
49 import com.sun.star.lang.XComponent;
50 import com.sun.star.lang.XMultiServiceFactory;
51 import com.sun.star.sheet.XSpreadsheetDocument;
52 import com.sun.star.sheet.XViewPane;
53 import com.sun.star.table.CellRangeAddress;
54 import com.sun.star.uno.AnyConverter;
55 import com.sun.star.uno.Type;
56 import com.sun.star.uno.UnoRuntime;
57 import com.sun.star.uno.XInterface;
58 import util.FormTools;
59 
60 /**
61  * Test for object which is represented by service
62  * <code>com.sun.star.sheet.SpreadsheetViewPane</code>. <p>
63  * Object implements the following interfaces :
64  * <ul>
65  *  <li> <code>com::sun::star::sheet::XViewPane</code></li>
66  *  <li> <code>com::sun::star::sheet::XCellRangeReferrer</code></li>
67  * </ul>
68  * @see com.sun.star.sheet.SpreadsheetViewPane
69  * @see com.sun.star.sheet.XViewPane
70  * @see com.sun.star.sheet.XCellRangeReferrer
71  * @see ifc.sheet._XViewPane
72  * @see ifc.sheet._XCellRangeReferrer
73  */
74 public class ScViewPaneObj extends TestCase {
75     static private XSpreadsheetDocument xSpreadsheetDoc;
76     static private SOfficeFactory SOF;
77     static private XInterface oObj;
78 
79     /**
80      * Creates Spreadsheet document.
81      */
82     public void initialize( TestParameters Param, PrintWriter log ) {
83         // get a soffice factory object
84         SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)Param.getMSF());
85 
86         try {
87             log.println("creating a spreadsheetdocument");
88             xSpreadsheetDoc = SOF.createCalcDoc(null);
89         } catch (com.sun.star.uno.Exception e) {
90             e.printStackTrace( log );
91             throw new StatusException( "Couldn't create document ", e );
92         }
93     }
94 
95     /**
96      * Disposes Spreadsheet document.
97      */
98     protected void cleanup( TestParameters tParam, PrintWriter log ) {
99         log.println("disposing xSpreadsheetDocument");
100         XComponent oComp = (XComponent)
101         UnoRuntime.queryInterface(XComponent.class, xSpreadsheetDoc);
102         util.DesktopTools.closeDoc(oComp);
103     }
104 
105     /**
106      * Creating a Testenvironment for the interfaces to be tested.
107      * Retieves the current controller of the spreadsheet document using the
108      * interface <code>XModel</code>. The controller contains the collection
109      * of the view panes so retrieves the view pane with index 0 from
110      * the collection. The retrived view pane is the instance of the service
111      * <code>com.sun.star.sheet.SpreadsheetViewPane</code>. Retrieves the address
112      * of the cell range that consists of the cells which are visible in the pane.
113      * Object relations created :
114      * <ul>
115      *  <li> <code>'DATAAREA'</code> for
116      *      {@link ifc.sheet._XViewPane}(the retrieved address)</li>
117      * </ul>
118      * @see com.sun.star.frame.XModel
119      */
120     protected TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
121         XDrawPage oDrawPage;
122 
123         XModel xm = (XModel)
124         UnoRuntime.queryInterface(XModel.class, xSpreadsheetDoc);
125         XController xc = xm.getCurrentController();
126         XIndexAccess xIA = (XIndexAccess)
127         UnoRuntime.queryInterface(XIndexAccess.class, xc);
128         try {
129             oObj = (XInterface) AnyConverter.toObject(
130                 new Type(XInterface.class),xIA.getByIndex(0));
131         } catch (com.sun.star.lang.WrappedTargetException e) {
132             e.printStackTrace(log);
133             throw new StatusException("Couldn't get by index", e);
134         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
135             e.printStackTrace(log);
136             throw new StatusException("Couldn't get by index", e);
137         } catch (com.sun.star.lang.IllegalArgumentException e) {
138             e.printStackTrace(log);
139             throw new StatusException("Couldn't get by index", e);
140         }
141 
142         TestEnvironment tEnv = new TestEnvironment(oObj);
143 
144         //Relation for XControlAccess
145         tEnv.addObjRelation("DOCUMENT", UnoRuntime.queryInterface(XComponent.class,xSpreadsheetDoc));
146         tEnv.addObjRelation("XControlAccess.isSheet", Boolean.TRUE);
147 
148         XViewPane VP = (XViewPane)
149         UnoRuntime.queryInterface(XViewPane.class, oObj);
150         CellRangeAddress dataArea = VP.getVisibleRange();
151         tEnv.addObjRelation("DATAAREA", dataArea);
152 
153         // XForm for com.sun.star.view.XFormLayerAccess
154         log.println("adding relation for com.sun.star.view.XFormLayerAccess: XForm");
155 
156         XForm myForm = null;
157         String kindOfControl="CommandButton";
158         XShape aShape = null;
159         try{
160             log.println("adding contol shape '" + kindOfControl + "'");
161             XComponent oComp = (XComponent) UnoRuntime.queryInterface(XComponent.class, xSpreadsheetDoc) ;
162 
163             aShape = FormTools.createControlShape(oComp, 3000, 4500, 15000, 10000, kindOfControl);
164 
165         } catch (Exception e){
166             e.printStackTrace(log);
167             throw new StatusException("Couldn't create following control shape : '" +
168                 kindOfControl + "': ", e);
169         }
170 
171         try {
172             log.println( "getting Drawpages" );
173             XDrawPagesSupplier oDPS = (XDrawPagesSupplier)
174             UnoRuntime.queryInterface(XDrawPagesSupplier.class,xSpreadsheetDoc);
175             XDrawPages oDP = (XDrawPages) oDPS.getDrawPages();
176             oDP.insertNewByIndex(1);
177             oDP.insertNewByIndex(2);
178             oDrawPage = (XDrawPage) AnyConverter.toObject(
179                 new Type(XDrawPage.class),oDP.getByIndex(0));
180             if (oDrawPage == null)
181                 log.println("ERROR: could not get DrawPage: null");
182 
183             oDrawPage.add(aShape);
184             log.println("getting XForm");
185             XNameContainer xForm = FormTools.getForms(oDrawPage);
186             try {
187                 myForm = (XForm) AnyConverter.toObject(new Type(XForm.class),xForm.getByName("Standard"));
188             } catch (WrappedTargetException ex) {
189                 log.println("ERROR: could not XFormm 'Standard': " + ex.toString());
190             } catch (com.sun.star.lang.IllegalArgumentException ex) {
191                 log.println("ERROR: could not XFormm 'Standard': " + ex.toString());
192             } catch (NoSuchElementException ex) {
193                 log.println("ERROR: could not XFormm 'Standard': " + ex.toString());
194             }
195             if (myForm == null)
196                 log.println("ERROR: could not get XForm: null");
197             tEnv.addObjRelation("XFormLayerAccess.XForm", myForm);
198         } catch (com.sun.star.lang.IndexOutOfBoundsException ex) {
199             log.println("ERROR: could not add ObjectRelation 'XFormLayerAccess.XForm': " + ex.toString());
200         } catch (WrappedTargetException ex) {
201             log.println("ERROR: could not add ObjectRelation 'XFormLayerAccess.XForm': " + ex.toString());
202         } catch (com.sun.star.lang.IllegalArgumentException ex) {
203             log.println("ERROR: could not add ObjectRelation 'XFormLayerAccess.XForm': " + ex.toString());
204         }
205 
206         return tEnv;
207     }
208 }
209 
210