1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir package mod._sc;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import com.sun.star.container.NoSuchElementException;
31*cdf0e10cSrcweir import java.io.PrintWriter;
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir import lib.StatusException;
34*cdf0e10cSrcweir import lib.TestCase;
35*cdf0e10cSrcweir import lib.TestEnvironment;
36*cdf0e10cSrcweir import lib.TestParameters;
37*cdf0e10cSrcweir import util.SOfficeFactory;
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir import com.sun.star.container.XIndexAccess;
40*cdf0e10cSrcweir import com.sun.star.container.XNameContainer;
41*cdf0e10cSrcweir import com.sun.star.drawing.XDrawPage;
42*cdf0e10cSrcweir import com.sun.star.drawing.XDrawPages;
43*cdf0e10cSrcweir import com.sun.star.drawing.XDrawPagesSupplier;
44*cdf0e10cSrcweir import com.sun.star.drawing.XShape;
45*cdf0e10cSrcweir import com.sun.star.form.XForm;
46*cdf0e10cSrcweir import com.sun.star.frame.XController;
47*cdf0e10cSrcweir import com.sun.star.frame.XModel;
48*cdf0e10cSrcweir import com.sun.star.lang.WrappedTargetException;
49*cdf0e10cSrcweir import com.sun.star.lang.XComponent;
50*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
51*cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheetDocument;
52*cdf0e10cSrcweir import com.sun.star.sheet.XViewPane;
53*cdf0e10cSrcweir import com.sun.star.table.CellRangeAddress;
54*cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
55*cdf0e10cSrcweir import com.sun.star.uno.Type;
56*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
57*cdf0e10cSrcweir import com.sun.star.uno.XInterface;
58*cdf0e10cSrcweir import util.FormTools;
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir /**
61*cdf0e10cSrcweir  * Test for object which is represented by service
62*cdf0e10cSrcweir  * <code>com.sun.star.sheet.SpreadsheetViewPane</code>. <p>
63*cdf0e10cSrcweir  * Object implements the following interfaces :
64*cdf0e10cSrcweir  * <ul>
65*cdf0e10cSrcweir  *  <li> <code>com::sun::star::sheet::XViewPane</code></li>
66*cdf0e10cSrcweir  *  <li> <code>com::sun::star::sheet::XCellRangeReferrer</code></li>
67*cdf0e10cSrcweir  * </ul>
68*cdf0e10cSrcweir  * @see com.sun.star.sheet.SpreadsheetViewPane
69*cdf0e10cSrcweir  * @see com.sun.star.sheet.XViewPane
70*cdf0e10cSrcweir  * @see com.sun.star.sheet.XCellRangeReferrer
71*cdf0e10cSrcweir  * @see ifc.sheet._XViewPane
72*cdf0e10cSrcweir  * @see ifc.sheet._XCellRangeReferrer
73*cdf0e10cSrcweir  */
74*cdf0e10cSrcweir public class ScViewPaneObj extends TestCase {
75*cdf0e10cSrcweir     static private XSpreadsheetDocument xSpreadsheetDoc;
76*cdf0e10cSrcweir     static private SOfficeFactory SOF;
77*cdf0e10cSrcweir     static private XInterface oObj;
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir     /**
80*cdf0e10cSrcweir      * Creates Spreadsheet document.
81*cdf0e10cSrcweir      */
82*cdf0e10cSrcweir     public void initialize( TestParameters Param, PrintWriter log ) {
83*cdf0e10cSrcweir         // get a soffice factory object
84*cdf0e10cSrcweir         SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)Param.getMSF());
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir         try {
87*cdf0e10cSrcweir             log.println("creating a spreadsheetdocument");
88*cdf0e10cSrcweir             xSpreadsheetDoc = SOF.createCalcDoc(null);
89*cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
90*cdf0e10cSrcweir             e.printStackTrace( log );
91*cdf0e10cSrcweir             throw new StatusException( "Couldn't create document ", e );
92*cdf0e10cSrcweir         }
93*cdf0e10cSrcweir     }
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir     /**
96*cdf0e10cSrcweir      * Disposes Spreadsheet document.
97*cdf0e10cSrcweir      */
98*cdf0e10cSrcweir     protected void cleanup( TestParameters tParam, PrintWriter log ) {
99*cdf0e10cSrcweir         log.println("disposing xSpreadsheetDocument");
100*cdf0e10cSrcweir         XComponent oComp = (XComponent)
101*cdf0e10cSrcweir         UnoRuntime.queryInterface(XComponent.class, xSpreadsheetDoc);
102*cdf0e10cSrcweir         util.DesktopTools.closeDoc(oComp);
103*cdf0e10cSrcweir     }
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir     /**
106*cdf0e10cSrcweir      * Creating a Testenvironment for the interfaces to be tested.
107*cdf0e10cSrcweir      * Retieves the current controller of the spreadsheet document using the
108*cdf0e10cSrcweir      * interface <code>XModel</code>. The controller contains the collection
109*cdf0e10cSrcweir      * of the view panes so retrieves the view pane with index 0 from
110*cdf0e10cSrcweir      * the collection. The retrived view pane is the instance of the service
111*cdf0e10cSrcweir      * <code>com.sun.star.sheet.SpreadsheetViewPane</code>. Retrieves the address
112*cdf0e10cSrcweir      * of the cell range that consists of the cells which are visible in the pane.
113*cdf0e10cSrcweir      * Object relations created :
114*cdf0e10cSrcweir      * <ul>
115*cdf0e10cSrcweir      *  <li> <code>'DATAAREA'</code> for
116*cdf0e10cSrcweir      *      {@link ifc.sheet._XViewPane}(the retrieved address)</li>
117*cdf0e10cSrcweir      * </ul>
118*cdf0e10cSrcweir      * @see com.sun.star.frame.XModel
119*cdf0e10cSrcweir      */
120*cdf0e10cSrcweir     protected TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
121*cdf0e10cSrcweir         XDrawPage oDrawPage;
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir         XModel xm = (XModel)
124*cdf0e10cSrcweir         UnoRuntime.queryInterface(XModel.class, xSpreadsheetDoc);
125*cdf0e10cSrcweir         XController xc = xm.getCurrentController();
126*cdf0e10cSrcweir         XIndexAccess xIA = (XIndexAccess)
127*cdf0e10cSrcweir         UnoRuntime.queryInterface(XIndexAccess.class, xc);
128*cdf0e10cSrcweir         try {
129*cdf0e10cSrcweir             oObj = (XInterface) AnyConverter.toObject(
130*cdf0e10cSrcweir                 new Type(XInterface.class),xIA.getByIndex(0));
131*cdf0e10cSrcweir         } catch (com.sun.star.lang.WrappedTargetException e) {
132*cdf0e10cSrcweir             e.printStackTrace(log);
133*cdf0e10cSrcweir             throw new StatusException("Couldn't get by index", e);
134*cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
135*cdf0e10cSrcweir             e.printStackTrace(log);
136*cdf0e10cSrcweir             throw new StatusException("Couldn't get by index", e);
137*cdf0e10cSrcweir         } catch (com.sun.star.lang.IllegalArgumentException e) {
138*cdf0e10cSrcweir             e.printStackTrace(log);
139*cdf0e10cSrcweir             throw new StatusException("Couldn't get by index", e);
140*cdf0e10cSrcweir         }
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir         TestEnvironment tEnv = new TestEnvironment(oObj);
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir         //Relation for XControlAccess
145*cdf0e10cSrcweir         tEnv.addObjRelation("DOCUMENT", UnoRuntime.queryInterface(XComponent.class,xSpreadsheetDoc));
146*cdf0e10cSrcweir         tEnv.addObjRelation("XControlAccess.isSheet", Boolean.TRUE);
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir         XViewPane VP = (XViewPane)
149*cdf0e10cSrcweir         UnoRuntime.queryInterface(XViewPane.class, oObj);
150*cdf0e10cSrcweir         CellRangeAddress dataArea = VP.getVisibleRange();
151*cdf0e10cSrcweir         tEnv.addObjRelation("DATAAREA", dataArea);
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir         // XForm for com.sun.star.view.XFormLayerAccess
154*cdf0e10cSrcweir         log.println("adding relation for com.sun.star.view.XFormLayerAccess: XForm");
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir         XForm myForm = null;
157*cdf0e10cSrcweir         String kindOfControl="CommandButton";
158*cdf0e10cSrcweir         XShape aShape = null;
159*cdf0e10cSrcweir         try{
160*cdf0e10cSrcweir             log.println("adding contol shape '" + kindOfControl + "'");
161*cdf0e10cSrcweir             XComponent oComp = (XComponent) UnoRuntime.queryInterface(XComponent.class, xSpreadsheetDoc) ;
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir             aShape = FormTools.createControlShape(oComp, 3000, 4500, 15000, 10000, kindOfControl);
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir         } catch (Exception e){
166*cdf0e10cSrcweir             e.printStackTrace(log);
167*cdf0e10cSrcweir             throw new StatusException("Couldn't create following control shape : '" +
168*cdf0e10cSrcweir                 kindOfControl + "': ", e);
169*cdf0e10cSrcweir         }
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir         try {
172*cdf0e10cSrcweir             log.println( "getting Drawpages" );
173*cdf0e10cSrcweir             XDrawPagesSupplier oDPS = (XDrawPagesSupplier)
174*cdf0e10cSrcweir             UnoRuntime.queryInterface(XDrawPagesSupplier.class,xSpreadsheetDoc);
175*cdf0e10cSrcweir             XDrawPages oDP = (XDrawPages) oDPS.getDrawPages();
176*cdf0e10cSrcweir             oDP.insertNewByIndex(1);
177*cdf0e10cSrcweir             oDP.insertNewByIndex(2);
178*cdf0e10cSrcweir             oDrawPage = (XDrawPage) AnyConverter.toObject(
179*cdf0e10cSrcweir                 new Type(XDrawPage.class),oDP.getByIndex(0));
180*cdf0e10cSrcweir             if (oDrawPage == null)
181*cdf0e10cSrcweir                 log.println("ERROR: could not get DrawPage: null");
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir             oDrawPage.add(aShape);
184*cdf0e10cSrcweir             log.println("getting XForm");
185*cdf0e10cSrcweir             XNameContainer xForm = FormTools.getForms(oDrawPage);
186*cdf0e10cSrcweir             try {
187*cdf0e10cSrcweir                 myForm = (XForm) AnyConverter.toObject(new Type(XForm.class),xForm.getByName("Standard"));
188*cdf0e10cSrcweir             } catch (WrappedTargetException ex) {
189*cdf0e10cSrcweir                 log.println("ERROR: could not XFormm 'Standard': " + ex.toString());
190*cdf0e10cSrcweir             } catch (com.sun.star.lang.IllegalArgumentException ex) {
191*cdf0e10cSrcweir                 log.println("ERROR: could not XFormm 'Standard': " + ex.toString());
192*cdf0e10cSrcweir             } catch (NoSuchElementException ex) {
193*cdf0e10cSrcweir                 log.println("ERROR: could not XFormm 'Standard': " + ex.toString());
194*cdf0e10cSrcweir             }
195*cdf0e10cSrcweir             if (myForm == null)
196*cdf0e10cSrcweir                 log.println("ERROR: could not get XForm: null");
197*cdf0e10cSrcweir             tEnv.addObjRelation("XFormLayerAccess.XForm", myForm);
198*cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException ex) {
199*cdf0e10cSrcweir             log.println("ERROR: could not add ObjectRelation 'XFormLayerAccess.XForm': " + ex.toString());
200*cdf0e10cSrcweir         } catch (WrappedTargetException ex) {
201*cdf0e10cSrcweir             log.println("ERROR: could not add ObjectRelation 'XFormLayerAccess.XForm': " + ex.toString());
202*cdf0e10cSrcweir         } catch (com.sun.star.lang.IllegalArgumentException ex) {
203*cdf0e10cSrcweir             log.println("ERROR: could not add ObjectRelation 'XFormLayerAccess.XForm': " + ex.toString());
204*cdf0e10cSrcweir         }
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir         return tEnv;
207*cdf0e10cSrcweir     }
208*cdf0e10cSrcweir }
209*cdf0e10cSrcweir 
210