package org.getahead.dwrdemo.chat;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.directwebremoting.ScriptBuffer;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;

/**
 * @author Joe Walker [joe at getahead dot ltd dot uk]
 */
public class JavascriptChat
{
    /**
     * @param text The new message text to add
     */
    public void addMessage(String text)
    {
        if (text != null && text.trim().length() > 0)
        {
            messages.addFirst(new Message(text));
            while (messages.size() > 10)
            {
                messages.removeLast();
            }
        }

        WebContext wctx = WebContextFactory.get();
        String currentPage = wctx.getCurrentPage();

        ScriptBuffer script = new ScriptBuffer();
        script.appendScript("receiveMessages(")
              .appendData(messages)
              .appendScript(");");

        // Loop over all the users on the current page
        Collection<ScriptSession> sessions = wctx.getScriptSessionsByPage(currentPage);
        for (Iterator<ScriptSession> it = sessions.iterator(); it.hasNext();)
        {
            ScriptSession otherSession = it.next();
            otherSession.addScript(script);
        }
    }

    /**
     * The current set of messages
     */
    private LinkedList<Message> messages = new LinkedList<Message>();

    /**
     * The log stream
     */
    protected static final Log log = LogFactory.getLog(JavascriptChat.class);
}

@@ -1,8 +1,602 @@
+/*%0A * Copyright 2005 Joe Walker%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *     http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A
 package 
@@ -656,35 +656,8 @@
 on;%0A
-import java.util.Iterator;%0A
 impo
@@ -771,51 +771,8 @@
 ry;%0A
-import org.directwebremoting.ScriptBuffer;%0A
 impo
@@ -899,16 +899,64 @@
 Factory;
+%0Aimport org.directwebremoting.proxy.ScriptProxy;
 %0A%0A/**%0A *
@@ -1500,506 +1500,194 @@
     
-ScriptBuffer script = new ScriptBuffer();%0A        script.appendScript(%22receiveMessages(%22)%0A              .appendData(messages)%0A              .appendScript(%22);%22);%0A%0A        // Loop over all the users on the current page%0A        Collection%3CScriptSession%3E sessions = wctx.getScriptSessionsByPage(currentPage);%0A        for (Iterator%3CScriptSession%3E it = sessions.iterator(); it.hasNext();)%0A        %7B%0A            ScriptSession otherSession = it.next();%0A            otherSession.addScript(script);%0A        %7D
+Collection%3CScriptSession%3E sessions = wctx.getScriptSessionsByPage(currentPage);%0A        ScriptProxy s = new ScriptProxy(sessions);%0A        s.addFunctionCall(%22receiveMessages%22, messages);
 %0A   

simplify function calling