/* $Id: EnsemblConnectionDialog.jav,v 1.1 2007/03/27 16:03:17 fedor Exp $ */
package biouml.modules.ensembl;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;

import org.ensembl.driver.AdaptorException;

import biouml.modules.ensembl.access.EnsemblConnector;

import ru.biosoft.util.OkCancelDialog;

/**
 *
 */
public class EnsemblConnectionDialog extends JDialog
{	
	protected static MessageBundle messageBundle = new MessageBundle();
	
	protected EnsemblConnectionDialog this_;
	
	protected EnsemblConnector connector = null;
	
	private   Component parent = null;
    	
	public EnsemblConnectionDialog(JFrame frame)
	{
		super(frame, messageBundle.getResourceString("ENSEMBL_CONNECTION_TITLE"), true);
		this_ = this;
		parent = frame;
		init(null, 0, null, true, null, null);
    }
	
	public EnsemblConnectionDialog(JFrame frame,String host, int port, String database, boolean dbPrefix, String user, String password)
	{
		super(frame, messageBundle.getResourceString("ENSEMBL_CONNECTION_TITLE"), true);
		this_ = this;
		parent = frame;
		init(host, port, database, dbPrefix, user, password);
    }
	
	public EnsemblConnectionDialog(JDialog frame)
	{
		super(frame, messageBundle.getResourceString("ENSEMBL_CONNECTION_TITLE"), true);
		this_ = this;
		parent = frame;
		init(null, 0, null, true, null, null);
    }
	
	public EnsemblConnectionDialog(JDialog frame,String host, int port, String database, boolean dbPrefix, String user, String password)
	{
		super(frame, messageBundle.getResourceString("ENSEMBL_CONNECTION_TITLE"), true);
		this_ = this;
		parent = frame;
		init(host, port, database, dbPrefix, user, password);
    }
	
	public EnsemblConnector getConnector()
	{
		return connector;
	}
	
	protected void connect(String host, int port, String database, boolean dbPrefix, String user, String password) throws AdaptorException
	{		
		connector = new EnsemblConnector(host, port, database, dbPrefix, user, password);
	}
	
	protected void cancel()
	{
		this_.hide();
	}
	
	protected void init(String host, int port, String database, boolean dbPrefix, String user, String password)
	{
		JPanel panel = new JPanel();
		panel.setBorder(new EmptyBorder(3, 5, 3, 5));
		panel.setLayout(new BorderLayout());
		
		panel.add(new EnsemblConnectionPanel(host, port, database, dbPrefix, user, password)
			{
				protected void connect() 
				{
					try
					{
						if (usePrefix)
							this_.connect(hostField.getText(), Integer.parseInt(portField.getText()), database_prefixField.getText(), true, userField.getText(), passwordField.getText());
						else
							this_.connect(hostField.getText(), Integer.parseInt(portField.getText()), databaseField.getText(), false, userField.getText(), passwordField.getText());
						cancel();
					}
					catch (Throwable t)
					{
						status.setText(messageBundle.getResourceString("ENSEMBL_CONNECTION_FAILED"));
					}
				}
				protected void cancel() 
				{
					this_.cancel();
				}
			}, BorderLayout.CENTER);
		
		getContentPane().add(panel);
		pack();
    	setResizable(false);
    	
    	addKeyListener(new KeyAdapter()
	        {
	            public void keyPressed(KeyEvent ke)
	            {
	                if (ke.getKeyCode() == ke.VK_ESCAPE)
	                    cancel();
	            }
	        });
	}
	
	public void doModal()
    {
		pack();
		if (parent != null)
			setLocationRelativeTo(parent);
        show();
        dispose();       
    }
	
}