/*
 * Created on Jan 27, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package gov.nasa.jsc.plum.PlumUtil;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

/**
 * @author randrews
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class ConnectPanel extends JFrame implements ActionListener{
    JTextField host,db,user;
    JPasswordField pass;
    JCheckBox debug;
    DatabaseConfiguration cfg;
    String menuclass;
    String dbtitle;
    
    public ConnectPanel(String menuclass,String title,String dbtitle,Object mainclass){
        super(title);
        this.dbtitle=dbtitle;
        this.menuclass=menuclass;
        JPanel panel=(JPanel) getContentPane();
        panel.setLayout(new BorderLayout());
        JPanel fields=new JPanel();
        JButton connect=new JButton("Connect");
        connect.addActionListener(this);
        getRootPane().setDefaultButton(connect);
        panel.add(fields,BorderLayout.CENTER);
        panel.add(connect,BorderLayout.SOUTH);
        fields.setLayout(new GridLayout(5,2));
        cfg=new DatabaseConfiguration(mainclass.getClass());
        
        fields.add(new JLabel("Server address:"));
        fields.add(host=new JTextField(cfg.getHost()));
        fields.add(new JLabel("Database name:"));
        fields.add(db=new JTextField(cfg.getDB()));
        fields.add(new JLabel("User name:"));
        fields.add(user=new JTextField(cfg.getUser()));
        fields.add(new JLabel("Password:"));
        fields.add(pass=new JPasswordField(cfg.getPass()));

        fields.add(new JLabel("Debug output to window: "));
        fields.add(debug=new JCheckBox());
        debug.setSelected(cfg.getDebug());
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
    }

    public void actionPerformed(ActionEvent arg0) {
        try{
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection conn = DriverManager.getConnection("jdbc:mysql://"
                    +host.getText() // server
                    +"/"+db.getText() // db
                    +"?user="+user.getText() // user
                    +"&password="+new String(pass.getPassword()) // password
            );
            cfg.setHost(host.getText());
            cfg.setUser(user.getText());
            cfg.setDB(db.getText());
            cfg.setPass(new String(pass.getPassword()));
            cfg.setDebug(debug.isSelected());
            DatabaseWindow window=new DatabaseWindow(conn,menuclass,debug.isSelected(),dbtitle);
            window.setVisible(true);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}
