Example usage for org.apache.wicket.protocol.ws.api WebSocketPushBroadcaster WebSocketPushBroadcaster

List of usage examples for org.apache.wicket.protocol.ws.api WebSocketPushBroadcaster WebSocketPushBroadcaster

Introduction

In this page you can find the example usage for org.apache.wicket.protocol.ws.api WebSocketPushBroadcaster WebSocketPushBroadcaster.

Prototype

public WebSocketPushBroadcaster(IWebSocketConnectionRegistry registry) 

Source Link

Usage

From source file:org.efaps.ui.wicket.components.connection.MessagePanel.java

License:Apache License

/**
 * @param _wicketId wicketId of this component
 * @param _pageReference reference to the page
 * @throws EFapsException on error/*w  w  w.ja v  a  2 s . c  o m*/
 */
public MessagePanel(final String _wicketId, final PageReference _pageReference) throws EFapsException {
    super(_wicketId);
    final Form<Void> msgForm = new Form<>("msgForm");
    add(msgForm);

    final AjaxSubmitLink sendMsgBtn = new AjaxSubmitLink("sendMsgBtn", msgForm) {

        private static final long serialVersionUID = 1L;

        @Override
        protected void onAfterSubmit(final AjaxRequestTarget _target, final Form<?> _form) {
            final StringBuilder msg = new StringBuilder();
            _form.visitChildren(TextArea.class, new IVisitor<TextArea<String>, Void>() {
                @Override
                public void component(final TextArea<String> _textArea, final IVisit<Void> _visit) {
                    _textArea.setEscapeModelStrings(false);
                    msg.append(_textArea.getDefaultModelObjectAsString());
                    _visit.stop();
                }
            });

            if (msg.length() > 0) {
                _form.visitChildren(CheckBox.class, new IVisitor<CheckBox, Void>() {

                    @Override
                    public void component(final CheckBox _checkBox, final IVisit<Void> _visit) {
                        final Boolean selected = (Boolean) _checkBox.getDefaultModelObject();
                        if (selected) {
                            final CheckBoxPanel panel = (CheckBoxPanel) _checkBox.getParent();
                            final UIUser user = (UIUser) panel.getDefaultModelObject();
                            final List<IWebSocketConnection> conns = RegistryManager
                                    .getConnections4User(user.getUserName());
                            for (final IWebSocketConnection conn : conns) {
                                conn.sendMessage(new PushMsg(msg.toString()));
                            }
                        }
                    }
                });
            }
        }
    };
    msgForm.add(sendMsgBtn);

    final AjaxSubmitLink broadcastMsgBtn = new AjaxSubmitLink("broadcastMsgBtn", msgForm) {

        private static final long serialVersionUID = 1L;

        @Override
        protected void onAfterSubmit(final AjaxRequestTarget _target, final Form<?> _form) {

            final StringBuilder msg = new StringBuilder();
            _form.visitChildren(TextArea.class, new IVisitor<TextArea<String>, Void>() {

                @Override
                public void component(final TextArea<String> _textArea, final IVisit<Void> _visit) {
                    _textArea.setEscapeModelStrings(false);
                    msg.append(_textArea.getDefaultModelObjectAsString());
                    _visit.stop();
                }
            });

            if (msg.length() > 0) {
                final WebSocketSettings webSocketSettings = WebSocketSettings.Holder.get(getApplication());
                final WebSocketPushBroadcaster broadcaster = new WebSocketPushBroadcaster(
                        webSocketSettings.getConnectionRegistry());
                broadcaster.broadcastAll(EFapsApplication.get(), new PushMsg(msg.toString()));
            }
        }
    };
    msgForm.add(broadcastMsgBtn);

    final TextArea<String> msg = new TextArea<>("msg", Model.of(""));
    msgForm.add(msg);

    final MessageTablePanel messageTable = new MessageTablePanel("messageTable", _pageReference,
            new UserProvider());
    msgForm.add(messageTable);
}