
package jxtray.swing;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import java.awt.BorderLayout;

import java.awt.Event;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.AdjustmentEvent;

import jxtray.util.UIUtilities;

public class PasswordBox extends JDialog
{
	public static final int OK_OPTION = 1;
	public static final int CANCEL_OPTION = 2;
	private JPasswordField passwd = null;
	private int code_ret = 0;
	
	public PasswordBox(JFrame parent,String title, String message)
	{
		super(parent,title,true);
		this.getContentPane().setLayout(new BorderLayout() );
		
		this.getContentPane().add(new JLabel(message),BorderLayout.NORTH);
		
		passwd = new JPasswordField();
		this.getContentPane().add(passwd,BorderLayout.CENTER);
		
		JxtButton bOk = new JxtButton("Ok");
		bOk.addActionListener ( new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				code_ret = OK_OPTION;
				setVisible(false);
			}
		  }
		);
		
		JxtButton bCancel = new JxtButton("Cancel");
		bCancel.addActionListener ( new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				code_ret = CANCEL_OPTION;
				setVisible(false);
			}
		  }
		);
		
		JPanel pButton = new JPanel();
		pButton.add(bOk); 
		pButton.add(bCancel);
		
		this.getContentPane().add(pButton,BorderLayout.SOUTH);
		this.pack();
	}
	
	public int getCodeRet()
	{
		return code_ret;
	}
	
	public String getPassword()
	{
		return new String(passwd.getPassword());
	}
	
	public static String showPasswordBox(JFrame parent,String title, String message)
	{
		PasswordBox b = new PasswordBox(parent,title,message);
		UIUtilities.center(b);
		b.setVisible(true);
		if ( b.getCodeRet() == OK_OPTION)
			return b.getPassword();
		else 
			return null;
	}
	

	
	
}
