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.

DateFieldEmptyValid.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package com.vaadin.tests.components.datefield;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. import java.util.Locale;
  5. import com.vaadin.data.Property.ValueChangeEvent;
  6. import com.vaadin.data.Property.ValueChangeListener;
  7. import com.vaadin.data.util.ObjectProperty;
  8. import com.vaadin.shared.ui.datefield.Resolution;
  9. import com.vaadin.shared.ui.label.ContentMode;
  10. import com.vaadin.tests.components.TestBase;
  11. import com.vaadin.tests.util.Log;
  12. import com.vaadin.ui.Button;
  13. import com.vaadin.ui.Button.ClickEvent;
  14. import com.vaadin.ui.Button.ClickListener;
  15. import com.vaadin.ui.Label;
  16. import com.vaadin.v7.ui.LegacyPopupDateField;
  17. @SuppressWarnings("serial")
  18. public class DateFieldEmptyValid extends TestBase {
  19. private Log log;
  20. private MyDateField df;
  21. private SimpleDateFormat formatter = new SimpleDateFormat(
  22. "MMMM d, yyyy hh:mm:ss aaa", Locale.US);
  23. public class MyDateField extends LegacyPopupDateField {
  24. @Override
  25. public boolean isEmpty() {
  26. return super.isEmpty();
  27. }
  28. }
  29. @Override
  30. protected void setup() {
  31. addComponent(new Label("<br/><br/>", ContentMode.HTML));
  32. log = new Log(8);
  33. addComponent(log);
  34. df = new MyDateField();
  35. df.setId("DateField");
  36. df.setRequired(true);
  37. df.setLocale(new Locale("fi", "FI"));
  38. df.setValue(new Date(100000000000L));
  39. df.setImmediate(true);
  40. df.setResolution(Resolution.DAY);
  41. df.addListener(new ValueChangeListener() {
  42. @Override
  43. public void valueChange(ValueChangeEvent event) {
  44. log.log("Value changeEvent");
  45. checkEmpty();
  46. }
  47. });
  48. addComponent(df);
  49. checkEmpty();
  50. Button b = new Button("Clear date");
  51. b.setId("clear");
  52. b.addListener(new ClickListener() {
  53. @Override
  54. public void buttonClick(ClickEvent event) {
  55. log.log("Clearing date aka setValue(null)");
  56. df.setValue(null);
  57. }
  58. });
  59. addComponent(b);
  60. b = new Button("Set date to 4.5.1990");
  61. b.setId("set4.5.1990");
  62. b.addListener(new ClickListener() {
  63. @Override
  64. @SuppressWarnings("deprecation")
  65. public void buttonClick(ClickEvent event) {
  66. log.log("Setting new value to datefield (4.5.1990)");
  67. df.setValue(new Date(1990 - 1900, 5 - 1, 4));
  68. }
  69. });
  70. addComponent(b);
  71. b = new Button("Set date to 5.6.2000 using a property data source");
  72. b.addListener(new ClickListener() {
  73. @Override
  74. @SuppressWarnings("deprecation")
  75. public void buttonClick(ClickEvent event) {
  76. log.log("Setting new object property (5.6.2000) to datefield");
  77. ObjectProperty<Date> dfProp = new ObjectProperty<Date>(
  78. new Date(2000 - 1900, 6 - 1, 5), Date.class);
  79. df.setPropertyDataSource(dfProp);
  80. }
  81. });
  82. b.setId("set-by-ds");
  83. addComponent(b);
  84. b = new Button(
  85. "Set date to 27.8.2005 by changing a new property data source from null, ds attached before value setting.");
  86. b.setId("set-via-ds");
  87. b.addListener(new ClickListener() {
  88. @Override
  89. @SuppressWarnings("deprecation")
  90. public void buttonClick(ClickEvent event) {
  91. log.log("Setting object property (with value null) to datefield and set value of property to 27.8.2005");
  92. ObjectProperty<Date> dfProp = new ObjectProperty<Date>(null,
  93. Date.class);
  94. df.setPropertyDataSource(dfProp);
  95. dfProp.setValue(new Date(2005 - 1900, 8 - 1, 27));
  96. }
  97. });
  98. addComponent(b);
  99. b = new Button("Check value");
  100. b.setId("check-value");
  101. b.addListener(new ClickListener() {
  102. @Override
  103. public void buttonClick(ClickEvent event) {
  104. log.log("Checking state");
  105. checkEmpty();
  106. }
  107. });
  108. addComponent(b);
  109. }
  110. private void checkEmpty() {
  111. Object value = df.getValue();
  112. if (value instanceof Date) {
  113. value = formatter.format(df.getValue());
  114. }
  115. log.log("DateField value is now " + value);
  116. // log.log("DateField value is now " + df.getValue());
  117. log.log("isEmpty: " + df.isEmpty() + ", isValid: " + df.isValid());
  118. }
  119. @Override
  120. protected String getDescription() {
  121. return "Tests the isEmpty() and isValid() functionality of a DateField. The field is required and has no other validators."
  122. + "IsEmpty() should return true when the field is truly empty i.e. contains no text, no matter how the field has been made empty. If the field contains any text, isEmpty() should return false."
  123. + "IsValid() should in this case return true if the field is not empty and vice versa.";
  124. }
  125. @Override
  126. protected Integer getTicketNumber() {
  127. return 5277;
  128. }
  129. }