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.

DateFieldDiscardValue.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package com.vaadin.v7.tests.components.datefield;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5. import com.vaadin.server.VaadinRequest;
  6. import com.vaadin.tests.components.AbstractReindeerTestUI;
  7. import com.vaadin.ui.Button;
  8. import com.vaadin.ui.Button.ClickEvent;
  9. import com.vaadin.ui.Button.ClickListener;
  10. import com.vaadin.ui.HorizontalLayout;
  11. import com.vaadin.ui.VerticalLayout;
  12. import com.vaadin.v7.data.util.ObjectProperty;
  13. import com.vaadin.v7.ui.DateField;
  14. /**
  15. * Test to demonstrate how discarding of field value works with various valid
  16. * and invalid data sources. Previously (Ticket #8069) the case where the
  17. * content of the datasource was null was not handled correctly. This value is a
  18. * valid data source value for the field, but discard did not actually discard
  19. * the value or remove error markers in this cases.
  20. *
  21. * @author Vaadin Ltd
  22. *
  23. */
  24. public class DateFieldDiscardValue extends AbstractReindeerTestUI {
  25. public static final String PROP_NONULL = "A field with a valid date in the data source property";
  26. public static final String PROP_NULL_VALUE = "A field with a null value in the data source property";
  27. public static final String PROP_NULL = "A field with a null datasource property";
  28. @Override
  29. protected void setup(VaadinRequest request) {
  30. String dateFormat = "dd/MM/yy";
  31. final DateField df = new DateField(PROP_NONULL);
  32. df.setDateFormat(dateFormat);
  33. df.setBuffered(true);
  34. Date date = null;
  35. try {
  36. date = new SimpleDateFormat(dateFormat).parse("25/07/16");
  37. } catch (ParseException e1) {
  38. // This cannot happen
  39. }
  40. ObjectProperty<Date> prop = new ObjectProperty<>(date, Date.class);
  41. df.setPropertyDataSource(prop);
  42. Button button = new Button("Discard 1");
  43. button.addClickListener(new ClickListener() {
  44. @Override
  45. public void buttonClick(ClickEvent event) {
  46. df.discard();
  47. }
  48. });
  49. VerticalLayout layout = new VerticalLayout();
  50. HorizontalLayout hLayout = new HorizontalLayout(df, button);
  51. layout.addComponent(hLayout);
  52. final DateField df1 = new DateField(PROP_NULL_VALUE);
  53. df1.setDateFormat(dateFormat);
  54. df1.setBuffered(true);
  55. prop = new ObjectProperty<>(null, Date.class);
  56. df1.setPropertyDataSource(prop);
  57. button = new Button("Discard 2");
  58. button.addClickListener(new ClickListener() {
  59. @Override
  60. public void buttonClick(ClickEvent event) {
  61. df1.discard();
  62. }
  63. });
  64. hLayout = new HorizontalLayout(df1, button);
  65. layout.addComponent(hLayout);
  66. final DateField df2 = new DateField(PROP_NULL);
  67. df2.setDateFormat(dateFormat);
  68. df2.setBuffered(true);
  69. df2.setPropertyDataSource(null);
  70. button = new Button("Discard 3");
  71. button.addClickListener(new ClickListener() {
  72. @Override
  73. public void buttonClick(ClickEvent event) {
  74. df2.discard();
  75. }
  76. });
  77. hLayout = new HorizontalLayout(df2, button);
  78. layout.addComponent(hLayout);
  79. setContent(layout);
  80. }
  81. }