/ch.elexis.noatext/src/ag/ion/bion/officelayer/internal/util/JavaNumberFormatter.java

https://bitbucket.org/marlovitsh/elexis-base-marlovitsh-new · Java · 209 lines · 58 code · 9 blank · 142 comment · 6 complexity · 53311eab301ad9e83f03afc69d3175bf MD5 · raw file

  1. /****************************************************************************
  2. * ubion.ORS - The Open Report Suite *
  3. * *
  4. * ------------------------------------------------------------------------ *
  5. * *
  6. * Subproject: NOA (Nice Office Access) *
  7. * *
  8. * *
  9. * The Contents of this file are made available subject to *
  10. * the terms of GNU Lesser General Public License Version 2.1. *
  11. * *
  12. * GNU Lesser General Public License Version 2.1 *
  13. * ======================================================================== *
  14. * Copyright 2003-2005 by IOn AG *
  15. * *
  16. * This library is free software; you can redistribute it and/or *
  17. * modify it under the terms of the GNU Lesser General Public *
  18. * License version 2.1, as published by the Free Software Foundation. *
  19. * *
  20. * This library is distributed in the hope that it will be useful, *
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
  23. * Lesser General Public License for more details. *
  24. * *
  25. * You should have received a copy of the GNU Lesser General Public *
  26. * License along with this library; if not, write to the Free Software *
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
  28. * MA 02111-1307 USA *
  29. * *
  30. * Contact us: *
  31. * http://www.ion.ag *
  32. * info@ion.ag *
  33. * *
  34. ****************************************************************************/
  35. /*
  36. * Last changes made by $Author: andreas $, $Date: 2006-10-04 14:14:28 +0200 (Mi, 04 Okt 2006) $
  37. */
  38. package ag.ion.bion.officelayer.internal.util;
  39. import java.awt.Color;
  40. import java.text.DecimalFormat;
  41. /**
  42. * Number formatter for java code.
  43. *
  44. * @author Andreas Bröcker
  45. * @version $Revision: 10398 $
  46. */
  47. public class JavaNumberFormatter {
  48. private Color textColor = null;
  49. private DecimalFormat decimalFormat = null;
  50. private JavaNumberFormatCondition javaNumberFormatCondition = null;
  51. private String contentBefore = null;
  52. private String contentAfter = null;
  53. private boolean isNegativePattern = false;
  54. //----------------------------------------------------------------------------
  55. /**
  56. * Sets color of the text.
  57. *
  58. * @param textColor color of the text
  59. *
  60. * @author Andreas Bröcker
  61. */
  62. public void setTextColor(Color textColor) {
  63. this.textColor = textColor;
  64. }
  65. //----------------------------------------------------------------------------
  66. /**
  67. * Sets decimal format.
  68. *
  69. * @param decimalFormat decimal format to be used
  70. *
  71. * @author Andreas Bröcker
  72. */
  73. public void setDecimalFormat(DecimalFormat decimalFormat) {
  74. this.decimalFormat = decimalFormat;
  75. }
  76. //----------------------------------------------------------------------------
  77. /**
  78. * Sets number format condition.
  79. *
  80. * @param javaNumberFormatCondition number format condition to be used
  81. *
  82. * @author Andreas Bröcker
  83. */
  84. public void setJavaNumberFormatCondition(JavaNumberFormatCondition javaNumberFormatCondition) {
  85. this.javaNumberFormatCondition = javaNumberFormatCondition;
  86. }
  87. //----------------------------------------------------------------------------
  88. /**
  89. * Sets content before pattern.
  90. *
  91. * @param contentBefore content before pattern
  92. *
  93. * @author Andreas Bröcker
  94. */
  95. public void setContentBefore(String contentBefore) {
  96. this.contentBefore = contentBefore;
  97. }
  98. //----------------------------------------------------------------------------
  99. /**
  100. * Sets conent after pattern.
  101. *
  102. * @param contentAfter content after pattern
  103. *
  104. * @author Andreas Bröcker
  105. */
  106. public void setContentAfter(String contentAfter) {
  107. this.contentAfter = contentAfter;
  108. }
  109. //----------------------------------------------------------------------------
  110. /**
  111. * Sets information whether this pattern is a negative pattern.
  112. *
  113. * @param isNegativePattern information whether this pattern is a negative pattern
  114. *
  115. * @author Andreas Bröcker
  116. */
  117. public void setIsNegativePattern(boolean isNegativePattern) {
  118. this.isNegativePattern = isNegativePattern;
  119. }
  120. //----------------------------------------------------------------------------
  121. /**
  122. * Returns text color. Returns null if no color was set.
  123. *
  124. * @return text color
  125. *
  126. * @author Andreas Bröcker
  127. */
  128. public Color getTextColor() {
  129. return textColor;
  130. }
  131. //----------------------------------------------------------------------------
  132. /**
  133. * Returns decimal format.
  134. *
  135. * @return decimal format
  136. *
  137. * @author Andreas Bröcker
  138. */
  139. public DecimalFormat getDecimalFormat() {
  140. return decimalFormat;
  141. }
  142. //----------------------------------------------------------------------------
  143. /**
  144. * Returns number format condition.
  145. *
  146. * @return number format condition
  147. *
  148. * @author Andreas Bröcker
  149. */
  150. public JavaNumberFormatCondition getJavaNumberFormatCondition() {
  151. return javaNumberFormatCondition;
  152. }
  153. //----------------------------------------------------------------------------
  154. /**
  155. * Returns content before pattern.
  156. *
  157. * @return content before pattern
  158. *
  159. * @author Andreas Bröcker
  160. */
  161. public String getContentBefore() {
  162. return contentBefore;
  163. }
  164. //----------------------------------------------------------------------------
  165. /**
  166. * Returns content after pattern.
  167. *
  168. * @return content after pattern
  169. *
  170. * @author Andreas Bröcker
  171. */
  172. public String getContentAfter() {
  173. return contentAfter;
  174. }
  175. //----------------------------------------------------------------------------
  176. /**
  177. * Formats submitted number.
  178. *
  179. * @param value value to be formatted
  180. *
  181. * @return formatted number
  182. *
  183. * @author Andreas Bröcker
  184. */
  185. public String format(double value) {
  186. String textValue = decimalFormat.format(value);
  187. if(isNegativePattern) {
  188. if(textValue.startsWith("-"))
  189. textValue = textValue.substring(1);
  190. }
  191. String returnValue = "";
  192. if(contentBefore != null)
  193. returnValue = contentBefore;
  194. returnValue+= textValue;
  195. if(contentAfter != null)
  196. returnValue+= contentAfter;
  197. return returnValue;
  198. }
  199. //----------------------------------------------------------------------------
  200. }