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.

Ticket1804.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package com.vaadin.tests.tickets;
  2. import java.util.Iterator;
  3. import java.util.LinkedList;
  4. import com.vaadin.data.Validator;
  5. import com.vaadin.data.util.MethodProperty;
  6. import com.vaadin.terminal.SystemError;
  7. import com.vaadin.ui.AbstractField;
  8. import com.vaadin.ui.Button;
  9. import com.vaadin.ui.Button.ClickEvent;
  10. import com.vaadin.ui.Label;
  11. import com.vaadin.ui.Label.ContentMode;
  12. import com.vaadin.ui.Root.LegacyWindow;
  13. import com.vaadin.ui.Select;
  14. import com.vaadin.ui.Window;
  15. public class Ticket1804 extends com.vaadin.Application.LegacyApplication {
  16. LinkedList<Select> listOfAllFields = new LinkedList<Select>();
  17. @Override
  18. public void init() {
  19. final LegacyWindow main = new LegacyWindow("#1804");
  20. setMainWindow(main);
  21. com.vaadin.ui.Select s;
  22. s = new Select("Select with null selection allowed; required=true");
  23. s.setNullSelectionAllowed(true);
  24. s.setRequired(true);
  25. listOfAllFields.add(s);
  26. s = new Select("Select with null selection NOT allowed; required=true");
  27. s.setNullSelectionAllowed(false);
  28. s.setRequired(true);
  29. listOfAllFields.add(s);
  30. s = new Select("Testcase from the ticket #1804");
  31. s.setNullSelectionAllowed(false);
  32. s.setPropertyDataSource(new MethodProperty<String>(new TestPojo(), "id"));
  33. s.addValidator(new EmptyStringValidator(
  34. "Selection required for test-field"));
  35. s.setRequired(true);
  36. listOfAllFields.add(s);
  37. s = new Select("Testcase from the ticket #1804, but without validator");
  38. s.setNullSelectionAllowed(false);
  39. s.setPropertyDataSource(new MethodProperty<String>(new TestPojo(), "id"));
  40. s.setRequired(true);
  41. listOfAllFields.add(s);
  42. s = new Select(
  43. "Testcase from the ticket #1804, but with required=false");
  44. s.setNullSelectionAllowed(false);
  45. s.setPropertyDataSource(new MethodProperty<String>(new TestPojo(), "id"));
  46. s.addValidator(new EmptyStringValidator(
  47. "Selection required for test-field"));
  48. listOfAllFields.add(s);
  49. s = new Select(
  50. "Testcase from the ticket #1804, but without validator and with required=false");
  51. s.setNullSelectionAllowed(false);
  52. s.setPropertyDataSource(new MethodProperty<String>(new TestPojo(), "id"));
  53. listOfAllFields.add(s);
  54. s = new Select(
  55. "Required=true, custom error message, null selection not allowed");
  56. s.setRequired(true);
  57. s.setNullSelectionAllowed(false);
  58. s.setPropertyDataSource(new MethodProperty<String>(new TestPojo(), "id"));
  59. s.setValue(null);
  60. s.setComponentError(new SystemError("Test error message"));
  61. listOfAllFields.add(s);
  62. for (Iterator<Select> i = listOfAllFields.iterator(); i.hasNext();) {
  63. s = i.next();
  64. main.addComponent(s);
  65. s.addItem("foo");
  66. s.addItem("");
  67. s.addItem("bar");
  68. if (s.isNullSelectionAllowed()) {
  69. s.addItem("<null>");
  70. s.setNullSelectionItemId("<null>");
  71. }
  72. s.setImmediate(true);
  73. }
  74. Button checkValidity = new Button("Check validity of the fields");
  75. main.addComponent(checkValidity);
  76. checkValidity.addListener(new Button.ClickListener() {
  77. public void buttonClick(ClickEvent event) {
  78. StringBuffer msg = new StringBuffer();
  79. for (Iterator<Select> i = listOfAllFields.iterator(); i
  80. .hasNext();) {
  81. AbstractField<?> af = i.next();
  82. msg.append("<h1>" + af.getCaption() + "</h1>\n");
  83. msg.append("Value=" + af.getValue() + "<br/>\n");
  84. if (af.isValid()) {
  85. msg.append("VALID\n<hr/>");
  86. } else {
  87. msg.append("INVALID<br/><i>" + af.getErrorMessage()
  88. + "</i><hr/>");
  89. }
  90. }
  91. Window w = new Window("Status of the fields");
  92. w.setModal(true);
  93. w.setHeight("80%");
  94. w.addComponent(new Label(msg.toString(), ContentMode.XHTML));
  95. main.addWindow(w);
  96. }
  97. });
  98. }
  99. public class TestPojo {
  100. String id = "";
  101. public String getId() {
  102. return id;
  103. }
  104. public void setId(String id) {
  105. this.id = id;
  106. }
  107. }
  108. /** Throws an exception when the string is empty or null. */
  109. static class EmptyStringValidator implements Validator {
  110. String msg;
  111. EmptyStringValidator(String msg) {
  112. this.msg = msg;
  113. }
  114. public void validate(Object value) throws InvalidValueException {
  115. if (value == null || value.toString().length() == 0) {
  116. throw new InvalidValueException(msg);
  117. }
  118. }
  119. }
  120. }