I am new to Vaadin and just implement a Prototype for testing the functionality. Currently I play around with Vaadin Server-Push feature. I like to do a simple counter which can be increased by Button. After that I like to open this side from different Browser and when I count up I like all Browser to be updated.
For now I've got that the current Browser is updated by a separated Thread.
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();
}
});
}
}
}
This view I embedded into my UI which is annotated by @Push
.
And here is my controller:
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++;
}
}
How do I have to extend this example that it works to update the UI in different browsers?