/**
 * LogonDialog collects answers to passwords prompts from GUI.
 * @author Lars Appel
 * @version 0.2
 */
public class HelloDialog extends javax.swing.JDialog {

  /** Initializes the Form */
  public HelloDialog(java.awt.Frame parent, boolean modal) {
    super (parent, modal);
    initComponents ();
    pack ();
  }

  /** My own constructor with custom title */
  public HelloDialog(java.awt.Frame parent, boolean modal, String title) {
    super (parent, modal);
    setTitle(title);
    initComponents ();
    pack ();
  }

  /**
   * Did the user exit the dialog with OK or close?
   */
  boolean okChosen;

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the FormEditor.
   */
  private void initComponents () {//GEN-BEGIN:initComponents
    addWindowListener (new java.awt.event.WindowAdapter () {
        public void windowClosing (java.awt.event.WindowEvent evt) {
          closeDialog (evt);
        }
      }
    );
    getContentPane ().setLayout (new java.awt.BorderLayout ());

    topPanel = new javax.swing.JPanel ();
    topPanel.setLayout (new java.awt.FlowLayout ());

      userPrompt = new javax.swing.JLabel ();
      userPrompt.setText ("Please enter Password");
      topPanel.add (userPrompt);

    getContentPane ().add (topPanel, "North");

    bottomPanel = new javax.swing.JPanel ();
    bottomPanel.setLayout (new java.awt.FlowLayout ());

      userAnswer = new javax.swing.JTextField ();
      userAnswer.setForeground (java.awt.Color.white);
      userAnswer.setColumns (10);
      userAnswer.addActionListener (new java.awt.event.ActionListener () {
          public void actionPerformed (java.awt.event.ActionEvent evt) {
            userAnswerActionPerformed (evt);
          }
        }
      );
      bottomPanel.add (userAnswer);

    getContentPane ().add (bottomPanel, "South");

  }//GEN-END:initComponents

  private void userAnswerActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_userAnswerActionPerformed
    // Add your handling code here:
    okChosen = true;
    setVisible (false);
    dispose ();
  }//GEN-LAST:event_userAnswerActionPerformed


  /** Closes the dialog */
  private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
    okChosen = false;
    setVisible (false);
    dispose ();
  }//GEN-LAST:event_closeDialog


// Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JPanel topPanel;
  private javax.swing.JPanel bottomPanel;
  private javax.swing.JLabel userPrompt;
  private javax.swing.JTextField userAnswer;
// End of variables declaration//GEN-END:variables


  void setPrompt(String prompt) {
    userPrompt.setText(prompt);
    userAnswer.setText("");
    userAnswer.requestFocus();
    pack();
  };

  String getAnswer() {
    if (okChosen)
      return userAnswer.getText();
    else
      return null;
  }

  public static String getPass(String title, String prompt) {
    HelloDialog d = new HelloDialog(null, true, title);
    d.setPrompt(prompt);
    d.setLocationRelativeTo(null);
    d.show();
    return d.getAnswer();
  }

  public static void main(java.lang.String[] args) {
    // test driver
    String x = HelloDialog.getPass(
      "hello lars.appel", "ENTER ACCOUNT (APPEL) PASSWORD> "
    );
    System.out.println(x);
    String y = HelloDialog.getPass(
      "hello lars.appel", "ENTER USER (LARS) PASSWORD> "
    );
    System.out.println(y);
    System.exit(0);
  }

}