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