/*
Tiamat - a role-playing game framework
LocationObjectDataListPanel.java - panel to list LocationObjectData
 
 Copyright  (C) 2006 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 com.huguesjohnson.tiamat.LocationObjectData;
import com.huguesjohnson.ui.ListPanel;

/** LocationObjectDataListPanel - panel to list LocationObjectData
 * @author Hugues Johnson
 */
public class LocationObjectDataListPanel 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=-3355044011261190480L;

	private class LocationObjectDataListItem{
		LocationObjectData locationData;
		LocationObjectDataListItem(LocationObjectData locationData){ this.locationData=locationData; }
		public String toString(){ return(this.locationData.getName()); }
    }    
    	
	public LocationObjectDataListPanel(String caption){
		super(caption);
	}
	
    /* begin overrides */
	public void addAction(){ this.addLocationData(); }
    public void editAction(){ this.editLocationData(); }
    /* end overrides */	
    
    @SuppressWarnings("serial")
	private void addLocationData(){
        LocationObjectDataDialog dialog=new LocationObjectDataDialog(null){
            public void closeDialog(boolean cancel){
                if(!cancel){
                    addLocationData(getLocationObjectData());
                }
                dispose();
            }
        };
        dialog.setVisible(true);
    }
	
    public void addLocationData(LocationObjectData locationData){
        this.addListItem(new LocationObjectDataListItem(locationData));
    }

    @SuppressWarnings("serial")
	private void editLocationData(){
    	LocationObjectDataListItem listItem=(LocationObjectDataListItem)this.getSelectedListItem();
    	LocationObjectDataDialog dialog=new LocationObjectDataDialog(null,listItem.locationData){
            public void closeDialog(boolean cancel){
                if(!cancel){
                    updateSelectedLocationData(getLocationObjectData());
                }
                dispose();
            }
        };
        dialog.setVisible(true);        
    }
    
    public void updateSelectedLocationData(LocationObjectData locationData){
        this.updateSelectedListItem(new LocationObjectDataListItem(locationData));
    }
    
    public LocationObjectData[] getAllObjects(){
        Object[] objects=this.getAllListItems();
        int length=objects.length;
        LocationObjectData[] exits=new LocationObjectData[length];
        for(int index=0;index<length;index++){
            exits[index]=((LocationObjectDataListItem)objects[index]).locationData;
        }
        return(exits);
    }
}