add(new Behavior() {
private static final long serialVersionUID = -2205036360048419129L;
@Override
public void renderHead(Component component, IHeaderResponse response) {
ChatDao dao = getBean(ChatDao.class);
try {
StringBuilder sb = new StringBuilder();
//FIXME limited count should be loaded with "earlier" link
for (ChatMessage m : dao.get(0, 30)) {
sb.append("addChatMessageInternal(").append(getMessage(m).toString()).append(");");
}
if (sb.length() > 0) {
sb.append("$('#").append(MESSAGE_AREA_ID).append("').emoticonize();");
response.render(OnDomReadyHeaderItem.forScript(sb.toString()));
}
} catch (JSONException e) {
}
super.renderHead(component, response);
}
});
add(new EmoticonsBehavior("#" + MESSAGE_AREA_ID));
add(new WebMarkupContainer("messages").setMarkupId(MESSAGE_AREA_ID));
ChatToolbar toolbar = new ChatToolbar("toolbarContainer");
final WysiwygEditor chatMessage = new WysiwygEditor("chatMessage", Model.of(""), toolbar);
add(new Form<Void>("sendForm").add(
toolbar
, chatMessage.setOutputMarkupId(true)
, new AjaxButton("send") {
private static final long serialVersionUID = 1L;
@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
ChatDao dao = getBean(ChatDao.class);
ChatMessage m = new ChatMessage();
m.setMessage(unescapeXml(chatMessage.getDefaultModelObjectAsString()));
m.setSent(new Date());
m.setFromUser(getBean(UserDao.class).get(getUserId()));
dao.update(m);
IWebSocketConnectionRegistry reg = IWebSocketSettings.Holder.get(getApplication()).getConnectionRegistry();
for (IWebSocketConnection c : reg.getConnections(getApplication())) {
try {
c.sendMessage(getMessage(m).toString());
} catch(Exception e) {