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 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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.v7.ui.renderers;
  17. import java.text.NumberFormat;
  18. import java.util.Locale;
  19. import com.vaadin.v7.ui.Grid.AbstractRenderer;
  20. import elemental.json.JsonValue;
  21. /**
  22. * A renderer for presenting number values.
  23. *
  24. * @since 7.4
  25. * @author Vaadin Ltd
  26. */
  27. @Deprecated
  28. public class NumberRenderer extends AbstractRenderer<Number> {
  29. private final Locale locale;
  30. private final NumberFormat numberFormat;
  31. private final String formatString;
  32. /**
  33. * Creates a new number renderer.
  34. * <p/>
  35. * The renderer is configured to render with the number's natural string
  36. * representation in the default locale.
  37. */
  38. public NumberRenderer() {
  39. this(Locale.getDefault());
  40. }
  41. /**
  42. * Creates a new number renderer.
  43. * <p/>
  44. * The renderer is configured to render the number as defined with the given
  45. * number format.
  46. *
  47. * @param numberFormat
  48. * the number format with which to display numbers
  49. * @throws IllegalArgumentException
  50. * if {@code numberFormat} is {@code null}
  51. */
  52. public NumberRenderer(NumberFormat numberFormat) {
  53. this(numberFormat, "");
  54. }
  55. /**
  56. * Creates a new number renderer.
  57. * <p/>
  58. * The renderer is configured to render the number as defined with the given
  59. * number format.
  60. *
  61. * @param numberFormat
  62. * the number format with which to display numbers
  63. * @param nullRepresentation
  64. * the textual representation of {@code null} value
  65. * @throws IllegalArgumentException
  66. * if {@code numberFormat} is {@code null}
  67. */
  68. public NumberRenderer(NumberFormat numberFormat, String nullRepresentation)
  69. throws IllegalArgumentException {
  70. super(Number.class, nullRepresentation);
  71. if (numberFormat == null) {
  72. throw new IllegalArgumentException("Number format may not be null");
  73. }
  74. locale = null;
  75. this.numberFormat = numberFormat;
  76. formatString = null;
  77. }
  78. /**
  79. * Creates a new number renderer.
  80. * <p/>
  81. * The renderer is configured to render with the number's natural string
  82. * representation in the given locale.
  83. *
  84. * @param locale
  85. * the locale in which to display numbers
  86. * @throws IllegalArgumentException
  87. * if {@code locale} is {@code null}
  88. */
  89. public NumberRenderer(Locale locale) throws IllegalArgumentException {
  90. this("%s", locale);
  91. }
  92. /**
  93. * Creates a new number renderer.
  94. * <p/>
  95. * The renderer is configured to render with the number's natural string
  96. * representation in the given locale.
  97. *
  98. * @param formatString
  99. * the format string with which to format the number
  100. * @param locale
  101. * the locale in which to display numbers
  102. * @throws IllegalArgumentException
  103. * if {@code locale} is {@code null}
  104. */
  105. public NumberRenderer(String formatString, Locale locale)
  106. throws IllegalArgumentException {
  107. this(formatString, locale, ""); // This will call #toString() during
  108. // formatting
  109. }
  110. /**
  111. * Creates a new number renderer.
  112. * <p/>
  113. * The renderer is configured to render with the given format string in the
  114. * default locale.
  115. *
  116. * @param formatString
  117. * the format string with which to format the number
  118. * @throws IllegalArgumentException
  119. * if {@code formatString} is {@code null}
  120. * @see <a href=
  121. * "http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax">Format
  122. * String Syntax</a>
  123. */
  124. public NumberRenderer(String formatString) throws IllegalArgumentException {
  125. this(formatString, Locale.getDefault(), "");
  126. }
  127. /**
  128. * Creates a new number renderer.
  129. * <p/>
  130. * The renderer is configured to render with the given format string in the
  131. * given locale.
  132. *
  133. * @param formatString
  134. * the format string with which to format the number
  135. * @param locale
  136. * the locale in which to present numbers
  137. * @throws IllegalArgumentException
  138. * if either argument is {@code null}
  139. * @see <a href=
  140. * "http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax">Format
  141. * String Syntax</a>
  142. */
  143. public NumberRenderer(String formatString, Locale locale,
  144. String nullRepresentation) {
  145. super(Number.class, nullRepresentation);
  146. if (formatString == null) {
  147. throw new IllegalArgumentException("Format string may not be null");
  148. }
  149. if (locale == null) {
  150. throw new IllegalArgumentException("Locale may not be null");
  151. }
  152. this.locale = locale;
  153. numberFormat = null;
  154. this.formatString = formatString;
  155. }
  156. @Override
  157. public JsonValue encode(Number value) {
  158. String stringValue;
  159. if (value == null) {
  160. stringValue = getNullRepresentation();
  161. } else if (formatString != null && locale != null) {
  162. stringValue = String.format(locale, formatString, value);
  163. } else if (numberFormat != null) {
  164. stringValue = numberFormat.format(value);
  165. } else {
  166. throw new IllegalStateException(String.format("Internal bug: "
  167. + "%s is in an illegal state: "
  168. + "[locale: %s, numberFormat: %s, formatString: %s]",
  169. getClass().getSimpleName(), locale, numberFormat,
  170. formatString));
  171. }
  172. return encode(stringValue, String.class);
  173. }
  174. @Override
  175. public String toString() {
  176. final String fieldInfo;
  177. if (numberFormat != null) {
  178. fieldInfo = "numberFormat: " + numberFormat;
  179. } else {
  180. fieldInfo = "locale: " + locale + ", formatString: " + formatString;
  181. }
  182. return String.format("%s [%s]", getClass().getSimpleName(), fieldInfo);
  183. }
  184. @Override
  185. public String getNullRepresentation() {
  186. return super.getNullRepresentation();
  187. }
  188. }