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

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