/**
 * Copyright (c) 2005 Red Hat, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * Component of: Red Hat Application Server
 *
 * Initial Developers: Gregory Lapouchnian
 *                     Patrick Smith
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */
package olstore.client;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

/**
 * Login dialog to authenticate the user before completing a purchase.
 */
public class LoginDialog extends JDialog implements ActionListener {

    /** Username field. */
    private JTextField username;

    /** Password field. */
    private JPasswordField password;

    /** Login button. */
    private JButton bLogin;

    /** Cancel button. */
    private JButton bCancel;

    /** Length of the input fields. */
    private static final int FIELD_LENGTH = 15;

    /** */
    private boolean loginPress = false;

    /**
     * Login dialog that accepts the username/password combination from the user
     * which is then passed to the webservice for authetication before proceeding
     * with the order.
     * @param parent the parent JFrame of this dialog
     */
    public LoginDialog(JFrame parent) {
        super(parent, "Enter username and password...");
        username = new JTextField(FIELD_LENGTH);
        password = new JPasswordField(FIELD_LENGTH);

        setBackground(OlstoreSwingClient.itemColor1);

        bLogin = new JButton("Login");
        bLogin.addActionListener(this);
        bLogin.setBackground(OlstoreSwingClient.buttonColor);

        bCancel = new JButton("Cancel");
        bCancel.addActionListener(this);
        bCancel.setBackground(OlstoreSwingClient.buttonColor);

        getContentPane().setLayout(new GridLayout(0, 2, 5, 5));

        JLabel userLabel = new JLabel("Username:");
        userLabel.setBackground(OlstoreSwingClient.itemColor1);
        JLabel passwordLabel = new JLabel("Password:");
        passwordLabel.setBackground(OlstoreSwingClient.itemColor1);
        getContentPane().add(userLabel);
        getContentPane().add(username);
        getContentPane().add(passwordLabel);
        getContentPane().add(password);
        getContentPane().add(bLogin);
        getContentPane().add(bCancel);

        setModal(true);

        pack();
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((int) d.getWidth() / 2 - getWidth(), (int) d.getHeight()
                / 2 - getHeight());
    }

    /**
     * Handle the login.
     * @param e the event that triggered the listener
     */
    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(bLogin)) {
            loginPress = true;
        }
        setModal(false);
        hide();
    }

    /**
     * Check whether the login button has been pressed.
     * @return true if the login button has been clicked, false otherwise.
     */
    public boolean login() {
        return loginPress;
    }

    /**
     * Get the username and password.
     * @return a string array containing the username and the password
     */
    public String[] getLogin() {
        String[] result = new String[2];

        result[0] = username.getText();
        result[1] = new String(password.getPassword());

        return result;

    }
}
