/**************************************************************************
* Copyright (c) 1996 Jim Crossley
*
* Although it's unlikely that anyone would actually want it,
* permission is hereby granted, without written agreement and
* without license or royalty fees, to use, copy, modify, and
* distribute this software and its documentation for any purpose,
* provided that the above copyright notice and the following three
* paragraphs appear in all copies of this software, its documentation
* and any derivative work.
*
* In no event shall Jim Crossley or Automated Design Systems, Inc. be
* liable to any party for direct, indirect, special, incidental, or
* consequential damages arising out of the use of this software and
* its documentation.
*
* The software provided hereunder is on an "as is" basis, and neither
* Jim Crossley nor Automated Design Systems, Inc. has any obligation
* to provide maintenance, support, updates, enhancements, or modifications.
*
* Derived or altered versions must be plainly marked as such, and
* must not be misrepresented as being the original software.
***************************************************************************/
package dba;

import java.awt.*;
import utilities.TabKeyPanel;

/**
 * This frame allows for the entry of a username and password and an attempt at
 * a database connection.  Its constructor expects an object that implements the
 * Connectable interface.  It is this object's connect() method that is called in
 * response to the user clicking the LoginBox's "Connect" button.
 *
 * @author Jim Crossley
 * @version 1.0, 9/17/1996
 */
public class LoginBox extends Frame {
    protected Connectable owner;

    public LoginBox(Connectable c) {

        super("LoginBox window");
        setBackground(Color.lightGray);

        //{{INIT_CONTROLS
        setLayout(null);
        addNotify();
        resize(insets().left + insets().right + 265, insets().top + insets().bottom + 133);

        TabKeyPanel p = new TabKeyPanel();
        p.setLayout(null);
        p.resize(this.size());

        lblLogin=new Label("Login:");
        lblLogin.setFont(new Font("Dialog",Font.BOLD,10));
        p.add(lblLogin);
        lblLogin.reshape(insets().left + 29,insets().top + 20,67,13);
        txtLogin=new TextField(14);
        p.add(txtLogin);
        txtLogin.reshape(insets().left + 108,insets().top + 13,120,21);
        lblPassword=new Label("Password:");
        lblPassword.setFont(new Font("Dialog",Font.BOLD,10));
        p.add(lblPassword);
        lblPassword.reshape(insets().left + 29,insets().top + 46,73,13);
        txtPassword=new TextField(14);
        p.add(txtPassword);
        txtPassword.reshape(insets().left + 108,insets().top + 39,120,21);
        btnConnect=new Button("Connect");
        p.add(btnConnect);
        btnConnect.reshape(insets().left + 41,insets().top + 80,81,21);
        btnCancel=new Button("Cancel");
        p.add(btnCancel);
        btnCancel.reshape(insets().left + 146,insets().top + 80,81,21);
        add(p);
        //}}

        owner = c;
    }

    /**
     * Initialize the focus at the Login textfield, and set the Password field's
     * echo character.
     */
    public synchronized void show() {
        txtLogin.requestFocus();
        txtPassword.setEchoCharacter('*');
    	move(50, 50);
    	super.show();
    }

    /**
     * We handle three events here:  the window's closing, and the clicking of either
     * the Connect or the Cancel button.
     */
    public boolean handleEvent(Event event) {
    	if (event.id == Event.ACTION_EVENT && event.target == btnConnect) {
    	    // Connect button
            if (owner.connect(txtLogin.getText(), txtPassword.getText())) {
                this.hide();
            }
            else {
                // display Error message
            }
	    	return true;
    	}
    	else
    	if (event.id == Event.ACTION_EVENT && event.target == btnCancel) {
    	    // Cancel button
	    	hide();
	    	return true;
    	}
    	else
    	if (event.id == Event.WINDOW_DESTROY) {
    	    // Close window
    	    hide();
    	    return true;
    	}
    	return super.handleEvent(event);
    }

    //{{DECLARE_CONTROLS
    Label lblLogin;
    TextField txtLogin;
    Label lblPassword;
    TextField txtPassword;
    Button btnConnect;
    Button btnCancel;
    //}}
}

/**
 * A means by which we can implement connect() as a callback function.
 */
interface Connectable {
    boolean connect(String login, String passwd);
}


