//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2005 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package com.threerings.jme.chat;

import java.util.StringTokenizer;

import com.jme.renderer.ColorRGBA;

import com.jmex.bui.BButton;
import com.jmex.bui.BContainer;
import com.jmex.bui.BTextArea;
import com.jmex.bui.BTextField;
import com.jmex.bui.event.ActionEvent;
import com.jmex.bui.event.ActionListener;
import com.jmex.bui.layout.BorderLayout;
import com.jmex.bui.layout.GroupLayout;

import com.threerings.util.Name;

import com.threerings.crowd.chat.client.ChatDirector;
import com.threerings.crowd.chat.client.ChatDisplay;
import com.threerings.crowd.chat.client.SpeakService;
import com.threerings.crowd.chat.data.ChatCodes;
import com.threerings.crowd.chat.data.ChatMessage;
import com.threerings.crowd.chat.data.SystemMessage;
import com.threerings.crowd.chat.data.UserMessage;
import com.threerings.crowd.data.PlaceObject;

import com.threerings.jme.JmeContext;
import com.threerings.jme.Log;

/**
 * Displays chat messages and allows for their input.
 */
public class ChatView extends BContainer
    implements ChatDisplay
{
    public ChatView (JmeContext ctx, ChatDirector chatdtr)
    {
        _chatdtr = chatdtr;
        setLayoutManager(new BorderLayout(2, 2));
        add(_text = new BTextArea(), BorderLayout.CENTER);
        _incont = new BContainer(GroupLayout.makeHStretch());
        add(_incont, BorderLayout.SOUTH);
        _incont.add(_input = new BTextField());
        _input.addListener(_inlist);
    }

    public void willEnterPlace (PlaceObject plobj)
    {
        setSpeakService(plobj.speakService, null);
    }

    public void didLeavePlace (PlaceObject plobj)
    {
        clearSpeakService();
    }

    public void setSpeakService (SpeakService spsvc, String localType)
    {
        if (spsvc != null) {
            _spsvc = spsvc;
            _localType = localType;
            _chatdtr.addChatDisplay(this);
        }
    }

    public void clearSpeakService ()
    {
        if (_spsvc != null) {
            _chatdtr.removeChatDisplay(this);
            _spsvc = null;
            _localType = null;
        }
    }

    public void setChatButton (BButton button)
    {
        _incont.add(button, GroupLayout.FIXED);
        button.addListener(_inlist);
    }

    /**
     * Instructs our chat input field to request focus.
     */
    public void requestFocus ()
    {
        _input.requestFocus();
    }

    // documentation inherited from interface ChatDisplay
    public void clear ()
    {
    }

    // documentation inherited from interface ChatDisplay
    public boolean displayMessage (ChatMessage msg, boolean alreadyDisplayed)
    {
        // we may be restricted in the chat types we handle
        if (!handlesType(msg.localtype)) {
            return false;
        }

        if (msg instanceof UserMessage) {
            UserMessage umsg = (UserMessage) msg;
            if (umsg.localtype == ChatCodes.USER_CHAT_TYPE) {
                append("[" + umsg.speaker + " whispers] ", ColorRGBA.green);
                append(umsg.message + "\n");
            } else {
                append("<" + umsg.speaker + "> ", ColorRGBA.blue);
                append(umsg.message + "\n");
            }
            return true;

        } else if (msg instanceof SystemMessage) {
            append(msg.message + "\n", ColorRGBA.red);
            return true;

        } else {
            Log.warning("Received unknown message type: " + msg + ".");
            return false;
        }
    }

    // documentation inherited
    public void setEnabled (boolean enabled)
    {
        _input.setEnabled(enabled);
    }

    protected void displayError (String message)
    {
        append(message + "\n", ColorRGBA.red);
    }

    protected void append (String text, ColorRGBA color)
    {
        _text.appendText(text, color);
    }

    protected void append (String text)
    {
        _text.appendText(text);
    }

    protected boolean handlesType (String localType)
    {
        return _localType == null || _localType.equals(localType);
    }

    protected boolean handleInput (String text)
    {
        if (text.length() == 0) {
            // no empty banter
            return false;
        }
        String msg = _chatdtr.requestChat(_spsvc, text, true);
        if (msg.equals(ChatCodes.SUCCESS)) {
            return true;
        } else {
            _chatdtr.displayFeedback(null, msg);
            return false;
        }
    }

    /** Used to trigger sending chat (on return key or button press). */
    protected ActionListener _inlist = new ActionListener() {
        public void actionPerformed (ActionEvent event) {
            if (handleInput(_input.getText())) {
                _input.setText("");
            }
        }
    };

    protected ChatDirector _chatdtr;
    protected SpeakService _spsvc;
    protected String _localType;

    protected BTextArea _text;
    protected BContainer _incont;
    protected BTextField _input;
}

@@ -3029,32 +3029,57 @@
 n button)%0A    %7B%0A
+        _inBtn = button;%0A
         _incont.
@@ -3082,21 +3082,21 @@
 ont.add(
-butto
+_inBt
 n, Group
@@ -3118,21 +3118,21 @@
         
-butto
+_inBt
 n.addLis
@@ -3387,24 +3387,119 @@
 ar ()%0A    %7B%0A
+        if (_input.hasFocus() %7C%7C _inBtn.hasFocus()) %7B%0A            _text.clearText();%0A        %7D%0A
     %7D%0A%0A    /
@@ -5960,10 +5960,40 @@
 _input;%0A
+    protected BButton _inBtn;%0A
 %7D%0A

Handle the clear command in a ChatView