// file: $ISIP_IFC/util/demo/isip_comm_client_dialog_manager/DatabasePanel.java
// version: $Id: DatabasePanel.java 10572 2006-06-26 21:54:34Z rei5 $ 
//

// import necessary java libraries
//
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/* 
 * DatabasePanel.java is a 1.4 application 
 * that requires no other files. 
 */
public class DatabasePanel extends JPanel
    implements ActionListener {
    
    //-------------------------------------------------------------------------
    //
    // public constants
    //
    //-------------------------------------------------------------------------
     
    public static String OK = "ok";
    public static String HELP = "help";
    public JButton okButton; 
    public JButton helpButton; 
    public JFrame controllingFrame; //needed for dialogs
    public JPasswordField passwordField;
    public JTextField userField;
    public JTextField hostField;
    public JTextField databaseField;
    public String username;
    public char[] password;
    public String hostname;
    public String database;

    
    //-------------------------------------------------------------------------
    //
    // public methods
    //
    //-------------------------------------------------------------------------

     /**
      * Constructor for this class. <P>
      */
    public DatabasePanel(JFrame f) {
        
	// Use the default FlowLayout.
        //
	controllingFrame = f;
	userField = new JTextField(20);
	userField.addActionListener(this);
	JLabel userlabel = new JLabel("Enter the username: ");
        userlabel.setLabelFor(userField);

        // Create everything.
        //
	passwordField = new JPasswordField(10);
        passwordField.setEchoChar('#');
        passwordField.setActionCommand(OK);
        passwordField.addActionListener(this);

        JLabel passwordlabel = new JLabel("Enter the password: ");
        passwordlabel.setLabelFor(passwordField);
	
	hostField = new JTextField(20);
	hostField.addActionListener(this);
	JLabel hostlabel = new JLabel("Enter the hostname: ");
        hostlabel.setLabelFor(hostField);

	databaseField = new JTextField(20);
	databaseField.addActionListener(this);
	JLabel databaselabel = new JLabel("Enter the database: ");
        databaselabel.setLabelFor(databaseField);
	
	JComponent buttonPane = createButtonPanel();

	// Lay out the text controls and the labels.
        //
	JPanel textControlsPane = new JPanel();
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();

        textControlsPane.setLayout(gridbag);
        JLabel[] labels = {userlabel, passwordlabel, hostlabel, databaselabel};
        JTextField[] textFields = {userField, passwordField, hostField, databaseField};
        addLabelTextRows(labels, textFields, gridbag, textControlsPane);

        c.gridwidth = GridBagConstraints.REMAINDER; //last
        c.anchor = GridBagConstraints.WEST;
        c.weightx = 1.0;
	textControlsPane.setBorder(
                BorderFactory.createCompoundBorder(
                                BorderFactory.createTitledBorder("Text Fields"),
                                BorderFactory.createEmptyBorder(5,5,5,5)));
        add(textControlsPane);
        add(buttonPane);
    }


    public void actionPerformed(ActionEvent e) {
    }
      

    /**
     * get user name <P>
     */
    public String getUserName() {
	
	return username;
    }
    
    /**
     * get the password <P>
     */
    public char[] getPassword() {
	
	return password;
    }
    
    /**
     * get the host name <P>
     */
    public String getHostName() {
	
	return hostname;
    }

    /**
     * get the database <P>
     */
    public String getDatabase() {
	
	return database;
    }
   
    /**
     * dispose the frame <P>
     */
    public void dispose() {
	
	controllingFrame.dispose();
    }
    

    //------------------------------------------------------------------------
    //
    // protected method
    //
    //-----------------------------------------------------------------------

     /**
     * reset focus. Must be called from the event-dispatching 
     * thread. <P>
     */
    protected void resetFocus() {
        passwordField.requestFocusInWindow();
    }
  
    /**
     * create the button panel <P>
     */
    protected JComponent createButtonPanel() {
        JPanel p = new JPanel(new GridLayout(0,1));
        okButton = new JButton("OK");
        helpButton = new JButton("Help");
	
        okButton.setActionCommand(OK);
        helpButton.setActionCommand(HELP);
        helpButton.addActionListener(this);
	
        p.add(okButton);
        p.add(helpButton);
        return p;
    }
    
    //------------------------------------------------------------------------
    //
    // private method
    //
    //-----------------------------------------------------------------------

    /**
     * add text rows <P>
     */
    private void addLabelTextRows(JLabel[] labels,
                                  JTextField[] textFields,
                                  GridBagLayout gridbag,
                                  Container container) {
        GridBagConstraints c = new GridBagConstraints();
        c.anchor = GridBagConstraints.EAST;
        int numLabels = labels.length;
	
        for (int i = 0; i < numLabels; i++) {
            c.gridwidth = GridBagConstraints.RELATIVE; // next-to-last
            c.fill = GridBagConstraints.NONE;      // reset to default
            c.weightx = 0.0;                       // reset to default
            container.add(labels[i], c);

            c.gridwidth = GridBagConstraints.REMAINDER;     // end row
            c.fill = GridBagConstraints.HORIZONTAL;
            c.weightx = 1.0;
            container.add(textFields[i], c);
        }
    }


}

