Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ListingTestUI.java 4.2KB

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