Skip to content

Instantly share code, notes, and snippets.

@mcollovati
Last active March 19, 2018 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcollovati/69412cd88eefbdebdb86bea45d2e2f1a to your computer and use it in GitHub Desktop.
Save mcollovati/69412cd88eefbdebdb86bea45d2e2f1a to your computer and use it in GitHub Desktop.
TextArea Push Example
public class CallsView extends HorizontalLayout implements MyListener {
private TextArea textArea;
public CallsView() {
setSizeFull();
textArea = new TextArea();
textArea.setSizeFull();
this.addComponent(textArea);
}
@Override
public void attach() {
super.attach();
MyListeners.addListener(this);
}
@Override
public void detach() {
super.detach();
MyListeners.removeListener(this);
}
@Override
public void onNewText(String msg) {
UI ui = getUI();
ui.access(() -> {
textArea.setValue(textArea.getValue()+"\n"+msg);
ui.push();
});
}
}
public interface MyListener {
void onNewText(String msg);
}
public class MyListeners {
private static CopyOnWriteArraySet<MyListener> listeners = new CopyOnWriteArraySet<>();
public static void addListener(MyListener listener) {
listeners.add(listener);
}
public static void removeListener(MyListener listener) {
listeners.remove(listener);
}
static void sendText(String message) {
listeners.forEach(runSafe(message));
}
private static Consumer<MyListener> runSafe(String message) {
return listener -> {
try {
listener.onNewText(message);
} catch (Exception ex) {
ex.printStackTrace();
}
};
}
}
@Theme("mytheme")
@Push(PushMode.MANUAL)
public class MyUI extends UI {
@Override
protected void init(VaadinRequest vaadinRequest) {
final VerticalLayout layout = new VerticalLayout();
layout.setSizeFull();
layout.addComponent(new CallsView());
setContent(layout);
}
@WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
@VaadinServletConfiguration(ui = MyUI.class, productionMode = false, widgetset = "AppWidgetset")
public static class MyUIServlet extends VaadinServlet {
}
// http://localhost:8080/command?message=MyNewMessage
@WebServlet(urlPatterns = "/command", name = "MyCmdServlet", asyncSupported = true)
public static class MyCommandServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
for (int i = 0; i < 5; i++) {
MyListeners.sendText(req.getParameter("message"));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment