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.

TextFieldWithPropertyFormatter.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package com.vaadin.tests.components.textfield;
  2. import java.math.BigDecimal;
  3. import java.text.DecimalFormat;
  4. import java.text.DecimalFormatSymbols;
  5. import java.util.Locale;
  6. import com.vaadin.tests.components.TestBase;
  7. import com.vaadin.ui.Button;
  8. import com.vaadin.v7.data.Property;
  9. import com.vaadin.v7.data.util.PropertyFormatter;
  10. import com.vaadin.v7.ui.TextField;
  11. public class TextFieldWithPropertyFormatter extends TestBase {
  12. private PropertyFormatter<BigDecimal> formatter;
  13. private Property<BigDecimal> property;
  14. @Override
  15. protected void setup() {
  16. /*
  17. * Formatter that: - formats in UK currency style - scales to 2 fraction
  18. * digits - rounds half up
  19. */
  20. // Property containing BigDecimal
  21. property = new Property<BigDecimal>() {
  22. private BigDecimal value;
  23. @Override
  24. public BigDecimal getValue() {
  25. return value;
  26. }
  27. @Override
  28. public void setValue(BigDecimal newValue) throws ReadOnlyException {
  29. value = newValue;
  30. }
  31. @Override
  32. public Class<BigDecimal> getType() {
  33. return BigDecimal.class;
  34. }
  35. @Override
  36. public boolean isReadOnly() {
  37. return false;
  38. }
  39. @Override
  40. public void setReadOnly(boolean newStatus) {
  41. // ignore
  42. }
  43. };
  44. formatter = new PropertyFormatter<BigDecimal>(property) {
  45. private final DecimalFormat df = new DecimalFormat("#,##0.00",
  46. new DecimalFormatSymbols(new Locale("en", "UK")));
  47. {
  48. df.setParseBigDecimal(true);
  49. // df.setRoundingMode(RoundingMode.HALF_UP);
  50. }
  51. @Override
  52. public String format(BigDecimal value) {
  53. final String retVal;
  54. if (value == null) {
  55. retVal = "";
  56. } else {
  57. retVal = df.format(value);
  58. }
  59. return retVal;
  60. }
  61. @Override
  62. public BigDecimal parse(String formattedValue) throws Exception {
  63. if (formattedValue != null
  64. && !formattedValue.trim().isEmpty()) {
  65. BigDecimal value = (BigDecimal) df.parse(formattedValue);
  66. value = value.setScale(2, BigDecimal.ROUND_HALF_UP);
  67. return value;
  68. }
  69. return null;
  70. }
  71. };
  72. final TextField tf1 = new TextField();
  73. tf1.setPropertyDataSource(formatter);
  74. addComponent(tf1);
  75. Button b = new Button(
  76. "Sync (typing 12345.6789 and clicking this should format field)");
  77. b.addClickListener(event -> {
  78. });
  79. addComponent(b);
  80. b = new Button("Set '12345.6789' to textfield on the server side");
  81. b.addClickListener(event -> tf1.setValue("12345.6789"));
  82. addComponent(b);
  83. }
  84. @Override
  85. protected String getDescription() {
  86. return "Should work";
  87. }
  88. @Override
  89. protected Integer getTicketNumber() {
  90. return 4394;
  91. }
  92. }