package gov.nasa.jsc.plum;

import gov.nasa.jsc.plum.PlumUtil.DatabaseWindow;
import gov.nasa.jsc.plum.PlumUtil.PlumUtilities;

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 java.util.Iterator;
import java.util.TreeMap;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;

/*
 * Created on Apr 29, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

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

    TreeMap locnames;
    int idx;
    Connection conn;
    DatabaseWindow parent;
    
    JComboBox location;
    JTextField rackname;
    JButton ok, cancel;
    
    public AddRackDialog(Connection conn,DatabaseWindow parent,Frame owner,String module) throws SQLException{
        super(owner,"Adding a rack to "+module);
    
        this.conn=conn;
        this.parent=parent;
        locnames=new TreeMap();
        
        ResultSet locs=conn.createStatement().executeQuery("select locations.name,locations.idx from locations,modules where locations.module=modules.idx and modules.acronym=\""+module+"\"");
        while(locs.next())
            locnames.put(locs.getObject("name"),locs.getObject("idx"));
        
        ResultSet rackidx=conn.createStatement().executeQuery("select max(idx) from racks");
        rackidx.next();
        idx=rackidx.getInt(1)+1;
        
        getContentPane().setLayout(new GridBagLayout());
        GridBagConstraints c=new GridBagConstraints();
        
        c.gridx=0;c.gridy=0;c.gridwidth=2;
        c.anchor=GridBagConstraints.WEST;
        c.fill=GridBagConstraints.BOTH;
        getContentPane().add(new JLabel("Adding a rack to "+module+":"),c);
        
        c.gridx=0;c.gridy=1;c.gridwidth=1;
        getContentPane().add(new JLabel("Rack Name:"),c);

        c.gridx=1;c.gridy=1;
        c.weightx=1;
        getContentPane().add(rackname=new JTextField(),c);

        c.gridx=0;c.gridy=2;
        c.weightx=0;
        getContentPane().add(new JLabel("Location:"),c);

        c.gridx=1;c.gridy=2;
        c.weightx=1;
        getContentPane().add(location=new JComboBox(),c);
        
        Iterator i=locnames.keySet().iterator();
        while(i.hasNext())
            location.addItem(i.next());
        
        c.gridx=0;c.gridy=3;
        getContentPane().add(ok=new JButton("OK"),c);
        ok.addActionListener(this);
        
        c.gridx=1;c.gridy=3;
        getContentPane().add(cancel=new JButton("Cancel"),c);
        cancel.addActionListener(this);

        pack();
    }

    /* (non-Javadoc)
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    public void actionPerformed(ActionEvent event) {
        try{
            if(event.getSource()==ok)
            {
                PlumUtilities.makeRack(conn,idx,((Integer)locnames.get(location.getSelectedItem())).intValue(),rackname.getText());
                parent.updateWindows();
            }
            
            // Whether Ok or cancel was pressed, we still dispose the dialog.
            dispose();
        } catch(SQLException e)
        {
            e.printStackTrace();
        }
    }
}
