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.

BigDecimalTextField.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright 2000-2014 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.tests.components.textfield;
  17. import java.io.Serializable;
  18. import java.math.BigDecimal;
  19. import java.util.Locale;
  20. import com.vaadin.data.fieldgroup.FieldGroup;
  21. import com.vaadin.data.util.BeanItem;
  22. import com.vaadin.server.VaadinRequest;
  23. import com.vaadin.tests.components.AbstractTestUIWithLog;
  24. import com.vaadin.ui.Button;
  25. import com.vaadin.ui.Button.ClickEvent;
  26. import com.vaadin.ui.Button.ClickListener;
  27. import com.vaadin.ui.FormLayout;
  28. import com.vaadin.ui.TextField;
  29. import com.vaadin.ui.VerticalLayout;
  30. /**
  31. * @since 7.2
  32. * @author Vaadin Ltd
  33. */
  34. public class BigDecimalTextField extends AbstractTestUIWithLog {
  35. @Override
  36. protected void setup(VaadinRequest request) {
  37. final VerticalLayout layout = new VerticalLayout();
  38. layout.setMargin(true);
  39. setLocale(new Locale("fi", "FI"));
  40. BeanBigDecimal beanBigDecimal = new BeanBigDecimal();
  41. BeanItem<BeanBigDecimal> beanItem = new BeanItem<BeanBigDecimal>(
  42. beanBigDecimal);
  43. FormLayout formLayout = new FormLayout();
  44. TextField textField = new TextField("BigDecimal field");
  45. textField.setImmediate(true);
  46. textField.setValue("12");
  47. formLayout.addComponent(textField);
  48. final FieldGroup fieldGroup = new FieldGroup(beanItem);
  49. fieldGroup.bind(textField, "decimal");
  50. Button setValue = new Button("Set value to 15,2", new ClickListener() {
  51. @Override
  52. public void buttonClick(ClickEvent event) {
  53. ((TextField) fieldGroup.getField("decimal")).setValue("15,2");
  54. }
  55. });
  56. Button button = new Button("Commit");
  57. button.addClickListener(new Button.ClickListener() {
  58. @Override
  59. public void buttonClick(Button.ClickEvent event) {
  60. try {
  61. fieldGroup.commit();
  62. log("Commit ok. Property value: "
  63. + fieldGroup.getItemDataSource()
  64. .getItemProperty("decimal").getValue());
  65. } catch (FieldGroup.CommitException e) {
  66. log("Commit failed: " + e.getMessage());
  67. }
  68. }
  69. });
  70. layout.addComponent(formLayout);
  71. layout.addComponent(setValue);
  72. layout.addComponent(button);
  73. setContent(layout);
  74. }
  75. /*
  76. * (non-Javadoc)
  77. *
  78. * @see com.vaadin.tests.components.AbstractTestUI#getTestDescription()
  79. */
  80. @Override
  81. protected String getTestDescription() {
  82. return "Tests that BigDecimals work correctly with TextFields";
  83. }
  84. /*
  85. * (non-Javadoc)
  86. *
  87. * @see com.vaadin.tests.components.AbstractTestUI#getTicketNumber()
  88. */
  89. @Override
  90. protected Integer getTicketNumber() {
  91. return 9997;
  92. }
  93. public static class BeanBigDecimal implements Serializable {
  94. BigDecimal decimal;
  95. public BeanBigDecimal() {
  96. }
  97. public BigDecimal getDecimal() {
  98. return decimal;
  99. }
  100. public void setDecimal(BigDecimal decimal) {
  101. this.decimal = decimal;
  102. }
  103. }
  104. }