/*
 * Created on Jan 28, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package gov.nasa.jsc.plum.PlumUtil;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

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

    Connection conn;
    JButton addcomment,deletecomment;
    JList commentlist;
    JTextField author;
    JTextArea comment;
    Comment current;
    String tblname,index,value;
    
    public CommentWindow(Connection conn,String tblname,String index,String value){
        super("Comments");
        this.conn=conn;
        this.tblname=tblname;
        this.index=index;
        this.value=value;
        current=null;
        
        addcomment=new JButton("Add Comment");
        addcomment.addActionListener(this);
        
        deletecomment=new JButton("Delete");
        deletecomment.addActionListener(this);
        
        commentlist=new JList();
        commentlist.setFixedCellWidth(150);
        DefaultListModel model=new DefaultListModel();
        commentlist.setModel(model);
        commentlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        commentlist.addListSelectionListener(this);
        try{
            
            ResultSet res=conn.createStatement().executeQuery("select idx from comments where "+
                    "tblname=\""+tblname+"\""+
                    " and indexfield=\""+index+"\""+
                    " and value=\""+value+"\"");
            while(res.next())
            {
                Statement stmt=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);
                ResultSet cr=stmt.executeQuery("select * from comments where idx="+res.getInt("idx"));
                cr.next();
                Comment c=new Comment(cr);
                model.addElement(c);
            }
        } catch(Exception e){
            e.printStackTrace();
        }
        
        author=new JTextField();
        author.setColumns(15);
        JPanel authorpanel=new JPanel();
        authorpanel.add(new JLabel("Author:"));
        authorpanel.add(author);
        
        comment=new JTextArea();
        comment.setRows(10);
        comment.setColumns(25);
        
        JPanel buttonpanel=new JPanel();
        buttonpanel.add(addcomment);
        buttonpanel.add(deletecomment);

        JPanel textpanel=new JPanel();
        textpanel.setLayout(new BorderLayout());
        
        textpanel.add(buttonpanel,BorderLayout.SOUTH);
        textpanel.add(authorpanel,BorderLayout.NORTH);
        textpanel.add(new JScrollPane(comment),BorderLayout.CENTER);
        
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(new JScrollPane(commentlist),BorderLayout.WEST);
        getContentPane().add(textpanel,BorderLayout.EAST);
        addWindowListener(this);
        pack();
    }
    
    
    
    
    
    
    
    
    public class Comment{
        private ResultSet res;
        public Comment(ResultSet res){
            this.res=res;
        }
        public String toString(){return getAuthor();}
        public String getAuthor(){
            try {
                return res.getString("author");
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return null;
        }
        public String getComment(){
            try {
                return res.getString("comment");
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return null;
        }
        
        public int getIdx(){
            try {
                return res.getInt("idx");
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return 0;
        }
        public void save() throws Exception{
            res.updateString("author",author.getText());
            res.updateString("comment",comment.getText());
            res.updateRow();
        }
    }
    
    
    
    
    
    
    
    
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==addcomment)
        {
            if(author.getText().equals("") || comment.getText().equals(""))return;
            Statement stmt;
            try {
                stmt=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);
                stmt.execute("insert into comments (tblname,indexfield,value) values (\""+
                        tblname+"\","+
                        "\""+index+"\","+
                        "\""+value+"\""+
                ")");
                ResultSet res=stmt.executeQuery("select * from comments where idx=last_insert_id()");
                res.next();
                Comment c=new Comment(res);
                c.save();
                ((DefaultListModel)commentlist.getModel()).addElement(c);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
        else if(e.getSource()==deletecomment)
        {
            try{
                Comment c=(Comment) commentlist.getSelectedValue();
                if(c==null)return;
                int selectedidx=commentlist.getSelectedIndex();
                DefaultListModel model=(DefaultListModel)commentlist.getModel();
                model.remove(selectedidx);
                conn.createStatement().execute("delete from comments where idx="+c.getIdx());
                current=null;
                int size=model.getSize();
                if(selectedidx>=size)
                    commentlist.setSelectedIndex(size-1);
                else
                    commentlist.setSelectedIndex(selectedidx);
            } catch(Exception e1) {
                e1.printStackTrace();
            }
        }
    }

    public void valueChanged(ListSelectionEvent arg0) {
        try{
            if(current!=null)
                current.save();
            Comment c=(Comment) commentlist.getSelectedValue();
            if(c==null)return;
            current=c;
            author.setText(c.getAuthor());
            comment.setText(c.getComment());
        } catch(Exception e){
            e.printStackTrace();
        }
    }

    public void windowClosing(WindowEvent arg0) {
        try{
            if(current!=null)
                current.save();
        } catch(Exception e){
            e.printStackTrace();
        }
    }

    public void windowClosed(WindowEvent arg0) {}
    public void windowOpened(WindowEvent arg0) {}
    public void windowIconified(WindowEvent arg0) {}
    public void windowDeiconified(WindowEvent arg0) {}
    public void windowActivated(WindowEvent arg0) {}
    public void windowDeactivated(WindowEvent arg0) {}
}
