// Source Code for AddRoomDialog.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 dialog which enables a user to add a new      //
// room into the server database.                                       //
// Inherits from the Dialog class (modal) and requires the parent frame //
// and a socket connected to the server, as parameters                  //  
public class AddRoomDialog extends Dialog implements ActionListener
{
   private GridBagLayout gbLayout;
   private GridBagConstraints gbConstraints; 
   private TextField rname, rcap;
   private Button cancel, ok, help;
   private Label rname_l, rcap_l, dum1;
   private ClientSocket clientsocket;
   private Frame frame;
   private MsgBox msgbox;
   private AdminHelpBox helpbox;
   private StringBuffer str;

   // The constructor for the class.  Requires a parent frame and socket connected //
   // to the server as parameters.                                                 //
   public AddRoomDialog(Frame parent, ClientSocket cs)
   {
     // Call Dialog class constructor //
     super (parent, "  Please enter new room Name and Capacity", true);

     // Set window size and type and copy parameters to local variables //
     setSize(300,300);
     setResizable(false);  
     frame = parent;
     clientsocket = cs;

     // 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 //
     dum1 = new Label (""); //used to provide spacing//
     rname = new TextField (20);
     rname.addActionListener (this);
     rcap = new TextField (20);
     rcap.addActionListener (this);
     cancel = new Button ("Cancel");
     cancel.addActionListener (this);
     ok = new Button ("OK");
     ok.addActionListener (this);
     help = new Button ("Help");
     help.addActionListener (this);
     rname_l = new Label("Room Name:");
     rcap_l = new Label ("Room Capacity:");

     // Now place the components in the Dialog, based on the grid bag layout //
     gbConstraints.fill = GridBagConstraints.HORIZONTAL;
     addObj (rname,        0, 1, 2, 1);
     addObj (rcap,         1, 1, 2, 1);
     addObj (rname_l,      0, 0, 1, 1);
     addObj (rcap_l,       1, 0, 1, 1);
     addObj (dum1,         2, 1, 2, 1);        
     gbConstraints.fill = GridBagConstraints.BOTH;
     addObj (help,         3, 0, 1, 2);
     addObj (ok,           3, 1, 2, 1);
     addObj (cancel,       4, 1, 2, 1);        

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

   // This function handles any action events from the user //
   public void actionPerformed (ActionEvent e)
   {   
      // If user pressed the cancel button //
      if ((e.getSource())==cancel)
         dispose();

      // If user pressed the ok button //
      else if ((e.getSource())==ok)
      {
        try 
        {
           // Send request to server //
           clientsocket.Write("addroom," + rname.getText() + "," + rcap.getText());
           String buf = new String (clientsocket.Read()); 

           // Parse numerical response from server //       
           if (buf.regionMatches(false, 0 , "170", 0,  3))
           {
             msgbox = new MsgBox (frame, "  SUCCESS", "Room Added Successfully!", 250, 150);
             msgbox.setVisible(true);
             dispose();
           }     

           else if (buf.regionMatches(false, 0 , "745", 0,  3))
           {
             msgbox = new MsgBox (frame, "  ROOM ERROR", "Room Already Exists!", 250, 150);
             msgbox.setVisible(true);
           }     

           else 
           {
             msgbox = new MsgBox (frame, "  INPUT ERROR", "Invalid Entry Detected!", 250, 150);
             msgbox.setVisible(true);
           }     
         }
         
         catch (IOException io)  // Connection must have been lost //
         {
           msgbox = new MsgBox (frame, "  NETWORK ERROR", "Network Error Detected!", 250, 150);
           msgbox.setVisible(true);
           dispose();
         }  
      }    

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

   // 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 add a new room to the\n");
      str.append("desktop server.  This room can be booked for a\n");
      str.append("group meeting, as each room has a capacity.\n");
      str.append("\n");
      str.append("For the name field, enter a text description\n");
      str.append("uniquely identifying this room.  For example\n");
      str.append("a room number.\n");
      str.append("\n");
      str.append("For the capacity field, enter the capacity that\n");
      str.append("this new room can accomodate.\n");
      str.append("\n");
      str.append("When done, cick on the OK button; otherwise,\n");
      str.append("click Cancel to return to the Admin menu.\n");
 
      // Create and show modal help box //
      helpbox = new AdminHelpBox (frame, "  Help Screen", str.toString(), true, 300, 300);
      helpbox.setVisible(true); 
   }
}