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.

MakeComponentVisibleWithPush.java 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package com.vaadin.tests.push;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import com.vaadin.annotations.Push;
  5. import com.vaadin.server.VaadinRequest;
  6. import com.vaadin.tests.components.AbstractTestUI;
  7. import com.vaadin.ui.Button;
  8. import com.vaadin.ui.Grid;
  9. import com.vaadin.ui.UI;
  10. import com.vaadin.ui.VerticalLayout;
  11. @Push
  12. public class MakeComponentVisibleWithPush extends AbstractTestUI {
  13. private VerticalLayout rootLayout;
  14. private Grid<Person> grid;
  15. private SearchThread searchThread;
  16. private List<Person> data;
  17. @Override
  18. protected void setup(VaadinRequest request) {
  19. data = new ArrayList<>();
  20. rootLayout = new VerticalLayout();
  21. setContent(rootLayout);
  22. grid = new Grid<Person>();
  23. grid.addColumn(Person::getName);
  24. grid.setVisible(false);
  25. rootLayout.addComponent(grid);
  26. Button doUpdateButton = new Button("Do Update", event -> {
  27. try {
  28. doUpdate();
  29. } catch (InterruptedException e) {
  30. }
  31. });
  32. rootLayout.addComponent(doUpdateButton);
  33. }
  34. private void doUpdate() throws InterruptedException {
  35. cancelSuggestThread();
  36. grid.setVisible(false);
  37. grid.setItems(data);
  38. UI ui = UI.getCurrent();
  39. searchThread = new SearchThread(ui);
  40. searchThread.start();
  41. }
  42. class SearchThread extends Thread {
  43. private UI ui;
  44. public SearchThread(UI ui) {
  45. this.ui = ui;
  46. }
  47. @Override
  48. public void run() {
  49. data = new ArrayList<Person>(data);
  50. data.add(new Person("Person " + (data.size() + 1)));
  51. if (!searchThread.isInterrupted()) {
  52. ui.access(() -> {
  53. grid.setItems(data);
  54. grid.setVisible(true);
  55. });
  56. }
  57. }
  58. }
  59. private void cancelSuggestThread() {
  60. if ((searchThread != null) && !searchThread.isInterrupted()) {
  61. searchThread.interrupt();
  62. searchThread = null;
  63. }
  64. }
  65. class Person {
  66. private String name;
  67. public Person(String name) {
  68. this.name = name;
  69. }
  70. public String getName() {
  71. return name;
  72. }
  73. public void setName(String name) {
  74. this.name = name;
  75. }
  76. }
  77. }