KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > getahead > dwrdemo > chat > JavaChat


1 package org.getahead.dwrdemo.chat;
2
3 import java.util.Collection JavaDoc;
4 import java.util.LinkedList JavaDoc;
5
6 import org.directwebremoting.WebContext;
7 import org.directwebremoting.WebContextFactory;
8 import org.directwebremoting.proxy.dwr.Util;
9 import org.directwebremoting.util.Logger;
10
11 /**
12  * @author Joe Walker [joe at getahead dot ltd dot uk]
13  */

14 public class JavaChat
15 {
16     /**
17      * @param text The new message text to add
18      */

19     public void addMessage(String JavaDoc text)
20     {
21         // Make sure we have a list of the list 10 messages
22
if (text != null && text.trim().length() > 0)
23         {
24             messages.addFirst(new Message(text));
25             while (messages.size() > 10)
26             {
27                 messages.removeLast();
28             }
29         }
30
31         WebContext wctx = WebContextFactory.get();
32         String JavaDoc currentPage = wctx.getCurrentPage();
33
34         // Clear the input box in the browser that kicked off this page only
35
Util utilThis = new Util(wctx.getScriptSession());
36         utilThis.setValue("text", "");
37
38         // For all the browsers on the current page:
39
Collection JavaDoc sessions = wctx.getScriptSessionsByPage(currentPage);
40         Util utilAll = new Util(sessions);
41
42         // Clear the list and add in the new set of messages
43
utilAll.removeAllOptions("chatlog");
44         utilAll.addOptions("chatlog", messages, "text");
45     }
46
47     /**
48      * The current set of messages
49      */

50     private LinkedList JavaDoc messages = new LinkedList JavaDoc();
51
52     /**
53      * The log stream
54      */

55     protected static final Logger log = Logger.getLogger(JavaChat.class);
56 }
57
Popular Tags