/*
Tiamat - a role-playing game framework
SkillListPanel.java - panel to list Skills
 
 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 java.lang.Object;
import java.lang.String;
import com.huguesjohnson.tiamat.Skill;
import com.huguesjohnson.tiamat.tools.SkillDialog;
import com.huguesjohnson.ui.ListPanel;

/** SkillListPanel - panel to list Skills
 * @author Hugues Johnson
 */
public class SkillListPanel 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=-916786113339787280L;

	//internal class to represent items in the list
    private class SkillListItem{
        private Skill skill;
        SkillListItem(Skill skill){ this.skill=skill; }
        Skill getSkill(){ return(this.skill); }
        public String toString(){ return(this.skill.getName()); }
    }    
    
    /** Creates a new instance of SkillListPanel
     */
    public SkillListPanel(){
        super("Skills");
    }

    /* begin overrides */
    
    /** the event that fires when the add button is clicked
     */
    public void addAction(){ this.addSkill(); }
    
    /** the event that fires when the edit button is clicked
     */
    public void editAction(){ this.editSkill(); }    

    /* end overrides */

    /** add a new Skill 
     */
    @SuppressWarnings("serial")
	private void addSkill(){
        SkillDialog skillDialog=new SkillDialog(null){
            public void closeDialog(boolean cancel){
                if(!cancel){
                    addSkill(getSkill());
                }
                dispose();
            }
        };
        skillDialog.setVisible(true);
    }
    
    /** add a new Skill 
     * @param skill the Skill to add
     */
    public void addSkill(Skill skill){
        this.addListItem(new SkillListItem(skill));
    }

    /** edit the selected Skill 
     */    
    @SuppressWarnings("serial")
	private void editSkill(){
        SkillListItem listItem=(SkillListItem)this.getSelectedListItem();
        Skill skill=listItem.getSkill();
        SkillDialog skillDialog=new SkillDialog(null,skill){
            public void closeDialog(boolean cancel){
                if(!cancel){
                    updateSelectedSkill(getSkill());
                }
                dispose();
            }
        };
        skillDialog.setVisible(true);        
    }
    
    /** update the selected Skill 
     * @param skill the Skill to replace the selected Skill with
     */    
    public void updateSelectedSkill(Skill skill){
        this.updateSelectedListItem(new SkillListItem(skill));
    }
    
    /** returns an array of all Skills in the panel
     * @return an array of all Skills in the panel
     */
    public Skill[] getAllSkills(){
        Object[] objects=this.getAllListItems();
        int length=objects.length;
        Skill[] skills=new Skill[length];
        for(int index=0;index<length;index++){
            skills[index]=((SkillListItem)objects[index]).getSkill();
        }
        return(skills);
    }
    
}