xref: /AOO41X/main/qadevOOo/tests/java/ifc/text/_XTextFieldsSupplier.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 ifc.text;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
31*cdf0e10cSrcweir import com.sun.star.container.XEnumeration;
32*cdf0e10cSrcweir import com.sun.star.container.XEnumerationAccess;
33*cdf0e10cSrcweir import com.sun.star.container.XIndexAccess;
34*cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
35*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
36*cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheet;
37*cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheetDocument;
38*cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheets;
39*cdf0e10cSrcweir import com.sun.star.table.XCell;
40*cdf0e10cSrcweir import com.sun.star.text.XDependentTextField;
41*cdf0e10cSrcweir import com.sun.star.text.XText;
42*cdf0e10cSrcweir import com.sun.star.text.XTextContent;
43*cdf0e10cSrcweir import com.sun.star.text.XTextCursor;
44*cdf0e10cSrcweir import com.sun.star.text.XTextFieldsSupplier;
45*cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
46*cdf0e10cSrcweir import com.sun.star.uno.Type;
47*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
48*cdf0e10cSrcweir import com.sun.star.uno.XInterface;
49*cdf0e10cSrcweir import java.io.PrintWriter;
50*cdf0e10cSrcweir import lib.MultiMethodTest;
51*cdf0e10cSrcweir import lib.StatusException;
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir /**
54*cdf0e10cSrcweir  *
55*cdf0e10cSrcweir  */
56*cdf0e10cSrcweir public class _XTextFieldsSupplier extends MultiMethodTest {
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir     public XTextFieldsSupplier oObj = null;                // oObj filled by MultiMethodTest
59*cdf0e10cSrcweir     private boolean mDispose = false;
60*cdf0e10cSrcweir     private boolean mbCreateFieldMaster = true;
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir     /**
63*cdf0e10cSrcweir      * Insert some text fields into a cell on the sheet, so this interface test
64*cdf0e10cSrcweir      * makes sense.
65*cdf0e10cSrcweir      */
66*cdf0e10cSrcweir     protected void before() {
67*cdf0e10cSrcweir         Object o = tEnv.getObjRelation("XTextFieldsSupplier.MAKEENTRY");
68*cdf0e10cSrcweir         if (o != null && ((Boolean)o).booleanValue()) {
69*cdf0e10cSrcweir             mDispose = true;
70*cdf0e10cSrcweir             mbCreateFieldMaster = false;
71*cdf0e10cSrcweir             XCell xCell = (XCell)tEnv.getObjRelation("MAKEENTRYINCELL");
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir             XSpreadsheetDocument xSheetDoc = (XSpreadsheetDocument)tEnv.getObjRelation("SPREADSHEET");
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir             XInterface oObj = null;
76*cdf0e10cSrcweir             XText oText = null;
77*cdf0e10cSrcweir             XTextContent oContent = null;
78*cdf0e10cSrcweir             XInterface aField = null;
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir             try {
81*cdf0e10cSrcweir                 // we want to create an instance of ScCellFieldObj.
82*cdf0e10cSrcweir                 // to do this we must get an MultiServiceFactory.
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir                 XMultiServiceFactory _oMSF = (XMultiServiceFactory)
85*cdf0e10cSrcweir                     UnoRuntime.queryInterface(XMultiServiceFactory.class, xSheetDoc);
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir                 aField = (XInterface)
88*cdf0e10cSrcweir                     _oMSF.createInstance("com.sun.star.text.TextField.URL");
89*cdf0e10cSrcweir                 oContent = (XTextContent)
90*cdf0e10cSrcweir                     UnoRuntime.queryInterface(XTextContent.class, aField);
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir                 XSpreadsheets oSheets = xSheetDoc.getSheets() ;
93*cdf0e10cSrcweir                 XIndexAccess oIndexSheets = (XIndexAccess)
94*cdf0e10cSrcweir                     UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
95*cdf0e10cSrcweir                 XSpreadsheet oSheet = (XSpreadsheet) AnyConverter.toObject(
96*cdf0e10cSrcweir                         new Type(XSpreadsheet.class),oIndexSheets.getByIndex(0));
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir                 String[] services = _oMSF.getAvailableServiceNames();
99*cdf0e10cSrcweir                 for (int i=0; i<services.length; i++) {
100*cdf0e10cSrcweir                     if (services[i].startsWith("com.sun.star.text.FieldMaster")) {
101*cdf0e10cSrcweir                         mbCreateFieldMaster = true;
102*cdf0e10cSrcweir                         log.println("service " + i + ": " + services[i]);
103*cdf0e10cSrcweir                     }
104*cdf0e10cSrcweir                 }
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir                 if (mbCreateFieldMaster) {
107*cdf0e10cSrcweir                     Object FieldMaster = _oMSF.createInstance("com.sun.star.text.FieldMaster.User");
108*cdf0e10cSrcweir                     XPropertySet PFieldMaster = (XPropertySet) UnoRuntime.queryInterface
109*cdf0e10cSrcweir                         (XPropertySet.class,(XInterface) FieldMaster);
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir                     XDependentTextField xTF = (XDependentTextField)
112*cdf0e10cSrcweir                         UnoRuntime.queryInterface(XDependentTextField.class,aField);
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir                     PFieldMaster.setPropertyValue("Content","Some content");
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir                     xTF.attachTextFieldMaster(PFieldMaster);
117*cdf0e10cSrcweir                 }
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir                 oText = (XText)UnoRuntime.queryInterface(XText.class, xCell);
120*cdf0e10cSrcweir                 XTextCursor the_Cursor = oText.createTextCursor();
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir                 oText.insertTextContent(
123*cdf0e10cSrcweir                     the_Cursor, oContent, true);
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir             } catch (com.sun.star.lang.WrappedTargetException e) {
126*cdf0e10cSrcweir                 log.println("Exception occured while creating test Object.");
127*cdf0e10cSrcweir                 e.printStackTrace(log);
128*cdf0e10cSrcweir                 throw new StatusException("Couldn't insert textField.URL", e);
129*cdf0e10cSrcweir             } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
130*cdf0e10cSrcweir                 log.println("Exception occured while creating test Object.");
131*cdf0e10cSrcweir                 e.printStackTrace(log);
132*cdf0e10cSrcweir                 throw new StatusException("Couldn't insert textField.URL", e);
133*cdf0e10cSrcweir             } catch (com.sun.star.lang.IllegalArgumentException e) {
134*cdf0e10cSrcweir                 log.println("Exception occured while creating test Object.");
135*cdf0e10cSrcweir                 e.printStackTrace(log);
136*cdf0e10cSrcweir                 throw new StatusException("Couldn't insert textField.URL", e);
137*cdf0e10cSrcweir             } catch (com.sun.star.uno.Exception e) {
138*cdf0e10cSrcweir                 log.println("Exception occured while creating test Object.");
139*cdf0e10cSrcweir                 e.printStackTrace(log);
140*cdf0e10cSrcweir                 throw new StatusException("Couldn't insert textField.URL", e);
141*cdf0e10cSrcweir             }
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir         }
144*cdf0e10cSrcweir     }
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir     public void _getTextFields() {
147*cdf0e10cSrcweir         XEnumerationAccess xEnumAccess = oObj.getTextFields();
148*cdf0e10cSrcweir         XEnumeration xEnum = xEnumAccess.createEnumeration();
149*cdf0e10cSrcweir         while(xEnum != null && xEnum.hasMoreElements()) {
150*cdf0e10cSrcweir             try {
151*cdf0e10cSrcweir                 Object o = xEnum.nextElement();
152*cdf0e10cSrcweir             }
153*cdf0e10cSrcweir             catch(com.sun.star.container.NoSuchElementException e) {
154*cdf0e10cSrcweir                 setMethodFalse("getTextFields()", e);
155*cdf0e10cSrcweir             }
156*cdf0e10cSrcweir             catch(com.sun.star.lang.WrappedTargetException e) {
157*cdf0e10cSrcweir                 setMethodFalse("getTextFields()", e);
158*cdf0e10cSrcweir             }
159*cdf0e10cSrcweir         }
160*cdf0e10cSrcweir         tRes.tested("getTextFields()", xEnum != null);
161*cdf0e10cSrcweir     }
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir     public void _getTextFieldMasters() {
164*cdf0e10cSrcweir         if (mbCreateFieldMaster) {
165*cdf0e10cSrcweir             XNameAccess xName = oObj.getTextFieldMasters();
166*cdf0e10cSrcweir             util.dbg.printInterfaces(xName);
167*cdf0e10cSrcweir             tRes.tested("getTextFieldMasters()", xName != null);
168*cdf0e10cSrcweir         }
169*cdf0e10cSrcweir         else {
170*cdf0e10cSrcweir             log.println("Could not test 'getTextFieldMasters' because no field masters can be created on this object.");
171*cdf0e10cSrcweir             tRes.tested("getTextFieldMasters()", true);
172*cdf0e10cSrcweir         }
173*cdf0e10cSrcweir     }
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir     /**
176*cdf0e10cSrcweir      * Just for convenience: log the exception and set the method false.
177*cdf0e10cSrcweir      * @param method The name of the method to set to false.
178*cdf0e10cSrcweir      * @param e The Exception that occured.
179*cdf0e10cSrcweir      */
180*cdf0e10cSrcweir     private void setMethodFalse(String method, Exception e) {
181*cdf0e10cSrcweir         log.println("Exception while executing '" + method + "'");
182*cdf0e10cSrcweir         e.printStackTrace((PrintWriter)log);
183*cdf0e10cSrcweir         tRes.tested(method, false);
184*cdf0e10cSrcweir     }
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir     protected void after() {
187*cdf0e10cSrcweir         if (mDispose)
188*cdf0e10cSrcweir             disposeEnvironment();
189*cdf0e10cSrcweir     }
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir }
192