/* * Isomorphic SmartGWT web presentation layer * Copyright 2000 and beyond 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. */ import com.smartgwt.client.types.Alignment; import com.smartgwt.client.types.Overflow; 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.smartgwtee.client.messaging.Messaging; import com.smartgwtee.client.messaging.MessagingCallback; import com.smartgwtee.sample.showcase.client.PanelFactory; import com.smartgwtee.sample.showcase.client.ShowcasePanel; public class ChatSample implements EntryPoint { public void onModuleLoad() { final Canvas chatLog = new Canvas(); chatLog.setWidth(500); chatLog.setHeight(200); chatLog.setOverflow(Overflow.AUTO); chatLog.setBackgroundColor("white"); chatLog.setBorder("2px solid gray"); chatLog.setContents("Chat Session<br>Open this page in multiple client browsers for multi-user chat."); final DynamicForm chatForm = new DynamicForm(); TextItem name = new TextItem("user"); name.setTitle("User name"); name.setRequired(true); TextAreaItem msg = new TextAreaItem("msg"); msg.setTitle("Message"); msg.setWidth(400); msg.setHeight(50); ButtonItem button = new ButtonItem("send", "Send"); button.setAlign(Alignment.CENTER); button.setColSpan(4); button.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { // Ensure the required 'user' field is not empty if (!chatForm.validate()) return; String userName = chatForm.getValueAsString("user"); String messageText = chatForm.getValueAsString("msg"); // If no message was entered, avoid sending an empty message to the server. if (messageText == null || messageText.equals("")) return; String message = "
" + userName + ":
" + messageText + "
"; Messaging.send("chatChannel", message); } }); chatForm.setFields(name, msg, button); String[] channels = Messaging.getSubscribedChannels(); boolean subscribed = false; for (int i = 0; i < channels.length; i++) { String channel = channels[i]; if(channel.equals("chatChannel")) { subscribed = true; } } if(!subscribed) { Messaging.subscribe("chatChannel", new MessagingCallback() { public void execute(Object data) { chatLog.setContents((String) data + chatLog.getContents()); } }); } VLayout layout = new VLayout(15); layout.addMember(chatLog); layout.addMember(chatForm); layout.draw(); } }