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.

GridFastAsyncUpdate.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package com.vaadin.tests.components.grid;
  2. import java.util.Calendar;
  3. import java.util.LinkedList;
  4. import java.util.List;
  5. import java.util.Random;
  6. import java.util.concurrent.ExecutorService;
  7. import java.util.concurrent.Executors;
  8. import java.util.logging.Level;
  9. import com.vaadin.annotations.Push;
  10. import com.vaadin.server.VaadinRequest;
  11. import com.vaadin.tests.components.AbstractTestUI;
  12. import com.vaadin.ui.Button;
  13. import com.vaadin.ui.Grid;
  14. import com.vaadin.ui.HorizontalLayout;
  15. import com.vaadin.ui.VerticalLayout;
  16. @Push
  17. public class GridFastAsyncUpdate extends AbstractTestUI {
  18. private int counter;
  19. private List<Item> items = new LinkedList<>();
  20. private Grid<Item> grid;
  21. private long loggingStart;
  22. private volatile boolean scrollLock = false;
  23. private final Runnable addRowsTask = () -> {
  24. System.out.println("Logging...");
  25. try {
  26. Random random = new Random();
  27. while (!Thread.currentThread().isInterrupted()) {
  28. Thread.sleep(random.nextInt(100));
  29. GridFastAsyncUpdate.this.access(() -> {
  30. ++counter;
  31. Item item = new Item(counter,
  32. (Calendar.getInstance().getTimeInMillis()
  33. - loggingStart),
  34. Level.INFO.toString(), "Message");
  35. items.add(item);
  36. grid.setItems(items);
  37. if (grid != null && !scrollLock) {
  38. grid.scrollToEnd();
  39. }
  40. });
  41. }
  42. } catch (InterruptedException e) {
  43. System.out.println("logging thread interrupted");
  44. }
  45. };
  46. @Override
  47. protected void setup(VaadinRequest request) {
  48. final VerticalLayout layout = new VerticalLayout();
  49. layout.setSizeFull();
  50. layout.setMargin(true);
  51. addComponent(layout);
  52. HorizontalLayout buttons = new HorizontalLayout();
  53. layout.addComponent(buttons);
  54. final ExecutorService logExecutor = Executors.newSingleThreadExecutor();
  55. final Button logButton = new Button("Start logging");
  56. logButton.addClickListener(clickEvent -> {
  57. if ("Start logging".equals(logButton.getCaption())) {
  58. loggingStart = Calendar.getInstance().getTimeInMillis();
  59. logExecutor.submit(addRowsTask);
  60. logButton.setCaption("Stop logging");
  61. } else {
  62. System.out.println("Stop logging...");
  63. try {
  64. logExecutor.shutdownNow();
  65. } catch (Exception e) {
  66. e.printStackTrace();
  67. }
  68. logButton.setCaption("Start logging");
  69. }
  70. });
  71. buttons.addComponent(logButton);
  72. final Button scrollButton = new Button("Stop scrolling");
  73. scrollButton.addClickListener(clickEvent -> {
  74. if (!scrollLock) {
  75. System.out.println("Stop scrolling");
  76. scrollButton.setCaption("Start scrolling");
  77. scrollLock = true;
  78. } else {
  79. System.out.println("Start scrolling");
  80. scrollButton.setCaption("Stop scrolling");
  81. scrollLock = false;
  82. }
  83. });
  84. buttons.addComponent(scrollButton);
  85. grid = new Grid<>();
  86. grid.addColumn(Item::getSequenceNumber).setCaption("");
  87. grid.addColumn(Item::getMillis).setCaption("");
  88. grid.addColumn(Item::getLevel).setCaption("");
  89. grid.addColumn(Item::getMessage).setCaption("");
  90. grid.setWidth("100%");
  91. grid.setSelectionMode(Grid.SelectionMode.SINGLE);
  92. grid.addSelectionListener(selectionEvent -> {
  93. if (!selectionEvent.getAllSelectedItems().isEmpty()) {
  94. disableScroll();
  95. }
  96. });
  97. layout.addComponent(grid);
  98. layout.setExpandRatio(grid, 1.0f);
  99. }
  100. protected void disableScroll() {
  101. scrollLock = true;
  102. }
  103. protected class Item {
  104. Integer sequenceNumber;
  105. Long millis;
  106. String level, message;
  107. public Item(Integer sequanceNumber, Long millis, String level,
  108. String message) {
  109. this.sequenceNumber = sequanceNumber;
  110. this.millis = millis;
  111. this.level = level;
  112. this.message = message;
  113. }
  114. public Integer getSequenceNumber() {
  115. return sequenceNumber;
  116. }
  117. public void setSequenceNumber(Integer sequenceNumber) {
  118. this.sequenceNumber = sequenceNumber;
  119. }
  120. public Long getMillis() {
  121. return millis;
  122. }
  123. public void setMillis(Long millis) {
  124. this.millis = millis;
  125. }
  126. public String getLevel() {
  127. return level;
  128. }
  129. public void setLevel(String level) {
  130. this.level = level;
  131. }
  132. public String getMessage() {
  133. return message;
  134. }
  135. public void setMessage(String message) {
  136. this.message = message;
  137. }
  138. }
  139. }