Last active
March 19, 2018 13:53
-
-
Save mcollovati/69412cd88eefbdebdb86bea45d2e2f1a to your computer and use it in GitHub Desktop.
TextArea Push Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface MyListener { | |
void onNewText(String msg); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
}; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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