1a1b4a26bSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3a1b4a26bSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4a1b4a26bSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5a1b4a26bSAndrew Rist  * distributed with this work for additional information
6a1b4a26bSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7a1b4a26bSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8a1b4a26bSAndrew Rist  * "License"); you may not use this file except in compliance
9a1b4a26bSAndrew Rist  * with the License.  You may obtain a copy of the License at
10a1b4a26bSAndrew Rist  *
11a1b4a26bSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12a1b4a26bSAndrew Rist  *
13a1b4a26bSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14a1b4a26bSAndrew Rist  * software distributed under the License is distributed on an
15a1b4a26bSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16a1b4a26bSAndrew Rist  * KIND, either express or implied.  See the License for the
17a1b4a26bSAndrew Rist  * specific language governing permissions and limitations
18a1b4a26bSAndrew Rist  * under the License.
19a1b4a26bSAndrew Rist  *
20a1b4a26bSAndrew Rist  *************************************************************/
21a1b4a26bSAndrew Rist 
22a1b4a26bSAndrew Rist 
23cdf0e10cSrcweir package com.sun.star.wizards.common;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
26cdf0e10cSrcweir import com.sun.star.util.XMacroExpander;
27cdf0e10cSrcweir import java.util.Calendar;
28cdf0e10cSrcweir 
29cdf0e10cSrcweir import com.sun.star.beans.Property;
30cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
31cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
32cdf0e10cSrcweir // import com.sun.star.i18n.NumberFormatIndex;
33cdf0e10cSrcweir import com.sun.star.lang.Locale;
34cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
35cdf0e10cSrcweir // import com.sun.star.uno.Any;
36cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
37cdf0e10cSrcweir import com.sun.star.uno.RuntimeException;
38cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
39cdf0e10cSrcweir import com.sun.star.util.DateTime;
40cdf0e10cSrcweir import com.sun.star.util.XNumberFormatsSupplier;
41cdf0e10cSrcweir import com.sun.star.util.XNumberFormatter;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir public class Helper
44cdf0e10cSrcweir {
45cdf0e10cSrcweir 
46cdf0e10cSrcweir     /** Creates a new instance of Helper */
Helper()47cdf0e10cSrcweir     public Helper()
48cdf0e10cSrcweir     {
49cdf0e10cSrcweir     }
50cdf0e10cSrcweir 
convertUnoDatetoInteger(com.sun.star.util.Date DateValue)51cdf0e10cSrcweir     public static long convertUnoDatetoInteger(com.sun.star.util.Date DateValue)
52cdf0e10cSrcweir     {
53cdf0e10cSrcweir         java.util.Calendar oCal = java.util.Calendar.getInstance();
54cdf0e10cSrcweir         oCal.set(DateValue.Year, DateValue.Month, DateValue.Day);
55cdf0e10cSrcweir         java.util.Date dTime = oCal.getTime();
56cdf0e10cSrcweir         long lTime = dTime.getTime();
57cdf0e10cSrcweir         return lTime / (3600 * 24000);
58cdf0e10cSrcweir     }
59cdf0e10cSrcweir 
setUnoPropertyValue(Object oUnoObject, String PropertyName, Object PropertyValue)60cdf0e10cSrcweir     public static void setUnoPropertyValue(Object oUnoObject, String PropertyName, Object PropertyValue)
61cdf0e10cSrcweir     {
62cdf0e10cSrcweir         try
63cdf0e10cSrcweir         {
64cdf0e10cSrcweir             XPropertySet xPSet = UnoRuntime.queryInterface(XPropertySet.class, oUnoObject);
65cdf0e10cSrcweir             if (xPSet.getPropertySetInfo().hasPropertyByName(PropertyName))
66cdf0e10cSrcweir             {
67cdf0e10cSrcweir                 xPSet.setPropertyValue(PropertyName, PropertyValue);
68cdf0e10cSrcweir             }
69cdf0e10cSrcweir             else
70cdf0e10cSrcweir             {
71cdf0e10cSrcweir                 Property[] selementnames = xPSet.getPropertySetInfo().getProperties();
72cdf0e10cSrcweir                 throw new java.lang.IllegalArgumentException("No Such Property: '" + PropertyName + "'");
73cdf0e10cSrcweir             }
74cdf0e10cSrcweir         }
75cdf0e10cSrcweir         catch (Exception exception)
76cdf0e10cSrcweir         {
77cdf0e10cSrcweir             exception.printStackTrace(System.out);
78cdf0e10cSrcweir         }
79cdf0e10cSrcweir     }
80cdf0e10cSrcweir 
getUnoObjectbyName(Object oUnoObject, String ElementName)81cdf0e10cSrcweir     public static Object getUnoObjectbyName(Object oUnoObject, String ElementName)
82cdf0e10cSrcweir     {
83cdf0e10cSrcweir         try
84cdf0e10cSrcweir         {
85cdf0e10cSrcweir             com.sun.star.container.XNameAccess xName = UnoRuntime.queryInterface(com.sun.star.container.XNameAccess.class, oUnoObject);
86cdf0e10cSrcweir             if (xName.hasByName(ElementName))
87cdf0e10cSrcweir             {
88cdf0e10cSrcweir                 return xName.getByName(ElementName);
89cdf0e10cSrcweir             }
90cdf0e10cSrcweir             else
91cdf0e10cSrcweir             {
92cdf0e10cSrcweir                 throw new RuntimeException();
93cdf0e10cSrcweir             }
94cdf0e10cSrcweir         }
95cdf0e10cSrcweir         catch (Exception exception)
96cdf0e10cSrcweir         {
97cdf0e10cSrcweir             exception.printStackTrace(System.out);
98cdf0e10cSrcweir             return null;
99cdf0e10cSrcweir         }
100cdf0e10cSrcweir     }
101cdf0e10cSrcweir 
getPropertyValue(PropertyValue[] CurPropertyValue, String PropertyName)102cdf0e10cSrcweir     public static Object getPropertyValue(PropertyValue[] CurPropertyValue, String PropertyName)
103cdf0e10cSrcweir     {
104cdf0e10cSrcweir         int MaxCount = CurPropertyValue.length;
105cdf0e10cSrcweir         for (int i = 0; i < MaxCount; i++)
106cdf0e10cSrcweir         {
107cdf0e10cSrcweir             if (CurPropertyValue[i] != null)
108cdf0e10cSrcweir             {
109cdf0e10cSrcweir                 if (CurPropertyValue[i].Name.equals(PropertyName))
110cdf0e10cSrcweir                 {
111cdf0e10cSrcweir                     return CurPropertyValue[i].Value;
112cdf0e10cSrcweir                 }
113cdf0e10cSrcweir             }
114cdf0e10cSrcweir         }
115cdf0e10cSrcweir         throw new RuntimeException();
116cdf0e10cSrcweir     }
117cdf0e10cSrcweir 
getUnoPropertyValue(Object oUnoObject, String PropertyName, java.lang.Class xClass)118cdf0e10cSrcweir     public static Object getUnoPropertyValue(Object oUnoObject, String PropertyName, java.lang.Class xClass)
119cdf0e10cSrcweir     {
120cdf0e10cSrcweir         try
121cdf0e10cSrcweir         {
122cdf0e10cSrcweir             if (oUnoObject != null)
123cdf0e10cSrcweir             {
124cdf0e10cSrcweir                 XPropertySet xPSet = UnoRuntime.queryInterface(XPropertySet.class, oUnoObject);
125cdf0e10cSrcweir                 Object oObject = xPSet.getPropertyValue(PropertyName);
126cdf0e10cSrcweir                 if (AnyConverter.isVoid(oObject))
127cdf0e10cSrcweir                 {
128cdf0e10cSrcweir                     return null;
129cdf0e10cSrcweir                 }
130cdf0e10cSrcweir                 else
131cdf0e10cSrcweir                 {
132cdf0e10cSrcweir                     return com.sun.star.uno.AnyConverter.toObject(new com.sun.star.uno.Type(xClass), oObject);
133cdf0e10cSrcweir                 }
134cdf0e10cSrcweir             }
135cdf0e10cSrcweir             return null;
136cdf0e10cSrcweir         }
137cdf0e10cSrcweir         catch (Exception exception)
138cdf0e10cSrcweir         {
139cdf0e10cSrcweir             exception.printStackTrace(System.out);
140cdf0e10cSrcweir             return null;
141cdf0e10cSrcweir         }
142cdf0e10cSrcweir     }
143cdf0e10cSrcweir 
getPropertyValuefromAny(Object[] CurPropertyValue, String PropertyName)144cdf0e10cSrcweir     public static Object getPropertyValuefromAny(Object[] CurPropertyValue, String PropertyName)
145cdf0e10cSrcweir     {
146cdf0e10cSrcweir         if (CurPropertyValue != null)
147cdf0e10cSrcweir         {
148cdf0e10cSrcweir             int MaxCount = CurPropertyValue.length;
149cdf0e10cSrcweir             for (int i = 0; i < MaxCount; i++)
150cdf0e10cSrcweir             {
151cdf0e10cSrcweir                 if (CurPropertyValue[i] != null)
152cdf0e10cSrcweir                 {
153cdf0e10cSrcweir                     PropertyValue aValue = (PropertyValue) CurPropertyValue[i];
154cdf0e10cSrcweir                     if (aValue != null && aValue.Name.equals(PropertyName))
155cdf0e10cSrcweir                     {
156cdf0e10cSrcweir                         return aValue.Value;
157cdf0e10cSrcweir                     }
158cdf0e10cSrcweir                 }
159cdf0e10cSrcweir             }
160cdf0e10cSrcweir         }
161cdf0e10cSrcweir         //  System.out.println("Property not found: " + PropertyName);
162cdf0e10cSrcweir         return null;
163cdf0e10cSrcweir     }
164cdf0e10cSrcweir 
getPropertyValuefromAny(Object[] CurPropertyValue, String PropertyName, java.lang.Class xClass)165cdf0e10cSrcweir     public static Object getPropertyValuefromAny(Object[] CurPropertyValue, String PropertyName, java.lang.Class xClass)
166cdf0e10cSrcweir     {
167cdf0e10cSrcweir         try
168cdf0e10cSrcweir         {
169cdf0e10cSrcweir             if (CurPropertyValue != null)
170cdf0e10cSrcweir             {
171cdf0e10cSrcweir                 int MaxCount = CurPropertyValue.length;
172cdf0e10cSrcweir                 for (int i = 0; i < MaxCount; i++)
173cdf0e10cSrcweir                 {
174cdf0e10cSrcweir                     if (CurPropertyValue[i] != null)
175cdf0e10cSrcweir                     {
176cdf0e10cSrcweir                         PropertyValue aValue = (PropertyValue) CurPropertyValue[i];
177cdf0e10cSrcweir                         if (aValue != null && aValue.Name.equals(PropertyName))
178cdf0e10cSrcweir                         {
179cdf0e10cSrcweir                             return com.sun.star.uno.AnyConverter.toObject(new com.sun.star.uno.Type(xClass), aValue.Value);
180cdf0e10cSrcweir                         }
181cdf0e10cSrcweir                     }
182cdf0e10cSrcweir                 }
183cdf0e10cSrcweir             }
184cdf0e10cSrcweir             //  System.out.println("Property not found: " + PropertyName);
185cdf0e10cSrcweir             return null;
186cdf0e10cSrcweir         }
187cdf0e10cSrcweir         catch (Exception exception)
188cdf0e10cSrcweir         {
189cdf0e10cSrcweir             exception.printStackTrace(System.out);
190cdf0e10cSrcweir             return null;
191cdf0e10cSrcweir         }
192cdf0e10cSrcweir     }
193cdf0e10cSrcweir 
getUnoPropertyValue(Object oUnoObject, String PropertyName)194cdf0e10cSrcweir     public static Object getUnoPropertyValue(Object oUnoObject, String PropertyName)
195cdf0e10cSrcweir     {
196cdf0e10cSrcweir         try
197cdf0e10cSrcweir         {
198cdf0e10cSrcweir             if (oUnoObject != null)
199cdf0e10cSrcweir             {
200cdf0e10cSrcweir                 XPropertySet xPSet = UnoRuntime.queryInterface(XPropertySet.class, oUnoObject);
201cdf0e10cSrcweir                 // Property[] aProps = xPSet.getPropertySetInfo().getProperties();
202cdf0e10cSrcweir                 return xPSet.getPropertyValue(PropertyName);
203cdf0e10cSrcweir             }
204cdf0e10cSrcweir         }
205cdf0e10cSrcweir         catch (Exception exception)
206cdf0e10cSrcweir         {
207cdf0e10cSrcweir             exception.printStackTrace(System.out);
208cdf0e10cSrcweir         }
209cdf0e10cSrcweir         return null;
210cdf0e10cSrcweir     }
211cdf0e10cSrcweir 
getUnoArrayPropertyValue(Object oUnoObject, String PropertyName)212cdf0e10cSrcweir     public static Object getUnoArrayPropertyValue(Object oUnoObject, String PropertyName)
213cdf0e10cSrcweir     {
214cdf0e10cSrcweir         try
215cdf0e10cSrcweir         {
216cdf0e10cSrcweir             if (oUnoObject != null)
217cdf0e10cSrcweir             {
218cdf0e10cSrcweir                 XPropertySet xPSet = UnoRuntime.queryInterface(XPropertySet.class, oUnoObject);
219cdf0e10cSrcweir                 Object oObject = xPSet.getPropertyValue(PropertyName);
220cdf0e10cSrcweir                 if (AnyConverter.isArray(oObject))
221cdf0e10cSrcweir                 {
222cdf0e10cSrcweir                     return getArrayValue(oObject);
223cdf0e10cSrcweir                 }
224cdf0e10cSrcweir             }
225cdf0e10cSrcweir         }
226cdf0e10cSrcweir         catch (Exception exception)
227cdf0e10cSrcweir         {
228cdf0e10cSrcweir             exception.printStackTrace(System.out);
229cdf0e10cSrcweir         }
230cdf0e10cSrcweir         return null;
231cdf0e10cSrcweir     }
232cdf0e10cSrcweir 
getUnoStructValue(Object oUnoObject, String PropertyName)233cdf0e10cSrcweir     public static Object getUnoStructValue(Object oUnoObject, String PropertyName)
234cdf0e10cSrcweir     {
235cdf0e10cSrcweir         try
236cdf0e10cSrcweir         {
237cdf0e10cSrcweir             if (oUnoObject != null)
238cdf0e10cSrcweir             {
239cdf0e10cSrcweir                 XPropertySet xPSet = UnoRuntime.queryInterface(XPropertySet.class, oUnoObject);
240cdf0e10cSrcweir                 if (xPSet.getPropertySetInfo().hasPropertyByName(PropertyName))
241cdf0e10cSrcweir                 {
242cdf0e10cSrcweir                     return xPSet.getPropertyValue(PropertyName);
243cdf0e10cSrcweir                 }
244cdf0e10cSrcweir             }
245cdf0e10cSrcweir             return null;
246cdf0e10cSrcweir         }
247cdf0e10cSrcweir         catch (Exception exception)
248cdf0e10cSrcweir         {
249cdf0e10cSrcweir             exception.printStackTrace(System.out);
250cdf0e10cSrcweir             return null;
251cdf0e10cSrcweir         }
252cdf0e10cSrcweir     }
253cdf0e10cSrcweir 
setUnoPropertyValues(Object oUnoObject, String[] PropertyNames, Object[] PropertyValues)254cdf0e10cSrcweir     public static void setUnoPropertyValues(Object oUnoObject, String[] PropertyNames, Object[] PropertyValues)
255cdf0e10cSrcweir     {
256cdf0e10cSrcweir         try
257cdf0e10cSrcweir         {
258cdf0e10cSrcweir             com.sun.star.beans.XMultiPropertySet xMultiPSetLst = UnoRuntime.queryInterface(com.sun.star.beans.XMultiPropertySet.class, oUnoObject);
259cdf0e10cSrcweir             if (xMultiPSetLst != null)
260cdf0e10cSrcweir             {
261cdf0e10cSrcweir                 xMultiPSetLst.setPropertyValues(PropertyNames, PropertyValues);
262cdf0e10cSrcweir             }
263cdf0e10cSrcweir             else
264cdf0e10cSrcweir             {
265cdf0e10cSrcweir                 for (int i = 0; i < PropertyNames.length; i++)
266cdf0e10cSrcweir                 {
267cdf0e10cSrcweir                     //System.out.println(PropertyNames[i] + "=" + PropertyValues[i]);
268cdf0e10cSrcweir                     setUnoPropertyValue(oUnoObject, PropertyNames[i], PropertyValues[i]);
269cdf0e10cSrcweir                 }
270cdf0e10cSrcweir             }
271cdf0e10cSrcweir         }
272cdf0e10cSrcweir         catch (Exception exception)
273cdf0e10cSrcweir         {
274cdf0e10cSrcweir             exception.printStackTrace(System.out);
275cdf0e10cSrcweir         }
276cdf0e10cSrcweir     }
277cdf0e10cSrcweir 
278cdf0e10cSrcweir     /**
279cdf0e10cSrcweir      * @author bc93774
280cdf0e10cSrcweir      * checks if the value of an object that represents an array is null.
281cdf0e10cSrcweir      * check beforehand if the Object is really an array with "AnyConverter.IsArray(oObject)
282*86e1cf34SPedro Giffuni      * @param oValue the parameter that has to represent an object
283cdf0e10cSrcweir      * @return a null reference if the array is empty
284cdf0e10cSrcweir      */
getArrayValue(Object oValue)285cdf0e10cSrcweir     public static Object getArrayValue(Object oValue)
286cdf0e10cSrcweir     {
287cdf0e10cSrcweir         try
288cdf0e10cSrcweir         {
289cdf0e10cSrcweir             Object oPropList = com.sun.star.uno.AnyConverter.toArray(oValue);
290cdf0e10cSrcweir             int nlen = java.lang.reflect.Array.getLength(oPropList);
291cdf0e10cSrcweir             if (nlen == 0)
292cdf0e10cSrcweir             {
293cdf0e10cSrcweir                 return null;
294cdf0e10cSrcweir             }
295cdf0e10cSrcweir             else
296cdf0e10cSrcweir             {
297cdf0e10cSrcweir                 return oPropList;
298cdf0e10cSrcweir             }
299cdf0e10cSrcweir         }
300cdf0e10cSrcweir         catch (Exception exception)
301cdf0e10cSrcweir         {
302cdf0e10cSrcweir             exception.printStackTrace(System.out);
303cdf0e10cSrcweir             return null;
304cdf0e10cSrcweir         }
305cdf0e10cSrcweir     }
306cdf0e10cSrcweir     private static long DAY_IN_MILLIS = (24 * 60 * 60 * 1000);
307cdf0e10cSrcweir 
308cdf0e10cSrcweir     public static class DateUtils
309cdf0e10cSrcweir     {
310cdf0e10cSrcweir 
311cdf0e10cSrcweir         private long docNullTime;
312cdf0e10cSrcweir         private XNumberFormatter formatter;
313cdf0e10cSrcweir         private XNumberFormatsSupplier formatSupplier;
314cdf0e10cSrcweir         private Calendar calendar;
315cdf0e10cSrcweir 
DateUtils(XMultiServiceFactory xmsf, Object document)316cdf0e10cSrcweir         public DateUtils(XMultiServiceFactory xmsf, Object document) throws Exception
317cdf0e10cSrcweir         {
318cdf0e10cSrcweir             XMultiServiceFactory docMSF = UnoRuntime.queryInterface(XMultiServiceFactory.class, document);
319cdf0e10cSrcweir 
320cdf0e10cSrcweir             Object defaults = docMSF.createInstance("com.sun.star.text.Defaults");
321cdf0e10cSrcweir             Locale l = (Locale) Helper.getUnoStructValue(defaults, "CharLocale");
322cdf0e10cSrcweir 
323cdf0e10cSrcweir             java.util.Locale jl = new java.util.Locale(
324cdf0e10cSrcweir                     l.Language, l.Country, l.Variant);
325cdf0e10cSrcweir 
326cdf0e10cSrcweir             calendar = Calendar.getInstance(jl);
327cdf0e10cSrcweir 
328cdf0e10cSrcweir             formatSupplier = UnoRuntime.queryInterface(XNumberFormatsSupplier.class, document);
329cdf0e10cSrcweir 
330cdf0e10cSrcweir             Object formatSettings = formatSupplier.getNumberFormatSettings();
331cdf0e10cSrcweir             com.sun.star.util.Date date = (com.sun.star.util.Date) Helper.getUnoPropertyValue(formatSettings, "NullDate");
332cdf0e10cSrcweir 
333cdf0e10cSrcweir             calendar.set(date.Year, date.Month - 1, date.Day);
334cdf0e10cSrcweir             docNullTime = getTimeInMillis();
335cdf0e10cSrcweir 
336cdf0e10cSrcweir             formatter = NumberFormatter.createNumberFormatter(xmsf, formatSupplier);
337cdf0e10cSrcweir         }
338cdf0e10cSrcweir 
339cdf0e10cSrcweir         /**
340cdf0e10cSrcweir          * @param format a constant of the enumeration NumberFormatIndex
341cdf0e10cSrcweir          * @return
342cdf0e10cSrcweir          */
getFormat(short format)343cdf0e10cSrcweir         public int getFormat(short format)
344cdf0e10cSrcweir         {
345cdf0e10cSrcweir             return NumberFormatter.getNumberFormatterKey(formatSupplier, format);
346cdf0e10cSrcweir         }
347cdf0e10cSrcweir 
getFormatter()348cdf0e10cSrcweir         public XNumberFormatter getFormatter()
349cdf0e10cSrcweir         {
350cdf0e10cSrcweir             return formatter;
351cdf0e10cSrcweir         }
352cdf0e10cSrcweir 
getTimeInMillis()353cdf0e10cSrcweir         private long getTimeInMillis()
354cdf0e10cSrcweir         {
355cdf0e10cSrcweir             java.util.Date dDate = calendar.getTime();
356cdf0e10cSrcweir             return dDate.getTime();
357cdf0e10cSrcweir         }
358cdf0e10cSrcweir 
359cdf0e10cSrcweir         /**
360cdf0e10cSrcweir          * @param date a VCL date in form of 20041231
361cdf0e10cSrcweir          * @return a document relative date
362cdf0e10cSrcweir          */
getDocumentDateAsDouble(int date)363cdf0e10cSrcweir         public synchronized double getDocumentDateAsDouble(int date)
364cdf0e10cSrcweir         {
365cdf0e10cSrcweir             calendar.clear();
366cdf0e10cSrcweir             calendar.set(date / 10000,
367cdf0e10cSrcweir                     (date % 10000) / 100 - 1,
368cdf0e10cSrcweir                     date % 100);
369cdf0e10cSrcweir 
370cdf0e10cSrcweir             long date1 = getTimeInMillis();
371cdf0e10cSrcweir             /*
372cdf0e10cSrcweir              * docNullTime and date1 are in millis, but
373cdf0e10cSrcweir              * I need a day...
374cdf0e10cSrcweir              */
375cdf0e10cSrcweir             return (date1 - docNullTime) / DAY_IN_MILLIS + 1;
376cdf0e10cSrcweir         }
377cdf0e10cSrcweir 
getDocumentDateAsDouble(DateTime date)378cdf0e10cSrcweir         public double getDocumentDateAsDouble(DateTime date)
379cdf0e10cSrcweir         {
380cdf0e10cSrcweir             return getDocumentDateAsDouble(date.Year * 10000 + date.Month * 100 + date.Day);
381cdf0e10cSrcweir         }
382cdf0e10cSrcweir 
getDocumentDateAsDouble(long javaTimeInMillis)383cdf0e10cSrcweir         public synchronized double getDocumentDateAsDouble(long javaTimeInMillis)
384cdf0e10cSrcweir         {
385cdf0e10cSrcweir             calendar.clear();
386cdf0e10cSrcweir             JavaTools.setTimeInMillis(calendar, javaTimeInMillis);
387cdf0e10cSrcweir 
388cdf0e10cSrcweir             long date1 = getTimeInMillis();
389cdf0e10cSrcweir 
390cdf0e10cSrcweir             /*
391cdf0e10cSrcweir              * docNullTime and date1 are in millis, but
392cdf0e10cSrcweir              * I need a day...
393cdf0e10cSrcweir              */
394cdf0e10cSrcweir             return (date1 - docNullTime) / DAY_IN_MILLIS + 1;
395cdf0e10cSrcweir 
396cdf0e10cSrcweir         }
397cdf0e10cSrcweir 
format(int formatIndex, int date)398cdf0e10cSrcweir         public String format(int formatIndex, int date)
399cdf0e10cSrcweir         {
400cdf0e10cSrcweir             return formatter.convertNumberToString(formatIndex, getDocumentDateAsDouble(date));
401cdf0e10cSrcweir         }
402cdf0e10cSrcweir 
format(int formatIndex, DateTime date)403cdf0e10cSrcweir         public String format(int formatIndex, DateTime date)
404cdf0e10cSrcweir         {
405cdf0e10cSrcweir             return formatter.convertNumberToString(formatIndex, getDocumentDateAsDouble(date));
406cdf0e10cSrcweir         }
407cdf0e10cSrcweir 
format(int formatIndex, long javaTimeInMillis)408cdf0e10cSrcweir         public String format(int formatIndex, long javaTimeInMillis)
409cdf0e10cSrcweir         {
410cdf0e10cSrcweir             return formatter.convertNumberToString(formatIndex, getDocumentDateAsDouble(javaTimeInMillis));
411cdf0e10cSrcweir         }
412cdf0e10cSrcweir     }
413cdf0e10cSrcweir 
getComponentContext(XMultiServiceFactory _xMSF)414cdf0e10cSrcweir     public static XComponentContext getComponentContext(XMultiServiceFactory _xMSF)
415cdf0e10cSrcweir     {
416cdf0e10cSrcweir         // Get the path to the extension and try to add the path to the class loader
417cdf0e10cSrcweir         final XPropertySet xProps = UnoRuntime.queryInterface(XPropertySet.class, _xMSF);
418cdf0e10cSrcweir         final PropertySetHelper aHelper = new PropertySetHelper(xProps);
419cdf0e10cSrcweir         final Object aDefaultContext = aHelper.getPropertyValueAsObject("DefaultContext");
420cdf0e10cSrcweir         return UnoRuntime.queryInterface(XComponentContext.class, aDefaultContext);
421cdf0e10cSrcweir     }
422cdf0e10cSrcweir 
getMacroExpander(XMultiServiceFactory _xMSF)423cdf0e10cSrcweir     public static XMacroExpander getMacroExpander(XMultiServiceFactory _xMSF)
424cdf0e10cSrcweir     {
425cdf0e10cSrcweir         final XComponentContext xComponentContext = getComponentContext(_xMSF);
426cdf0e10cSrcweir         final Object aSingleton = xComponentContext.getValueByName("/singletons/com.sun.star.util.theMacroExpander");
427cdf0e10cSrcweir         return UnoRuntime.queryInterface(XMacroExpander.class, aSingleton);
428cdf0e10cSrcweir     }
429cdf0e10cSrcweir }
430