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.

NoLayoutUpdateWhichNeedsLayout.java 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package com.vaadin.tests.components;
  2. import java.util.concurrent.Executors;
  3. import java.util.concurrent.ScheduledExecutorService;
  4. import java.util.concurrent.TimeUnit;
  5. import com.vaadin.server.VaadinRequest;
  6. import com.vaadin.ui.Button;
  7. import com.vaadin.ui.Button.ClickEvent;
  8. import com.vaadin.ui.GridLayout;
  9. import com.vaadin.ui.Label;
  10. import com.vaadin.ui.ProgressBar;
  11. import com.vaadin.ui.UI;
  12. import com.vaadin.ui.VerticalLayout;
  13. import com.vaadin.ui.Window;
  14. @SuppressWarnings("serial")
  15. public class NoLayoutUpdateWhichNeedsLayout extends UI {
  16. private ProgressBar progressBar;
  17. private Window w;
  18. @Override
  19. protected void init(VaadinRequest request) {
  20. final VerticalLayout layout = new VerticalLayout();
  21. layout.setMargin(true);
  22. setContent(layout);
  23. setPollInterval(1000);
  24. Button button = new Button(
  25. "1. Execute scheduled task and show progress in a window");
  26. button.setId("openWindow");
  27. button.addClickListener(new Button.ClickListener() {
  28. private Window w2;
  29. @Override
  30. public void buttonClick(ClickEvent event) {
  31. GridLayout glo = new GridLayout();
  32. progressBar = new ProgressBar();
  33. progressBar.setIndeterminate(false);
  34. progressBar.setId("progress");
  35. glo.addComponent(progressBar);
  36. w2 = new Window("test");
  37. w2.setWidth("600px");
  38. w2.setHeight("200px");
  39. w2.setContent(glo);
  40. w2.center();
  41. Button closeB = new Button(
  42. "2. Click to close after the progress is updated");
  43. closeB.setId("closeWindow");
  44. closeB.addClickListener(clickEvent -> {
  45. w2.close();
  46. w2 = null;
  47. });
  48. glo.addComponent(closeB);
  49. addWindow(w2);
  50. scheduleTask();
  51. }
  52. });
  53. Button openWin = new Button("3. Finally, Click to open a new Window");
  54. openWin.addClickListener(event -> {
  55. w = new Window("test");
  56. w.setWidth("300px");
  57. w.setHeight("300px");
  58. w.setContent(new VerticalLayout(
  59. new Label("simple test label component")));
  60. w.center();
  61. getUI().addWindow(w);
  62. });
  63. Button stopPolling = new Button("Stop polling",
  64. event -> setPollInterval(-1));
  65. layout.addComponents(button, openWin, stopPolling);
  66. }
  67. protected void scheduleTask() {
  68. Thread t = new Thread() {
  69. @Override
  70. public void run() {
  71. getUI().access(() -> updateProgresBar(50));
  72. }
  73. };
  74. ScheduledExecutorService worker = Executors
  75. .newSingleThreadScheduledExecutor();
  76. worker.schedule(t, 2, TimeUnit.SECONDS);
  77. }
  78. public void updateProgresBar(int pc) {
  79. progressBar.setValue((float) (pc / 100.0));
  80. }
  81. }