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.

AbstractFieldTest.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package com.vaadin.tests.components.abstractfield;
  2. import java.time.LocalDate;
  3. import java.time.format.DateTimeFormatter;
  4. import java.util.ArrayList;
  5. import java.util.Collection;
  6. import java.util.Collections;
  7. import java.util.List;
  8. import java.util.Locale;
  9. import com.vaadin.data.HasValue.ValueChangeEvent;
  10. import com.vaadin.data.HasValue.ValueChangeListener;
  11. import com.vaadin.shared.Registration;
  12. import com.vaadin.tests.components.AbstractComponentTest;
  13. import com.vaadin.ui.AbstractField;
  14. import com.vaadin.ui.MenuBar;
  15. import com.vaadin.ui.MenuBar.MenuItem;
  16. public abstract class AbstractFieldTest<T extends AbstractField<V>, V>
  17. extends AbstractComponentTest<T> {
  18. private boolean sortValueChanges = true;
  19. protected Registration valueChangeListenerRegistration;
  20. @Override
  21. protected void createActions() {
  22. super.createActions();
  23. createBooleanAction("Required", CATEGORY_STATE, false, requiredCommand);
  24. createValueChangeListener(CATEGORY_LISTENERS);
  25. // * invalidcommitted
  26. // * commit()
  27. // * discard()
  28. // * writethrough
  29. // * readthrough
  30. // * addvalidator
  31. // * isvalid
  32. // * invalidallowed
  33. // * error indicator
  34. //
  35. // * validation visible
  36. // * ShortcutListener
  37. }
  38. @Override
  39. protected void populateSettingsMenu(MenuItem settingsMenu) {
  40. super.populateSettingsMenu(settingsMenu);
  41. if (AbstractField.class.isAssignableFrom(getTestClass())) {
  42. MenuItem abstractField = settingsMenu.addItem("AbstractField",
  43. null);
  44. abstractField.addItem("Show value", new MenuBar.Command() {
  45. @Override
  46. public void menuSelected(MenuItem selectedItem) {
  47. for (T a : getTestComponents()) {
  48. log(a.getClass().getSimpleName() + " value: "
  49. + formatValue(a.getValue()));
  50. }
  51. }
  52. });
  53. MenuItem sortValueChangesItem = abstractField.addItem(
  54. "Show sorted value changes", new MenuBar.Command() {
  55. @Override
  56. public void menuSelected(MenuItem selectedItem) {
  57. sortValueChanges = selectedItem.isChecked();
  58. log("Show sorted value changes: "
  59. + sortValueChanges);
  60. }
  61. });
  62. sortValueChangesItem.setCheckable(true);
  63. sortValueChangesItem.setChecked(true);
  64. }
  65. }
  66. private void createValueChangeListener(String category) {
  67. createBooleanAction("Value change listener", category, false,
  68. valueChangeListenerCommand);
  69. }
  70. protected Command<T, Boolean> valueChangeListenerCommand = new Command<T, Boolean>() {
  71. private ValueChangeListener<V> valueChangeListener = new ValueChangeListener<V>() {
  72. @Override
  73. public void valueChange(ValueChangeEvent<V> event) {
  74. log(event.getClass().getSimpleName() + ", new value: "
  75. + formatValue(event.getValue()));
  76. }
  77. };
  78. @Override
  79. public void execute(T c, Boolean value, Object data) {
  80. if (value) {
  81. if (valueChangeListenerRegistration == null) {
  82. valueChangeListenerRegistration = c
  83. .addValueChangeListener(valueChangeListener);
  84. }
  85. } else {
  86. if (valueChangeListenerRegistration != null) {
  87. valueChangeListenerRegistration.remove();
  88. valueChangeListenerRegistration = null;
  89. }
  90. }
  91. }
  92. };
  93. protected Command<T, V> setValueCommand = new Command<T, V>() {
  94. @Override
  95. public void execute(T c, V value, Object data) {
  96. c.setValue(value);
  97. }
  98. };
  99. @SuppressWarnings({ "rawtypes", "unchecked" })
  100. private String formatValue(Object o) {
  101. if (o instanceof Collection && sortValueChanges) {
  102. // Sort collections to avoid problems with values printed in
  103. // different order
  104. try {
  105. List<Comparable> c = new ArrayList<Comparable>((Collection) o);
  106. Collections.sort(c);
  107. o = c;
  108. } catch (Exception e) {
  109. // continue with unsorted if sorting fails for some reason
  110. log("Exception while sorting value: " + e.getMessage());
  111. }
  112. }
  113. // Distinguish between null and 'null'
  114. String value = "null";
  115. if (o != null) {
  116. if (o instanceof LocalDate) {
  117. LocalDate date = (LocalDate) o;
  118. // Dec 31, 2068
  119. String pattern = "MMM d, yyyy";
  120. DateTimeFormatter format = DateTimeFormatter.ofPattern(pattern,
  121. Locale.ENGLISH);
  122. value = format.format(date);
  123. } else {
  124. value = "'" + o + "'";
  125. }
  126. }
  127. return value;
  128. }
  129. }