/*
 * R2D2 class Builds the GUI interface for encrypting
 * messages with the help of R2D2
 *
 * CSCE 155 Fall 2005
 * Assignment 2
 * @author
 * @version
 */
 
// import statements (add them here if you have any)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author  
 */
public class R2D2 extends JFrame implements ActionListener{
	
	
	// Variables declaration 
    // add more variables if you like
    
    // clear button
    private JButton clearButton;
    
    // encrypt button
    private JButton encryptButton;
    
    // for showing label "Encrypted Message"
    private JLabel encryptedMessageLabel;
    
    // for displaying encrypted message in a scroll pane
    private JScrollPane encryptedMessageScrollPane;
    
    // textArea displaying the encrypted message
    private JTextArea encryptedMessageTextArea;
    
    // textArea for displaying the information about this form
    private JTextArea formDescriptionTextArea;
    
    // form heading
    private JLabel formHeadingLabel;
    
    // label for input message text area
    private JLabel inputMessageLabel;
    
    // scrollPane for the user to input message
    private JScrollPane inputMessageScrollPane;
    
    // textArea for the user to write the input message
    private JTextArea inputMessageTextArea;
    
    // label for obiwan's image
    private JLabel obiWanImageLabel;
    
    // label for r2d2's image
    private JLabel r2D2ImageLabel;
    
    // "Time Taken" label
    private JLabel timeTakenLabel;
    
    // label for showing "nano seonds"
    private JLabel timeTakenNanoSecondsLabel;
    
    // textField displaying the time taken for encryption
    private JTextField timeTakenTextField;
    
    // label for the receiverTextField shows "Receiver"
    private JLabel receiverLabel;
    
    // textField for typing in the receiver of the message
    private JTextField receiverTextField;
    
    // label for urgencyTextField shows "Urgency"
    private JLabel urgencyLabel;
    
    // textField for typing in the urgency of the message
    private JTextField urgencyTextField;
    
    
    
    
    
    // End of variables declaration
    
    
	
	
    
    /** Creates new form R2D2 */
    public R2D2() {
        setOutputDisplay();
    }
    

     /** This method is called from within the constructor to
     * initialize the form.
     * 
     *  This method initializes all the GUI components
     */
    private void setOutputDisplay() {
        
	   // --------------------------------------------------------------------------
	   // The following declares GUI components, such as JButton, JLabel, and 
	   // JTextField.  Check out the online API to find out what these are 
	   // and how to use them
	   // --------------------------------------------------------------------------
               
                
		// layout
        getContentPane().setLayout(null);

        // default close operation and preferred size of the frame
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(600, 700));
        
        // -------------------------------------------------------------------------
      	// Set 1
        // For "form heading" 
        // 1.  initialize the label
        // 2.  font properties 
        // 3.  the label describing the display
        // 4.  bounds
        // 5.  added to contentPane
        // -------------------------------------------------------------------------
        formHeadingLabel = new JLabel();
        formHeadingLabel.setFont(new Font("Courier New", 1, 12));
        formHeadingLabel.setText("R2D2 Message Encryption Service");
        formHeadingLabel.setBounds(210, 20, 217, 14);
        getContentPane().add(formHeadingLabel);
        // -------------------------------------------------------------------------
        
        // -------------------------------------------------------------------------
        // set 2
        // declares the form description text area
        formDescriptionTextArea = new JTextArea();
        formDescriptionTextArea.setText("This Form encrypts messages and calculates\nthe total time taken for encryption");
        getContentPane().add(formDescriptionTextArea);
        formDescriptionTextArea.setBounds(100, 50, 440, 40);
        formDescriptionTextArea.setEditable(false);
        // -------------------------------------------------------------------------
        
        // -------------------------------------------------------------------------
        // set 3
        inputMessageLabel = new JLabel();
        // you have to set the bounds, size and locations for inputMessageLabel here
        // you have to set the text for inputMessageLabel to "Input Message" here
        getContentPane().add(inputMessageLabel);
        // -------------------------------------------------------------------------
        
        
        // -------------------------------------------------------------------------
        // set 4
        // the input message scroll pane and text area
        // 1. initialize
        // 2. set bounds
        // 3. initialize textArea
        // 4. put the textArea in the scrollPane
        // 5. add the scrollPane to the contentPane
        inputMessageScrollPane = new JScrollPane();
        inputMessageScrollPane.setBounds(190, 110, 300, 140);
        inputMessageTextArea = new JTextArea();
        inputMessageScrollPane.setViewportView(inputMessageTextArea);
        getContentPane().add(inputMessageScrollPane);
        // -------------------------------------------------------------------------


        // -------------------------------------------------------------------------
        // Set 5
        // For "Encrypted Message" label
        encryptedMessageLabel = new JLabel();
        // you have to set the bounds, size and locations for encryptedMessageLabel here
        // you have to set the text for encryptedMessageLabel to "Encrypted Message" here
        getContentPane().add(encryptedMessageLabel);
        // -------------------------------------------------------------------------

        
        // -------------------------------------------------------------------------
        // set 6
        // the encrypted message scroll pane and text area
        // 1. initialize
        // 2. set bounds
        // 3. initialize textArea
        // 4. put the textArea in the scrollPane
        // 5. add the scrollPane to the contentPane
        encryptedMessageScrollPane = new JScrollPane();
        encryptedMessageScrollPane.setBounds(190, 280, 300, 140);
        encryptedMessageTextArea = new JTextArea();
        encryptedMessageScrollPane.setViewportView(encryptedMessageTextArea);
        getContentPane().add(encryptedMessageScrollPane);
        encryptedMessageScrollPane.setBounds(190, 280, 300, 140);
        // -------------------------------------------------------------------------

        
        // -------------------------------------------------------------------------
        // set 7 r2d2ImageLabel
        // 1. Initialize r2d2ImageLabel
        // 2. specify the image file
        // 3. set bounds for the label
        // 4. add the image to the contentPane        
        // -------------------------------------------------------------------------

        // -------------------------------------------------------------------------
        // set 8 obiWanImageLabel 
        // 1. Initialize obiWanImageLabel
        // 2. specify the image file
        // 3. set bounds for the label 
        // 4. add the image to the contentPane   
        obiWanImageLabel = new JLabel();
        obiWanImageLabel.setIcon(new ImageIcon("obi-wan.jpg"));
        obiWanImageLabel.setBounds(510, 290, 71, 111);
        getContentPane().add(obiWanImageLabel);    
        // -------------------------------------------------------------------------
        
        // -------------------------------------------------------------------------
        // set 9 receiverLabel
        // 1. Initialize receiverLabel
        // 2. set text
        // 3. set bounds for the label 
        // 4. add the label to the contentPane       
        // -------------------------------------------------------------------------
               
        
        
        // -------------------------------------------------------------------------
        // set 10 receiverTextField
        // 1. Initialize receiverTextField
        // 2. set text
        // 3. set bounds for the receiverTextField
        // 4. add the receiverTextField to the contentPane       
        // -------------------------------------------------------------------------
        

        // -------------------------------------------------------------------------
        // set 11 urgencyLabel
        // 1. Initialize urgencyLabel
        // 2. set text
        // 3. set bounds for the label 
        // 4. add the label to the contentPane       
        // -------------------------------------------------------------------------
        
                
        
        
        // -------------------------------------------------------------------------
        // set 12 urgencyTextField
        // 1. Initialize urgencyTextField
        // 2. set text
        // 3. set bounds for the urgencyTextField
        // 4. add the urgencyTextField to the contentPane       
        // -------------------------------------------------------------------------
        
        
        // -------------------------------------------------------------------------
        // set 13 
        // clearButton
        // 1. Initialize the clear Button
        // 2. Set button text label to "Clear"
        // 3. Set bounds
        // 4. add ActionListener to the clearButton
        // 5. add clear button to the content pane
        // -------------------------------------------------------------------------
        
        
        // -------------------------------------------------------------------------
		// set 14
        // encryptButton
        encryptButton = new JButton();
        encryptButton.setText("Encrypt");
        encryptButton.setBounds(330, 570, 120, 23);
        encryptButton.addActionListener(this);
        getContentPane().add(encryptButton);
        // -------------------------------------------------------------------------

        // -------------------------------------------------------------------------
        // set 15
        // for timeTakenLabel
        timeTakenLabel = new JLabel();
        // 1. set the label to "Time Taken"
        // 2. set the bounds
        getContentPane().add(timeTakenLabel);
        // -------------------------------------------------------------------------

        
        // -------------------------------------------------------------------------
        // set 16
        // textField for displaying the time taken for encryption
        timeTakenTextField = new JTextField();
        // 1. set the label of the textField to "0"
        // 2. set bounds
        // 3. add the textField to the contentPane
        // -------------------------------------------------------------------------

        // -------------------------------------------------------------------------
        // set 17
        // create the label showing "nano seconds"
        timeTakenNanoSecondsLabel = new JLabel();
        // 1. set the text to "nano seconds"
        // 2. set the bounds
        // 3. add the label to the contentPane
        // -------------------------------------------------------------------------

        // pack the components and show
        pack();
        setVisible(true);
    } // end of setOutputDisplay


    /**
    * This is a very very special method. 
    * We will talk about this in Event-Driven Programming.  But, you are
    * ENCOURAGED to try to figure out what it does.  This is really fun stuff.
    * It basically reacts to user-generated events such as the selection of a
    * button, the entry into a textfield, etc. and then calls other methods to
    * carry out tasks that correspond to the user-generated events
    * Basically, if the user clicks the encryptButton, it should compute all
    * the necessary output.  If the user clicks the clearButton, it should
    * empty all the textfields and set 0 in the timeTakenTextField
    * Note: for the extra credit part, you will need to also handle the
    * "change picture" of a JButton object.
    */
    public void actionPerformed(ActionEvent event){
        
        String command = event.getActionCommand();
        
        if(command.equals("Encrypt")){  // if user clicks encrypt button
        
            if(this.inputMessageTextArea.getText().length()==0){
                
	            //popUp the JOptionPane and inform the user about the error
                                        
            } // end of if inputMessage is zero length
            
            else{ // the user input is not blank, so encrypt the message
            
        	} //end of else if inputMessage is not blank
            
            
        } // end of if user clicks Encrypt
        
        
        
        // complete the event handler for the clearButton
        if(command.equals("Clear")){  // if user clicks encrypt button
        
           // you should use the clearFields method to clear all the fields
            
        } // end of if user clicks Clear
        
        
    } // end of actionPerformed
    
   
    
    
    /**
    * This method clears the entries of all Textfield, TextArea objects. 
    * It puts "0" in timeTakenTextField, and empty strings inputMessageTextArea, encryptedMessageTextArea,
    * receiverTextField and urgencyTextField
    *
    * Yet to be implemented.
    */

   private void clearFields()  { 

      // you will have to implement this

   }  // end clearFields
   
   
   
    /**
    * This method encrypts the input message and calculates the time required for encryption
    * 
    * Rewrite this method so that it encrypts the 
	* input message and calculates the time required for encryption
    * Please consult the following Java Class APIs: 
    *  a. StringBuffer class (used for manipulating Strings)
    *  b. String class (read the split method)
    *  c. StringBuilder class (you may use it to reverse strings)
    *  d. System class (for getting the value of the system timer in nanoseconds)
    * You may use the following steps to encrypt the string
    *  a. mark the start time 
    *  b. separate the words of the input message (based on space) and store them in a String array
    *  c. loop (for/while) through the array, reverse each word and store it in the StringBuffer
    *  d. mark the endTime of the encryption process (timeTaken = endTime - startTime)
    *  e. display the encrypted message and the timeTaken using displayValues method
    *
    * Partially implemented only.
    */
    public void encryptMessage(){    
	    
	    
	    
	    return;
    } // end of encryptMessage      
    
    
    
    /**
    * This method displays the two values in two Textfield objects: 
    * puts encryptedMessage in encryptedMessageTextArea and timeTaken in timeTakenTextField
    * Yet to be implemented.
    */

   private void displayValues(String encryptedMessage, String timeTaken)  {

      // you will be find out how to do this.  Check out the online API to find
      // out what methods you will have to use

   } // end displayValues
    
                                          
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        R2D2 application = new R2D2();
    } // end of main method
    

    
} // end of class R2D2

