/*
Tiamat - a role-playing game framework
TargetListPanel.java - panel to list Targets
 
 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.Target;
import com.huguesjohnson.tiamat.tools.TargetDialog;
import com.huguesjohnson.ui.ListPanel;

/** TargetListPanel - panel to list Targets
 * @author  Hugues Johnson
 */
public class TargetListPanel 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=6293421633028781124L;

	//internal class to represent list items
    private class TargetListItem{
        private Target target;
        TargetListItem(Target target){ this.target=target; }
        Target getTarget(){ return(this.target); }
        public String toString(){ return("["+this.target.getAffectedEntity()+"] "+this.target.getTargetPath()+"="+this.target.getValue()); }
    }    
    
    /** Creates a new instance of TargetListPanel 
     */
    public TargetListPanel(){
        super("Targets");
    }

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

    /* end overrides */

    /** add a new Target 
     */
    @SuppressWarnings("serial")
	private void addTarget(){
        TargetDialog targetDialog=new TargetDialog(null){
            public void closeDialog(boolean cancel){
                if(!cancel){
                    addTarget(getTarget());
                }
                dispose();
            }
        };
        targetDialog.setVisible(true);
    }
    
    /** add a new Target 
     * @param target the Target to add
     */
    public void addTarget(Target target){
        this.addListItem(new TargetListItem(target));
    }

    /** edit the selected Target 
     */    
    @SuppressWarnings("serial")
	private void editTarget(){
        TargetListItem listItem=(TargetListItem)this.getSelectedListItem();
        Target target=listItem.getTarget();
        TargetDialog targetDialog=new TargetDialog(null,target){
            public void closeDialog(boolean cancel){
                if(!cancel){
                    updateSelectedTarget(getTarget());
                }
                dispose();
            }
        };
        targetDialog.setVisible(true);        
    }
    
    /** update the selected Target 
     * @param target the Target to replace the selected Target with
     */
    public void updateSelectedTarget(Target target){
        this.updateSelectedListItem(new TargetListItem(target));
    }
    
    /** returns an array of all Targets in the panel
     * @return an array of all Targets in the panel
     */
    public Target[] getAllTargets(){
        Object[] objects=this.getAllListItems();
        int length=objects.length;
        Target[] targets=new Target[length];
        for(int index=0;index<length;index++){
            targets[index]=((TargetListItem)objects[index]).getTarget();
        }
        return(targets);
    }
    
}