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.

DateFieldRangeValidation.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package com.vaadin.tests.components.datefield;
  2. import java.util.Date;
  3. import java.util.Locale;
  4. import com.vaadin.data.HasValue;
  5. import com.vaadin.tests.components.TestBase;
  6. import com.vaadin.tests.util.CheckBoxWithPropertyDataSource;
  7. import com.vaadin.ui.CheckBox;
  8. import com.vaadin.v7.data.util.BeanItem;
  9. import com.vaadin.v7.data.validator.RangeValidator;
  10. import com.vaadin.v7.ui.PopupDateField;
  11. public class DateFieldRangeValidation extends TestBase {
  12. public class Range {
  13. private Date from, to;
  14. private boolean fromInclusive = true;
  15. private boolean toInclusive = true;
  16. public boolean isFromInclusive() {
  17. return fromInclusive;
  18. }
  19. public void setFromInclusive(boolean fromInclusive) {
  20. this.fromInclusive = fromInclusive;
  21. }
  22. public boolean isToInclusive() {
  23. return toInclusive;
  24. }
  25. public void setToInclusive(boolean toInclusive) {
  26. this.toInclusive = toInclusive;
  27. }
  28. public Date getFrom() {
  29. return from;
  30. }
  31. public void setFrom(Date from) {
  32. this.from = from;
  33. }
  34. public Date getTo() {
  35. return to;
  36. }
  37. public void setTo(Date to) {
  38. this.to = to;
  39. }
  40. }
  41. private Range range = new Range();
  42. private PopupDateField actualDateField;
  43. private HasValue.ValueChangeListener<Boolean> refreshField = event -> actualDateField
  44. .markAsDirty();
  45. @Override
  46. protected void setup() {
  47. BeanItem<Range> bi = new BeanItem<>(range);
  48. range.setFrom(new Date(2011 - 1900, 12 - 1, 4));
  49. range.setTo(new Date(2011 - 1900, 12 - 1, 15));
  50. PopupDateField fromField = createDateField();
  51. fromField.setPropertyDataSource(bi.getItemProperty("from"));
  52. CheckBox fromInclusive = new CheckBoxWithPropertyDataSource(
  53. "From inclusive", bi.getItemProperty("fromInclusive"));
  54. CheckBox toInclusive = new CheckBoxWithPropertyDataSource(
  55. "To inclusive", bi.getItemProperty("toInclusive"));
  56. fromInclusive.addValueChangeListener(refreshField);
  57. toInclusive.addValueChangeListener(refreshField);
  58. PopupDateField toField = createDateField();
  59. toField.setPropertyDataSource(bi.getItemProperty("to"));
  60. actualDateField = createDateField();
  61. actualDateField.setValue(new Date(2011 - 1900, 12 - 1, 1));
  62. actualDateField.addValidator(
  63. new RangeValidator<Date>("", Date.class, null, null) {
  64. @Override
  65. public boolean isMinValueIncluded() {
  66. return range.isFromInclusive();
  67. }
  68. @Override
  69. public boolean isMaxValueIncluded() {
  70. return range.isToInclusive();
  71. }
  72. @Override
  73. public Date getMaxValue() {
  74. return range.getTo();
  75. }
  76. @Override
  77. public Date getMinValue() {
  78. return range.getFrom();
  79. }
  80. @Override
  81. public String getErrorMessage() {
  82. return "Date must be in range " + getMinValue() + " - "
  83. + getMaxValue();
  84. }
  85. });
  86. addComponent(fromField);
  87. addComponent(fromInclusive);
  88. addComponent(toField);
  89. addComponent(toInclusive);
  90. addComponent(actualDateField);
  91. }
  92. private PopupDateField createDateField() {
  93. PopupDateField df = new PopupDateField();
  94. df.setLocale(new Locale("en", "US"));
  95. df.setBuffered(false);
  96. df.setImmediate(true);
  97. return df;
  98. }
  99. @Override
  100. protected String getDescription() {
  101. return "Tests the DateField range validator. The first field sets the minimum date, the second the maximum. Checkboxes control if the selected date is ok or not.";
  102. }
  103. @Override
  104. protected Integer getTicketNumber() {
  105. // TODO Auto-generated method stub
  106. return null;
  107. }
  108. }