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