1 /* 2 * This file is part of the LibreOffice project. 3 * 4 * This Source Code Form is subject to the terms of the Mozilla Public 5 * License, v. 2.0. If a copy of the MPL was not distributed with this 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 * 8 * This file incorporates work covered by the following license notice: 9 * 10 * Licensed to the Apache Software Foundation (ASF) under one or more 11 * contributor license agreements. See the NOTICE file distributed 12 * with this work for additional information regarding copyright 13 * ownership. The ASF licenses this file to you under the Apache 14 * License, Version 2.0 (the "License"); you may not use this file 15 * except in compliance with the License. You may obtain a copy of 16 * the License at http://www.apache.org/licenses/LICENSE-2.0 . 17 */ 18 19 package com.sun.star.wizards.common; 20 21 import java.util.Date; 22 23 import com.sun.star.beans.XPropertySet; 24 import com.sun.star.lang.Locale; 25 import com.sun.star.lang.XMultiServiceFactory; 26 import com.sun.star.uno.AnyConverter; 27 import com.sun.star.uno.UnoRuntime; 28 import com.sun.star.uno.XInterface; 29 import com.sun.star.util.NumberFormat; 30 import com.sun.star.util.XNumberFormatTypes; 31 import com.sun.star.util.XNumberFormats; 32 import com.sun.star.util.XNumberFormatsSupplier; 33 import com.sun.star.util.XNumberFormatter; 34 35 36 public class NumberFormatter 37 { 38 39 private int iDateFormatKey = -1; 40 private int iDateTimeFormatKey = -1; 41 private int iNumberFormatKey = -1; 42 private int iTextFormatKey = -1; 43 private int iTimeFormatKey = -1; 44 private int iLogicalFormatKey = -1; 45 private long lDateCorrection; 46 private XNumberFormatter xNumberFormatter; 47 private XNumberFormats xNumberFormats; 48 private XNumberFormatTypes xNumberFormatTypes; 49 private XPropertySet xNumberFormatSettings; 50 private final Locale aLocale; 51 static private final Locale enLocale = new Locale( "en", "US", "" ); 52 53 NumberFormatter(XMultiServiceFactory _xMSF, XNumberFormatsSupplier _xNumberFormatsSupplier, Locale _aLocale)54 public NumberFormatter(XMultiServiceFactory _xMSF, XNumberFormatsSupplier _xNumberFormatsSupplier, Locale _aLocale) throws Exception 55 { 56 aLocale = _aLocale; 57 Object oNumberFormatter = _xMSF.createInstance("com.sun.star.util.NumberFormatter"); 58 xNumberFormats = _xNumberFormatsSupplier.getNumberFormats(); 59 xNumberFormatSettings = _xNumberFormatsSupplier.getNumberFormatSettings(); 60 xNumberFormatter = UnoRuntime.queryInterface(XNumberFormatter.class, oNumberFormatter); 61 xNumberFormatter.attachNumberFormatsSupplier(_xNumberFormatsSupplier); 62 xNumberFormatTypes = UnoRuntime.queryInterface(XNumberFormatTypes.class, xNumberFormats); 63 64 } 65 NumberFormatter(XNumberFormatsSupplier _xNumberFormatsSupplier, Locale _aLocale)66 public NumberFormatter(XNumberFormatsSupplier _xNumberFormatsSupplier, Locale _aLocale) throws Exception 67 { 68 aLocale = _aLocale; 69 xNumberFormats = _xNumberFormatsSupplier.getNumberFormats(); 70 xNumberFormatSettings = _xNumberFormatsSupplier.getNumberFormatSettings(); 71 xNumberFormatTypes = UnoRuntime.queryInterface(XNumberFormatTypes.class, xNumberFormats); 72 } 73 74 convertStringToNumber(int _nkey, String _sString)75 public double convertStringToNumber(int _nkey, String _sString)throws Exception 76 { 77 return xNumberFormatter.convertStringToNumber(_nkey, _sString); 78 } 79 80 81 /** 82 * @param dateCorrection The lDateCorrection to set. 83 */ setNullDateCorrection(long dateCorrection)84 public void setNullDateCorrection(long dateCorrection) 85 { 86 lDateCorrection = dateCorrection; 87 } 88 89 defineNumberFormat(String _FormatString)90 public int defineNumberFormat(String _FormatString) 91 { 92 try 93 { 94 int NewFormatKey = xNumberFormats.queryKey(_FormatString, aLocale, true); 95 if (NewFormatKey == -1) 96 { 97 NewFormatKey = xNumberFormats.addNewConverted(_FormatString, enLocale, aLocale); 98 } 99 return NewFormatKey; 100 } 101 catch (Exception e) 102 { 103 e.printStackTrace(System.err); 104 return -1; 105 } 106 } 107 108 109 /** 110 * returns a numberformat for a FormatString. 111 */ defineNumberFormat(String _FormatString, Locale _aLocale)112 private int defineNumberFormat(String _FormatString, Locale _aLocale) 113 { 114 try 115 { 116 int NewFormatKey = xNumberFormats.queryKey(_FormatString, _aLocale, true); 117 if (NewFormatKey == -1) 118 { 119 NewFormatKey = xNumberFormats.addNew(_FormatString, _aLocale); 120 } 121 return NewFormatKey; 122 } 123 catch (Exception e) 124 { 125 e.printStackTrace(System.err); 126 return -1; 127 } 128 } 129 130 131 setNumberFormat(XInterface _xFormatObject, int _FormatKey, NumberFormatter _oNumberFormatter)132 public void setNumberFormat(XInterface _xFormatObject, int _FormatKey, NumberFormatter _oNumberFormatter) 133 { 134 try 135 { 136 XPropertySet xNumberFormat = _oNumberFormatter.xNumberFormats.getByKey(_FormatKey); 137 String FormatString = AnyConverter.toString(Helper.getUnoPropertyValue(xNumberFormat, "FormatString")); 138 Locale oLocale = (Locale) Helper.getUnoPropertyValue(xNumberFormat, "Locale"); 139 int NewFormatKey = defineNumberFormat(FormatString, oLocale); 140 XPropertySet xPSet = UnoRuntime.queryInterface(XPropertySet.class, _xFormatObject); 141 if (xPSet.getPropertySetInfo().hasPropertyByName("FormatsSupplier")) 142 { 143 xPSet.setPropertyValue("FormatsSupplier", _oNumberFormatter.xNumberFormatter.getNumberFormatsSupplier()); 144 } 145 if (xPSet.getPropertySetInfo().hasPropertyByName("NumberFormat")) 146 { 147 xPSet.setPropertyValue("NumberFormat", Integer.valueOf(NewFormatKey)); 148 } 149 else if (xPSet.getPropertySetInfo().hasPropertyByName("FormatKey")) 150 { 151 xPSet.setPropertyValue("FormatKey", Integer.valueOf(NewFormatKey)); 152 } 153 else 154 { 155 // TODO: throws an exception in a try catch environment, very helpful? 156 throw new Exception(); 157 } 158 } 159 catch (Exception exception) 160 { 161 exception.printStackTrace(System.err); 162 } 163 } 164 165 getNullDateCorrection()166 public long getNullDateCorrection() 167 { 168 com.sun.star.util.Date dNullDate = (com.sun.star.util.Date) Helper.getUnoStructValue(this.xNumberFormatSettings, "NullDate"); 169 long lNullDate = Helper.convertUnoDatetoInteger(dNullDate); 170 java.util.Calendar oCal = java.util.Calendar.getInstance(); 171 oCal.set(1900, 1, 1); 172 Date dTime = oCal.getTime(); 173 long lTime = dTime.getTime(); 174 long lDBNullDate = lTime / (3600 * 24000); 175 lDateCorrection = lDBNullDate - lNullDate; 176 return lDateCorrection; 177 } 178 179 setBooleanReportDisplayNumberFormat()180 public int setBooleanReportDisplayNumberFormat() 181 { 182 String FormatString = "[=1]" + '"' + (char)9745 + '"' + ";[=0]" + '"' + (char)58480 + '"' + ";0"; 183 iLogicalFormatKey = xNumberFormats.queryKey(FormatString, aLocale, true); 184 try 185 { 186 if (iLogicalFormatKey == -1) 187 { 188 iLogicalFormatKey = xNumberFormats.addNew(FormatString, aLocale); 189 } 190 } 191 catch (Exception e) 192 { //MalformedNumberFormat 193 e.printStackTrace(); 194 iLogicalFormatKey = xNumberFormatTypes.getStandardFormat(NumberFormat.LOGICAL, aLocale); 195 } 196 return iLogicalFormatKey; 197 } 198 199 200 /** 201 * @return Returns the iDateFormatKey. 202 */ getDateFormatKey()203 public int getDateFormatKey() 204 { 205 if (iDateFormatKey == -1) 206 { 207 iDateFormatKey = xNumberFormatTypes.getStandardFormat(NumberFormat.DATE, aLocale); 208 } 209 return iDateFormatKey; 210 } 211 /** 212 * @return Returns the iDateTimeFormatKey. 213 */ getDateTimeFormatKey()214 public int getDateTimeFormatKey() 215 { 216 if (iDateTimeFormatKey == -1) 217 { 218 iDateTimeFormatKey = xNumberFormatTypes.getStandardFormat(NumberFormat.DATETIME, aLocale); 219 } 220 return iDateTimeFormatKey; 221 } 222 /** 223 * @return Returns the iLogicalFormatKey. 224 */ getLogicalFormatKey()225 public int getLogicalFormatKey() 226 { 227 if (iLogicalFormatKey == -1) 228 { 229 iLogicalFormatKey = xNumberFormatTypes.getStandardFormat(NumberFormat.LOGICAL, aLocale); 230 } 231 return iLogicalFormatKey; 232 } 233 /** 234 * @return Returns the iNumberFormatKey. 235 */ getNumberFormatKey()236 public int getNumberFormatKey() 237 { 238 if (iNumberFormatKey == -1) 239 { 240 iNumberFormatKey = xNumberFormatTypes.getStandardFormat(NumberFormat.NUMBER, aLocale); 241 } 242 return iNumberFormatKey; 243 } 244 /** 245 * @return Returns the iTextFormatKey. 246 */ getTextFormatKey()247 public int getTextFormatKey() 248 { 249 if (iTextFormatKey == -1) 250 { 251 iTextFormatKey = xNumberFormatTypes.getStandardFormat(NumberFormat.TEXT, aLocale); 252 } 253 return iTextFormatKey; 254 } 255 /** 256 * @return Returns the iTimeFormatKey. 257 */ getTimeFormatKey()258 public int getTimeFormatKey() 259 { 260 if (iTimeFormatKey == -1) 261 { 262 iTimeFormatKey = xNumberFormatTypes.getStandardFormat(NumberFormat.TIME, aLocale); 263 } 264 return iTimeFormatKey; 265 } 266 } 267