// Source Code for KillServerDialog.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 kill the       //
// server, running remotely                                            //
// Inherits from the Dialog class (modal) and requires the parent frame //
// and a socket connected to the server, as parameters                  //  
public class KillServerDialog extends Dialog implements ActionListener
{
   private GridBagLayout gbLayout;
   private GridBagConstraints gbConstraints; 
   private Button cancel, ok, help;
   private Label msg, msg2, msg3;
   private Label dum1;
   private boolean pressed;
   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 KillServerDialog(Frame parent, ClientSocket cs)
   {
     // Call Dialog class constructor //
     super (parent, "  Warning! Server will be shut Down!", true);

     // Set window size and type and initialize variables //
     setSize(300,300);
     setResizable(false);  
     pressed = 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//
     cancel = new Button ("Cancel");
     cancel.addActionListener (this);
     ok = new Button ("OK");
     ok.addActionListener (this);
     help = new Button ("Help");
     help.addActionListener (this);
     msg = new Label("Pressing OK will shut the server");
     msg2 = new Label("down!  Any logged in users will be");
     msg3 = new Label("kicked off.  Are you sure?");

     // Add a listener for closing the window prematurely and set the textfields //
     // to an empty value                                   		         // 
     gbConstraints.fill = GridBagConstraints.HORIZONTAL;
     addObj (msg,         0, 1, 1, 1);
     addObj (msg2,        1, 1, 1, 1);
     addObj (msg3,        2, 1, 1, 1);
     addObj (dum1,        3, 0, 1, 1);        
     gbConstraints.fill = GridBagConstraints.BOTH;
     addObj (help,        4, 0, 1, 2);
     addObj (ok,          4, 1, 1, 1);
     addObj (cancel,      5, 1, 1, 1);        

     // Add a listener for closing the window prematurely//
     addWindowListener (new CloseWindow());
   } 

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

      // If user pressed the ok button //
      else if ((e.getSource())==ok)
      {
      try 
        {
	   // Send request to server //
           clientsocket.Write("kill");
           String buf = new String (clientsocket.Read()); 
  
           // Parse numerical response from server //    
           if (buf.regionMatches(false, 0 , "250", 0,  3))
           {
             msgbox = new MsgBox (frame, "  SUCCESS", "Server shut down Successfully!", 250, 150);
             msgbox.setVisible(true);
             pressed = false;
             dispose();
           }     

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

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

   // This function returns whether or not the okay button was pressed //
   public boolean isPressed()
   {  
      return pressed;
   }

 
   // 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 shut down the desktop\n");
      str.append("server.  Note:  This will disconnect all users\n");
      str.append("that are logged on, including the admin (you).\n");
      str.append("\n");
      str.append("It is recommended to save the current database, in\n");
      str.append("memory, into a file - otherwise all new data will be\n");
      str.append("lost.  Use the Save Database option from the\n");
      str.append("Admin Utilities Menu.  If the database is not\n");
      str.append("saved, then all the new data since the last\n");
      str.append("save will be lost!\n");
      str.append("\n");
      str.append("To kill the server, 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); 
   }
}