/* 
______________________________________________________________________
*
* Copyright (c) 1996-2004 QUEST SOFTWARE INC.  All Rights Reserved.
* http://java.quest.com
*
* This software is the confidential and proprietary information of
* Quest Software Inc. ("Confidential Information").  You shall not disclose
* such Confidential Information and shall use it only in accordance with the
* terms of the license agreement you entered into with Quest Software.
*
* QUEST SOFTWARE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY
* OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE, OR NON-INFRINGEMENT. QUEST SOFTWARE SHALL NOT BE LIABLE FOR ANY
* DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
* ______________________________________________________________________
*/

//   RCSID -- $RCSfile: LoginDialog.java,v $ $Revision: 1.2 $
//            $Date: 2006-03-17 21:13:09 $  $Locker:  $  Quest Software Inc.

package com.klg.jclass.datasource;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import com.klg.jclass.datasource.LocaleBundle;

/** A user login dialog is used to prompt user entering login information.
 */
public class LoginDialog extends JDialog implements ActionListener {

static final long serialVersionUID = -1805582303571200505L;

private JButton btnOk, btnCancel;
private JTextField  loginField,             // user id text entry field
dbField,                // database name text entry field
driverField;                
private JPasswordField passwordField;       // password text entry field
private JLabel url;

private boolean isCancelled = false;

public LoginDialog(Frame frame) {
    super(frame);
    setModal(true);
    setTitle(LocaleBundle.string(LocaleBundle.login));
    init();
}

public void init() {
    getContentPane().setLayout(new BorderLayout());

    JPanel loginPanel = new JPanel();
    JPanel button_panel = new JPanel();
    url = new JLabel("");
    loginField = new JTextField("",30);
    passwordField = new JPasswordField("",30);
    dbField = new JTextField("",30);
    driverField = new JTextField("",30);
    JLabel lbl_url = new JLabel(LocaleBundle.string(LocaleBundle.url));
    JLabel lbl_login_name = new JLabel(LocaleBundle.string(LocaleBundle.login_name));
    JLabel lbl_password = new JLabel(LocaleBundle.string(LocaleBundle.password));
    JLabel lbl_database = new JLabel(LocaleBundle.string(LocaleBundle.database));
    JLabel lbl_driver = new JLabel(LocaleBundle.string(LocaleBundle.driver));
    btnOk = new JButton(LocaleBundle.string(LocaleBundle.ok));  
    btnCancel = new JButton(LocaleBundle.string(LocaleBundle.cancel));  

    // create the login panel
    loginPanel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    int rows = 5, cols = 2;
    int x = 0, y = 0;
    c.anchor = GridBagConstraints.WEST;
    c.fill = GridBagConstraints.NONE;

    c.gridx = x++ % cols;
    c.gridy = y++ % rows;
    c.insets = new Insets(5, 5, 0, 5);

    loginPanel.add(lbl_url, c);
    c.gridx = x++ % cols;
    c.insets = new Insets(5, 5, 0, 5);
    loginPanel.add(url, c);

    c.gridx = x++ % cols;
    c.gridy = y++ % rows;
    c.insets = new Insets(5, 5, 0, 5);
    loginPanel.add(lbl_login_name, c);
    c.gridx = x++ % cols;
    c.insets = new Insets(5, 5, 0, 5);
    loginPanel.add(loginField, c);

    c.gridx = x++ % cols;
    c.gridy = y++ % rows;
    c.insets = new Insets(5, 5, 0, 5);
    loginPanel.add(lbl_password, c);
    c.gridx = x++ % cols;
    c.insets = new Insets(5, 5, 0, 5);
    loginPanel.add(passwordField, c);

    c.gridx = x++ % cols;
    c.gridy = y++ % rows;
    c.insets = new Insets(5, 5, 0, 5);
    loginPanel.add(lbl_database, c);
    c.gridx = x++ % cols;
    c.insets = new Insets(5, 5, 0, 5);
    loginPanel.add(dbField, c);

    c.gridx = x++ % cols;
    c.gridy = y++ % rows;
    c.insets = new Insets(5, 5, 5, 5);
    loginPanel.add(lbl_driver, c);
    c.gridx = x++ % cols;
    c.insets = new Insets(5, 5, 5, 5);
    loginPanel.add(driverField, c);

    passwordField.setEchoChar('*');

    button_panel.add(btnOk);
    button_panel.add(btnCancel);
    btnOk.addActionListener(this);
    btnCancel.addActionListener(this);

    getContentPane().add(loginPanel, BorderLayout.CENTER);
    getContentPane().add(button_panel, BorderLayout.SOUTH);

    addWindowListener(new WindowAdapter() {
                          public void windowClosing(WindowEvent e) {
                              cancel(); 
                          }
                      });

    DialogKeyListener l = new DialogKeyListener();
    addKeyListener(l);
    loginField.addKeyListener(l);
    passwordField.addKeyListener(l);
    dbField.addKeyListener(l);
    driverField.addKeyListener(l);
    btnOk.addKeyListener(l);
    btnCancel.addKeyListener(l);
}

/** Get the user/login entered. */
public String getUser() {
    return loginField.getText();
}

/** Get the user password entered. */
public String getPassword() {
    return new String(passwordField.getPassword());
}

/** Get the database entered. */
public String getDatabase() {
    return dbField.getText();
}

/** Get the driver entered. */
public String getDriver() {
    return driverField.getText();
}

/** Set the JDBC URL connection string for display. */
public void setURL(String url) {
    this.url.setText(url);
}

/** Set the database. */
public void setDatabase(String db) {
    this.dbField.setText(db);
}

/** Set the driver. */
public void setDriver(String driver) {
    this.driverField.setText(driver);
}

public boolean isCancelled() {
    return isCancelled;
}

private void ok() {
    setVisible(false);
}

private void cancel() {
    isCancelled = true;
    setVisible(false);
}

public void actionPerformed(ActionEvent e) {
    if (e.getSource() instanceof JButton) {
        if (e.getSource().equals(btnOk)) {
            ok();
        }
        else if (e.getSource().equals(btnCancel)) {
            cancel();
        }
    }
}

/** Centers the dialog relative to the parent frame. */
public void setVisible(boolean visible) {
    if (visible) {
        centerDialog();
    }
    super.setVisible(visible);
}

/** 
 * Centers the dialog relative to the parent frame or screen.
 */
void centerDialog() {
    int x = 0;
    int y = 0;
    Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
    x = (screenDim.width - getSize().width) / 2;
    y = (screenDim.height - getSize().height) / 2;
    setLocation(x, y);
}


class DialogKeyListener extends KeyAdapter {

    public void keyPressed(KeyEvent event) {
        switch (event.getKeyCode()) {
        case KeyEvent.VK_ESCAPE: 
            cancel();
            break;
        case KeyEvent.VK_ENTER: 
            ok();
            break;
        }
    }
}

}
