/*
 * Created on Feb 7, 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 gov.nasa.jsc.plum.PlumUtil.DatabaseWindow;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Iterator;
import java.util.TreeMap;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;


/**
 * @author randrews
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class RunConstraintWindow extends JFrame implements ActionListener{

    private DatabaseWindow parent;
    private Connection conn;
    private int run;
    private TreeMap constraints;
    
    /**
     * @param parent
     * @param conn
     * @param run
     * @throws java.awt.HeadlessException
     */
    public RunConstraintWindow(DatabaseWindow parent, Connection conn, int run)
            throws HeadlessException {
        super("Select constraints");
        this.parent = parent;
        this.conn = conn;
        this.run = run;
        
        JButton save=new JButton("Save Changes");
        save.addActionListener(this);
        
        JPanel names=new JPanel();
        JPanel checks=new JPanel();
        
        try{
            constraints=new TreeMap();
            ResultSet cons=conn.createStatement().executeQuery("select distinct name from constraints");
            while(cons.next())
                constraints.put(cons.getString("name"),new JCheckBox());

            ResultSet runcons=conn.createStatement().executeQuery("select cons from runcons where run="+run);
            while(runcons.next())
                ((JCheckBox) constraints.get(runcons.getString("cons"))).setSelected(true);
            
            int numcons=constraints.size();
            names.setLayout(new GridLayout(numcons,1));
            checks.setLayout(new GridLayout(numcons,1));
            
            Iterator i=constraints.keySet().iterator();
            while(i.hasNext())
            {
                String name=(String) i.next();
                names.add(new JLabel(name));
                checks.add((JCheckBox)constraints.get(name));
            }
        } catch(Exception e){
            e.printStackTrace();
        }
        
        JPanel panel=(JPanel) getContentPane();
        panel.setLayout(new BorderLayout());
        
        JPanel controls=new JPanel();
        controls.setLayout(new BorderLayout());
        
        controls.add(names,BorderLayout.WEST);
        controls.add(checks,BorderLayout.EAST);
        
        panel.add(new JScrollPane(controls),BorderLayout.CENTER);
        panel.add(save,BorderLayout.SOUTH);
        pack();
    }
    
    /* (non-Javadoc)
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    public void actionPerformed(ActionEvent arg0) {
        try{
            conn.createStatement().execute("delete from runcons where run="+run);
            Iterator i=constraints.keySet().iterator();
            while(i.hasNext())
            {
                String cons=(String) i.next();
                JCheckBox cb=(JCheckBox) constraints.get(cons);
                if(cb.isSelected()==false)continue;
                conn.createStatement().execute("insert into runcons (run,cons) values ("+run+",\""+cons+"\")");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        
    }
}
