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.

AbstractDateTimeFieldTest.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package com.vaadin.tests.components.datefield;
  2. import java.text.DateFormat;
  3. import java.text.SimpleDateFormat;
  4. import java.time.LocalDateTime;
  5. import java.util.LinkedHashMap;
  6. import java.util.Locale;
  7. import com.vaadin.shared.ui.datefield.DateTimeResolution;
  8. import com.vaadin.tests.components.abstractfield.AbstractFieldTest;
  9. import com.vaadin.ui.AbstractLocalDateTimeField;
  10. public abstract class AbstractDateTimeFieldTest<T extends AbstractLocalDateTimeField>
  11. extends AbstractFieldTest<T, LocalDateTime> {
  12. private Command<T, LocalDateTime> setValue = new Command<T, LocalDateTime>() {
  13. @Override
  14. public void execute(T c, LocalDateTime 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, LocalDateTime> options = new LinkedHashMap<>();
  31. options.put("(null)", null);
  32. options.put("(current time)", LocalDateTime.now());
  33. options.put("2010-12-12", LocalDateTime.of(2010, 12, 12, 6, 34, 23));
  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. createSelectAction("Date format", category, options, "-",
  59. dateFormatCommand);
  60. }
  61. private String getDatePattern(Locale locale, int dateStyle) {
  62. DateFormat dateFormat = DateFormat.getDateInstance(dateStyle, locale);
  63. if (dateFormat instanceof SimpleDateFormat) {
  64. String pattern = ((SimpleDateFormat) dateFormat).toPattern();
  65. return pattern;
  66. }
  67. return null;
  68. }
  69. private void createResolutionSelectAction(String category) {
  70. LinkedHashMap<String, DateTimeResolution> options = new LinkedHashMap<>();
  71. options.put("Year", DateTimeResolution.YEAR);
  72. options.put("Month", DateTimeResolution.MONTH);
  73. options.put("Day", DateTimeResolution.DAY);
  74. options.put("Hour", DateTimeResolution.HOUR);
  75. options.put("Min", DateTimeResolution.MINUTE);
  76. options.put("Sec", DateTimeResolution.SECOND);
  77. createSelectAction("Resolution", category, options, "Year",
  78. resolutionCommand);
  79. }
  80. private Command<T, DateTimeResolution> resolutionCommand = new Command<T, DateTimeResolution>() {
  81. @Override
  82. public void execute(T c, DateTimeResolution value, Object data) {
  83. c.setResolution(value);
  84. }
  85. };
  86. private Command<T, Boolean> lenientCommand = new Command<T, Boolean>() {
  87. @Override
  88. public void execute(T c, Boolean value, Object data) {
  89. c.setLenient(false);
  90. }
  91. };
  92. private Command<T, Boolean> weekNumberCommand = new Command<T, Boolean>() {
  93. @Override
  94. public void execute(T c, Boolean value, Object data) {
  95. c.setShowISOWeekNumbers(value);
  96. }
  97. };
  98. private Command<T, String> dateFormatCommand = new Command<T, String>() {
  99. @Override
  100. public void execute(T c, String value, Object data) {
  101. c.setDateFormat(value);
  102. }
  103. };
  104. }