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.

LegacyAbstractFieldTest.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package com.vaadin.tests.components.abstractfield;
  2. import java.text.SimpleDateFormat;
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.Collections;
  6. import java.util.Date;
  7. import java.util.LinkedHashMap;
  8. import java.util.List;
  9. import java.util.Locale;
  10. import com.vaadin.tests.components.AbstractComponentTest;
  11. import com.vaadin.ui.MenuBar;
  12. import com.vaadin.ui.MenuBar.MenuItem;
  13. import com.vaadin.v7.data.Property;
  14. import com.vaadin.v7.data.Property.ReadOnlyStatusChangeEvent;
  15. import com.vaadin.v7.data.Property.ReadOnlyStatusChangeListener;
  16. import com.vaadin.v7.data.Property.ValueChangeListener;
  17. import com.vaadin.v7.ui.AbstractField;
  18. public abstract class LegacyAbstractFieldTest<T extends AbstractField>
  19. extends AbstractComponentTest<T>
  20. implements ValueChangeListener, ReadOnlyStatusChangeListener {
  21. private boolean sortValueChanges = true;
  22. @Override
  23. protected void createActions() {
  24. super.createActions();
  25. createBooleanAction("Required", CATEGORY_STATE, false, requiredCommand);
  26. createRequiredErrorSelect(CATEGORY_DECORATIONS);
  27. createValueChangeListener(CATEGORY_LISTENERS);
  28. createReadOnlyStatusChangeListener(CATEGORY_LISTENERS);
  29. // * invalidcommitted
  30. // * commit()
  31. // * discard()
  32. // * writethrough
  33. // * readthrough
  34. // * addvalidator
  35. // * isvalid
  36. // * invalidallowed
  37. // * error indicator
  38. //
  39. // * validation visible
  40. // * ShortcutListener
  41. }
  42. @Override
  43. protected void populateSettingsMenu(MenuItem settingsMenu) {
  44. super.populateSettingsMenu(settingsMenu);
  45. if (AbstractField.class.isAssignableFrom(getTestClass())) {
  46. MenuItem abstractField = settingsMenu.addItem("LegacyAbstractField",
  47. null);
  48. abstractField.addItem("Show value", new MenuBar.Command() {
  49. @Override
  50. public void menuSelected(MenuItem selectedItem) {
  51. for (T a : getTestComponents()) {
  52. log(a.getClass().getSimpleName() + " value: "
  53. + getValue(a));
  54. }
  55. }
  56. });
  57. MenuItem sortValueChangesItem = abstractField.addItem(
  58. "Show sorted value changes", new MenuBar.Command() {
  59. @Override
  60. public void menuSelected(MenuItem selectedItem) {
  61. sortValueChanges = selectedItem.isChecked();
  62. log("Show sorted value changes: "
  63. + sortValueChanges);
  64. }
  65. });
  66. sortValueChangesItem.setCheckable(true);
  67. sortValueChangesItem.setChecked(true);
  68. }
  69. }
  70. private void createRequiredErrorSelect(String category) {
  71. LinkedHashMap<String, String> options = new LinkedHashMap<>();
  72. options.put("-", null);
  73. options.put(TEXT_SHORT, TEXT_SHORT);
  74. options.put("Medium", TEXT_MEDIUM);
  75. options.put("Long", TEXT_LONG);
  76. options.put("Very long", TEXT_VERY_LONG);
  77. createSelectAction("Required error message", category, options, "-",
  78. requiredErrorMessageCommand);
  79. }
  80. private void createValueChangeListener(String category) {
  81. createBooleanAction("Value change listener", category, false,
  82. valueChangeListenerCommand);
  83. }
  84. private void createReadOnlyStatusChangeListener(String category) {
  85. createBooleanAction("Read only status change listener", category, false,
  86. readonlyStatusChangeListenerCommand);
  87. }
  88. protected Command<T, Boolean> valueChangeListenerCommand = new Command<T, Boolean>() {
  89. @Override
  90. public void execute(T c, Boolean value, Object data) {
  91. if (value) {
  92. c.addListener(
  93. (ValueChangeListener) LegacyAbstractFieldTest.this);
  94. } else {
  95. c.removeListener(
  96. (ValueChangeListener) LegacyAbstractFieldTest.this);
  97. }
  98. }
  99. };
  100. protected Command<T, Boolean> readonlyStatusChangeListenerCommand = new Command<T, Boolean>() {
  101. @Override
  102. public void execute(T c, Boolean value, Object data) {
  103. if (value) {
  104. c.addListener(
  105. (ReadOnlyStatusChangeListener) LegacyAbstractFieldTest.this);
  106. } else {
  107. c.removeListener(
  108. (ReadOnlyStatusChangeListener) LegacyAbstractFieldTest.this);
  109. }
  110. }
  111. };
  112. protected Command<T, Object> setValueCommand = new Command<T, Object>() {
  113. @Override
  114. public void execute(T c, Object value, Object data) {
  115. c.setValue(value);
  116. }
  117. };
  118. @Override
  119. public void valueChange(
  120. com.vaadin.v7.data.Property.ValueChangeEvent event) {
  121. log(event.getClass().getSimpleName() + ", new value: "
  122. + getValue(event.getProperty()));
  123. }
  124. @SuppressWarnings({ "rawtypes", "unchecked" })
  125. private String getValue(Property property) {
  126. Object o = property.getValue();
  127. if (o instanceof Collection && sortValueChanges) {
  128. // Sort collections to avoid problems with values printed in
  129. // different order
  130. try {
  131. List<Comparable> c = new ArrayList<Comparable>((Collection) o);
  132. Collections.sort(c);
  133. o = c;
  134. } catch (Exception e) {
  135. // continue with unsorted if sorting fails for some reason
  136. log("Exception while sorting value: " + e.getMessage());
  137. }
  138. }
  139. // Distinguish between null and 'null'
  140. String value = "null";
  141. if (o != null) {
  142. if (o instanceof Date) {
  143. Date d = (Date) o;
  144. // Dec 31, 2068 23:09:26.531
  145. String pattern = "MMM d, yyyy HH:mm:ss.SSS";
  146. SimpleDateFormat format = new SimpleDateFormat(pattern,
  147. new Locale("en", "US"));
  148. value = format.format(d);
  149. } else {
  150. value = "'" + o + "'";
  151. }
  152. }
  153. return value;
  154. }
  155. @Override
  156. public void readOnlyStatusChange(ReadOnlyStatusChangeEvent event) {
  157. log(event.getClass().getSimpleName());
  158. }
  159. protected void createSetTextValueAction(String category) {
  160. String subCategory = "Set text value";
  161. createCategory(subCategory, category);
  162. List<String> values = new ArrayList<>();
  163. values.add("Test");
  164. values.add("A little longer value");
  165. values.add(
  166. "A very long value with very much text. All in all it is 74 characters long");
  167. createClickAction("(empty string)", subCategory, setValueCommand, "");
  168. createClickAction("(null)", subCategory, setValueCommand, null);
  169. for (String value : values) {
  170. createClickAction(value, subCategory, setValueCommand, value);
  171. }
  172. }
  173. }