super(parameters);
add(new Label("push-implementation-title", pushImplementationTitle));
final String chatRoomName = "#wicket-push";
final ChatRoom chatRoom = getChatService().getChatRoom(chatRoomName);
/*
* add form and fields
*/
final Form<Object> formChat = new Form<Object>("chatForm",
new CompoundPropertyModel<Object>(this));
add(formChat);
// chat history field
final Label chatHistoryField = new Label("chatHistory", "");
chatHistoryField.setEscapeModelStrings(false);
chatHistoryField.setOutputMarkupId(true);
formChat.add(chatHistoryField);
// chat room name
formChat.add(new Label("chatroomName", chatRoomName));
// user field
formChat.add(new TextField<String>("user"));
// message field
final TextField<String> messageField = new TextField<String>("message");
messageField.setOutputMarkupId(true);
formChat.add(messageField);
// send button
formChat.add(sendMessage = new AjaxButton("send", formChat)
{
private static final long serialVersionUID = 1L;
@Override
protected void onError(final AjaxRequestTarget target, final Form<?> form)
{
// nothing
}
@Override
protected void onSubmit(final AjaxRequestTarget target, final Form<?> form)
{
// send a message in the chat room
getChatService().getChatRoom(chatRoomName).sendAsync(user, message);
// clear message area add focus it
target.appendJavaScript("document.getElementById('" + messageField.getMarkupId() +
"').value =''");
target.focusComponent(messageField);
}
});
/*
* install push node
*/
final IPushNode<Message> pushNode = pushServiceRef.get().installNode(this,
new AbstractPushEventHandler<Message>()
{
private static final long serialVersionUID = 1L;
@Override
public void onEvent(final AjaxRequestTarget target, final Message message,
final IPushNode<Message> node, final IPushEventContext<Message> ctx)
{
appendHTML(target, chatHistoryField, _renderMessage(message));
}
});
// disconnect button
formChat.add(new AjaxButton("disconnect", formChat)
{
private static final long serialVersionUID = 1L;
@Override
protected void onError(final AjaxRequestTarget target, final Form<?> form)
{
// nothing
}
@Override
protected void onSubmit(final AjaxRequestTarget target, final Form<?> form)
{
pushServiceRef.get().uninstallNode(WicketAbstractChatPage.this, pushNode);
target.add(setEnabled(false));
target.add(sendMessage.setEnabled(false));
}
});
/*
* connect to chat room
*/
chatRoom.addListener(new IChatListener()
{
@Override
public void onMessage(final Message msg)
{
if (pushServiceRef.get().isConnected(pushNode))
pushServiceRef.get().publish(pushNode, msg);
else
chatRoom.removeListener(this);
}
});
/*
* render chat history
*/
final StringBuilder sb = new StringBuilder();
for (final Message msg : chatRoom.getChatHistory())
sb.append(_renderMessage(msg));
chatHistoryField.setDefaultModelObject(sb);
/*
* install disconnect listener
*/
pushServiceRef.get().addNodeDisconnectedListener(new IPushNodeDisconnectedListener()
{
@Override
public void onDisconnect(final IPushNode<?> node)
{
if (node.equals(pushNode))
{
chatRoom.sendAsync("<System>", "A USER JUST LEFT THE ROOM.");
pushServiceRef.get().removeNodeDisconnectedListener(this);
}
}
});