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.

ListingTestUI.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package com.vaadin.tokka.tests.components;
  2. import java.util.ArrayList;
  3. import java.util.Comparator;
  4. import java.util.List;
  5. import java.util.Random;
  6. import com.vaadin.annotations.Theme;
  7. import com.vaadin.server.VaadinRequest;
  8. import com.vaadin.tests.components.AbstractTestUI;
  9. import com.vaadin.tokka.server.communication.data.DataSource;
  10. import com.vaadin.tokka.server.communication.data.SingleSelection;
  11. import com.vaadin.tokka.ui.components.fields.TextField;
  12. import com.vaadin.tokka.ui.components.grid.Grid;
  13. import com.vaadin.tokka.ui.components.nativeselect.NativeSelect;
  14. import com.vaadin.ui.Button;
  15. import com.vaadin.ui.Button.ClickEvent;
  16. import com.vaadin.ui.HorizontalLayout;
  17. import com.vaadin.ui.Notification;
  18. import com.vaadin.ui.VerticalLayout;
  19. @Theme("valo")
  20. public class ListingTestUI extends AbstractTestUI {
  21. static Random r = new Random();
  22. static class Bean {
  23. private String value;
  24. private Integer intVal;
  25. public Bean(String value, Integer intVal) {
  26. this.value = value;
  27. this.intVal = intVal;
  28. }
  29. public String getValue() {
  30. return value;
  31. }
  32. public void setValue(String value) {
  33. this.value = value;
  34. }
  35. public Integer getIntVal() {
  36. return intVal;
  37. }
  38. public void setIntVal(Integer intVal) {
  39. this.intVal = intVal;
  40. }
  41. @Override
  42. public String toString() {
  43. return "Bean { value: " + value + ", intVal: " + intVal + " }";
  44. }
  45. public static List<Bean> generateRandomBeans() {
  46. String[] values = new String[] { "Foo", "Bar", "Baz" };
  47. List<Bean> beans = new ArrayList<Bean>();
  48. for (int i = 0; i < 100; ++i) {
  49. beans.add(new Bean(values[r.nextInt(values.length)], r
  50. .nextInt(100)));
  51. }
  52. return beans;
  53. }
  54. }
  55. @Override
  56. protected void setup(VaadinRequest request) {
  57. VerticalLayout layout = new VerticalLayout();
  58. layout.setMargin(true);
  59. layout.setSpacing(true);
  60. NativeSelect<String> select = new NativeSelect<>(
  61. DataSource.create(createOptions(50)));
  62. layout.addComponent(select);
  63. HorizontalLayout hLayout = new HorizontalLayout();
  64. hLayout.addComponent(new Button("Notify", e -> select
  65. .getSelectionModel().getSelected()
  66. .forEach(s -> Notification.show(s))));
  67. hLayout.addComponent(new Button("Random select", e -> {
  68. DataSource<String, ?> ds = select.getDataSource();
  69. int skip = r.nextInt((int) ds.apply(null).count());
  70. ds.apply(null).skip(skip).findFirst()
  71. .ifPresent(select.getSelectionModel()::select);
  72. }));
  73. TextField textField = new TextField();
  74. hLayout.addComponent(textField);
  75. hLayout.addComponent(new Button("Reset options",
  76. e -> select.setOptions(createOptions(Integer.parseInt(textField
  77. .getValue())))));
  78. layout.addComponent(hLayout);
  79. Grid<Bean> grid = new Grid<Bean>();
  80. addComponent(layout);
  81. layout.addComponent(grid);
  82. grid.addColumn("String Value", Bean::getValue);
  83. grid.addColumn("Integer Value", Bean::getIntVal);
  84. grid.addColumn("toString", Bean::toString);
  85. grid.setDataSource(DataSource.create(Bean.generateRandomBeans())
  86. .sortingBy(Comparator.comparing(Bean::getValue)));
  87. addComponent(new Button("Toggle Grid Selection",
  88. new Button.ClickListener() {
  89. private boolean hasSelection = true;
  90. @Override
  91. public void buttonClick(ClickEvent event) {
  92. if (hasSelection) {
  93. grid.setSelectionModel(null);
  94. } else {
  95. grid.setSelectionModel(new SingleSelection<>());
  96. }
  97. hasSelection = !hasSelection;
  98. }
  99. }));
  100. }
  101. private List<String> createOptions(int max) {
  102. List<String> options = new ArrayList<>();
  103. for (int i = 0; i < max; ++i) {
  104. options.add("Option " + i);
  105. }
  106. return options;
  107. }
  108. }