// Source Code for FindServerFrame.class   //
// Client Code for Desktop Calendar    //

// Code based on using Server #18     //
// Written by Khalid Mufti - Group 31 //

import java.io.*;
import java.awt.*;
import java.awt.event.*;

// This class handles the frame which enables a user to locate the   //
// server by entering an ip address and port.		             //	
// Inherits from the Frame class and requires the frame title as a   //
// parameter.							     //	

public class FindServerFrame extends Frame implements ActionListener
{
   private ClientFrame clientframe;
   private ClientSocket clientsocket;
   private MsgBox msgbox;
   private Button connect, help, quit;
   private GridBagLayout gbLayout;
   private GridBagConstraints gbConstraints; 
   private TextField ipname, portnum;
   private Label iplabel, portlabel;
   private Label dummylabel; 
   private AdminHelpBox helpbox;
   private StringBuffer str;


   // The constructor for the class.  Requires a frame title as a parameter. //
   public FindServerFrame(String s)
   {
     // Call Dialog class constructor //
     super(s);

     // Set window size and type //
     setSize(400,300);
     setResizable(false);  

     // Instantiate grid for layout purposes //
     gbLayout = new GridBagLayout();
     gbConstraints = new GridBagConstraints();
     setLayout (gbLayout);       

     // Allocate memory for all new items in Dialog, and add action listeners //
     gbConstraints.fill = GridBagConstraints.NONE;
     connect = new Button ("Connect With Remote Server");
     connect.addActionListener (this);
     help  = new Button ("Help");
     help.addActionListener (this);
     quit  = new Button ("Quit");
     quit.addActionListener (this);
     ipname = new TextField (20);
     ipname.addActionListener (this);
     portnum = new TextField (20);
     portnum.addActionListener (this);
     iplabel = new Label ("Server IP:");
     portlabel = new Label ("Port Number:");
     dummylabel = new Label ("");   //used to provide spacing//

     // Now place the components in the Dialog, based on the grid bag layout // 
     gbConstraints.weightx=0;
     gbConstraints.weighty=0;
     addObj (iplabel,    0, 0, 1, 1);
     addObj (ipname,     0, 1, 2, 1);
     addObj (portlabel,  1, 0, 1, 1);
     addObj (portnum,    1, 1, 2, 1);
     addObj (dummylabel, 2, 0, 2, 1);
     gbConstraints.weightx=0;
     gbConstraints.weighty=0;
     gbConstraints.fill = GridBagConstraints.BOTH;
     addObj (help,          3, 1, 1, 2);
     gbConstraints.weightx=0;
     gbConstraints.weighty=0;
     addObj (connect,         3, 2, 1, 1);
     addObj (quit,          4, 2, 1, 1);

     // Add a listener for closing the window prematurely and set the textfields //
     // to an empty value                                                        //
     addWindowListener (new CloseWindow());
     ipname.setText ("127.0.0.1");
     portnum.setText ("5000");
   } 

   // This function handles any action events from the user //
   public void actionPerformed (ActionEvent e)
   {   
      // If user pressed the connect button //
      if ((e.getSource())==connect)
      {
         if (CreateSocket())  //If socket created fine //
         {
            clientframe = new ClientFrame ("  Welcome to the Automated Desktop Calendar/Scheduler", clientsocket);  
            if (clientsocket.isOkay())  // if Connection established //
            {
               clientframe.setVisible (true);
	       dispose();       
            }
            
            else  // Could not create socket locally //
            {
               msgbox = new MsgBox (this, "  NETWORK ERROR", "Connection to Host Failed!", 250, 150);
               msgbox.setVisible(true);
            }
         }
      }

      // If user pressed the help button //
      else if ((e.getSource())==help)
      {
         helper();
      }	

      // If user pressed the quit button //
      else if ((e.getSource())==quit)
      {
         dispose();
      }
   }

   // This function attempts to connect to the remote server socket.  On success //
   // true is returned, otherwise false 					 //
   private boolean CreateSocket ()
   {
      clientsocket = new ClientSocket(ipname.getText(),Integer.parseInt(portnum.getText()));     
      if (clientsocket == null)
         return false;
      else
         return true;
   }

   // This function simply adds an object (registers it) to the Dialog //
   // Further, it sets various parts of the grid bag layout            // 
   private void addObj (Component c, int row, int column, int width, int height)
   {
      gbConstraints.gridx=column;
      gbConstraints.gridy=row;   
      gbConstraints.gridwidth=width;
      gbConstraints.gridheight=height;
      gbLayout.setConstraints (c, gbConstraints);
      add (c);
   }    

   // This function simply pops up a help dialog box that displays help on using //
   // this dialog screen.                                                        //
   private void helper()
   {
      // Set what the help box should say //
      str = new StringBuffer(); 
      str.append("This screen is used to locate the desktop server\n");
      str.append("located on the local network or a remote network.\n");
      str.append("\n");
      str.append("For the server IP field, enter the ip where the\n");
      str.append("server is, either in the 10.10.10.10 format or in\n");
      str.append("the www.ecf.toronto.edu format.\n");  
      str.append("\n");
      str.append("For the port number, enter the port at which the\n");
      str.append("server is listening for connections.\n"); 
      str.append("\n");
      str.append("When done, cick on the Connect button; otherwise,\n");
      str.append("click Quit to exit.\n");

      // Create and show modal help box //
      helpbox = new AdminHelpBox (this, "  Help Screen", str.toString(), true, 300, 300);
      helpbox.setVisible(true); 
   }
}

