/*
Tiamat - a role-playing game framework
ProfileListPanel.java - panel to list a character's profile
 
 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.String;
import com.huguesjohnson.tiamat.tools.ProfileDialog;
import com.huguesjohnson.ui.ListPanel;

/** ProfileListPanel - panel to list a character's profile
 * @author Hugues Johnson
 */
public class ProfileListPanel 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=2733005493411557591L;

	//internal class to represent items in the list
    private class ProfileListItem{
        private String key;
        public String getKey(){ return(this.key); }
        private String value;
        public String getValue(){ return(this.value); }
        ProfileListItem(String key,String value){ this.key=key; this.value=value; }
        public String toString(){ return(this.key+"="+this.value); }
    }    
    
    /** Creates a new instance of ProfileListPanel */
    public ProfileListPanel(){
        super("Profile");
    }

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

    /* end overrides */

    /** add a new profile element
     */
    @SuppressWarnings("serial")
	private void addProfileElement(){
        ProfileDialog profileDialog=new ProfileDialog(null){
            public void closeDialog(boolean cancel){
                if(!cancel){
                    addProfileElement(getKey(),getValue());
                }
                dispose();
            }
        };
        profileDialog.setVisible(true);
    }
    
    /** add a new profile elements
     * @param key the key 
     * @param value the value
     */
    public void addProfileElement(String key,String value){
        this.addListItem(new ProfileListItem(key,value));
    }

    /** edit the selected profile element
     */
    @SuppressWarnings("serial")
	private void editProfileElement(){
        ProfileListItem listItem=(ProfileListItem)this.getSelectedListItem();
        String key=listItem.getKey();
        String value=listItem.getValue();
        ProfileDialog profileDialog=new ProfileDialog(null,key,value){
            public void closeDialog(boolean cancel){
                if(!cancel){
                    updateSelectedProfileElement(getKey(),getValue());
                }
                dispose();
            }
        };
        profileDialog.setVisible(true);        
    }
    
    /** replace the selected element
     * @param key the new key 
     * @param value the new value
     */
    public void updateSelectedProfileElement(String key,String value){
        this.updateSelectedListItem(new ProfileListItem(key,value));
    }
    
    /** return all key-value pairs in the collection, [0][n]=key for item n, [1][n]=value for item n 
     * @return all key-value pairs in the collection
     */
    public String[][] getAllKeyValuePairs(){
        Object[] objects=this.getAllListItems();
        int length=objects.length;
        String[][] allPairs=new String[2][length];
        for(int index=0;index<length;index++){
            allPairs[0][index]=((ProfileListItem)objects[index]).getKey();
            allPairs[1][index]=((ProfileListItem)objects[index]).getValue();
        }
        return(allPairs);
    }
    
}