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.5KB

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