/* Imports the javax.swing package classes. */
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JDialog;
import javax.swing.JPasswordField;
import javax.swing.BorderFactory;
/*Imports the java.awt package classes.*/
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.FlowLayout;
import java.awt.Font;
/*Imports the javax.swing.event package classes.*/
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.JSeparator;
/* 
class ChatLogin - Creates the Chat Login window for the chat application.
      Method:
      - actionPerformed()
      - main()
*/
public class ChatLogin extends JDialog implements ActionListener
{
   JPanel panel;
   JPanel pane;
   /* Declares the objects of the JLabel class. */
   JLabel titleLabel;
   JLabel nameLabel;
   JLabel pwdLabel;
   JLabel ipLabel;
   /* Declares the objects of JTextField class */
   JTextField nameText;
   JPasswordField pwdText;
   JTextField ipText;   
   /*Declares the objects of the JButton class.*/
   JButton connect;
   JButton cancel;
   GridBagLayout gbl;
   GridBagConstraints gbc;
   int value;
   /* Defines the default constructor.*/
   public ChatLogin()
   {
      /*Sets the title of the Font dialog box.*/
      setTitle("Chat Login Window");
      /*Sets the size of Font dialog box.*/
      setSize(280, 170);
      /*Sets resizable button to FALSE.*/
      setResizable(false);
      /*Initializes the object of the GridBagLayout class*/
      gbl = new GridBagLayout();
      /* Sets the Layout. */
      getContentPane().setLayout(gbl);
      /*Creates an object of the GridBagConstraints class.*/
      gbc = new GridBagConstraints();
      /* 
         Initializes the title label object and adds it to the 1, 1, 2, 1 position with WEST
         alignment. 
      */
         gbc.gridx = 1;   
         gbc.gridy = 1;
         gbc.gridwidth = 2;
         gbc.gridheight = 1;
         gbc.anchor = GridBagConstraints.CENTER;
         pane = new JPanel();
      pane.setLayout(new FlowLayout());
      titleLabel = new JLabel("Chat Login Window");
      titleLabel.setFont(new Font("Verdana",Font.BOLD,20));
      pane.add(titleLabel);
      getContentPane().add(pane, gbc);
      gbc.gridx = 1;   
      gbc.gridy = 2;
      gbc.gridwidth = 1;
      gbc.gridheight = 1;
      gbc.anchor = GridBagConstraints.WEST;
      getContentPane().add(new JSeparator(), gbc);
      /* 
         Initializes the IP address label object and adds it to the 1, 3, 1, 1 position with
         EAST alignment 
      */   
      gbc.gridx = 1;   
      gbc.gridy = 3;
      gbc.gridwidth = 1;
      gbc.gridheight = 1;
      gbc.anchor = GridBagConstraints.EAST;
      ipLabel = new JLabel("Server IP: ");
      getContentPane().add(ipLabel, gbc);
      /* 
Initializes the IP address text field object and adds it to the 2, 3, 1, 1 position with WEST alignment 
      */
      gbc.gridx = 2;   
      gbc.gridy = 3;
      gbc.gridwidth = 1;
      gbc.gridheight = 1;
      gbc.anchor = GridBagConstraints.WEST;
      ipText = new JTextField("192.168.0.36", 15);
      ipText.setFont(new Font("Verdana",Font.PLAIN,12));
      getContentPane().add(ipText, gbc);
      gbc.gridx = 1;   
      gbc.gridy = 4;
      gbc.gridwidth = 1;
      gbc.gridheight = 1;
      gbc.anchor = GridBagConstraints.WEST;
      getContentPane().add(new JSeparator(), gbc);
      /* 
Initializes the user name label object and adds it to the 1, 5, 1, 1 position with EAST alignment. 
      */   
      gbc.gridx = 1;   
      gbc.gridy = 5;
      gbc.gridwidth = 1;
      gbc.gridheight = 1;
      gbc.anchor = GridBagConstraints.EAST;
      nameLabel = new JLabel("User Name: ");
      getContentPane().add(nameLabel, gbc);
      /* 
         Initializes the user name text field object and adds it to the 2, 5, 1, 1 position with
         WEST alignment. 
      */
      gbc.gridx = 2;   
      gbc.gridy = 5;
      gbc.gridwidth = 1;
      gbc.gridheight = 1;
      gbc.anchor = GridBagConstraints.WEST;
      nameText = new JTextField(15);
      nameText.setFont(new Font("Verdana",Font.PLAIN,12));
      getContentPane().add(nameText, gbc);
      gbc.gridx = 1;   
      gbc.gridy = 6;
      gbc.gridwidth = 1;
      gbc.gridheight = 1;
      gbc.anchor = GridBagConstraints.WEST;
      getContentPane().add(new JSeparator(), gbc);
      /* 
         Initializes the password label object and adds it to the 1, 7, 1, 1 position with EAST
         alignment. 
      */   
      gbc.gridx = 1;   
      gbc.gridy = 7;
      gbc.gridwidth = 1;
      gbc.gridheight = 1;
      gbc.anchor = GridBagConstraints.EAST;
      pwdLabel = new JLabel("Password: ");
      getContentPane().add(pwdLabel, gbc);
      /* 
         Initializes the password text field object and adds it to the 2, 7, 1, 1 position with
         WEST alignment. 
      */
      gbc.gridx = 2;   
      gbc.gridy = 7;
      gbc.gridwidth = 1;
      gbc.gridheight = 1;
      gbc.anchor = GridBagConstraints.WEST;
      pwdText = new JPasswordField(15);
      pwdText.setFont(new Font("Verdana",Font.PLAIN,12));
      pwdText.addActionListener(this);
      getContentPane().add(pwdText, gbc);
      gbc.gridx = 1;   
      gbc.gridy = 8;
      gbc.gridwidth = 1;
      gbc.gridheight = 1;
      gbc.anchor = GridBagConstraints.WEST;
      getContentPane().add(new JSeparator(), gbc);
      /* 
         Initializes the OK and Cancel button. Adds the button to the 2, 9, 1, 1 position with
         WEST alignment. 
      */
      gbc.gridx = 2;   
      gbc.gridy = 9;
      gbc.gridwidth = 1;
      gbc.gridheight = 1;
      gbc.anchor = GridBagConstraints.WEST;
      panel = new JPanel();
      panel.setLayout(new FlowLayout(FlowLayout.LEFT));
      connect = new JButton("Connect");
      connect.addActionListener(this);
      cancel = new JButton("Cancel");
      cancel.addActionListener(this);
      panel.add(connect);
      panel.add(cancel);
      getContentPane().add(panel, gbc);

      addWindowListener(new WindowAdapter()
      {
         public void windowClosing(WindowEvent we)
         {
            System.exit(0);
         }
      });   
   }
   /*
actionPerformed() - This method is called when the user clicks the any button.
Parameters:	ae: Represents an object of the ActionEvent class that contains the details of the event.
   Return Value: NA
   */
   public void actionPerformed(ActionEvent ae)
   {
      if(ae.getSource() == connect)
      {
/*Creates an object of the ChatClient class.*/
         ChatClient tcc = new ChatClient();
/*Calls the con() method to connect to the server.*/
tcc.con(nameText.getText(), pwdText.getText(), ipText.getText());
         this.setVisible(false);               
      }
      else if(ae.getSource() == cancel)
      {
         System.exit(0);
      }
      else if(ae.getSource() == pwdText)
      {
         /*Creates an object of the ChatClient class.*/
         ChatClient tcc = new ChatClient();
         /*Calls the con() method to connect to the server.*/
         tcc.con(nameText.getText(), pwdText.getText(), ipText.getText());
         this.setVisible(false);               
         }
         }
         /*Main method.*/   
         public static void main(String args[])
         {
            try
            {
               /*Sets the window look and feel to the application.*/
               UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            }
            catch(Exception e){}      
            ChatLogin cl = new ChatLogin();
            cl.show();
      }
}