# ChatWindow.py : Panel containing the chat-related widgets
#
# Distributed under the terms described in the LICENCE file.
#
# Developed by akuchlin@mems-exchange.org
#
# Contains one single-line text input field, and two text boxes which
# display the results of the chatting, and the list of attached users.

__revision__ = "$Id: ChatWindow.py,v 1.21.2.2 2002/02/26 20:09:34 akuchlin Exp $"

from java import awt
import gui_util
import string

class ChatWindow(awt.Panel, awt.event.ActionListener):
    def __init__ (self, client):
        awt.Panel.__init__(self)
        self.client = client
        self.userlist = []
        self.user_id = None
        
	self.setLayout(awt.GridBagLayout())

        # Create the text field that will list the users.
        self.userlist_box = awt.TextArea("", 6, 20)
        self.userlist_box.setEditable(0)

        # Create the text field that will contain the discussion
        self.chat = awt.TextArea("", 6, 40, awt.TextArea.SCROLLBARS_VERTICAL_ONLY)
        self.chat.setEditable(0)

        # Create the input text field
        self.input = awt.TextField()
        self.input.addActionListener(self)

        gui_util.constrain(self, self.chat, 0,0, 1,1,
                  awt.GridBagConstraints.BOTH, 
		  awt.GridBagConstraints.CENTER)
        gui_util.constrain(self, self.userlist_box, 1,0, 1,1,
                  awt.GridBagConstraints.VERTICAL,
		  awt.GridBagConstraints.CENTER)
        gui_util.constrain(self, self.input, 0,1, 2,1,
                  awt.GridBagConstraints.HORIZONTAL)

    def actionPerformed (self, e):
        import string
        msg = string.strip(self.input.getText()) 
        self.client.conn.send_message('chatmsg msg="%s"' % msg)
        self.input.setText( "" )

    def set_controller (self, user_id):
        "Mark a new user as being in control -- None if no one's in control"
        self.user_id = user_id
        self.update_users()
    
    def update_users (self):
        "Update the display of users after making a change to self.userlist"
        L = self.userlist[:]
        if self.user_id is not None and self.user_id in L:
            index = L.index(self.user_id)
            L[index] = L[index] + " - IN CONTROL"

        L.sort()
        text = string.join(L, '\n')
        text = string.strip(text)
        self.userlist_box.setText(text)

    def clear_users (self):
        """clear_users()
        Clear the list of connected users.
        """
        del self.userlist[:]
        self.update_users()
        
    # Commands from the server
    def cmd_chatmsg (self, sock, id, msg):
        newtext = "<%s>\n  %s\n" % (id, msg)
        self.chat.append(newtext)
        length = len(self.chat.getText())
        self.chat.setCaretPosition(length)

    def cmd_userlistadd (self, sock, id):
        self.userlist.append(id)
        self.update_users()

    def cmd_userlistdelete (self, sock, id):
        if id in self.userlist:
            self.userlist.remove(id)
            self.update_users()

