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 7.3KB

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