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.

ComboBoxSelectingNewItemValueChange.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package com.vaadin.tests.components.combobox;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.Collections;
  5. import java.util.List;
  6. import com.vaadin.server.VaadinRequest;
  7. import com.vaadin.shared.ui.ContentMode;
  8. import com.vaadin.shared.ui.combobox.ComboBoxClientRpc;
  9. import com.vaadin.ui.Button;
  10. import com.vaadin.ui.CheckBox;
  11. import com.vaadin.ui.ComboBox;
  12. import com.vaadin.ui.Label;
  13. import com.vaadin.ui.Notification;
  14. public class ComboBoxSelectingNewItemValueChange extends ComboBoxSelecting {
  15. final class CustomComboBox extends ComboBox<String> {
  16. private CustomComboBox(String caption, Collection<String> options) {
  17. super(caption, options);
  18. }
  19. public ComboBoxClientRpc getComboBoxClientRpc() {
  20. return getRpcProxy(ComboBoxClientRpc.class);
  21. }
  22. }
  23. CustomComboBox comboBox;
  24. List<String> items = new ArrayList<>();
  25. int valueChangeEventCount = 0;
  26. int selectionChangeEventCount = 0;
  27. Label valueLabel = new Label();
  28. Label valueChangeLabel = new Label(null, ContentMode.HTML);
  29. CheckBox delay = new CheckBox("Slow adding process", false);
  30. CheckBox reject = new CheckBox("Reject new values", false);
  31. CheckBox noSelection = new CheckBox("Don't select new values", false);
  32. @Override
  33. protected void setup(VaadinRequest request) {
  34. initItems();
  35. comboBox = new CustomComboBox(null, items);
  36. comboBox.setTextInputAllowed(true);
  37. comboBox.setEmptySelectionAllowed(true);
  38. comboBox.addValueChangeListener(event -> {
  39. String value = event.getValue();
  40. if (value != null) {
  41. valueLabel.setValue(value);
  42. } else {
  43. valueLabel.setValue("null");
  44. }
  45. });
  46. configureNewItemHandling();
  47. comboBox.addValueChangeListener(e -> {
  48. ++valueChangeEventCount;
  49. updateLabel(e.isUserOriginated());
  50. });
  51. comboBox.addSelectionListener(e -> {
  52. ++selectionChangeEventCount;
  53. updateLabel(e.isUserOriginated());
  54. });
  55. Button checkButton = new Button("Check ComboBox value", e -> {
  56. Notification.show("selected: " + comboBox.getValue());
  57. });
  58. Button resetButton = new Button("Reset options", e -> {
  59. comboBox.setValue(null);
  60. initItems();
  61. comboBox.getDataProvider().refreshAll();
  62. valueLabel.setValue("");
  63. valueChangeLabel.setValue("Reset");
  64. valueChangeEventCount = 0;
  65. selectionChangeEventCount = 0;
  66. });
  67. valueLabel.setId("value");
  68. valueChangeLabel.setId("change");
  69. delay.setId("delay");
  70. reject.setId("reject");
  71. noSelection.setId("noSelection");
  72. resetButton.setId("reset");
  73. addComponents(comboBox, valueLabel, valueChangeLabel, checkButton,
  74. resetButton, delay, reject, noSelection);
  75. }
  76. @SuppressWarnings("deprecation")
  77. protected void configureNewItemHandling() {
  78. comboBox.setNewItemHandler(text -> {
  79. if (Boolean.TRUE.equals(delay.getValue())) {
  80. try {
  81. Thread.sleep(2000);
  82. } catch (InterruptedException e1) {
  83. e1.printStackTrace();
  84. }
  85. }
  86. if (Boolean.TRUE.equals(reject.getValue())) {
  87. valueChangeLabel.setValue("item " + text + " discarded");
  88. comboBox.getComboBoxClientRpc().newItemNotAdded(text);
  89. } else {
  90. items.add(text);
  91. Collections.sort(items);
  92. valueChangeLabel
  93. .setValue("adding new item... count: " + items.size());
  94. if (Boolean.TRUE.equals(noSelection.getValue())) {
  95. comboBox.getComboBoxClientRpc().newItemNotAdded(text);
  96. }
  97. comboBox.getDataProvider().refreshAll();
  98. }
  99. });
  100. }
  101. private void initItems() {
  102. items.clear();
  103. for (char c = 'a'; c <= 'z'; c++) {
  104. for (int i = 0; i < 100; i++) {
  105. items.add("" + c + i);
  106. }
  107. }
  108. }
  109. private void updateLabel(boolean userOriginated) {
  110. valueChangeLabel.setValue("Value change count: " + valueChangeEventCount
  111. + "\nSelection change count: " + selectionChangeEventCount
  112. + "\nuser originated: " + userOriginated);
  113. }
  114. @Override
  115. protected String getTestDescription() {
  116. return "New item should trigger value change when accepted "
  117. + "and restore the field to previous value when rejected.";
  118. }
  119. @Override
  120. protected Integer getTicketNumber() {
  121. return 10284;
  122. }
  123. }