You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NumberRenderer.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.ui.renderers;
  17. import java.text.NumberFormat;
  18. import java.util.Locale;
  19. import com.vaadin.shared.ui.grid.renderers.NumberRendererState;
  20. import elemental.json.JsonValue;
  21. /**
  22. * A renderer for presenting number values.
  23. *
  24. * @since 7.4
  25. * @author Vaadin Ltd
  26. */
  27. public class NumberRenderer extends AbstractRenderer<Object, Number> {
  28. private final Locale locale;
  29. private final NumberFormat numberFormat;
  30. private final String formatString;
  31. /**
  32. * Creates a new number renderer.
  33. * <p/>
  34. * The renderer is configured to render with the number's natural string
  35. * representation in the default locale.
  36. */
  37. public NumberRenderer() {
  38. this(Locale.getDefault());
  39. }
  40. /**
  41. * Creates a new number renderer.
  42. * <p/>
  43. * The renderer is configured to render the number as defined with the given
  44. * number format.
  45. *
  46. * @param numberFormat
  47. * the number format with which to display numbers
  48. * @throws IllegalArgumentException
  49. * if {@code numberFormat} is {@code null}
  50. */
  51. public NumberRenderer(NumberFormat numberFormat) {
  52. this(numberFormat, "");
  53. }
  54. /**
  55. * Creates a new number renderer.
  56. * <p/>
  57. * The renderer is configured to render the number as defined with the given
  58. * number format.
  59. *
  60. * @param numberFormat
  61. * the number format with which to display numbers
  62. * @param nullRepresentation
  63. * the textual representation of {@code null} value
  64. * @throws IllegalArgumentException
  65. * if {@code numberFormat} is {@code null}
  66. */
  67. public NumberRenderer(NumberFormat numberFormat, String nullRepresentation)
  68. throws IllegalArgumentException {
  69. super(Number.class, nullRepresentation);
  70. if (numberFormat == null) {
  71. throw new IllegalArgumentException("Number format may not be null");
  72. }
  73. locale = null;
  74. this.numberFormat = numberFormat;
  75. formatString = null;
  76. }
  77. /**
  78. * Creates a new number renderer.
  79. * <p/>
  80. * The renderer is configured to render with the number's natural string
  81. * representation in the given locale.
  82. *
  83. * @param locale
  84. * the locale in which to display numbers
  85. * @throws IllegalArgumentException
  86. * if {@code locale} is {@code null}
  87. */
  88. public NumberRenderer(Locale locale) throws IllegalArgumentException {
  89. this("%s", locale);
  90. }
  91. /**
  92. * Creates a new number renderer.
  93. * <p/>
  94. * The renderer is configured to render with the number's natural string
  95. * representation in the given locale.
  96. *
  97. * @param formatString
  98. * the format string with which to format the number
  99. * @param locale
  100. * the locale in which to display numbers
  101. * @throws IllegalArgumentException
  102. * if {@code locale} is {@code null}
  103. */
  104. public NumberRenderer(String formatString, Locale locale)
  105. throws IllegalArgumentException {
  106. this(formatString, locale, ""); // This will call #toString() during
  107. // formatting
  108. }
  109. /**
  110. * Creates a new number renderer.
  111. * <p/>
  112. * The renderer is configured to render with the given format string in the
  113. * default locale.
  114. *
  115. * @param formatString
  116. * the format string with which to format the number
  117. * @throws IllegalArgumentException
  118. * if {@code formatString} is {@code null}
  119. * @see <a href=
  120. * "http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax">
  121. * Format String Syntax</a>
  122. */
  123. public NumberRenderer(String formatString) throws IllegalArgumentException {
  124. this(formatString, Locale.getDefault(), "");
  125. }
  126. /**
  127. * Creates a new number renderer.
  128. * <p/>
  129. * The renderer is configured to render with the given format string in the
  130. * given locale.
  131. *
  132. * @param formatString
  133. * the format string with which to format the number
  134. * @param locale
  135. * the locale in which to present numbers
  136. * @throws IllegalArgumentException
  137. * if either argument is {@code null}
  138. * @see <a href=
  139. * "http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax">
  140. * Format String Syntax</a>
  141. */
  142. public NumberRenderer(String formatString, Locale locale,
  143. String nullRepresentation) {
  144. super(Number.class, nullRepresentation);
  145. if (formatString == null) {
  146. throw new IllegalArgumentException("Format string may not be null");
  147. }
  148. if (locale == null) {
  149. throw new IllegalArgumentException("Locale may not be null");
  150. }
  151. this.locale = locale;
  152. numberFormat = null;
  153. this.formatString = formatString;
  154. }
  155. @Override
  156. public JsonValue encode(Number value) {
  157. String stringValue;
  158. if (value == null) {
  159. stringValue = getNullRepresentation();
  160. } else if (formatString != null && locale != null) {
  161. stringValue = String.format(locale, formatString, value);
  162. } else if (numberFormat != null) {
  163. stringValue = numberFormat.format(value);
  164. } else {
  165. throw new IllegalStateException(String.format("Internal bug: "
  166. + "%s is in an illegal state: "
  167. + "[locale: %s, numberFormat: %s, formatString: %s]",
  168. getClass().getSimpleName(), locale, numberFormat,
  169. formatString));
  170. }
  171. return encode(stringValue, String.class);
  172. }
  173. @Override
  174. public String toString() {
  175. final String fieldInfo;
  176. if (numberFormat != null) {
  177. fieldInfo = "numberFormat: " + numberFormat;
  178. } else {
  179. fieldInfo = "locale: " + locale + ", formatString: " + formatString;
  180. }
  181. return String.format("%s [%s]", getClass().getSimpleName(), fieldInfo);
  182. }
  183. @Override
  184. public String getNullRepresentation() {
  185. return super.getNullRepresentation();
  186. }
  187. @Override
  188. protected NumberRendererState getState() {
  189. return (NumberRendererState) super.getState();
  190. }
  191. @Override
  192. protected NumberRendererState getState(boolean markAsDirty) {
  193. return (NumberRendererState) super.getState(markAsDirty);
  194. }
  195. }