NumberFormatter.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. import traceback
  19. from com.sun.star.lang import Locale
  20. class NumberFormatter(object):
  21. def __init__(self, _xNumberFormatsSupplier, _aLocale, _xMSF=None):
  22. self.iDateFormatKey = -1
  23. self.iDateTimeFormatKey = -1
  24. self.iNumberFormatKey = -1
  25. self.iTextFormatKey = -1
  26. self.iTimeFormatKey = -1
  27. self.iLogicalFormatKey = -1
  28. self.bNullDateCorrectionIsDefined = False
  29. self.aLocale = _aLocale
  30. if _xMSF is not None:
  31. self.xNumberFormatter = _xMSF.createInstance(
  32. "com.sun.star.util.NumberFormatter")
  33. self.xNumberFormats = _xNumberFormatsSupplier.NumberFormats
  34. self.xNumberFormatSettings = \
  35. _xNumberFormatsSupplier.NumberFormatSettings
  36. self.xNumberFormatter.attachNumberFormatsSupplier(
  37. _xNumberFormatsSupplier)
  38. '''
  39. @param _xMSF
  40. @param _xNumberFormatsSupplier
  41. @return
  42. @throws Exception
  43. @deprecated
  44. '''
  45. @classmethod
  46. def createNumberFormatter(self, _xMSF, _xNumberFormatsSupplier):
  47. oNumberFormatter = _xMSF.createInstance(
  48. "com.sun.star.util.NumberFormatter")
  49. oNumberFormatter.attachNumberFormatsSupplier(_xNumberFormatsSupplier)
  50. return oNumberFormatter
  51. '''
  52. gives a key to pass to a NumberFormat object. <br/>
  53. example: <br/>
  54. <pre>
  55. XNumberFormatsSupplier nsf =
  56. (XNumberFormatsSupplier)UnoRuntime.queryInterface(...,document)
  57. int key = Desktop.getNumberFormatterKey(
  58. nsf, ...star.i18n.NumberFormatIndex.DATE...)
  59. XNumberFormatter nf = Desktop.createNumberFormatter(xmsf, nsf);
  60. nf.convertNumberToString( key, 1972 );
  61. </pre>
  62. @param numberFormatsSupplier
  63. @param type - a constant out of i18n.NumberFormatIndex enumeration.
  64. @return a key to use with a util.NumberFormat instance.
  65. '''
  66. @classmethod
  67. def getNumberFormatterKey(self, numberFormatsSupplier, Type):
  68. return numberFormatsSupplier.NumberFormats.getFormatIndex(
  69. Type, Locale())
  70. def convertNumberToString(self, _nkey, _dblValue, _xNumberFormatter=None):
  71. if _xNumberFormatter is None:
  72. return self.xNumberFormatter.convertNumberToString(
  73. _nkey, _dblValue)
  74. else:
  75. return _xNumberFormatter.convertNumberToString(_nkey, _dblValue)
  76. def convertStringToNumber(self, _nkey, _sString):
  77. return self.xNumberFormatter.convertStringToNumber(_nkey, _sString)