/*
ListPanel.java - panel to hold a list and add/edit/delete buttons
 
 Copyright  (C) 2004 Hugues Johnson
 
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
the GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package com.huguesjohnson.ui;

import java.lang.Object;
import java.lang.String;
import javax.swing.DefaultListModel;
import javax.swing.border.TitledBorder;

/** ListPanel - panel to hold a list and add/edit/delete buttons
 * protected blocks of code were generated through the NetBeans 3.6 form editor
 * @author Hugues Johnson
 */
public class ListPanel extends javax.swing.JPanel{
	/** Determines if a de-serialized file is compatible with this class. See <a href="http://java.sun.com/j2se/1.5.0/docs/guide/serialization/spec/class.html">Java Object Serialization Specifications</a>. */
	private static final long serialVersionUID=-8321690132238005542L;
	/** Creates new form ListPanel
     * @param title the title of the panel
     */
    public ListPanel(String title){
        //call the initComponents method created by NetBeans
        initComponents();
        //set the border & title
        TitledBorder border=(TitledBorder)this.getBorder();
        border.setTitle(title);
        //set the list model
        this.list.setModel(new DefaultListModel());
        //disable edit and delete buttons
        this.buttonDelete.setEnabled(false);
        this.buttonEdit.setEnabled(false);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
    	this.listPanel = new javax.swing.JPanel();
    	this.scrollPaneList = new javax.swing.JScrollPane();
    	this.list = new javax.swing.JList();
    	this.panelButtons = new javax.swing.JPanel();
    	this.buttonAdd = new javax.swing.JButton();
    	this.buttonEdit = new javax.swing.JButton();
    	this.buttonDelete = new javax.swing.JButton();

        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.Y_AXIS));

        setBorder(new javax.swing.border.TitledBorder(""));
        this.listPanel.setLayout(new javax.swing.BoxLayout(this.listPanel, javax.swing.BoxLayout.X_AXIS));

        this.list.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        this.list.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listValueChanged(evt);
            }
        });

        this.scrollPaneList.setViewportView(this.list);

        this.listPanel.add(this.scrollPaneList);

        this.add(this.listPanel);

        this.panelButtons.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        this.buttonAdd.setText("Add");
        this.buttonAdd.setToolTipText("Add item to the list");
        this.buttonAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonAddActionPerformed(evt);
            }
        });

        this.panelButtons.add(this.buttonAdd);

        this.buttonEdit.setText("Edit");
        this.buttonEdit.setToolTipText("Edit the seleted item");
        this.buttonEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonEditActionPerformed(evt);
            }
        });

        this.panelButtons.add(this.buttonEdit);

        this.buttonDelete.setText("Delete");
        this.buttonDelete.setToolTipText("Delete the selected item");
        this.buttonDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonDeleteActionPerformed(evt);
            }
        });

        this.panelButtons.add(this.buttonDelete);

        this.add(this.panelButtons);

    }//GEN-END:initComponents

    void buttonDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonDeleteActionPerformed
        this.deleteAction();
    }//GEN-LAST:event_buttonDeleteActionPerformed

    void buttonEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonEditActionPerformed
        this.editAction();
    }//GEN-LAST:event_buttonEditActionPerformed

    void buttonAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAddActionPerformed
        this.addAction();
    }//GEN-LAST:event_buttonAddActionPerformed

    void listValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listValueChanged
        if(!this.buttonEdit.isEnabled()){ this.buttonEdit.setEnabled(true); }
        if(!this.buttonDelete.isEnabled()){ this.buttonDelete.setEnabled(true); }
    }//GEN-LAST:event_listValueChanged
    
    /* begin stuff to override */

    /** the action that fires with the add button is clicked
     */
    public void addAction(){ }
    
    /** the action that fires with the edit button is clicked
     */
    public void editAction(){ }
    
    /* end stuff to override */

    /* begin protected  methods */
    
    /** deletes the selected item
     */
    protected void deleteAction(){ 
        //get the selected item
        DefaultListModel model=(DefaultListModel)this.list.getModel();
        int selectedIndex=this.list.getSelectedIndex();
        model.remove(selectedIndex);
        //if no items left disable the edit and delete buttons
        if(model.size()<1){
            this.buttonEdit.setEnabled(false); 
            this.buttonDelete.setEnabled(false);
        } else{ //select the previous item
            if(selectedIndex>0){
                this.list.setSelectedIndex(selectedIndex-1);
            } else{
                this.list.setSelectedIndex(0);
            }
        }    
    }
    
    /** add an item to the list
     * @param listItem the item to add
     */
    protected void addListItem(Object listItem){
        DefaultListModel model=(DefaultListModel)this.list.getModel();
        model.addElement(listItem);    
    }
    
    /** returns the selected item
     * @return the selected item
     */
    protected Object getSelectedListItem(){
        DefaultListModel model=(DefaultListModel)this.list.getModel();
        int selectedIndex=this.list.getSelectedIndex();    
        return(model.get(selectedIndex));
    }
    
    /** update the selected item in the list
     * @param listItem the item replace the selected item with
     */
    protected void updateSelectedListItem(Object listItem){
        DefaultListModel model=(DefaultListModel)this.list.getModel();
        int selectedIndex=this.list.getSelectedIndex();
        model.set(selectedIndex,listItem);    
    }
    
    /** returns an array of all items in the panel
     * @return an array of all items in the panel, new Object[0] if the list is empty
     */
    protected Object[] getAllListItems(){
        DefaultListModel model=(DefaultListModel)this.list.getModel();
        int count=model.size();
        if(count<1){
            return(new Object[0]);
        }
		Object[] objects=new Object[count];
		for(int index=0;index<count;index++){
		    objects[index]=model.get(index);
		}    
		return(objects);
    }

    /* end protected methods */

    /** returns number of items in the panel
     * @return number of items in the panel
     */
    public int getCount(){
        DefaultListModel model=(DefaultListModel)this.list.getModel();
        return(model.size());
    }
    
    /** clear the panel
     */
    public void clear(){
        this.list.setModel(new DefaultListModel());
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonAdd;
    private javax.swing.JButton buttonDelete;
    private javax.swing.JButton buttonEdit;
    private javax.swing.JList list;
    private javax.swing.JPanel listPanel;
    private javax.swing.JPanel panelButtons;
    private javax.swing.JScrollPane scrollPaneList;
    // End of variables declaration//GEN-END:variables
}