/*
 * Created on Feb 3, 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.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

/**
 * @author randrews
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public abstract class ListPanel extends JPanel implements ActionListener, Updateable{
    private JList list;
    protected DefaultListModel model;
    private JButton newbtn;
    private JButton deletebtn;
    protected DatabaseWindow parent;


    public ListPanel(DatabaseWindow parent){
        this.parent=parent;
        parent.addWindow(this);
        list=new JList();
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        model=new DefaultListModel();
        list.setModel(model);

        MouseListener ml = new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                if(e.getClickCount() == 2)
                    handleSelection(list.getSelectedValue());
            }
        };
        list.addMouseListener(ml);
        
        newbtn=new JButton("New");
        newbtn.addActionListener(this);
        deletebtn=new JButton("Delete");
        deletebtn.addActionListener(this);
        
        JPanel btnpanel=new JPanel();
        btnpanel.setLayout(new GridLayout(1,2));
        
        btnpanel.add(newbtn);
        btnpanel.add(deletebtn);
        
        setLayout(new BorderLayout());
        add(new JScrollPane(list),BorderLayout.CENTER);
        add(btnpanel,BorderLayout.SOUTH);
    }
    
    public void fillModel(Vector listvalues){
        model.clear();
        Iterator i=listvalues.iterator();
        while(i.hasNext())
            model.addElement(i.next());
    }

    public void actionPerformed(ActionEvent e){
        if(e.getSource()==newbtn)
            newElement();
        else if(e.getSource()==deletebtn)
            deleteElement(list.getSelectedValue());
    }
    /**
     * @param selectedValue the thing that was selected. Could be null.
     */
    public void deleteElement(Object selectedValue){
        model.removeElement(selectedValue);
    }

    /**
     * Called when the "new" button is pressed 
     */
    public void newElement(){
        
    }

    /**
     * @param selected the thing that was just clicked on in the list
     */
    abstract public void handleSelection(Object selected);
    
    /**
     * @return Returns the list.
     */
    public JList getList() {
        return list;
    }
    /**
     * @return Returns the model.
     */
    public DefaultListModel getModel() {
        return model;
    }
    
    public void updateWindow(){
    
    }
    
    public void finalize(){
        parent.removeWindow(this);
    }
}
