/*
Tiamat - a role-playing game framework
CharacterListPanel.java - panel to list Characters
 
 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.tools.CharacterDialog;
import com.huguesjohnson.ui.ListPanel;

/** CharacterListPanel - panel to list Characters
 * @author Hugues Johnson
 */
public class CharacterListPanel 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=1924832097363065259L;

	private class CharacterListItem{
        private String characterFilePath;
        CharacterListItem(String filePath){ this.characterFilePath=filePath; }
        String getFilePath(){ return(this.characterFilePath); }
        public String toString(){ return(this.characterFilePath); }
    }    
    
    /** Creates a new instance of CharacterListPanel */
    public CharacterListPanel(){
        super("Characters");
    }

    /* begin overrides */
    public void addAction(){ this.addCharacter(); }
    
    public void editAction(){ this.editCharacter(); }    

    /* end overrides */

    /** add a new Character */
    @SuppressWarnings("serial")
	private void addCharacter(){
        CharacterDialog characterDialog=new CharacterDialog(null){
            public void closeDialog(boolean cancel){
                if(!cancel){
                    addCharacter(getCharacterPath());
                }
                dispose();
            }
        };
        characterDialog.setVisible(true);
    }
    
    public void addCharacter(String characterFilePath){
        this.addListItem(new CharacterListItem(characterFilePath));
    }

    /** edit the selected Character */    
    @SuppressWarnings("serial")
	private void editCharacter(){
        CharacterListItem listItem=(CharacterListItem)this.getSelectedListItem();
        String characterFilePath=listItem.getFilePath();
        CharacterDialog characterDialog=new CharacterDialog(null,characterFilePath){
            public void closeDialog(boolean cancel){
                if(!cancel){
                    updateSelectedCharacter(getCharacterPath());
                }
                dispose();
            }
        };
        characterDialog.setVisible(true);        
    }
    
    public void updateSelectedCharacter(String characterFilePath){
        this.updateSelectedListItem(new CharacterListItem(characterFilePath));
    }
    
    public String[] getAllCharacterPaths(){
        Object[] objects=this.getAllListItems();
        int length=objects.length;
        String[] characterPaths=new String[length];
        for(int index=0;index<length;index++){
            characterPaths[index]=((CharacterListItem)objects[index]).getFilePath();
        }
        return(characterPaths);
    }
    
}