/* Program: Questionnaire
 * Author: Rytis Umbrasas
 * School: Vilnius Lyceum
 * IDE: NetBeans 4.1
 */

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

class login extends application{
    
    private JLabel LabelWlc, LabelName, LabelLastName, LabelTest;
    private JButton ButtonOk, ButtonExit;
    private JTextField FieldName, FieldLastName;
    private JCheckBox CheckAnonymous;
    private JOptionPane AlertMessage;
   
    public login()
    {
        /* Setting up the labels and text fields */
        LabelWlc = new JLabel("Welcome to Questionnaire!");
        addComponent(LabelWlc, 0, 0, 2, 1);
        
        LabelName = new JLabel("First name");
        addComponent(LabelName, 0, 1, 1, 1);
        
        LabelLastName = new JLabel("Last name");
        addComponent(LabelLastName, 0, 2, 1, 1);        
        
        FieldName = new JTextField(15);
        FieldName.setToolTipText("Enter your first name here." +
                " If you would prefer to be anonymous hit the checkbox below.");
        addComponent(FieldName, 1, 1, 1, 1);
        
        FieldLastName = new JTextField(15);
        FieldLastName.setToolTipText("Enter your last name here." +
                " If you would prefer to be anonymous hit the checkbox below.");
        addComponent(FieldLastName, 1, 2, 1, 1);
        
        CheckAnonymous = new JCheckBox("anonymous");
        CheckAnonymous.setToolTipText("Check the box and neither your name," +
                " nor last name will be collected");
        CheckAnonymous.addItemListener(new CheckAnonymousHandler());
        addComponent(CheckAnonymous, 0, 3, 2, 1);
        
        JButton ButtonOk = new JButton("OK");
        ButtonOk.setToolTipText("Start the questionnaire");
        ButtonOk.addActionListener(new ButtonOkHandler());
        addComponent(ButtonOk, 0, 5, 1, 1);
        
        JButton ButtonExit = new JButton("Exit");
        ButtonExit.setToolTipText("Exit the application");
        ButtonExit.addActionListener(new ButtonExitHandler());
        addComponent(ButtonExit, 1, 5, 1, 1);
        
        setSize(500, 500);
        setVisible(true);
    }

     private class CheckAnonymousHandler implements ItemListener{
     /* Disables and cleans Name and Lastname text fields if they are enabled or 
      * enables them if they are disabled
      */
         
        public void itemStateChanged(ItemEvent event){
            if (FieldName.isEnabled()){
                FieldName.setEnabled(false);
                FieldName.setText(null);
            }
            else
                FieldName.setEnabled(true);
            
            if (FieldLastName.isEnabled()){
                FieldLastName.setEnabled(false);
                FieldLastName.setText(null);
            }
            else
                FieldLastName.setEnabled(true);
        }
    }
    
    private class ButtonOkHandler implements ActionListener{
    /* Checks if name and last name fields are filled or if anonymous checkbox is checked,
     * Outputs an error message if requirements are not met
     */
        public void actionPerformed(ActionEvent event){
            try {
                if (((FieldName.getText().compareTo("") != 0) && 
                    (FieldLastName.getText().compareTo("") != 0)) || 
                    (CheckAnonymous.isSelected())){
                    questionnaire = new test(FieldName.getText(), FieldLastName.getText());
                    questionnaire.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    loginScreen.dispose();
                }
                else{
                    AlertMessage = new JOptionPane();
                    AlertMessage.showMessageDialog(null, "Please type your name and" +
                            " last name or check anonymous checkbox!");
                }
            }
            catch(IOException e){
                System.out.println(e.toString());
            }
        }
    }
    
    private class ButtonExitHandler implements ActionListener{
    /* If the button exit is clicked, the program is disposed */    
        public void actionPerformed(ActionEvent event){
            loginScreen.dispose();
        }
    }
}