1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package ifc.text;
25 
26 import com.sun.star.beans.XPropertySet;
27 import com.sun.star.container.XEnumeration;
28 import com.sun.star.container.XEnumerationAccess;
29 import com.sun.star.container.XIndexAccess;
30 import com.sun.star.container.XNameAccess;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.sheet.XSpreadsheet;
33 import com.sun.star.sheet.XSpreadsheetDocument;
34 import com.sun.star.sheet.XSpreadsheets;
35 import com.sun.star.table.XCell;
36 import com.sun.star.text.XDependentTextField;
37 import com.sun.star.text.XText;
38 import com.sun.star.text.XTextContent;
39 import com.sun.star.text.XTextCursor;
40 import com.sun.star.text.XTextFieldsSupplier;
41 import com.sun.star.uno.AnyConverter;
42 import com.sun.star.uno.Type;
43 import com.sun.star.uno.UnoRuntime;
44 import com.sun.star.uno.XInterface;
45 import java.io.PrintWriter;
46 import lib.MultiMethodTest;
47 import lib.StatusException;
48 
49 /**
50  *
51  */
52 public class _XTextFieldsSupplier extends MultiMethodTest {
53 
54     public XTextFieldsSupplier oObj = null;                // oObj filled by MultiMethodTest
55     private boolean mDispose = false;
56     private boolean mbCreateFieldMaster = true;
57 
58     /**
59      * Insert some text fields into a cell on the sheet, so this interface test
60      * makes sense.
61      */
before()62     protected void before() {
63         Object o = tEnv.getObjRelation("XTextFieldsSupplier.MAKEENTRY");
64         if (o != null && ((Boolean)o).booleanValue()) {
65             mDispose = true;
66             mbCreateFieldMaster = false;
67             XCell xCell = (XCell)tEnv.getObjRelation("MAKEENTRYINCELL");
68 
69             XSpreadsheetDocument xSheetDoc = (XSpreadsheetDocument)tEnv.getObjRelation("SPREADSHEET");
70 
71             XInterface oObj = null;
72             XText oText = null;
73             XTextContent oContent = null;
74             XInterface aField = null;
75 
76             try {
77                 // we want to create an instance of ScCellFieldObj.
78                 // to do this we must get an MultiServiceFactory.
79 
80                 XMultiServiceFactory _oMSF = (XMultiServiceFactory)
81                     UnoRuntime.queryInterface(XMultiServiceFactory.class, xSheetDoc);
82 
83                 aField = (XInterface)
84                     _oMSF.createInstance("com.sun.star.text.TextField.URL");
85                 oContent = (XTextContent)
86                     UnoRuntime.queryInterface(XTextContent.class, aField);
87 
88                 XSpreadsheets oSheets = xSheetDoc.getSheets() ;
89                 XIndexAccess oIndexSheets = (XIndexAccess)
90                     UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
91                 XSpreadsheet oSheet = (XSpreadsheet) AnyConverter.toObject(
92                         new Type(XSpreadsheet.class),oIndexSheets.getByIndex(0));
93 
94                 String[] services = _oMSF.getAvailableServiceNames();
95                 for (int i=0; i<services.length; i++) {
96                     if (services[i].startsWith("com.sun.star.text.FieldMaster")) {
97                         mbCreateFieldMaster = true;
98                         log.println("service " + i + ": " + services[i]);
99                     }
100                 }
101 
102                 if (mbCreateFieldMaster) {
103                     Object FieldMaster = _oMSF.createInstance("com.sun.star.text.FieldMaster.User");
104                     XPropertySet PFieldMaster = (XPropertySet) UnoRuntime.queryInterface
105                         (XPropertySet.class,(XInterface) FieldMaster);
106 
107                     XDependentTextField xTF = (XDependentTextField)
108                         UnoRuntime.queryInterface(XDependentTextField.class,aField);
109 
110                     PFieldMaster.setPropertyValue("Content","Some content");
111 
112                     xTF.attachTextFieldMaster(PFieldMaster);
113                 }
114 
115                 oText = (XText)UnoRuntime.queryInterface(XText.class, xCell);
116                 XTextCursor the_Cursor = oText.createTextCursor();
117 
118                 oText.insertTextContent(
119                     the_Cursor, oContent, true);
120 
121             } catch (com.sun.star.lang.WrappedTargetException e) {
122                 log.println("Exception occured while creating test Object.");
123                 e.printStackTrace(log);
124                 throw new StatusException("Couldn't insert textField.URL", e);
125             } catch (com.sun.star.lang.IndexOutOfBoundsException 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.IllegalArgumentException 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.uno.Exception 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             }
138 
139         }
140     }
141 
_getTextFields()142     public void _getTextFields() {
143         XEnumerationAccess xEnumAccess = oObj.getTextFields();
144         XEnumeration xEnum = xEnumAccess.createEnumeration();
145         while(xEnum != null && xEnum.hasMoreElements()) {
146             try {
147                 Object o = xEnum.nextElement();
148             }
149             catch(com.sun.star.container.NoSuchElementException e) {
150                 setMethodFalse("getTextFields()", e);
151             }
152             catch(com.sun.star.lang.WrappedTargetException e) {
153                 setMethodFalse("getTextFields()", e);
154             }
155         }
156         tRes.tested("getTextFields()", xEnum != null);
157     }
158 
_getTextFieldMasters()159     public void _getTextFieldMasters() {
160         if (mbCreateFieldMaster) {
161             XNameAccess xName = oObj.getTextFieldMasters();
162             util.dbg.printInterfaces(xName);
163             tRes.tested("getTextFieldMasters()", xName != null);
164         }
165         else {
166             log.println("Could not test 'getTextFieldMasters' because no field masters can be created on this object.");
167             tRes.tested("getTextFieldMasters()", true);
168         }
169     }
170 
171     /**
172      * Just for convenience: log the exception and set the method false.
173      * @param method The name of the method to set to false.
174      * @param e The Exception that occured.
175      */
setMethodFalse(String method, Exception e)176     private void setMethodFalse(String method, Exception e) {
177         log.println("Exception while executing '" + method + "'");
178         e.printStackTrace((PrintWriter)log);
179         tRes.tested(method, false);
180     }
181 
after()182     protected void after() {
183         if (mDispose)
184             disposeEnvironment();
185     }
186 
187 }
188