/* * Isomorphic SmartGWT web presentation layer * Copyright (c) 2011 Isomorphic Software, Inc. * * OWNERSHIP NOTICE * Isomorphic Software owns and reserves all rights not expressly granted in this source code, * including all intellectual property rights to the structure, sequence, and format of this code * and to all designs, interfaces, algorithms, schema, protocols, and inventions expressed herein. * * If you have any questions, please email
. * * This entire comment must accompany any portion of Isomorphic Software source code that is * copied or moved from this file. */ package com.smartgwt.sample.showcase.client.messaging; import com.smartgwt.client.rpc.Messaging; import com.smartgwt.client.rpc.MessagingCallback; import com.smartgwt.client.rpc.RPCCallback; import com.smartgwt.client.rpc.RPCRequest; import com.smartgwt.client.rpc.RPCResponse; import com.smartgwt.client.types.Overflow; import com.smartgwt.client.util.SC; import com.smartgwt.client.widgets.Canvas; import com.smartgwt.client.widgets.form.DynamicForm; import com.smartgwt.client.widgets.form.fields.ButtonItem; import com.smartgwt.client.widgets.form.fields.TextAreaItem; import com.smartgwt.client.widgets.form.fields.TextItem; import com.smartgwt.client.widgets.form.fields.events.ClickEvent; import com.smartgwt.client.widgets.form.fields.events.ClickHandler; import com.smartgwt.client.widgets.layout.VLayout; import com.google.gwt.core.client.EntryPoint; public class SimpleChatSample implements EntryPoint { private Canvas chatLog; private DynamicForm chatForm; @Override public void onModuleLoad() { VLayout layout = new VLayout(); layout.setWidth(500); layout.setHeight100(); chatLog = new Canvas(); chatLog.setBackgroundColor("white"); chatLog.setBorder("2px solid gray"); chatLog.setContents("Chat Session
" + "Open this page in multiple client browsers for multi-user chat.
"); chatLog.setWidth(500); chatLog.setHeight(200); chatLog.setOverflow(Overflow.AUTO); layout.addMember(chatLog); chatForm = new DynamicForm(); chatForm.setHeight(200); TextItem userName = new TextItem("user"); userName.setRequired(true); userName.setTitle("User Name"); TextAreaItem messageArea = new TextAreaItem("msg"); messageArea.setHeight(50); messageArea.setTitle("Message"); messageArea.setWidth(400); ButtonItem send = new ButtonItem(); send.setTitle("Send"); send.setColSpan("*"); send.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { sendChatMessage(); } }); chatForm.setItems(userName, messageArea, send); Messaging.subscribe("chatChannel", new MessagingCallback() { @Override public void execute(Object data) { chatLog.setContents(chatLog.getContents() + (String)data); } }); layout.addMember(chatForm); layout.draw(); } public void sendChatMessage() { if (!chatForm.validate()) return; String userName = (String) chatForm.getValue("user"); Object messageText = chatForm.getValue("msg"); if (messageText == null) return; String message = "
" + userName + ":
" + (String)messageText + "
"; Messaging.send("chatChannel", message, new RPCCallback () { public void execute(RPCResponse response, Object rawData, RPCRequest request) { if (response.getStatus() != RPCResponse.STATUS_SUCCESS) SC.say("Failed send message to server."); } }); } }