/*
Tiamat - a role-playing game framework
LocationNavigationEventHandler.java - handles Locations in the project

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.adventureStudio.event;

import javax.swing.filechooser.FileFilter;

import com.huguesjohnson.fileFilter.XmlFileFilter;
import com.huguesjohnson.serialization.XmlFormatter;
import com.huguesjohnson.tiamat.Location;
import com.huguesjohnson.tiamat.tools.BaseEditorFrame;
import com.huguesjohnson.tiamat.tools.LocationEditorFrame;
import com.huguesjohnson.tiamat.tools.adventureStudio.NavigationTreeNode;

/** LocationNavigationEventHandler - handles Locations in the project
* @author Hugues Johnson
*/
public class LocationNavigationEventHandler extends NavigationEventHandler{
	public LocationNavigationEventHandler(String targetDir){
		this.setTargetDir(targetDir);
	}

	public LocationNavigationEventHandler(String targetDir,String fileName){
		this.setTargetDir(targetDir);
		this.setFileName(fileName);
	}
	
	public boolean canEdit(){
		if(this.getFileName()==null){ //then this is a root node
			return(false);
		}
		return(true);
	}
	public BaseEditorFrame onEdit(){
		LocationEditorFrame editor=new LocationEditorFrame(this.getFilePath());
		return(editor);
	}

	public boolean canAdd(){ return(true); }
	public NavigationTreeNode onAdd(){
		String name=EventUtilities.promptForName("Location");
		if(name==null){
			this.setCancel(true);
			return(null);
		}
		String locationFileName=new String(name);
		if(!locationFileName.endsWith(".location.xml")){
			locationFileName=locationFileName+".location.xml";
		}
		//create the location file
		Location location=new Location();
		try{
			XmlFormatter formatter=new XmlFormatter();
			formatter.saveTo(location,this.getTargetDir()+locationFileName);
		} catch(Exception x){
			EventUtilities.showErrorDialog(x);
			this.setCancel(true);
			return(null);			
		}
		//return a NavigationTreeNode for this new file
		this.setCancel(false);
		NavigationTreeNode node=new NavigationTreeNode(name,this.getTargetDir()+locationFileName,new LocationNavigationEventHandler(this.getTargetDir(),locationFileName));
		return(node);
	}

	public boolean canRemove(){ 
		if(this.getFileName()==null){ //then this is a root node
			return(false);
		}
		return(true);
	}
	public void onRemove(){
		this.setCancel(!EventUtilities.confirmDelete(this.getFileName(),this.getTargetDir()));
	}

	public boolean canRename(){ 
		if(this.getFileName()==null){ //then this is a root node
			return(false);
		} 
		return(true);
	}
	public void onRename(){ 
		String newName=EventUtilities.renameFile(this.getFileName(),this.getTargetDir());
		if(newName!=null){
			if(!newName.equals(this.getFileName())){
				this.setFileName(newName);
				//rename the Item
				try{
					XmlFormatter formatter=new XmlFormatter();
					Location location=(Location)formatter.loadFrom(this.getFilePath());
					formatter.saveTo(location,this.getTargetDir()+newName);
				} catch(Exception x){
					EventUtilities.showErrorDialog(x);
					this.setCancel(true);
				}				
			}
		}
	}

	public FileFilter getFilter(){
		return(new XmlFileFilter());
	}
}