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

import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
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 NewFieldDialog extends JDialog implements ActionListener{

    Connection conn;
    JComboBox tables;
    JTextField fieldname,fieldtype,fielddefault;
    JButton addfield;
    
    public NewFieldDialog(Connection conn, Frame owner){
        super(owner,"Add Field to Table");
        this.conn=conn;
        
        getContentPane().setLayout(new GridBagLayout());
        GridBagConstraints c=new GridBagConstraints();
        c.gridx=0;c.gridy=0;
        c.gridwidth=1;c.gridheight=1;
        c.weightx=0.0;c.weighty=0.5;
        c.anchor=GridBagConstraints.WEST;
        c.fill=GridBagConstraints.HORIZONTAL;
        c.ipadx=5;
        
        getContentPane().add(new JLabel("Table to add field to:"),c);
        
        c.gridx=1;c.weightx=1.0;
        getContentPane().add(tables=new JComboBox(),c);
        
        c.gridx=0;c.gridy++;
        c.weightx=0.0;
        getContentPane().add(new JLabel("Name of field to add:"),c);
        
        c.gridx=1;c.weightx=1.0;
        getContentPane().add(fieldname=new JTextField(),c);
        
        c.gridx=0;c.gridy++;
        c.weightx=0.0;
        getContentPane().add(new JLabel("Type of field:"),c);

        c.gridx=1;c.weightx=1.0;
        getContentPane().add(fieldtype=new JTextField("double"),c);

        c.gridx=0;c.gridy++;
        c.weightx=0.0;
        getContentPane().add(new JLabel("Default value:"),c);

        c.gridx=1;c.weightx=1.0;
        getContentPane().add(fielddefault=new JTextField("0.0"),c);

        c.gridx=0;c.gridy++;
        c.weightx=1.0;c.gridwidth=2;
        c.weighty=1.0;
        getContentPane().add(addfield=new JButton("Add Field"),c);
        addfield.addActionListener(this);
        
        try {
            ResultSet tablesres=conn.createStatement().executeQuery("show tables");
            while(tablesres.next())
            {
                tables.addItem(tablesres.getObject(1));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        
        pack();

    }

    public void actionPerformed(ActionEvent arg0) {
        try{
            String table,name,type,def;
            table=(String)tables.getSelectedItem();
            name=fieldname.getText();
            type=fieldtype.getText();
            def=fielddefault.getText();
            
            String query="alter table "+table+" add "+name+" "+type;
            if(!def.equals(""))query+=" not null default "+def;
            
            conn.createStatement().execute(query);
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
}
