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.

AbstractDateFieldTest.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package com.vaadin.tests.components.datefield;
  2. import java.text.DateFormat;
  3. import java.text.SimpleDateFormat;
  4. import java.time.LocalDate;
  5. import java.util.LinkedHashMap;
  6. import java.util.Locale;
  7. import com.vaadin.shared.ui.datefield.DateResolution;
  8. import com.vaadin.tests.components.abstractfield.AbstractFieldTest;
  9. import com.vaadin.ui.AbstractLocalDateField;
  10. public abstract class AbstractDateFieldTest<T extends AbstractLocalDateField>
  11. extends AbstractFieldTest<T, LocalDate> {
  12. private Command<T, LocalDate> setValue = new Command<T, LocalDate>() {
  13. @Override
  14. public void execute(T c, LocalDate value, Object data) {
  15. c.setValue(value);
  16. }
  17. };
  18. @Override
  19. protected void createActions() {
  20. super.createActions();
  21. createResolutionSelectAction(CATEGORY_FEATURES);
  22. createBooleanAction("Lenient", CATEGORY_FEATURES, false,
  23. lenientCommand);
  24. createBooleanAction("Show week numbers", CATEGORY_FEATURES, false,
  25. weekNumberCommand);
  26. createDateFormatSelectAction(CATEGORY_FEATURES);
  27. createSetValueAction(CATEGORY_FEATURES);
  28. }
  29. private void createSetValueAction(String category) {
  30. LinkedHashMap<String, LocalDate> options = new LinkedHashMap<>();
  31. options.put("(null)", null);
  32. options.put("(current time)", LocalDate.now());
  33. options.put("2010-12-12", LocalDate.of(2010, 12, 12));
  34. createMultiClickAction("Set value", category, options, setValue, null);
  35. }
  36. private void createDateFormatSelectAction(String category) {
  37. LinkedHashMap<String, String> options = new LinkedHashMap<>();
  38. options.put("-", null);
  39. options.put("d M yyyy", "d M yyyy");
  40. options.put("d MM yyyy", "d MM yyyy");
  41. options.put("d MMM yyyy", "d MMM yyyy");
  42. options.put("d MMMM yyyy", "d MMMM yyyy");
  43. options.put("dd M yyyy", "dd M yyyy");
  44. options.put("ddd M yyyy", "ddd M yyyy");
  45. options.put("d M y", "d M y");
  46. options.put("d M yy", "d M yy");
  47. options.put("d M yyy", "d M yyy");
  48. options.put("d M yyyy", "d M yyyy");
  49. options.put("d M 'custom text' yyyy", "d M 'custom text' yyyy");
  50. options.put("'day:'d', month:'M', year: 'yyyy",
  51. "'day:'d', month:'M', year: 'yyyy");
  52. options.put(getDatePattern(new Locale("fi", "FI"), DateFormat.LONG),
  53. getDatePattern(new Locale("fi", "FI"), DateFormat.LONG));
  54. options.put(getDatePattern(new Locale("fi", "FI"), DateFormat.MEDIUM),
  55. getDatePattern(new Locale("fi", "FI"), DateFormat.MEDIUM));
  56. options.put(getDatePattern(new Locale("fi", "FI"), DateFormat.SHORT),
  57. getDatePattern(new Locale("fi", "FI"), DateFormat.SHORT));
  58. options.put("ww yyyy", "ww yyyy");
  59. createSelectAction("Date format", category, options, "-",
  60. dateFormatCommand);
  61. }
  62. private String getDatePattern(Locale locale, int dateStyle) {
  63. DateFormat dateFormat = DateFormat.getDateInstance(dateStyle, locale);
  64. if (dateFormat instanceof SimpleDateFormat) {
  65. String pattern = ((SimpleDateFormat) dateFormat).toPattern();
  66. return pattern;
  67. }
  68. return null;
  69. }
  70. private void createResolutionSelectAction(String category) {
  71. LinkedHashMap<String, DateResolution> options = new LinkedHashMap<>();
  72. options.put("Year", DateResolution.YEAR);
  73. options.put("Month", DateResolution.MONTH);
  74. options.put("Day", DateResolution.DAY);
  75. createSelectAction("Resolution", category, options, "Year",
  76. resolutionCommand);
  77. }
  78. private Command<T, DateResolution> resolutionCommand = new Command<T, DateResolution>() {
  79. @Override
  80. public void execute(T c, DateResolution value, Object data) {
  81. c.setResolution(value);
  82. }
  83. };
  84. private Command<T, Boolean> lenientCommand = new Command<T, Boolean>() {
  85. @Override
  86. public void execute(T c, Boolean value, Object data) {
  87. c.setLenient(false);
  88. }
  89. };
  90. private Command<T, Boolean> weekNumberCommand = new Command<T, Boolean>() {
  91. @Override
  92. public void execute(T c, Boolean value, Object data) {
  93. c.setShowISOWeekNumbers(value);
  94. }
  95. };
  96. private Command<T, String> dateFormatCommand = new Command<T, String>() {
  97. @Override
  98. public void execute(T c, String value, Object data) {
  99. c.setDateFormat(value);
  100. }
  101. };
  102. }