package de.javasoft.synthetica.democenter.examples.logindialog;

import java.awt.EventQueue;
import java.util.HashMap;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;

import org.jdesktop.swingx.JXLoginDialog;
import org.jdesktop.swingx.auth.LoginService;
import org.jdesktop.swingx.auth.PasswordStore;
import org.jdesktop.swingx.auth.UserNameStore;

/**
 * A simple LoginDialog application used to demonstrate and explain  
 * basic functionality.
 */
@SuppressWarnings("serial")
public class SimpleLoginDialog extends JFrame
{
//@highlight
  //For simple userNameStore and passwordStore implemtation we use a map.
  private static HashMap<String, char[]> users = new HashMap<String, char[]>();
  //Put some values into the user map - first name = username, last name = password. 
  static 
  {
    users.put("Adam", "Sandler".toCharArray());
    users.put("Tom", "Hanks".toCharArray());
    users.put("Will", "Smith".toCharArray());
  }
  private boolean authenticated = false;
//@/highlight
  
  /**
   * Constructor for the main application.  
   */
  public SimpleLoginDialog()
  {
    super("Simple LoginDialog Demo");    
//@highlight
    login();
    if (!authenticated)
      return;
//@/highlight
    add(new JLabel("Main Application"));
    //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setSize(640, 480);
    setLocationRelativeTo(null);        //center
    setVisible(true);    
  }

  /**
   * Display login dialog and set authenticated class member variable.  
   */
  private void login()
  {
//@highlight
    //Simple LoginService implementation
    LoginService ls = new LoginService()
//@/highlight
    {
      @Override
      public boolean authenticate(String name, char[] password, String server) throws Exception
      {
        if (users.get(name) != null && java.util.Arrays.equals(password, users.get(name)))
        {            
          authenticated = true;
        }
        return authenticated;
      }
    };
    
//@highlight
    //PasswordStore implementation
    PasswordStore ps = new PasswordStore()
//@/highlight
    {
      @Override
      public boolean set(String username, String server, char[] password)
      {
        users.put(username, password);
        return true;
      }

      @Override
      public char[] get(String username, String server)
      {
        return users.get(username);
      }

      @Override
      public void removeUserPassword(String username)
      {
      }
    };
    
//@highlight
    //UserNameStore implementation
    UserNameStore us = new UserNameStore()
//@/highlight
    {
      @Override
      public String[] getUserNames()
      {        
        return users.keySet().toArray(new String[]{});
      }

      @Override
      public void setUserNames(String[] names)
      {
      }

      @Override
      public void loadUserNames()
      {
      }

      @Override
      public void saveUserNames()
      {
      }

      @Override
      public boolean containsUserName(String name)
      {
        return users.containsKey(name);
      }

      @Override
      public void addUserName(String userName)
      {
      }

      @Override
      public void removeUserName(String userName)
      {
      }
    };
      
//@highlight
    JXLoginDialog dialog = new JXLoginDialog(ls, ps, us);
    dialog.setModal(true);
    dialog.setVisible(true);      
//@/highlight
  }

  /**
   * Static main method for application startup. 
   */
  public static void main(String[] args)
  {
    EventQueue.invokeLater(new Runnable()
    {
      public void run()
      {
        try
        {
          UIManager.setLookAndFeel("de.javasoft.plaf.synthetica.SyntheticaStandardLookAndFeel");
          new SimpleLoginDialog();
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }
    });
  }
}
