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 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 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 */ 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 */ 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 119 public String convertNumberToString(int _nkey, double _dblValue) 120 { 121 return xNumberFormatter.convertNumberToString(_nkey, _dblValue); 122 } 123 124 125 public static String convertNumberToString(XNumberFormatter _xNumberFormatter, int _nkey, double _dblValue) 126 { 127 return _xNumberFormatter.convertNumberToString(_nkey, _dblValue); 128 } 129 130 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 */ 140 public void setNullDateCorrection(long dateCorrection) 141 { 142 lDateCorrection = dateCorrection; 143 } 144 145 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 */ 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 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 xPSet.setPropertyValue("FormatsSupplier", _oNumberFormatter.xNumberFormatter.getNumberFormatsSupplier()); 201 if (xPSet.getPropertySetInfo().hasPropertyByName("NumberFormat")) 202 { 203 xPSet.setPropertyValue("NumberFormat", new Integer(NewFormatKey)); 204 } 205 else if (xPSet.getPropertySetInfo().hasPropertyByName("FormatKey")) 206 { 207 xPSet.setPropertyValue("FormatKey", new Integer(NewFormatKey)); 208 } 209 else 210 { 211 // TODO: throws a exception in a try catch environment, very helpful? 212 throw new Exception(); 213 } 214 } 215 catch (Exception exception) 216 { 217 exception.printStackTrace(System.out); 218 } 219 } 220 221 222 public long getNullDateCorrection() 223 { 224 if (!this.bNullDateCorrectionIsDefined) 225 { 226 com.sun.star.util.Date dNullDate = (com.sun.star.util.Date) Helper.getUnoStructValue(this.xNumberFormatSettings, "NullDate"); 227 long lNullDate = Helper.convertUnoDatetoInteger(dNullDate); 228 java.util.Calendar oCal = java.util.Calendar.getInstance(); 229 oCal.set(1900, 1, 1); 230 Date dTime = oCal.getTime(); 231 long lTime = dTime.getTime(); 232 long lDBNullDate = lTime / (3600 * 24000); 233 lDateCorrection = lDBNullDate - lNullDate; 234 return lDateCorrection; 235 } 236 else 237 { 238 return this.lDateCorrection; 239 } 240 } 241 242 243 public int setBooleanReportDisplayNumberFormat() 244 { 245 String FormatString = "[=1]" + '"' + (char)9745 + '"' + ";[=0]" + '"' + (char)58480 + '"' + ";0"; 246 iLogicalFormatKey = xNumberFormats.queryKey(FormatString, aLocale, true); 247 try 248 { 249 if (iLogicalFormatKey == -1) 250 { 251 iLogicalFormatKey = xNumberFormats.addNew(FormatString, aLocale); 252 } 253 } 254 catch (Exception e) 255 { //MalformedNumberFormat 256 e.printStackTrace(); 257 iLogicalFormatKey = xNumberFormatTypes.getStandardFormat(NumberFormat.LOGICAL, aLocale); 258 } 259 return iLogicalFormatKey; 260 } 261 262 263 /** 264 * @return Returns the iDateFormatKey. 265 */ 266 public int getDateFormatKey() 267 { 268 if (iDateFormatKey == -1) 269 { 270 iDateFormatKey = xNumberFormatTypes.getStandardFormat(NumberFormat.DATE, aLocale); 271 } 272 return iDateFormatKey; 273 } 274 /** 275 * @return Returns the iDateTimeFormatKey. 276 */ 277 public int getDateTimeFormatKey() 278 { 279 if (iDateTimeFormatKey == -1) 280 { 281 iDateTimeFormatKey = xNumberFormatTypes.getStandardFormat(NumberFormat.DATETIME, aLocale); 282 } 283 return iDateTimeFormatKey; 284 } 285 /** 286 * @return Returns the iLogicalFormatKey. 287 */ 288 public int getLogicalFormatKey() 289 { 290 if (iLogicalFormatKey == -1) 291 { 292 iLogicalFormatKey = xNumberFormatTypes.getStandardFormat(NumberFormat.LOGICAL, aLocale); 293 } 294 return iLogicalFormatKey; 295 } 296 /** 297 * @return Returns the iNumberFormatKey. 298 */ 299 public int getNumberFormatKey() 300 { 301 if (iNumberFormatKey == -1) 302 { 303 iNumberFormatKey = xNumberFormatTypes.getStandardFormat(NumberFormat.NUMBER, aLocale); 304 } 305 return iNumberFormatKey; 306 } 307 /** 308 * @return Returns the iTextFormatKey. 309 */ 310 public int getTextFormatKey() 311 { 312 if (iTextFormatKey == -1) 313 { 314 iTextFormatKey = xNumberFormatTypes.getStandardFormat(NumberFormat.TEXT, aLocale); 315 } 316 return iTextFormatKey; 317 } 318 /** 319 * @return Returns the iTimeFormatKey. 320 */ 321 public int getTimeFormatKey() 322 { 323 if (iTimeFormatKey == -1) 324 { 325 iTimeFormatKey = xNumberFormatTypes.getStandardFormat(NumberFormat.TIME, aLocale); 326 } 327 return iTimeFormatKey; 328 } 329 } 330