/*
Tiamat - a role-playing game framework
LocationExitListPanel.java - panel to list LocationExits
 
 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.LocationExit;
import com.huguesjohnson.ui.ListPanel;

/** LocationExitListPanel - panel to list LocationExits
 * @author Hugues Johnson
 */
public class LocationExitListPanel 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=-2215191293251017418L;

	private class LocationExitListItem{
		LocationExit exit;
		LocationExitListItem(LocationExit exit){ this.exit=exit; }
		public String toString(){ return(this.exit.getDirection()+" - "+this.exit.getName()); }
    }    
    	
	public LocationExitListPanel(){
		super("Exits");
	}
	

    /* begin overrides */
	public void addAction(){ this.addExit(); }
    public void editAction(){ this.editExit(); }    
    /* end overrides */	
    
    @SuppressWarnings("serial")
	private void addExit(){
        LocationExitDialog exitDialog=new LocationExitDialog(null){
            public void closeDialog(boolean cancel){
                if(!cancel){
                    addExit(getLocationExit());
                }
                dispose();
            }
        };
        exitDialog.setVisible(true);
    }
	
    public void addExit(LocationExit exit){
        this.addListItem(new LocationExitListItem(exit));
    }

    @SuppressWarnings("serial")
	private void editExit(){
    	LocationExitListItem listItem=(LocationExitListItem)this.getSelectedListItem();
        LocationExitDialog exitDialog=new LocationExitDialog(null,listItem.exit){
            public void closeDialog(boolean cancel){
                if(!cancel){
                    updateSelectedExit(getLocationExit());
                }
                dispose();
            }
        };
        exitDialog.setVisible(true);        
    }
    
    public void updateSelectedExit(LocationExit exit){
        this.updateSelectedListItem(new LocationExitListItem(exit));
    }
    
    public LocationExit[] getAllExits(){
        Object[] objects=this.getAllListItems();
        int length=objects.length;
        LocationExit[] exits=new LocationExit[length];
        for(int index=0;index<length;index++){
            exits[index]=((LocationExitListItem)objects[index]).exit;
        }
        return(exits);
    }
}