我是 Vaadin 的新手,只是实现了一个原型来测试功能。目前我在玩 Vaadin Server-Push 功能。我喜欢做一个可以通过 Button 增加的简单计数器。之后我喜欢从不同的浏览器打开这一面,当我计数时,我喜欢更新所有浏览器。
现在我知道当前的浏览器是由一个单独的线程更新的。
public class PushTestView extends VerticalLayout implements View {
private static final long serialVersionUID = -8056849522760207972L;
private Label counter;
public void enter(ViewChangeEvent event) {
removeAllComponents();
PushTestController controller = PushTestController.getInstance();
counter = new Label("" + controller.getCounter());
Button button = new Button("Count up");
button.addClickListener(new ClickListener() {
private static final long serialVersionUID = -1437136914245802675L;
@Override
public void buttonClick(ClickEvent event) {
InitializerThread thread = new InitializerThread();
thread.run();
}
});
addComponent(counter);
addComponent(button);
}
class InitializerThread extends Thread {
@Override
public void run() {
// Init done, update the UI after doing locking
UI.getCurrent().access(new Runnable() {
@Override
public void run() {
// Here the UI is locked and can be updated
PushTestController.getInstance().countUp();
counter.setValue("" + PushTestController.getInstance().getCounter());
UI.getCurrent().notifyAll();
}
});
}
}
}
这个视图嵌入到我的 UI 中,由@Push
. 这是我的控制器:
public class PushTestController {
private static PushTestController instance = new PushTestController();
private int counter = 0;
private PushTestController() {
//Private Constructor for Singleton
}
public static PushTestController getInstance() {
return instance;
}
public int getCounter() {
return counter;
}
public String getCounterString() {
return "" + counter;
}
public void countUp() {
counter++;
}
}
我如何扩展此示例以更新不同浏览器中的 UI?