summaryrefslogtreecommitdiff
path: root/wizards/com/sun/star/wizards/common/NumberFormatter.java
blob: 9a6ca217e6c94a4b9d5a57aa9e1e77c66a2f8b52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

package com.sun.star.wizards.common;

import java.util.Date;

import com.sun.star.beans.XPropertySet;
import com.sun.star.lang.Locale;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.uno.AnyConverter;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XInterface;
import com.sun.star.util.NumberFormat;
import com.sun.star.util.XNumberFormatTypes;
import com.sun.star.util.XNumberFormats;
import com.sun.star.util.XNumberFormatsSupplier;
import com.sun.star.util.XNumberFormatter;


public class NumberFormatter
{

    private int iDateFormatKey = -1;
    private int iDateTimeFormatKey = -1;
    private int iNumberFormatKey = -1;
    private int iTextFormatKey = -1;
    private int iTimeFormatKey = -1;
    private int iLogicalFormatKey = -1;
    private long lDateCorrection;
    private XNumberFormatter xNumberFormatter;
    private XNumberFormats xNumberFormats;
    private XNumberFormatTypes xNumberFormatTypes;
    private XPropertySet xNumberFormatSettings;
    private final Locale aLocale;
    static private final Locale enLocale = new Locale( "en", "US", "" );


    public NumberFormatter(XMultiServiceFactory _xMSF, XNumberFormatsSupplier _xNumberFormatsSupplier, Locale _aLocale) throws Exception
    {
        aLocale = _aLocale;
        Object oNumberFormatter = _xMSF.createInstance("com.sun.star.util.NumberFormatter");
        xNumberFormats = _xNumberFormatsSupplier.getNumberFormats();
        xNumberFormatSettings = _xNumberFormatsSupplier.getNumberFormatSettings();
        xNumberFormatter = UnoRuntime.queryInterface(XNumberFormatter.class, oNumberFormatter);
        xNumberFormatter.attachNumberFormatsSupplier(_xNumberFormatsSupplier);
        xNumberFormatTypes = UnoRuntime.queryInterface(XNumberFormatTypes.class, xNumberFormats);

    }

    public NumberFormatter(XNumberFormatsSupplier _xNumberFormatsSupplier, Locale _aLocale) throws Exception
    {
        aLocale = _aLocale;
        xNumberFormats = _xNumberFormatsSupplier.getNumberFormats();
        xNumberFormatSettings = _xNumberFormatsSupplier.getNumberFormatSettings();
        xNumberFormatTypes = UnoRuntime.queryInterface(XNumberFormatTypes.class, xNumberFormats);
    }


    public double convertStringToNumber(int _nkey, String _sString)throws Exception
    {
        return xNumberFormatter.convertStringToNumber(_nkey, _sString);
    }


    /**
     * @param dateCorrection The lDateCorrection to set.
     */
    public void setNullDateCorrection(long dateCorrection)
    {
        lDateCorrection = dateCorrection;
    }


    public int defineNumberFormat(String _FormatString)
    {
        try
        {
            int NewFormatKey = xNumberFormats.queryKey(_FormatString, aLocale, true);
            if (NewFormatKey == -1)
            {
                NewFormatKey = xNumberFormats.addNewConverted(_FormatString, enLocale, aLocale);
            }
            return NewFormatKey;
        }
        catch (Exception e)
        {
            e.printStackTrace(System.err);
            return -1;
        }
    }


    /**
     * returns a numberformat for a FormatString.
     */
    private int defineNumberFormat(String _FormatString, Locale _aLocale)
    {
        try
        {
            int NewFormatKey = xNumberFormats.queryKey(_FormatString, _aLocale, true);
            if (NewFormatKey == -1)
            {
                NewFormatKey = xNumberFormats.addNew(_FormatString, _aLocale);
            }
            return NewFormatKey;
        }
        catch (Exception e)
        {
            e.printStackTrace(System.err);
            return -1;
        }
    }



    public void setNumberFormat(XInterface _xFormatObject, int _FormatKey, NumberFormatter _oNumberFormatter)
    {
        try
        {
            XPropertySet xNumberFormat = _oNumberFormatter.xNumberFormats.getByKey(_FormatKey);
            String FormatString = AnyConverter.toString(Helper.getUnoPropertyValue(xNumberFormat, "FormatString"));
            Locale oLocale = (Locale) Helper.getUnoPropertyValue(xNumberFormat, "Locale");
            int NewFormatKey = defineNumberFormat(FormatString, oLocale);
            XPropertySet xPSet = UnoRuntime.queryInterface(XPropertySet.class, _xFormatObject);
            if (xPSet.getPropertySetInfo().hasPropertyByName("FormatsSupplier"))
            {
                xPSet.setPropertyValue("FormatsSupplier", _oNumberFormatter.xNumberFormatter.getNumberFormatsSupplier());
            }
            if (xPSet.getPropertySetInfo().hasPropertyByName("NumberFormat"))
            {
                xPSet.setPropertyValue("NumberFormat", Integer.valueOf(NewFormatKey));
            }
            else if (xPSet.getPropertySetInfo().hasPropertyByName("FormatKey"))
            {
                xPSet.setPropertyValue("FormatKey", Integer.valueOf(NewFormatKey));
            }
            else
            {
                // TODO: throws a exception in a try catch environment, very helpful?
                throw new Exception();
            }
        }
        catch (Exception exception)
        {
            exception.printStackTrace(System.err);
        }
    }


    public long getNullDateCorrection()
    {
        com.sun.star.util.Date dNullDate = (com.sun.star.util.Date) Helper.getUnoStructValue(this.xNumberFormatSettings, "NullDate");
        long lNullDate = Helper.convertUnoDatetoInteger(dNullDate);
        java.util.Calendar oCal = java.util.Calendar.getInstance();
        oCal.set(1900, 1, 1);
        Date dTime = oCal.getTime();
        long lTime = dTime.getTime();
        long lDBNullDate = lTime / (3600 * 24000);
        lDateCorrection = lDBNullDate - lNullDate;
        return lDateCorrection;
    }


    public int setBooleanReportDisplayNumberFormat()
    {
        String FormatString = "[=1]" + '"' + (char)9745 + '"' + ";[=0]" + '"' + (char)58480 + '"' + ";0";
        iLogicalFormatKey = xNumberFormats.queryKey(FormatString, aLocale, true);
        try
        {
            if (iLogicalFormatKey == -1)
            {
                iLogicalFormatKey = xNumberFormats.addNew(FormatString, aLocale);
            }
        }
        catch (Exception e)
        {         //MalformedNumberFormat
            e.printStackTrace();
            iLogicalFormatKey = xNumberFormatTypes.getStandardFormat(NumberFormat.LOGICAL, aLocale);
        }
        return iLogicalFormatKey;
    }


    /**
     * @return Returns the iDateFormatKey.
     */
    public int getDateFormatKey()
    {
        if (iDateFormatKey == -1)
        {
            iDateFormatKey = xNumberFormatTypes.getStandardFormat(NumberFormat.DATE, aLocale);
        }
        return iDateFormatKey;
    }
    /**
     * @return Returns the iDateTimeFormatKey.
     */
    public int getDateTimeFormatKey()
    {
        if (iDateTimeFormatKey == -1)
        {
            iDateTimeFormatKey = xNumberFormatTypes.getStandardFormat(NumberFormat.DATETIME, aLocale);
        }
        return iDateTimeFormatKey;
    }
    /**
     * @return Returns the iLogicalFormatKey.
     */
    public int getLogicalFormatKey()
    {
        if (iLogicalFormatKey == -1)
        {
            iLogicalFormatKey = xNumberFormatTypes.getStandardFormat(NumberFormat.LOGICAL, aLocale);
        }
        return iLogicalFormatKey;
    }
    /**
     * @return Returns the iNumberFormatKey.
     */
    public int getNumberFormatKey()
    {
        if (iNumberFormatKey == -1)
        {
            iNumberFormatKey = xNumberFormatTypes.getStandardFormat(NumberFormat.NUMBER, aLocale);
        }
        return iNumberFormatKey;
    }
    /**
     * @return Returns the iTextFormatKey.
     */
    public int getTextFormatKey()
    {
        if (iTextFormatKey == -1)
        {
            iTextFormatKey = xNumberFormatTypes.getStandardFormat(NumberFormat.TEXT, aLocale);
        }
        return iTextFormatKey;
    }
    /**
     * @return Returns the iTimeFormatKey.
     */
    public int getTimeFormatKey()
    {
        if (iTimeFormatKey == -1)
        {
            iTimeFormatKey = xNumberFormatTypes.getStandardFormat(NumberFormat.TIME, aLocale);
        }
        return iTimeFormatKey;
    }
}