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