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

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.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

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

    private Connection conn;
    private DatabaseWindow parent;
    private ResultSet run;
    private RowPanel panel;
    
    private JButton runbtn,commentbtn,statusbtn,constraintbtn;
    
    /**
     * @param conn
     * @param parent
     * @throws java.awt.HeadlessException
     */
    public RunWindow(Connection conn, DatabaseWindow parent,ResultSet run)
            throws HeadlessException,SQLException {
        super(run.getString("name"));
        this.conn = conn;
        this.parent = parent;
        this.run=run;
        
        panel=new RowPanel(run,conn,"runs");
        panel.setParent(parent);
        
        runbtn=new JButton("Run");
        runbtn.addActionListener(this);
        
        commentbtn=new JButton("Comments");
        commentbtn.addActionListener(this);
        
        statusbtn=new JButton("Selection Status");
        statusbtn.addActionListener(this);
        
        constraintbtn=new JButton("Constraints");
        constraintbtn.addActionListener(this);
        
        JPanel cp=(JPanel) getContentPane();
        cp.setLayout(new BorderLayout());
        
        JPanel btnpanel=new JPanel();
        btnpanel.setLayout(new GridLayout(4,1));
        
        btnpanel.add(runbtn);
        btnpanel.add(commentbtn);
        btnpanel.add(statusbtn);
        btnpanel.add(constraintbtn);

        cp.add(panel,BorderLayout.CENTER);
        cp.add(btnpanel,BorderLayout.SOUTH);
        pack();
    }

    /* (non-Javadoc)
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    public void actionPerformed(ActionEvent arg0) {
        try{
            if(arg0.getSource()==statusbtn)
            {
                SelectionWindow window=new SelectionWindow(parent,conn,run.getInt("idx"));
                window.setVisible(true);
            }
            else if(arg0.getSource()==constraintbtn)
            {
                RunConstraintWindow window=new RunConstraintWindow(parent,conn,run.getInt("idx"));
                window.setVisible(true);
            }
            else if(arg0.getSource()==commentbtn)
            {
                CommentWindow window=new CommentWindow(conn,"runs","idx",run.getObject("idx").toString());
                window.setVisible(true);
            }
            else if(arg0.getSource()==runbtn)
            {
                ProgressWindow window=new ProgressWindow(parent,conn,parent.getCfg(),run);
                window.setVisible(true);
            }
        } catch(Exception e){
            e.printStackTrace();
        }
    }
    

}
