/*
Tiamat - a role-playing game framework
ItemListPanel.java - panel to list Items
 
 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.tiamat.tools;

import com.huguesjohnson.tiamat.Item;
import com.huguesjohnson.ui.ListPanel;
import java.io.File;

/** ItemListPanel - panel to list Items
 * @author Hugues Johnson
 */
public class ItemListPanel extends ListPanel{

	/** 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=-586558962969644986L;
	
	//absolute path to the directory where images are stored
    private transient String absoluteDir;
    public void setAbsoluteDir(String absoluteDir){
        if(absoluteDir.endsWith(File.separator)){
            this.absoluteDir=absoluteDir;
        } else{
            this.absoluteDir=absoluteDir+File.separator;
        }
        Item[] allitems=this.getAllItems();
        if(allitems!=null){
            int length=allitems.length;
            for(int index=0;index<length;index++){
                allitems[index].setAbsoluteDir(absoluteDir);
            }
        }
    }
    
    //internal class to represent items in the list
    private class ItemListItem{
        private Item item;
        ItemListItem(Item item){ this.item=item; }
        Item getItem(){ return(this.item); }
        public String toString(){ return(this.item.getName()); }
    }    
    
    /** Creates a new instance of ItemListPanel 
     */
    public ItemListPanel(String absoluteDir){
        super("Items");
        this.absoluteDir=absoluteDir;
    }

    /* begin overrides */
    
    /** action that fires when add button is clicked
     */
    public void addAction(){ this.addItem(); }
    
    /** action that fires when edit button is clicked
     */
    public void editAction(){ this.editItem(); }    

    /* end overrides */

    /** add a new Item 
     */
    @SuppressWarnings("serial")
	private void addItem(){
        ItemDialog itemDialog=new ItemDialog(null,this.absoluteDir){
            public void closeDialog(boolean cancel){
                if(!cancel){
                    addItem(getItem());
                }
                dispose();
            }
        };
        itemDialog.setVisible(true);
    }
    
    /** add a new Item
     * @param item the Item to add
     */
    public void addItem(Item item){
        this.addListItem(new ItemListItem(item));
    }

    /** edit the selected Item 
     */    
    @SuppressWarnings("serial")
	private void editItem(){
        ItemListItem listItem=(ItemListItem)this.getSelectedListItem();
        Item item=listItem.getItem();
        ItemDialog itemDialog=new ItemDialog(null,item,this.absoluteDir){
            public void closeDialog(boolean cancel){
                if(!cancel){
                    updateSelectedItem(getItem());
                }
                dispose();
            }
        };
        itemDialog.setVisible(true);        
    }

    /** update the selected Item
     * @param item the Item to replace the selected item with
     */
    public void updateSelectedItem(Item item){
        this.updateSelectedListItem(new ItemListItem(item));
    }
    
    /** returns an array of all Items in this panel
     * @return array of all Items in this panel, if the list is empty it will return a zero-sized array
     */
    public Item[] getAllItems(){
        Object[] objects=this.getAllListItems();
        int length=objects.length;
        Item[] items=new Item[length];
        for(int index=0;index<length;index++){
            items[index]=((ItemListItem)objects[index]).getItem();
        }
        return(items);
    }
    
}