package extjtech;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import com.sssw.rt.util.*;

// The LoginHandlerDlg class shows how you can code a
// SilverStream login handler for use in external Java 
// clients. You'll supply an instance of this class in the
// AgRuntime.init() method when preparing to connect your 
// client to a SilverStream server. This enables the server
// to call back to that LoginHandlerDlg instance when it 
// requires user authentication. 
//
// LoginHandlerDlg implements the SilverStream interface
// AgiUserLogin, which has one method: prompt(). You must
// code that method to prompt the user for name and password,
// then return those values to the server (in an instance of
// the SilverStream class AgoUserLoginInfo). 
//
// This example prompts the user by displaying a dialog.
// Alternatively, you could read standard input (as shown in
// class LoginHandler) or command-line args to obtain the 
// name and password.
public class LoginHandlerDlg implements AgiUserLogin {

  // Instance variables for the LoginHandlerDlg class.
     String user;
     String password;
     JFrame frame;
     JDialog dialog;
     JTextField namefield;
     JPasswordField passfield;

  public AgoUserLoginInfo prompt(String realm,
                                 String username,
                                 boolean forceDialog) {

    // Start setting up the dialog to display. Include 
    // the name of the security realm (supplied by the
    // server) in its title.
    frame = new JFrame();
    dialog = new JDialog(frame, "Login for " + realm, true);

    // Set up the username field. If the server supplies a
    // username value from a prior login try, display it.
    JLabel namelabel = new JLabel("Username: ");
    namefield = new JTextField(10);
    namefield.setText(username);

    // Set up the password field.
    JLabel passlabel = new JLabel("Password: ");
    passfield = new JPasswordField(10);
    passfield.setEchoChar('#');

    // Set up the OK button, including an event handler 
    // that executes when a user clicks the button. This
    // event handler obtains the username and password 
    // entered in the fields, then closes the dialog. 
    JButton button = new JButton("OK");
    button.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        user = namefield.getText();
        password = new String(passfield.getPassword());
        frame.dispose();
      }
    });

    // Lay out the labels in a panel.
    JPanel labelpane = new JPanel();
    labelpane.setLayout(new GridLayout(0, 1));
    labelpane.add(namelabel);
    labelpane.add(passlabel);

    // Lay out the fields in a panel.
    JPanel fieldpane = new JPanel();
    fieldpane.setLayout(new GridLayout(0, 1));
    fieldpane.add(namefield);
    fieldpane.add(passfield);

    // Now put everything in another panel: labels on
    // left, fields on right, button on bottom.
    JPanel contentpane = new JPanel();
    contentpane.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
    contentpane.setLayout(new BorderLayout());
    contentpane.add(labelpane, BorderLayout.CENTER);
    contentpane.add(fieldpane, BorderLayout.EAST);
    contentpane.add(button, BorderLayout.SOUTH);

    // Finish setting up the dialog, then show it.
    dialog.setContentPane(contentpane);
    dialog.pack();
    dialog.setLocation(100, 100);
    dialog.setVisible(true);

    // Return the username and password to the server for
    // authentication.
    return new AgoUserLoginInfo(user, password);
  }
}
