import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class UsersFrame extends JFrame
	implements ActionListener
{
	public static final int WIDTH = 400;
	public static final int HEIGHT = 300;

	private JLabel userLabel;
	private JLabel pwdLabel;
	private JTextField userTextField;
	private JTextField passwordTextField;
	private JButton signinButton;
	private JButton cancelButton;

	public UsersFrame()
	{
		setSize(WIDTH, HEIGHT);
		WindowDestroyer listener = new WindowDestroyer();
		addWindowListener(listener);

		Container contentPane = getContentPane();
		contentPane.setLayout(new FlowLayout());

		//User name components
		userLabel = new JLabel("User Name: ");
		contentPane.add(userLabel);
		userTextField = new JTextField(30);
		contentPane.add(userTextField);

		//Password components
		pwdLabel = new JLabel("Password: ");
		contentPane.add(pwdLabel);
		passwordTextField = new JTextField(30);
		contentPane.add(passwordTextField);

		//Buttons
		signinButton = new JButton("Sign In Now!");
		contentPane.add(signinButton);
		signinButton.addActionListener(this);

		cancelButton = new JButton("Cancel");
		contentPane.add(cancelButton);
		cancelButton.addActionListener(this);
	}

	public void actionPerformed(ActionEvent e)
	{
		String actionCommand = e.getActionCommand();

		if(actionCommand.equals("Sign In Now!"))
		{
			try
			{
				String password = passwordTextField.getText();
				if(!checkPasswordLength(password))
				{
					throw new ShortPasswordException();
				}
				else
				{
					JOptionPane.showMessageDialog(null,
						"User name " + userTextField.getText() +
						"\nPassword " + passwordTextField.getText());
					userTextField.setText(" ");
					passwordTextField.setText(" ");
				}
			}
			catch(ShortPasswordException spe)
			{
				JOptionPane.showMessageDialog(null, spe.getMessage());
			}
		}
		else if(actionCommand.equals("Cancel"))
		{
			userTextField.setText(" ");
			passwordTextField.setText(" ");
		}
	}

	public boolean checkPasswordLength(String pwd) throws
		ShortPasswordException
	{
		if(pwd.length() >= 8)
			return true;
		else
			return false;
	}

	public static void main(String[] args)
	{
		UsersFrame gui = new UsersFrame();
		gui.setVisible(true);
	}
}