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