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 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. @Override
  23. protected void setup(VaadinRequest request) {
  24. VerticalLayout layout = new VerticalLayout();
  25. layout.setMargin(true);
  26. layout.setSpacing(true);
  27. NativeSelect<String> select = new NativeSelect<>(
  28. DataSource.create(createOptions(50)));
  29. layout.addComponent(select);
  30. HorizontalLayout hLayout = new HorizontalLayout();
  31. hLayout.addComponent(new Button("Notify", e -> select
  32. .getSelectionModel().getSelected()
  33. .forEach(s -> Notification.show(s))));
  34. hLayout.addComponent(new Button("Random select", e -> {
  35. DataSource<String, ?> ds = select.getDataSource();
  36. int skip = r.nextInt((int) ds.apply(null).count());
  37. ds.apply(null).skip(skip).findFirst()
  38. .ifPresent(select.getSelectionModel()::select);
  39. }));
  40. TextField textField = new TextField();
  41. hLayout.addComponent(textField);
  42. hLayout.addComponent(new Button("Reset options",
  43. e -> select.setOptions(createOptions(Integer.parseInt(textField
  44. .getValue())))));
  45. layout.addComponent(hLayout);
  46. Grid<Bean> grid = new Grid<Bean>();
  47. addComponent(layout);
  48. layout.addComponent(grid);
  49. grid.addColumn("String Value", Bean::getValue);
  50. grid.addColumn("Integer Value", Bean::getIntVal);
  51. grid.addColumn("toString", Bean::toString);
  52. grid.setDataSource(DataSource.create(Bean.generateRandomBeans())
  53. .sortingBy(Comparator.comparing(Bean::getValue)));
  54. addComponent(new Button("Toggle Grid Selection",
  55. new Button.ClickListener() {
  56. private boolean hasSelection = true;
  57. @Override
  58. public void buttonClick(ClickEvent event) {
  59. if (hasSelection) {
  60. grid.setSelectionModel(null);
  61. } else {
  62. grid.setSelectionModel(new SingleSelection<>());
  63. }
  64. hasSelection = !hasSelection;
  65. }
  66. }));
  67. }
  68. private List<String> createOptions(int max) {
  69. List<String> options = new ArrayList<>();
  70. for (int i = 0; i < max; ++i) {
  71. options.add("Option " + i);
  72. }
  73. return options;
  74. }
  75. }