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

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
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 java.sql.Statement;
import java.util.TreeMap;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;

/**
 * @author randrews
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class SumsWindow extends JFrame implements ActionListener{
    private DatabaseWindow parent;
    private Connection conn;
    private String name;
    private TreeMap racks,triggers;
    private RowPanel panel;
    private JButton setcolor;
    private Statement stmt;
    
    public SumsWindow(DatabaseWindow parent, Connection conn, String name)
    throws HeadlessException {
        super(name);
        this.parent = parent;
        this.conn = conn;
        this.name = name;
        
        try {
            stmt=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);
            System.out.println(name);
            ResultSet res=stmt.executeQuery("select * from sums where name=\""+name+"\"");
            res.next();
//            res.updateObject("name","cross");
            panel=new RowPanel(res,conn,"sums","name");
        
            
            getContentPane().setLayout(new GridBagLayout());
            GridBagConstraints c=new GridBagConstraints();
            
            c.gridx=0;c.gridy=0;
            c.weightx=1;c.weighty=1;
            getContentPane().add(panel,c);
            
            c.gridy++;c.fill=GridBagConstraints.HORIZONTAL;
            getContentPane().add(setcolor=new JButton("Set color"),c);
            setcolor.addActionListener(this);
            
            pack();
            
//            stmt=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);
//            ResultSet res=stmt.executeQuery("select * from dumb where name=\""+"Fred"+"\"");
//            res.next();
//            System.out.println(res.getString("name"));
//            res.updateString("name","Bill");
//            res.updateRow();
//            System.out.println(res.getString("name"));
            
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
    }

    public void actionPerformed(ActionEvent arg0) {
        double red=((Double)panel.get("red")).doubleValue();
        double green=((Double)panel.get("green")).doubleValue();
        double blue=((Double)panel.get("blue")).doubleValue();
        
        Color old=new Color((float)red,(float)green,(float)blue);
        Color col=JColorChooser.showDialog(this,"Color for line",old);
        if(col==null)return;
        panel.set("red",new Double((double)(col.getRed())/255.0));
        panel.set("green",new Double((double)(col.getGreen())/255.0));
        panel.set("blue",new Double((double)(col.getBlue())/255.0));
    }
}
