01: /*
02: *
03: * Copyright 2005 Joe Walker
04: *
05: * Licensed under the Apache License, Version 2.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18:
19: package uk.ltd.getahead.dwrdemo.chat;
20:
21: import java.util.Collection;
22: import java.util.LinkedList;
23:
24: import org.directwebremoting.WebContext;
25: import org.directwebremoting.WebContextFactory;
26: import org.directwebremoting.proxy.dwr.Util;
27: import org.directwebremoting.util.Logger;
28:
29: /**
30: * @author Joe Walker [joe at getahead dot ltd dot uk]
31: */
32: public class JavaChat {
33: /**
34: * @param text The new message text to add
35: */
36: public void addMessage(String text) {
37: // Make sure we have a list of the list 10 messages
38: if (text != null && text.trim().length() > 0) {
39: messages.addFirst(new Message(text));
40: while (messages.size() > 10) {
41: messages.removeLast();
42: }
43: }
44:
45: WebContext wctx = WebContextFactory.get();
46: String currentPage = wctx.getCurrentPage();
47:
48: // Clear the input box in the browser that kicked off this page only
49: Util utilThis = new Util(wctx.getScriptSession());
50: utilThis.setValue("text", "");
51:
52: // For all the browsers on the current page:
53: Collection sessions = wctx.getScriptSessionsByPage(currentPage);
54: Util utilAll = new Util(sessions);
55:
56: // Clear the list and add in the new set of messages
57: utilAll.removeAllOptions("chatlog");
58: utilAll.addOptions("chatlog", messages, "text");
59: }
60:
61: /**
62: * The current set of messages
63: */
64: private LinkedList messages = new LinkedList();
65:
66: /**
67: * The log stream
68: */
69: protected static final Logger log = Logger
70: .getLogger(JavaChat.class);
71: }
|