import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;

/**
* MiniChat class is where code starts execution.  It sets up the GUI, gets the host IP, and sets up the event listeners.
*
* @author Michael Wales
*/

public class MiniChat extends JApplet implements ActionListener
{
   /**
   * This is the container used to hold the chat text.  The container is used by the ChatScrollPane
   */
   public Container ChatRootPane;
   
   /**
   * This is the actual big chat text area.
   */
   public JTextArea ChatText;
   
   /**
   * This field is where the user types in a new message for the chat window.  It also has an action listener for when the user
   * hits return it is the same action as pressing the send button.
   */
   public JTextField UserMessage;
   
   /**
   * Button that sends the message to other users.  Has same event listener as the UserMessage JTextField
   */
   public JButton SendMessage;
   
   /**
   * This scroll pane is used to create a vertically scrollable chat window
   */
   public JScrollPane ChatScrollPane;
   
   /**
   * Applets content pane
   */
   public JPanel MainPanel;
   
   /**
   * Layout manager for the MainPanel object
   */
   public FlowLayout MainPanelLayout;
   
   /**
   * Object that actually performs the network communication stuff with the server
   */
   public ChatConnection MiniChatConnection;
   
   /**
   * Initializes all the user interface components.  Sets up the GUI properties, and displays the GUI.
   */
   public void initializeComponents()
   {
      // Initialize the atomic GUI components
      ChatText = new JTextArea("Mini Chat by Michael Wales...   ", 20, 40);
      ChatText.setEditable(false);
      ChatText.setLineWrap(true);
      
      ChatScrollPane = new JScrollPane(ChatText);
      ChatScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
      
      UserMessage = new JTextField(34);
      UserMessage.setActionCommand("SendMessage");
      UserMessage.addActionListener(this);
      
      SendMessage = new JButton("Send");
      SendMessage.setActionCommand("SendMessage");
      SendMessage.addActionListener(this);
      
      // Intialize the main panel and add all the components to it
      MainPanel = new JPanel( new FlowLayout() );
      MainPanel.setPreferredSize( new Dimension(450,385) );
      MainPanel.add(ChatScrollPane);
      MainPanel.add(UserMessage);
      MainPanel.add(SendMessage);
   }
   
   /**
   * Gets the IP address of the host computer.  Handles all the exception stuff internally.
   * @return String representation of the IP address
   */
   private String getHostIPAddress()
   {
      String host = getCodeBase().getHost();
      String IPAddress = null;
   
      try
      {
         IPAddress = InetAddress.getByName(host).getHostName();
      }
      catch (Exception E)
      {
         System.out.println("MiniChat ERROR:  Couldn't resolve host IP");
      }
   
      return IPAddress;  
   }
   
   /**
   * Applet interface method that is called initially.  This method calls the GUI initialization method
   * and opens the MiniChatConnection object
   */
   public void init()
   {
      initializeComponents();
      setContentPane( MainPanel );
      
      MiniChatConnection = new ChatConnection(ChatText, ChatScrollPane);
      MiniChatConnection.open(getHostIPAddress(), 6789);
   }
   
   /**
   * This event listener only listens for the send message event, all others are ignored.  When a message
   * is sent, it executes the MiniChatConnection sendMessage method.
   */
   public void actionPerformed(ActionEvent AEvent)
   {
      String ActionCommand;
      
      ActionCommand = AEvent.getActionCommand();
      
      if ( ActionCommand.equals("SendMessage") )
      {
         // SendMessage button has been pressed
         // ChatText.append("You said:  " + UserMessage.getText() + "\n");
         MiniChatConnection.sendMessage( UserMessage.getText());
         UserMessage.setText("");
      }
      else
      {
         // An unhandled action event has occured
         System.out.println("Unknown event occured:  " + ActionCommand);
      }
   }
         
};

      
   
   
   