// Sample code file: ChatDialog.java

// Warning: This code has been marked up for HTML

/*
   Copyright (c) 2000 Novell, Inc.  All Rights Reserved.

   THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES. 
   USE AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO THE LICENSE AGREEMENT 
   ACCOMPANYING THE SOFTWARE DEVELOPMENT KIT (SDK) THAT CONTAINS THIS WORK. 
   PURSUANT TO THE SDK LICENSE AGREEMENT, NOVELL HEREBY GRANTS TO DEVELOPER A 
   ROYALTY-FREE, NON-EXCLUSIVE LICENSE TO INCLUDE NOVELL'S SAMPLE CODE IN ITS 
   PRODUCT. NOVELL GRANTS DEVELOPER WORLDWIDE DISTRIBUTION RIGHTS TO MARKET, 
   DISTRIBUTE, OR SELL NOVELL'S SAMPLE CODE AS A COMPONENT OF DEVELOPER'S 
   PRODUCTS. NOVELL SHALL HAVE NO OBLIGATIONS TO DEVELOPER OR DEVELOPER'S 
   CUSTOMERS WITH RESPECT TO THIS CODE. 
*/
 

package com.novell.Chat;

//Java imports
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ResourceBundle;

// ConsoleOne imports
import com.novell.application.console.snapin.*;
import com.novell.utility.localization.Loc;

/**
 * Displays the dialog used when chatting between users.  This
 * dialog consists of two tabs.  One to display the messages and
 * the other to list the users present in the chat room.
 */
public class ChatDialog extends JDialog implements ActionListener
{       
        /**
         * The ChatConnection that will handle the communications.
         */
        ChatConnection connection;
        
        //The GUI Components
        JTabbedPane tabbedPane = new JTabbedPane();
        JPanel buttonPanel = new JPanel();
        ChatPanel chatPanel;
        InfoPanel infoPanel;
        JButton endChatButton = new JButton();
    
    /**
     * Creates the GUI.
     *
     * @param parent The parent Frame.
     */
        public ChatDialog(Frame parent, ChatConnection connection)
        {
                super(parent);          
                
                this.connection = connection;
                
                //Construct the GUI
                setTitle(Chat.chatRes.getString("Dialog Title") + connection.getConnectionName());
                chatPanel = new ChatPanel(connection);
                if(connection.getType().equals(Chat.CHATROOM_TYPE))
                    infoPanel = new InfoPanel();
                getContentPane().setLayout(new GridBagLayout());
                getContentPane().add(tabbedPane, new SimpleGridBagConstraints(0,0,1,1,1.0,1.0,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(6,6,0,6),0,0));
                tabbedPane.add(chatPanel, Chat.chatRes.getString("Chat"));
                if(connection.getType().equals(Chat.CHATROOM_TYPE))
                    tabbedPane.add(infoPanel, Chat.chatRes.getString("Information"));
                tabbedPane.setSelectedIndex(0);
                buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT,5,5));
                getContentPane().add(buttonPanel, new SimpleGridBagConstraints(0,1,1,1,1.0,0.0,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0));
                Loc.setText(endChatButton, Chat.chatRes.getString("End Chat Button"));
                buttonPanel.add(endChatButton);
                endChatButton.addActionListener(this);
                
                pack();         
                
                this.setLocation(parent.getLocation().x + ((parent.getSize().width - this.getSize().width) / 2),  parent.getLocation().y + ((parent.getSize().height - this.getSize().height) / 2));
        }
        
        /** 
     * Called when a message is received from the server.
     * Displays that message on the chatPanel's text area.
     *
     * @param message The new message received.
     */
    public void displayMessage(Color color, String name, String message)
    {
        chatPanel.displayMessage(color, name, message);
    }

    /** 
     * Implementation of the ActionListener Interface.
     *
     * Called when the end chat button is pressed.
     * 
     * @param event The ActionEvent.
     */
        public void actionPerformed(ActionEvent event)
        {
                Object object = event.getSource();
                
                if (object == endChatButton)
                {
                    connection.disconnect();
                        this.dispose();
                }
        }

        /**
         * Called when the user list needs to be updated.
         *
         * @param chatRoom The chat room object to get the list from.
         */
        public void updateUserList(ObjectEntry chatRoom)
        {
            if(connection.getType().equals(Chat.CHATROOM_TYPE))
                infoPanel.updateUserList(chatRoom);   
        }
}