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 package com.sun.star.wizards.text;
24 
25 import java.util.Calendar;
26 import java.util.GregorianCalendar;
27 import java.util.Vector;
28 
29 import com.sun.star.text.XDependentTextField;
30 import com.sun.star.text.XTextContent;
31 import com.sun.star.text.XTextCursor;
32 import com.sun.star.text.XTextDocument;
33 import com.sun.star.text.XTextFieldsSupplier;
34 import com.sun.star.text.XTextRange;
35 import com.sun.star.beans.XPropertySet;
36 import com.sun.star.container.XEnumeration;
37 import com.sun.star.lang.XComponent;
38 import com.sun.star.lang.XMultiServiceFactory;
39 import com.sun.star.lang.XServiceInfo;
40 import com.sun.star.uno.AnyConverter;
41 import com.sun.star.uno.Exception;
42 import com.sun.star.uno.UnoRuntime;
43 import com.sun.star.uno.XInterface;
44 import com.sun.star.util.DateTime;
45 import com.sun.star.util.XRefreshable;
46 import com.sun.star.util.XUpdatable;
47 import com.sun.star.wizards.common.Helper;
48 import com.sun.star.wizards.common.PropertyNames;
49 
50 public class TextFieldHandler
51 {
52 
53     public XTextFieldsSupplier xTextFieldsSupplier;
54     private XMultiServiceFactory xMSFDoc;
55 
56     /**
57      * Creates a new instance of TextFieldHandler
58      * @param xMSF
59      * @param xTextDocument
60      */
TextFieldHandler(XMultiServiceFactory xMSF, XTextDocument xTextDocument)61     public TextFieldHandler(XMultiServiceFactory xMSF, XTextDocument xTextDocument)
62     {
63         this.xMSFDoc = xMSF;
64         xTextFieldsSupplier = UnoRuntime.queryInterface(XTextFieldsSupplier.class, xTextDocument);
65     }
66 
refreshTextFields()67     public void refreshTextFields()
68     {
69         XRefreshable xUp = UnoRuntime.queryInterface(XRefreshable.class, xTextFieldsSupplier.getTextFields());
70         xUp.refresh();
71     }
72 
getUserFieldContent(XTextCursor xTextCursor)73     public String getUserFieldContent(XTextCursor xTextCursor)
74     {
75         try
76         {
77             XTextRange xTextRange = xTextCursor.getEnd();
78             Object oTextField = Helper.getUnoPropertyValue(xTextRange, "TextField");
79             if (com.sun.star.uno.AnyConverter.isVoid(oTextField))
80             {
81                 return PropertyNames.EMPTY_STRING;
82             }
83             else
84             {
85                 XDependentTextField xDependent = UnoRuntime.queryInterface(XDependentTextField.class, oTextField);
86                 XPropertySet xMaster = xDependent.getTextFieldMaster();
87                 return (String) xMaster.getPropertyValue("Content");
88             }
89         }
90         catch (com.sun.star.uno.Exception exception)
91         {
92             exception.printStackTrace(System.out);
93         }
94         return PropertyNames.EMPTY_STRING;
95     }
96 
insertUserField(XTextCursor xTextCursor, String FieldName, String FieldTitle)97     public void insertUserField(XTextCursor xTextCursor, String FieldName, String FieldTitle)
98     {
99         try
100         {
101             XInterface xField = (XInterface) xMSFDoc.createInstance("com.sun.star.text.TextField.User");
102             XDependentTextField xDepField = UnoRuntime.queryInterface(XDependentTextField.class, xField);
103             XTextContent xFieldContent = UnoRuntime.queryInterface(XTextContent.class, xField);
104             if (xTextFieldsSupplier.getTextFieldMasters().hasByName("com.sun.star.text.FieldMaster.User." + FieldName))
105             {
106                 Object oMaster = xTextFieldsSupplier.getTextFieldMasters().getByName("com.sun.star.text.FieldMaster.User." + FieldName);
107                 XComponent xComponent = UnoRuntime.queryInterface(XComponent.class, oMaster);
108                 xComponent.dispose();
109             }
110             XPropertySet xPSet = createUserField(FieldName, FieldTitle);
111             xDepField.attachTextFieldMaster(xPSet);
112             xTextCursor.getText().insertTextContent(xTextCursor, xFieldContent, false);
113 
114 //            try
115 //            {
116 //                XPropertySet xTestProp = xDepField.getTextFieldMaster();
117 //                String UserFieldName = (String) xTestProp.getPropertyValue(PropertyNames.PROPERTY_NAME);
118 //                // UserFieldName == FieldName?
119 //                int dummy = 0;
120 //            }
121 //            catch (com.sun.star.uno.Exception e)
122 //            {
123 //                int dummy2 = 0;
124 //            }
125 
126         }
127         catch (com.sun.star.uno.Exception exception)
128         {
129             exception.printStackTrace(System.out);
130         }
131     }
132 
createUserField(String FieldName, String FieldTitle)133     public XPropertySet createUserField(String FieldName, String FieldTitle) throws com.sun.star.uno.Exception
134     {
135         Object oMaster = xMSFDoc.createInstance("com.sun.star.text.FieldMaster.User");
136         XPropertySet xPSet = UnoRuntime.queryInterface(XPropertySet.class, oMaster);
137         xPSet.setPropertyValue(PropertyNames.PROPERTY_NAME, FieldName);
138         xPSet.setPropertyValue("Content", FieldTitle);
139 
140         // DEBUG
141         // String sFieldName = (String)xPSet.getPropertyValue(PropertyNames.PROPERTY_NAME);
142 
143         return xPSet;
144     }
145 
getTextFieldsByProperty(String _PropertyName, Object _aPropertyValue, String _TypeName)146     private XDependentTextField[] getTextFieldsByProperty(String _PropertyName, Object _aPropertyValue, String _TypeName) throws Exception
147     {
148         try
149         {
150             XDependentTextField[] xDependentFields;
151             Vector xDependentVector = new Vector();
152             if (xTextFieldsSupplier.getTextFields().hasElements())
153             {
154                 XEnumeration xEnum = xTextFieldsSupplier.getTextFields().createEnumeration();
155                 while (xEnum.hasMoreElements())
156                 {
157                     Object oTextField = xEnum.nextElement();
158                     XDependentTextField xDependent = UnoRuntime.queryInterface(XDependentTextField.class, oTextField);
159                     XPropertySet xPropertySet = xDependent.getTextFieldMaster();
160                     if (xPropertySet.getPropertySetInfo().hasPropertyByName(_PropertyName))
161                     {
162                         Object oValue = xPropertySet.getPropertyValue(_PropertyName);
163                         // TODO replace the following comparison via com.sun.star.uno.Any.Type
164                         if (AnyConverter.isString(oValue))
165                         {
166                             if (_TypeName.equals("String"))
167                             {
168                                 String sValue = AnyConverter.toString(oValue);
169                                 if (sValue.equals(_aPropertyValue))
170                                 {
171                                     xDependentVector.addElement(xDependent);
172                                 }
173                             }
174                         }
175                         else if (AnyConverter.isShort(oValue))
176                         {
177                             if (_TypeName.equals("Short"))
178                             {
179                                 short iShortParam = ((Short) _aPropertyValue).shortValue();
180                                 short ishortValue = AnyConverter.toShort(oValue);
181                                 if (ishortValue == iShortParam)
182                                 {
183                                     xDependentVector.addElement(xDependent);
184                                 }
185                             }
186                         }
187                     }
188                 }
189             }
190             if (xDependentVector.size() > 0)
191             {
192                 xDependentFields = new XDependentTextField[xDependentVector.size()];
193                 xDependentVector.toArray(xDependentFields);
194                 return xDependentFields;
195             }
196         }
197         catch (Exception e)
198         {
199             // TODO Auto-generated catch block
200             e.printStackTrace(System.out);
201         }
202         return null;
203     }
204 
changeUserFieldContent(String _FieldName, String _FieldContent)205     public void changeUserFieldContent(String _FieldName, String _FieldContent)
206     {
207         try
208         {
209             XDependentTextField[] xDependentTextFields = getTextFieldsByProperty(PropertyNames.PROPERTY_NAME, _FieldName, "String");
210             if (xDependentTextFields != null)
211             {
212                 for (int i = 0; i < xDependentTextFields.length; i++)
213                 {
214                     xDependentTextFields[i].getTextFieldMaster().setPropertyValue("Content", _FieldContent);
215                 }
216                 refreshTextFields();
217             }
218         }
219         catch (Exception e)
220         {
221             e.printStackTrace(System.out);
222         }
223     }
224 
updateDocInfoFields()225     public void updateDocInfoFields()
226     {
227         try
228         {
229             XEnumeration xEnum = xTextFieldsSupplier.getTextFields().createEnumeration();
230             while (xEnum.hasMoreElements())
231             {
232                 Object oTextField = xEnum.nextElement();
233                 XServiceInfo xSI = UnoRuntime.queryInterface(XServiceInfo.class, oTextField);
234 
235                 if (xSI.supportsService("com.sun.star.text.TextField.ExtendedUser"))
236                 {
237                     XUpdatable xUp = UnoRuntime.queryInterface(XUpdatable.class, oTextField);
238                     xUp.update();
239                 }
240                 if (xSI.supportsService("com.sun.star.text.TextField.User"))
241                 {
242                     XUpdatable xUp = UnoRuntime.queryInterface(XUpdatable.class, oTextField);
243                     xUp.update();
244                 }
245             }
246         }
247         catch (Exception e)
248         {
249             e.printStackTrace();
250         }
251     }
252 
updateDateFields()253     public void updateDateFields()
254     {
255         try
256         {
257             XEnumeration xEnum = xTextFieldsSupplier.getTextFields().createEnumeration();
258             Calendar cal = new GregorianCalendar();
259             DateTime dt = new DateTime();
260             dt.Day = (short) cal.get(Calendar.DAY_OF_MONTH);
261             dt.Year = (short) cal.get(Calendar.YEAR);
262             dt.Month = (short) cal.get(Calendar.MONTH);
263             dt.Month++;
264 
265             while (xEnum.hasMoreElements())
266             {
267                 Object oTextField = xEnum.nextElement();
268                 XServiceInfo xSI = UnoRuntime.queryInterface(XServiceInfo.class, oTextField);
269 
270                 if (xSI.supportsService("com.sun.star.text.TextField.DateTime"))
271                 {
272                     XPropertySet xPSet = UnoRuntime.queryInterface(XPropertySet.class, oTextField);
273                     xPSet.setPropertyValue("IsFixed", Boolean.FALSE);
274                     xPSet.setPropertyValue("DateTimeValue", dt);
275                 }
276             }
277         }
278         catch (Exception e)
279         {
280             e.printStackTrace();
281         }
282     }
283 
fixDateFields(boolean _bSetFixed)284     public void fixDateFields(boolean _bSetFixed)
285     {
286         try
287         {
288             XEnumeration xEnum = xTextFieldsSupplier.getTextFields().createEnumeration();
289             while (xEnum.hasMoreElements())
290             {
291                 Object oTextField = xEnum.nextElement();
292                 XServiceInfo xSI = UnoRuntime.queryInterface(XServiceInfo.class, oTextField);
293                 if (xSI.supportsService("com.sun.star.text.TextField.DateTime"))
294                 {
295                     XPropertySet xPSet = UnoRuntime.queryInterface(XPropertySet.class, oTextField);
296                     xPSet.setPropertyValue("IsFixed", Boolean.valueOf(_bSetFixed));
297                 }
298             }
299         }
300         catch (Exception e)
301         {
302             e.printStackTrace();
303         }
304     }
305 
removeUserFieldByContent(String _FieldContent)306     public void removeUserFieldByContent(String _FieldContent)
307     {
308         try
309         {
310             XDependentTextField[] xDependentTextFields = getTextFieldsByProperty("Content", _FieldContent, "String");
311             if (xDependentTextFields != null)
312             {
313                 for (int i = 0; i < xDependentTextFields.length; i++)
314                 {
315                     xDependentTextFields[i].dispose();
316                 }
317             }
318         }
319         catch (Exception e)
320         {
321             e.printStackTrace(System.out);
322         }
323     }
324 
changeExtendedUserFieldContent(short UserDataPart, String _FieldContent)325     public void changeExtendedUserFieldContent(short UserDataPart, String _FieldContent)
326     {
327         try
328         {
329             XDependentTextField[] xDependentTextFields = getTextFieldsByProperty("UserDataType", new Short(UserDataPart), "Short");
330             if (xDependentTextFields != null)
331             {
332                 for (int i = 0; i < xDependentTextFields.length; i++)
333                 {
334                     xDependentTextFields[i].getTextFieldMaster().setPropertyValue("Content", _FieldContent);
335                 }
336             }
337             refreshTextFields();
338         }
339         catch (Exception e)
340         {
341             e.printStackTrace(System.out);
342         }
343     }
344 }
345