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