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.

DateFieldUnparsableDate.java 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package com.vaadin.tests.components.datefield;
  2. import java.time.LocalDate;
  3. import com.vaadin.data.Result;
  4. import com.vaadin.tests.components.TestBase;
  5. import com.vaadin.ui.AbstractDateField;
  6. import com.vaadin.v7.data.util.converter.Converter;
  7. public class DateFieldUnparsableDate extends TestBase {
  8. public class MyDateField extends AbstractDateField {
  9. LocalDate oldDate = null;
  10. public MyDateField(String caption) {
  11. super(caption);
  12. addValueChangeListener(event -> oldDate = getValue());
  13. }
  14. @Override
  15. protected Result<LocalDate> handleUnparsableDateString(
  16. String dateString) throws Converter.ConversionException {
  17. return Result.ok(oldDate);
  18. }
  19. }
  20. public class MyDateField2 extends AbstractDateField {
  21. public MyDateField2(String caption) {
  22. super(caption);
  23. }
  24. @Override
  25. protected Result<LocalDate> handleUnparsableDateString(
  26. String dateString) throws Converter.ConversionException {
  27. return Result.ok(null);
  28. }
  29. }
  30. public class MyDateField3 extends AbstractDateField {
  31. public MyDateField3(String caption) {
  32. super(caption);
  33. }
  34. @Override
  35. protected Result<LocalDate> handleUnparsableDateString(
  36. String dateString) throws Converter.ConversionException {
  37. return Result.error("You should not enter invalid dates!");
  38. }
  39. }
  40. public class MyDateField4 extends AbstractDateField {
  41. public MyDateField4(String caption) {
  42. super(caption);
  43. }
  44. @Override
  45. protected Result<LocalDate> handleUnparsableDateString(
  46. String dateString) throws Converter.ConversionException {
  47. if (dateString != null && dateString.equals("today")) {
  48. return Result.ok(LocalDate.now());
  49. }
  50. return Result.error("You should not enter invalid dates!");
  51. }
  52. }
  53. @Override
  54. protected void setup() {
  55. MyDateField df = new MyDateField(
  56. "Returns the old value for invalid dates");
  57. addComponent(df);
  58. MyDateField2 df2 = new MyDateField2("Returns empty for invalid dates");
  59. addComponent(df2);
  60. MyDateField3 df3 = new MyDateField3(
  61. "Throws an exception for invalid dates");
  62. addComponent(df3);
  63. MyDateField4 df4 = new MyDateField4("Can convert 'today'");
  64. addComponent(df4);
  65. }
  66. @Override
  67. protected String getDescription() {
  68. return "DateFields in various configurations (according to caption). All handle unparsable dates differently";
  69. }
  70. @Override
  71. protected Integer getTicketNumber() {
  72. return 4236;
  73. }
  74. }