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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright 2000-2018 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.client.renderers;
  17. import com.google.gwt.i18n.client.NumberFormat;
  18. import com.vaadin.client.widget.grid.RendererCellReference;
  19. /**
  20. * Renders a number into a cell using a specific {@link NumberFormat}. By
  21. * default uses the default number format returned by
  22. * {@link NumberFormat#getDecimalFormat()}.
  23. *
  24. * @since 7.4
  25. * @author Vaadin Ltd
  26. */
  27. public class NumberRenderer implements Renderer<Number> {
  28. private NumberFormat format;
  29. public NumberRenderer() {
  30. this(NumberFormat.getDecimalFormat());
  31. }
  32. public NumberRenderer(NumberFormat format) {
  33. setFormat(format);
  34. }
  35. /**
  36. * Gets the number format that the number should be formatted in.
  37. *
  38. * @return the number format used to render the number
  39. */
  40. public NumberFormat getFormat() {
  41. return format;
  42. }
  43. /**
  44. * Sets the number format to use for formatting the number.
  45. *
  46. * @param format
  47. * the format to use
  48. * @throws IllegalArgumentException
  49. * when the format is null
  50. */
  51. public void setFormat(NumberFormat format) throws IllegalArgumentException {
  52. if (format == null) {
  53. throw new IllegalArgumentException("Format cannot be null");
  54. }
  55. this.format = format;
  56. }
  57. @Override
  58. public void render(RendererCellReference cell, Number number) {
  59. cell.getElement().setInnerText(format.format(number));
  60. }
  61. }