//
//  Copyright (C) 1999 AT&T Laboratories Cambridge.  All Rights Reserved.
//
//  This is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This software 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 General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this software; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
//  USA.
//
// ----------------------------------------------------------------------
package nxm.vnc.libvnc.tmde;

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

/**
   The panel which implements the user authentication scheme
   Based off of AuthPanel.java - whk
   @version $Id: AuthProxy.java,v 1.2 2013/03/08 16:54:57 ntn Exp $
*/
class AuthProxy extends Frame implements ActionListener {
  /** NOTE: this should be changed if the structure/fields of this class changes. */
  private static final long serialVersionUID = 2013030801332L; // for Serializable interface

  Label title, message, usernameprompt, passwordprompt;
  TextField username;
  TextField password;
  Button ok;
  Button cancel;
  protected boolean canceled = false;

  /** Constructor. */
  public AuthProxy() {

    title = new Label("Proxy Authentication",Label.CENTER);
    title.setFont(new Font("Helvetica", Font.BOLD, 18));
    usernameprompt = new Label("Username:",Label.CENTER);
    passwordprompt = new Label("Password:",Label.CENTER);

    username = new TextField(10);
    username.setForeground(Color.black);
    username.setBackground(Color.white);
    password = new TextField(10);
    password.setForeground(Color.black);
    password.setBackground(Color.white);
    password.setEchoChar('*');

    ok = new Button("OK");
    cancel = new Button("Cancel");

    message = new Label("",Label.CENTER);
    message.setFont(new Font("Courier", Font.BOLD, 16));

    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints gbc = new GridBagConstraints();

    setLayout(gridbag);

    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gridbag.setConstraints(title,gbc);
    add(title);

    gbc.fill = GridBagConstraints.HORIZONTAL;
    gridbag.setConstraints(message,gbc);
    add(message);

    gbc.fill = GridBagConstraints.NONE;
    gbc.gridwidth = 1;
    gridbag.setConstraints(usernameprompt,gbc);
    add(usernameprompt);

    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gridbag.setConstraints(username,gbc);
    add(username);
    username.addActionListener(this);

    gbc.fill = GridBagConstraints.NONE;
    gbc.gridwidth = 1;
    gridbag.setConstraints(passwordprompt,gbc);
    add(passwordprompt);

    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gridbag.setConstraints(password,gbc);
    add(password);
    password.addActionListener(this);

    Panel buttonPanel = new Panel();
    buttonPanel.add(ok);
    buttonPanel.add(cancel);
    gbc.ipady = 10;
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.insets = new Insets(0,20,0,0);
    gbc.ipadx = 40;
    gridbag.setConstraints(buttonPanel,gbc);
    add(buttonPanel);
    ok.addActionListener(this);
    cancel.addActionListener(this);
  }

  /** Move keyboard focus to the password text field object. */
  public void moveFocusToPasswordField() {
    password.requestFocus();
  }

  /** This method is called when a button is pressed or return is pressed in the password text field. */
  public synchronized void actionPerformed(ActionEvent evt) {
    if (evt.getSource() == password || evt.getSource() == ok) {
      canceled = false;
      notify();
    } else if (evt.getSource() == cancel) {
      canceled = true;
      notify();
    } else if (evt.getSource() == username) {
      password.requestFocus();
    }
  }

  public synchronized boolean isCanceled() {
    return canceled;
  }

  public synchronized void setMessage(String newMessage) {
   message.setText(newMessage);
   this.pack();
  }
}
