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 com.sun.star.wizards.common;
25 
26 import java.util.Date;
27 
28 import com.sun.star.beans.XPropertySet;
29 import com.sun.star.lang.Locale;
30 import com.sun.star.lang.XMultiServiceFactory;
31 import com.sun.star.uno.AnyConverter;
32 import com.sun.star.uno.UnoRuntime;
33 import com.sun.star.uno.XInterface;
34 import com.sun.star.util.NumberFormat;
35 import com.sun.star.util.XNumberFormatTypes;
36 import com.sun.star.util.XNumberFormats;
37 import com.sun.star.util.XNumberFormatsSupplier;
38 import com.sun.star.util.XNumberFormatter;
39 
40 
41 public class NumberFormatter
42 {
43 
44     public int iDateFormatKey = -1;
45     public int iDateTimeFormatKey = -1;
46     public int iNumberFormatKey = -1;
47     public int iTextFormatKey = -1;
48     public int iTimeFormatKey = -1;
49     public int iLogicalFormatKey = -1;
50     public long lDateCorrection;
51     public XNumberFormatter xNumberFormatter;
52     public XNumberFormats xNumberFormats;
53     public XNumberFormatTypes xNumberFormatTypes;
54     public XPropertySet xNumberFormatSettings;
55     private boolean bNullDateCorrectionIsDefined = false;
56     private Locale aLocale;
57 
58 
NumberFormatter(XMultiServiceFactory _xMSF, XNumberFormatsSupplier _xNumberFormatsSupplier, Locale _aLocale)59     public NumberFormatter(XMultiServiceFactory _xMSF, XNumberFormatsSupplier _xNumberFormatsSupplier, Locale _aLocale) throws Exception
60     {
61         aLocale = _aLocale;
62         Object oNumberFormatter = _xMSF.createInstance("com.sun.star.util.NumberFormatter");
63         xNumberFormats = _xNumberFormatsSupplier.getNumberFormats();
64         xNumberFormatSettings = _xNumberFormatsSupplier.getNumberFormatSettings();
65         xNumberFormatter = UnoRuntime.queryInterface(XNumberFormatter.class, oNumberFormatter);
66         xNumberFormatter.attachNumberFormatsSupplier(_xNumberFormatsSupplier);
67         xNumberFormatTypes = UnoRuntime.queryInterface(XNumberFormatTypes.class, xNumberFormats);
68 
69     }
70 
NumberFormatter(XNumberFormatsSupplier _xNumberFormatsSupplier, Locale _aLocale)71     public NumberFormatter(XNumberFormatsSupplier _xNumberFormatsSupplier, Locale _aLocale) throws Exception
72     {
73         aLocale = _aLocale;
74         xNumberFormats = _xNumberFormatsSupplier.getNumberFormats();
75         xNumberFormatSettings = _xNumberFormatsSupplier.getNumberFormatSettings();
76         xNumberFormatTypes = UnoRuntime.queryInterface(XNumberFormatTypes.class, xNumberFormats);
77     }
78 
79 
80     /**
81      * @param _xMSF
82      * @param _xNumberFormatsSupplier
83      * @return
84      * @throws Exception
85      * @deprecated
86      *
87      */
createNumberFormatter(XMultiServiceFactory _xMSF, XNumberFormatsSupplier _xNumberFormatsSupplier)88     public static XNumberFormatter createNumberFormatter(XMultiServiceFactory _xMSF, XNumberFormatsSupplier _xNumberFormatsSupplier) throws Exception
89     {
90         Object oNumberFormatter = _xMSF.createInstance("com.sun.star.util.NumberFormatter");
91         XNumberFormatter xNumberFormatter = UnoRuntime.queryInterface(XNumberFormatter.class, oNumberFormatter);
92         xNumberFormatter.attachNumberFormatsSupplier(_xNumberFormatsSupplier);
93         return xNumberFormatter;
94     }
95 
96 
97     /**
98      * gives a key to pass to a NumberFormat object. <br/>
99      * example: <br/>
100      * <pre>
101      * XNumberFormatsSupplier nsf = (XNumberFormatsSupplier)UnoRuntime.queryInterface(...,document);
102      * int key = Desktop.getNumberFormatterKey( nsf, ...star.i18n.NumberFormatIndex.DATE...);
103      * XNumberFormatter nf = Desktop.createNumberFormatter(xmsf, nsf);
104      * nf.convertNumberToString( key, 1972 );
105      * </pre>
106      * @param numberFormatsSupplier
107      * @param type - a constant out of i18n.NumberFormatIndex enumeration.
108      * @return a key to use with a util.NumberFormat instance.
109      *
110      */
getNumberFormatterKey( Object numberFormatsSupplier, short type)111     public static int getNumberFormatterKey( Object numberFormatsSupplier, short type)
112     {
113         Object numberFormatTypes = UnoRuntime.queryInterface(XNumberFormatsSupplier.class,numberFormatsSupplier).getNumberFormats();
114         Locale l = new Locale();
115         return UnoRuntime.queryInterface(XNumberFormatTypes.class,numberFormatTypes).getFormatIndex(type, l);
116     }
117 
118 
convertNumberToString(int _nkey, double _dblValue)119     public String convertNumberToString(int _nkey, double _dblValue)
120     {
121         return xNumberFormatter.convertNumberToString(_nkey, _dblValue);
122     }
123 
124 
convertNumberToString(XNumberFormatter _xNumberFormatter, int _nkey, double _dblValue)125     public static String convertNumberToString(XNumberFormatter _xNumberFormatter, int _nkey, double _dblValue)
126     {
127         return _xNumberFormatter.convertNumberToString(_nkey, _dblValue);
128     }
129 
130 
convertStringToNumber(int _nkey, String _sString)131     public double convertStringToNumber(int _nkey, String _sString)throws Exception
132     {
133         return xNumberFormatter.convertStringToNumber(_nkey, _sString);
134     }
135 
136 
137     /**
138      * @param dateCorrection The lDateCorrection to set.
139      */
setNullDateCorrection(long dateCorrection)140     public void setNullDateCorrection(long dateCorrection)
141     {
142         lDateCorrection = dateCorrection;
143     }
144 
145 
defineNumberFormat(String _FormatString)146     public int defineNumberFormat(String _FormatString)
147     {
148         try
149         {
150             int NewFormatKey = xNumberFormats.queryKey(_FormatString, aLocale, true);
151             if (NewFormatKey == -1)
152             {
153                 NewFormatKey = xNumberFormats.addNew(_FormatString, aLocale);
154             }
155             return NewFormatKey;
156         }
157         catch (Exception e)
158         {
159             e.printStackTrace(System.out);
160             return -1;
161         }
162     }
163 
164 
165     /**
166      * returns a numberformat for a FormatString.
167      * @param _FormatString
168      * @param _aLocale
169      * @return
170      */
defineNumberFormat(String _FormatString, Locale _aLocale)171     public int defineNumberFormat(String _FormatString, Locale _aLocale)
172     {
173         try
174         {
175             int NewFormatKey = xNumberFormats.queryKey(_FormatString, _aLocale, true);
176             if (NewFormatKey == -1)
177             {
178                 NewFormatKey = xNumberFormats.addNew(_FormatString, _aLocale);
179             }
180             return NewFormatKey;
181         }
182         catch (Exception e)
183         {
184             e.printStackTrace(System.out);
185             return -1;
186         }
187     }
188 
189 
190 
setNumberFormat(XInterface _xFormatObject, int _FormatKey, NumberFormatter _oNumberFormatter)191     public void setNumberFormat(XInterface _xFormatObject, int _FormatKey, NumberFormatter _oNumberFormatter)
192     {
193         try
194         {
195             XPropertySet xNumberFormat = _oNumberFormatter.xNumberFormats.getByKey(_FormatKey); //CurDBField.DBFormatKey);
196             String FormatString = AnyConverter.toString(Helper.getUnoPropertyValue(xNumberFormat, "FormatString"));
197             Locale oLocale = (Locale) Helper.getUnoPropertyValue(xNumberFormat, "Locale");
198             int NewFormatKey = defineNumberFormat(FormatString, oLocale);
199             XPropertySet xPSet = UnoRuntime.queryInterface(XPropertySet.class, _xFormatObject);
200             if (xPSet.getPropertySetInfo().hasPropertyByName("FormatsSupplier"))
201                 xPSet.setPropertyValue("FormatsSupplier", _oNumberFormatter.xNumberFormatter.getNumberFormatsSupplier());
202             if (xPSet.getPropertySetInfo().hasPropertyByName("NumberFormat"))
203             {
204                 xPSet.setPropertyValue("NumberFormat", new Integer(NewFormatKey));
205             }
206             else if (xPSet.getPropertySetInfo().hasPropertyByName("FormatKey"))
207             {
208                 xPSet.setPropertyValue("FormatKey", new Integer(NewFormatKey));
209             }
210             else
211             {
212                 // TODO: throws a exception in a try catch environment, very helpful?
213                 throw new Exception();
214             }
215         }
216         catch (Exception exception)
217         {
218             exception.printStackTrace(System.out);
219         }
220     }
221 
222 
getNullDateCorrection()223     public long getNullDateCorrection()
224     {
225         if (!this.bNullDateCorrectionIsDefined)
226         {
227             com.sun.star.util.Date dNullDate = (com.sun.star.util.Date) Helper.getUnoStructValue(this.xNumberFormatSettings, "NullDate");
228             long lNullDate = Helper.convertUnoDatetoInteger(dNullDate);
229             java.util.Calendar oCal = java.util.Calendar.getInstance();
230             oCal.set(1900, 1, 1);
231             Date dTime = oCal.getTime();
232             long lTime = dTime.getTime();
233             long lDBNullDate = lTime / (3600 * 24000);
234             lDateCorrection = lDBNullDate - lNullDate;
235             return lDateCorrection;
236         }
237         else
238         {
239             return this.lDateCorrection;
240         }
241     }
242 
243 
setBooleanReportDisplayNumberFormat()244     public int setBooleanReportDisplayNumberFormat()
245     {
246         String FormatString = "[=1]" + '"' + (char)9745 + '"' + ";[=0]" + '"' + (char)58480 + '"' + ";0";
247         iLogicalFormatKey = xNumberFormats.queryKey(FormatString, aLocale, true);
248         try
249         {
250             if (iLogicalFormatKey == -1)
251             {
252                 iLogicalFormatKey = xNumberFormats.addNew(FormatString, aLocale);
253             }
254         }
255         catch (Exception e)
256         {         //MalformedNumberFormat
257             e.printStackTrace();
258             iLogicalFormatKey = xNumberFormatTypes.getStandardFormat(NumberFormat.LOGICAL, aLocale);
259         }
260         return iLogicalFormatKey;
261     }
262 
263 
264     /**
265      * @return Returns the iDateFormatKey.
266      */
getDateFormatKey()267     public int getDateFormatKey()
268     {
269         if (iDateFormatKey == -1)
270         {
271             iDateFormatKey = xNumberFormatTypes.getStandardFormat(NumberFormat.DATE, aLocale);
272         }
273         return iDateFormatKey;
274     }
275     /**
276      * @return Returns the iDateTimeFormatKey.
277      */
getDateTimeFormatKey()278     public int getDateTimeFormatKey()
279     {
280         if (iDateTimeFormatKey == -1)
281         {
282             iDateTimeFormatKey = xNumberFormatTypes.getStandardFormat(NumberFormat.DATETIME, aLocale);
283         }
284         return iDateTimeFormatKey;
285     }
286     /**
287      * @return Returns the iLogicalFormatKey.
288      */
getLogicalFormatKey()289     public int getLogicalFormatKey()
290     {
291         if (iLogicalFormatKey == -1)
292         {
293             iLogicalFormatKey = xNumberFormatTypes.getStandardFormat(NumberFormat.LOGICAL, aLocale);
294         }
295         return iLogicalFormatKey;
296     }
297     /**
298      * @return Returns the iNumberFormatKey.
299      */
getNumberFormatKey()300     public int getNumberFormatKey()
301     {
302         if (iNumberFormatKey == -1)
303         {
304             iNumberFormatKey = xNumberFormatTypes.getStandardFormat(NumberFormat.NUMBER, aLocale);
305         }
306         return iNumberFormatKey;
307     }
308     /**
309      * @return Returns the iTextFormatKey.
310      */
getTextFormatKey()311     public int getTextFormatKey()
312     {
313         if (iTextFormatKey == -1)
314         {
315             iTextFormatKey = xNumberFormatTypes.getStandardFormat(NumberFormat.TEXT, aLocale);
316         }
317         return iTextFormatKey;
318     }
319     /**
320      * @return Returns the iTimeFormatKey.
321      */
getTimeFormatKey()322     public int getTimeFormatKey()
323     {
324         if (iTimeFormatKey == -1)
325         {
326             iTimeFormatKey = xNumberFormatTypes.getStandardFormat(NumberFormat.TIME, aLocale);
327         }
328         return iTimeFormatKey;
329     }
330 }
331