// Sample code file: Login.java

// Warning: This code has been marked up for HTML

/***************************************************************************
$name: Login.java 
$version: 1.0 
$date_modified: 210104
$description:  
$owner: Beans for Novell Services Team 
Copyright (c) 1998 Novell, Inc. All Rights Reserved.

THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES.
USE AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO THE LICENSE AGREEMENT
ACCOMPANYING THE SOFTWARE DEVELOPMENT KIT (SDK) THAT CONTAINS THIS WORK.
PURSUANT TO THE SDK LICENSE AGREEMENT, NOVELL HEREBY GRANTS TO DEVELOPER A
ROYALTY-FREE, NON-EXCLUSIVE LICENSE TO INCLUDE NOVELL'S SAMPLE CODE IN ITS
PRODUCT. NOVELL GRANTS DEVELOPER WORLDWIDE DISTRIBUTION RIGHTS TO MARKET,
DISTRIBUTE, OR SELL NOVELL'S SAMPLE CODE AS A COMPONENT OF DEVELOPER'S
PRODUCTS. NOVELL SHALL HAVE NO OBLIGATIONS TO DEVELOPER OR DEVELOPER'S
CUSTOMERS WITH RESPECT TO THIS CODE.
****************************************************************************/

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class Login extends JDialog implements ActionListener, KeyListener{
       
       // Member variables ('m' -> member)
       private String m_fullName = null;
       private String m_serverName = null;
       private String m_context = null;
       private String m_cn = null;
       private String m_password = null;
       private String m_entryFullName = null;

       // Gui Components ('g' -> GUI component)
       private JPanel           g_root = null;
       private JLabel           g_cnLabel = null;
       private JTextField       g_cnText = null;
       private JLabel           g_passwordLabel = null;
       private JPasswordField   g_passwordText = null;
       private JTabbedPane      g_tabbedPane = null;
       private JLabel           g_serverNameLabel = null;
       private JTextField       g_serverNameText = null;
       private JLabel           g_contextLabel = null;
       private JTextField       g_contextText = null;
       private JButton          g_login = null;
       private JButton          g_cancel = null;
       private UAMConsole       g_testUI = null;
       
       Login(UAMConsole frame, String title)
       {
            super(frame.getFrame(),title,true);
            g_testUI = frame;

            g_root = new JPanel();
            g_root.setLayout( new BoxLayout(g_root, BoxLayout.Y_AXIS) );
          
            JPanel pan4 = new JPanel();
            pan4.setLayout(new BoxLayout(pan4,BoxLayout.Y_AXIS));
            
            JPanel pan11 = new JPanel();
            pan11.setLayout(new FlowLayout(FlowLayout.LEFT));
            g_serverNameLabel = new JLabel("Server Name :");
            g_serverNameLabel.setDisplayedMnemonic('s');
            g_serverNameLabel.setLabelFor(g_serverNameText);
            g_serverNameText = new JTextField(30);
            pan11.add( g_serverNameLabel );
            pan11.add( g_serverNameText );
            g_serverNameText.addKeyListener( this );
            
            g_root.add(pan11);

            JPanel pan2 = new JPanel();
           pan2.setLayout(new FlowLayout(FlowLayout.LEFT));
            g_cnLabel = new JLabel("   UserName :");
            
            g_cnText = new JTextField(30);
            pan2.add( g_cnLabel );
            pan2.add( g_cnText );
            g_cnText.addKeyListener( this );
            pan4.add(pan2);
            
            JPanel pan3 = new JPanel();
            pan3.setLayout(new FlowLayout(FlowLayout.LEFT));
            g_passwordLabel = new JLabel("   Password   :");
            g_passwordText = new JPasswordField(30);
            pan3.add( g_passwordLabel );
            pan3.add( g_passwordText );
            g_passwordText.addKeyListener( this );
            pan4.add(pan3);
            
            g_root.add(pan4);
   
            JPanel pan5 = new JPanel();
            pan5.setLayout(new FlowLayout(FlowLayout.RIGHT));
            g_login = new JButton("  Login  ");
            g_cancel = new JButton("  Cancel  ");
            g_login.setMnemonic('l');
            g_cancel.setMnemonic('c');
            pan5.add(g_login);
            pan5.add(g_cancel);
            Insets insets = pan5.getInsets();
            g_root.add(pan5);
            
            g_login.addActionListener(this);
            g_cancel.addActionListener(this);

            this.getContentPane().add( g_root );
            this.pack();
            //this.setLocation(x,y );
            
       }
       
       public void setVisible(boolean b)
       {
            if (b == true)             
              Utilities.centerWindow(this);
            super.setVisible(b);
            g_serverNameText.requestFocus();
            
            
       }
       
       public void actionPerformed(ActionEvent evt)
       {
           if(evt.getSource() == g_login) 
             this.checkFields();
           if(evt.getSource() == g_cancel)
      {
              m_password = "";
            g_passwordText.setText("");
          this.dispose();
      }
       }

       public void keyTyped(KeyEvent e) {}
       public void keyReleased(KeyEvent e) {}
       public void keyPressed(KeyEvent evt)
       {
            if(evt.getKeyCode() == KeyEvent.VK_ENTER)
              checkFields();
             if (evt.getKeyCode() == KeyEvent.VK_ESCAPE)
             {
                m_password = "";
                g_passwordText.setText("");
              this.dispose();
             }
            // super(evt);
       }

       private void checkFields()
       {
             m_cn = g_cnText.getText();
             m_password = new String(g_passwordText.getPassword());
             m_serverName = g_serverNameText.getText();
             if((m_cn == null) || (m_cn.equals("")))
        {
                 JOptionPane.showMessageDialog(this, "Enter the common name","Error",JOptionPane.ERROR_MESSAGE);
                 if (m_password != null)
                   g_passwordText.setText("");
                  g_cnText.requestFocus();
                    return;
             }
             else if(m_password == null) {
                   JOptionPane.showMessageDialog(this, "Enter the Password","Error", JOptionPane.ERROR_MESSAGE);
                   g_passwordText.setText("");
                   g_passwordText.requestFocus();
                   return;
             }
             else if((m_serverName == null) || (m_serverName.equals(""))) {
                  JOptionPane.showMessageDialog(this, "Enter the ServerName","Error",JOptionPane.ERROR_MESSAGE);
                  if (m_password != null)
                   g_passwordText.setText("");
                  g_serverNameText.requestFocus();
                    return;
             }
             try
             {
                        g_testUI.authenticate(m_serverName, m_cn,m_password);
             }catch (Exception e)
             {
                return;
             }
             m_password = "";
             g_passwordText.setText("");
             this.dispose();
            //setVisible(false);
             
      }

}