// Sample code file: ChatPanel.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 javax.swing.*;
import javax.swing.text.*;
import java.beans.*;
import java.util.Enumeration;
import java.util.ResourceBundle;
import java.awt.*;
import java.awt.event.*;

/**
 * Displays all text received from the chat room, and provides the user
 * with a method of inputing and sending messages.
 */
public class ChatPanel extends JPanel implements ActionListener, KeyListener
{    
    /**
     * Handles the communications.
     */
    ChatConnection connection;
    
    //GUI components
        JScrollPane messageScrollPane = new JScrollPane();
        JScrollPane inputScrollPane = new JScrollPane();
        JTextArea inputText = new JTextArea();
        JButton sendButton = new JButton();
        Style defaultStyle;
        Style userStyle;
        Style statusStyle;
        StyleContext styleContext = new StyleContext(); 
        DefaultStyledDocument doc = new DefaultStyledDocument(styleContext);
        JTextPane messageText = new JTextPane(doc);
    
    /**
     * Constructs the GUI.
     */
        public ChatPanel(ChatConnection connection)
        {
            this.connection = connection;
            
                setLayout(new GridBagLayout());
                messageScrollPane.setAutoscrolls(true);
                messageScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
                add(messageScrollPane, new SimpleGridBagConstraints(0,0,2,1,1.0,1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,new Insets(6,6,0,6),0,0));
                messageText.setEditable(false);
                messageText.setAutoscrolls(true);
                messageScrollPane.getViewport().add(messageText);
                inputScrollPane.setAutoscrolls(true);
                inputScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
                add(inputScrollPane, new SimpleGridBagConstraints(0,1,1,1,1.0,0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,new Insets(6,6,6,6),0,0));
                inputScrollPane.getViewport().add(inputText);
                inputText.setLineWrap(true);
                inputText.setWrapStyleWord(true);
                sendButton.setText(Chat.chatRes.getString("Send Button"));
                sendButton.setBorderPainted(false);
                sendButton.setMnemonic((int)'S');
                add(sendButton, new SimpleGridBagConstraints(1,1,1,1,0.0,0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,new Insets(6,0,6,6),0,0));
                sendButton.addActionListener(this);     
                inputText.addKeyListener(this);
                
                //Sets the default sizes of the text areas.
                messageScrollPane.setPreferredSize(new Dimension(500, 250));
                inputScrollPane.setPreferredSize(new Dimension(400, 55));       
                inputScrollPane.setMaximumSize(new Dimension(400, 55)); 
                inputScrollPane.setMinimumSize(new Dimension(400, 55)); 
                
                inputText.setFont(new Font("Dialog", Font.PLAIN, 12));
                
                defaultStyle = styleContext.addStyle("Default", null);
        StyleConstants.setForeground(defaultStyle, Color.black);
        StyleConstants.setFontFamily(defaultStyle, "Dialog");
        doc.setLogicalStyle(0, defaultStyle);
        
        userStyle = styleContext.addStyle(null, null);
        StyleConstants.setBold(userStyle, true);
        
        statusStyle = styleContext.addStyle(null, null);
        StyleConstants.setBold(statusStyle, true);
        StyleConstants.setForeground(statusStyle, Color.black);
        }
                
        /** 
     * Called when a message is received from the server.
     * Displays the message on the messageText text area.
     *
     * @param message The new message received.
     */
        public void displayMessage(Color color, String name, String message)
    {
        //Makes sure an new line character is present at the end.
        if(!message.endsWith("\n"))
            message += "\n";
        
        try
        {                 
            //This means that it's a status message
            if(name.length() == 0)
                doc.insertString(doc.getLength(), message, statusStyle);
            else
            {            
                //Display the name in the appropriate color
                StyleConstants.setForeground(userStyle, color);            
                doc.insertString(doc.getLength(), name + ":  ", userStyle);
                
                //and then the text in black.
                doc.insertString(doc.getLength(), message, defaultStyle);
            }
            
            //Makes sure it scrolls to the bottom of the text.
            //The JFC doesn't always rememeber to do that.
            messageText.setCaretPosition(messageText.getText().length());
        }
        catch(BadLocationException e)
        {
            System.out.println(e.getMessage());
        }
    }
    
    /** 
     * Sends the text to the server.
     */
    public void sendText()
    {
        connection.sendMessage(inputText.getText());
                inputText.setText("");
    }

    /** 
     * Implementation of the ActionListener Interface.
     *
     * Called when the send button is pressed.
     * 
     * @param event The ActionEvent.
     */
        public void actionPerformed(ActionEvent event)
        {
                Object object = event.getSource();
                
                if (object == sendButton)
                    sendText();
        }
        
        /**
         * Implementation of the KeyListener Interface.
         *
         * Checks for the enter key and then sends the message.
         *
         * @param e The KeyEvent.
         */
        public void keyReleased(KeyEvent e)
        {
            if(e.getKeyCode() == KeyEvent.VK_ENTER)
                sendText();      
        }
        public void keyTyped(KeyEvent e){}
        public void keyPressed(KeyEvent e){}            
}