merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / ifc / text / _XTextFieldsSupplier.java
blobe61d8af2341dee05d739353b69abb6f136cad335
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XTextFieldsSupplier.java,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package ifc.text;
33 import com.sun.star.beans.XPropertySet;
34 import com.sun.star.container.XEnumeration;
35 import com.sun.star.container.XEnumerationAccess;
36 import com.sun.star.container.XIndexAccess;
37 import com.sun.star.container.XNameAccess;
38 import com.sun.star.lang.XMultiServiceFactory;
39 import com.sun.star.sheet.XSpreadsheet;
40 import com.sun.star.sheet.XSpreadsheetDocument;
41 import com.sun.star.sheet.XSpreadsheets;
42 import com.sun.star.table.XCell;
43 import com.sun.star.text.XDependentTextField;
44 import com.sun.star.text.XText;
45 import com.sun.star.text.XTextContent;
46 import com.sun.star.text.XTextCursor;
47 import com.sun.star.text.XTextFieldsSupplier;
48 import com.sun.star.uno.AnyConverter;
49 import com.sun.star.uno.Type;
50 import com.sun.star.uno.UnoRuntime;
51 import com.sun.star.uno.XInterface;
52 import java.io.PrintWriter;
53 import lib.MultiMethodTest;
54 import lib.StatusException;
56 /**
59 public class _XTextFieldsSupplier extends MultiMethodTest {
61 public XTextFieldsSupplier oObj = null; // oObj filled by MultiMethodTest
62 private boolean mDispose = false;
63 private boolean mbCreateFieldMaster = true;
65 /**
66 * Insert some text fields into a cell on the sheet, so this interface test
67 * makes sense.
69 protected void before() {
70 Object o = tEnv.getObjRelation("XTextFieldsSupplier.MAKEENTRY");
71 if (o != null && ((Boolean)o).booleanValue()) {
72 mDispose = true;
73 mbCreateFieldMaster = false;
74 XCell xCell = (XCell)tEnv.getObjRelation("MAKEENTRYINCELL");
76 XSpreadsheetDocument xSheetDoc = (XSpreadsheetDocument)tEnv.getObjRelation("SPREADSHEET");
78 XInterface oObj = null;
79 XText oText = null;
80 XTextContent oContent = null;
81 XInterface aField = null;
83 try {
84 // we want to create an instance of ScCellFieldObj.
85 // to do this we must get an MultiServiceFactory.
87 XMultiServiceFactory _oMSF = (XMultiServiceFactory)
88 UnoRuntime.queryInterface(XMultiServiceFactory.class, xSheetDoc);
90 aField = (XInterface)
91 _oMSF.createInstance("com.sun.star.text.TextField.URL");
92 oContent = (XTextContent)
93 UnoRuntime.queryInterface(XTextContent.class, aField);
95 XSpreadsheets oSheets = xSheetDoc.getSheets() ;
96 XIndexAccess oIndexSheets = (XIndexAccess)
97 UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
98 XSpreadsheet oSheet = (XSpreadsheet) AnyConverter.toObject(
99 new Type(XSpreadsheet.class),oIndexSheets.getByIndex(0));
101 String[] services = _oMSF.getAvailableServiceNames();
102 for (int i=0; i<services.length; i++) {
103 if (services[i].startsWith("com.sun.star.text.FieldMaster")) {
104 mbCreateFieldMaster = true;
105 log.println("service " + i + ": " + services[i]);
109 if (mbCreateFieldMaster) {
110 Object FieldMaster = _oMSF.createInstance("com.sun.star.text.FieldMaster.User");
111 XPropertySet PFieldMaster = (XPropertySet) UnoRuntime.queryInterface
112 (XPropertySet.class,(XInterface) FieldMaster);
114 XDependentTextField xTF = (XDependentTextField)
115 UnoRuntime.queryInterface(XDependentTextField.class,aField);
117 PFieldMaster.setPropertyValue("Content","Some content");
119 xTF.attachTextFieldMaster(PFieldMaster);
122 oText = (XText)UnoRuntime.queryInterface(XText.class, xCell);
123 XTextCursor the_Cursor = oText.createTextCursor();
125 oText.insertTextContent(
126 the_Cursor, oContent, true);
128 } catch (com.sun.star.lang.WrappedTargetException e) {
129 log.println("Exception occured while creating test Object.");
130 e.printStackTrace(log);
131 throw new StatusException("Couldn't insert textField.URL", e);
132 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
133 log.println("Exception occured while creating test Object.");
134 e.printStackTrace(log);
135 throw new StatusException("Couldn't insert textField.URL", e);
136 } catch (com.sun.star.lang.IllegalArgumentException e) {
137 log.println("Exception occured while creating test Object.");
138 e.printStackTrace(log);
139 throw new StatusException("Couldn't insert textField.URL", e);
140 } catch (com.sun.star.uno.Exception e) {
141 log.println("Exception occured while creating test Object.");
142 e.printStackTrace(log);
143 throw new StatusException("Couldn't insert textField.URL", e);
149 public void _getTextFields() {
150 XEnumerationAccess xEnumAccess = oObj.getTextFields();
151 XEnumeration xEnum = xEnumAccess.createEnumeration();
152 while(xEnum != null && xEnum.hasMoreElements()) {
153 try {
154 Object o = xEnum.nextElement();
156 catch(com.sun.star.container.NoSuchElementException e) {
157 setMethodFalse("getTextFields()", e);
159 catch(com.sun.star.lang.WrappedTargetException e) {
160 setMethodFalse("getTextFields()", e);
163 tRes.tested("getTextFields()", xEnum != null);
166 public void _getTextFieldMasters() {
167 if (mbCreateFieldMaster) {
168 XNameAccess xName = oObj.getTextFieldMasters();
169 util.dbg.printInterfaces(xName);
170 tRes.tested("getTextFieldMasters()", xName != null);
172 else {
173 log.println("Could not test 'getTextFieldMasters' because no field masters can be created on this object.");
174 tRes.tested("getTextFieldMasters()", true);
179 * Just for convenience: log the exception and set the method false.
180 * @param method The name of the method to set to false.
181 * @param e The Exception that occured.
183 private void setMethodFalse(String method, Exception e) {
184 log.println("Exception while executing '" + method + "'");
185 e.printStackTrace((PrintWriter)log);
186 tRes.tested(method, false);
189 protected void after() {
190 if (mDispose)
191 disposeEnvironment();