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.

DateTextHandling.java 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package com.vaadin.tests.components.datefield;
  2. import java.time.LocalDate;
  3. import java.time.format.DateTimeFormatter;
  4. import java.time.format.FormatStyle;
  5. import java.util.Locale;
  6. import com.vaadin.annotations.Widgetset;
  7. import com.vaadin.data.Binder;
  8. import com.vaadin.data.Result;
  9. import com.vaadin.server.VaadinRequest;
  10. import com.vaadin.tests.components.AbstractTestUI;
  11. import com.vaadin.ui.Button;
  12. import com.vaadin.ui.DateField;
  13. import com.vaadin.ui.Label;
  14. import com.vaadin.ui.Notification;
  15. import com.vaadin.ui.VerticalLayout;
  16. @Widgetset("com.vaadin.DefaultWidgetSet")
  17. public class DateTextHandling extends AbstractTestUI {
  18. public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter
  19. .ofLocalizedDate(FormatStyle.MEDIUM).withLocale(Locale.UK);
  20. @Override
  21. protected void setup(VaadinRequest request) {
  22. final VerticalLayout layout = new VerticalLayout();
  23. DateField dateField = new DateField("Date") {
  24. @Override
  25. protected Result<LocalDate> handleUnparsableDateString(
  26. String dateString) {
  27. if (dateString.equalsIgnoreCase("Y2K")) {
  28. return Result.ok(LocalDate.of(2000, 1, 1));
  29. } else {
  30. return super.handleUnparsableDateString(dateString);
  31. }
  32. }
  33. ;
  34. };
  35. dateField.setParseErrorMessage("Parse error");
  36. dateField.setDateOutOfRangeMessage("Out of range");
  37. layout.addComponent(dateField);
  38. Label errorLabel = new Label();
  39. errorLabel.setId("errorLabel");
  40. layout.addComponent(errorLabel);
  41. Binder<Void> binder = new Binder<>();
  42. binder.forField(dateField).withStatusLabel(errorLabel)
  43. .bind(o -> dateField.getValue(), (aVoid, date) -> {
  44. });
  45. Button buttonValidate = new Button("Validate!");
  46. buttonValidate.addClickListener(event1 -> {
  47. binder.validate();
  48. if (dateField.getValue() == null) {
  49. Notification.show("NULL");
  50. } else {
  51. Notification
  52. .show(DATE_TIME_FORMATTER.format(dateField.getValue()));
  53. }
  54. });
  55. layout.addComponent(buttonValidate);
  56. Button setValueButton = new Button("Set 2011-12-13",
  57. event -> dateField.setValue(LocalDate.of(2011, 12, 13)));
  58. layout.addComponent(setValueButton);
  59. addComponent(layout);
  60. }
  61. }