/*
 * LoginDialog.java
 *
 * Created on April 17, 2002, 3:26 AM
 */

package project.adminclient;

import project.common.BufferedIO;
import java.io.*;
import java.lang.*;

/**
 *
 * @author  matt
 */
public class LoginDialog extends javax.swing.JDialog {
  
    private BufferedIO io;
    private boolean succ;
    
    /** Creates new form LoginDialog */
    public LoginDialog(java.awt.Frame parent, boolean modal, BufferedIO bio) {
        super(parent, modal);
        initComponents();
        io = bio;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        username = new javax.swing.JTextField();
        password = new javax.swing.JPasswordField();
        loginButton = new javax.swing.JButton();
        
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
        
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Security System Login");
        setModal(true);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });
        
        jLabel1.setText("Username");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 40, -1, -1));
        
        jLabel2.setText("Password");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 80, -1, -1));
        
        username.setText("administrator");
        username.setToolTipText("Enter your username");
        getContentPane().add(username, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 30, 170, 30));
        
        password.setToolTipText("Enter your password");
        getContentPane().add(password, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 70, 170, 30));
        
        loginButton.setText("Login");
        loginButton.setToolTipText("Click to login");
        loginButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginButtonActionPerformed(evt);
            }
        });
        
        getContentPane().add(loginButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 130, -1, -1));
        
        pack();
    }//GEN-END:initComponents

    private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginButtonActionPerformed
        //System.out.println(io.readln());
        io.println("TYPE A");
        System.out.println(io.readln());
        io.println("USER " + username.getText());
        System.out.println(io.readln());
        io.println("PASS " + password.getText());
        String s = new String(io.readln());
        if(s.indexOf("OK") == -1)
            succ = false;
        else
            succ = true;
        setVisible(false);
        dispose();
    }//GEN-LAST:event_loginButtonActionPerformed
    
    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog

    public boolean showDialog()
    {
        show();
        return succ;
    }
    
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        boolean success;
        BufferedIO bio = new BufferedIO(new BufferedInputStream(System.in), System.out);
        success = new LoginDialog(new javax.swing.JFrame(), true, bio).showDialog();
        if(success)
            System.out.println("Honk!");
        else
            System.out.println("Moo!");
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField username;
    private javax.swing.JPasswordField password;
    private javax.swing.JButton loginButton;
    // End of variables declaration//GEN-END:variables
   
}
