/*
Tiamat - a role-playing game framework
AttributeListPanel.java - panel to list Attributes
 
 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.Attribute;
import com.huguesjohnson.tiamat.tools.AttributeDialog;
import com.huguesjohnson.ui.ListPanel;

/** AttributeListPanel - panel to list Attributes
 * @author  Hugues Johnson
 */
public class AttributeListPanel 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=1302420867826534793L;

	//internal class to represent items in the list
    private class AttributeListItem{
        private Attribute attribute;
        AttributeListItem(Attribute attribute){ this.attribute=attribute; }
        Attribute getAttribute(){ return(this.attribute); }
        public String toString(){ return(this.attribute.getName()+" ["+this.attribute.getCurrentValue()+"/"+this.attribute.getBaseValue()+"]"); }
    }    
    
    /** Creates a new instance of AttributeListPanel 
     */
    public AttributeListPanel(){
        super("Attributes");
    }

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

    /* end overrides */

    /** add a new Attribute 
     */
    @SuppressWarnings("serial")
	private void addAttribute(){
        AttributeDialog attributeDialog=new AttributeDialog(null){
            public void closeDialog(boolean cancel){
                if(!cancel){
                    addAttribute(getAttribute());
                }
                dispose();
            }
        };
        attributeDialog.setVisible(true);
    }

    /** add a new Attribute
     * @param attribute the attribute to add
     */
    public void addAttribute(Attribute attribute){
        this.addListItem(new AttributeListItem(attribute));
    }

    /** edit the selected Attribute 
     */    
    @SuppressWarnings("serial")
	private void editAttribute(){
        AttributeListItem listItem=(AttributeListItem)this.getSelectedListItem();
        Attribute attribute=listItem.getAttribute();
        AttributeDialog attributeDialog=new AttributeDialog(null,attribute){
            public void closeDialog(boolean cancel){
                if(!cancel){
                    updateSelectedAttribute(getAttribute());
                }
                dispose();
            }
        };
        attributeDialog.setVisible(true);        
    }
    
    /** update the selected Attribute
     * @param attribute the Attribute to replace the selected one with
     */
    public void updateSelectedAttribute(Attribute attribute){
        this.updateSelectedListItem(new AttributeListItem(attribute));
    }
    
    /** return an array of all Attributes in this panel
     * @return array of all Attributes in this panel
     */
    public Attribute[] getAllAttributes(){
        Object[] objects=this.getAllListItems();
        int length=objects.length;
        Attribute[] attributes=new Attribute[length];
        for(int index=0;index<length;index++){
            attributes[index]=((AttributeListItem)objects[index]).getAttribute();
        }
        return(attributes);
    }
    
}