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

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

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

// This class handles the dialog which enables a user to view a list    //
// of current users on the server.                                      //
// Inherits from the Dialog class (modal) and requires the parent frame //
// and a socket connected to the server, as parameters                  //  
public class ViewUserDialog extends Dialog implements ActionListener
{
   private GridBagLayout gbLayout;
   private GridBagConstraints gbConstraints; 
   private Button ok, help;
   private List userlist;
   private Label dum1;
   private ClientSocket clientsocket;
   private Frame frame;
   private MsgBox msgbox;
   private StringTokenizer tokens;
   private AdminHelpBox helpbox;
   private StringBuffer str;

   // The constructor for the class.  Requires a parent frame and socket connected //
   // to the server as parameters.                                                 //
   public ViewUserDialog(Frame parent, ClientSocket cs)
   {
     // Call Dialog class constructor //
     super (parent, "  Current User List", 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//     
     ok = new Button ("OK");
     ok.addActionListener (this);
     help = new Button ("Help");
     help.addActionListener (this);

     // Now place the components in the Dialog, based on the grid bag layout //
     gbConstraints.fill = GridBagConstraints.HORIZONTAL;
     addObj (dum1,        10, 0, 3, 1);        
     gbConstraints.fill = GridBagConstraints.BOTH;
     addObj (help,        11, 0, 1, 2);
     addObj (ok,          11, 1, 2, 1);

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

     // Get list of users from server //
     try 
     {
        // Send request to server //
        clientsocket.Write("listallusers,");
        String buf = new String (clientsocket.Read()); 

        // Parse numerical response from server //     
        if (buf.regionMatches(false, 0 , "190", 0,  3))
        {
           // Tokenize string to extract user names //
           tokens = new StringTokenizer (buf, ",");
           userlist = new List (5, false);
           tokens.nextToken(); // Throw away response number //
           while (true)
           { 
              String buffer = new String (tokens.nextToken()); 
              if (buffer.regionMatches(false, 0 , "END", 0,  3)) // End of user list //
 	         break;	            
              if ((buffer.indexOf("_personal")) == -1)
                 userlist.add (buffer);   // Add to list //
           }

           addObj (userlist, 0, 0, 2, 1);        
        }     

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

   // This function handles any action events from the user //
   public void actionPerformed (ActionEvent e)
   {   
      // If user pressed the ok button //
      if ((e.getSource())==ok)
      {
         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 lists all the users on the desktop\n");
      str.append("server.  This can also be used to determine a\n");
      str.append("new username for a new user.\n");
      str.append("\n");
      str.append("Once you are finished looking at the list,\n");
      str.append("click OK 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); 
   }
}