/*
Tiamat - a role-playing game framework
ItemNavigationEventHandler.java - handles Items 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.Item;
import com.huguesjohnson.tiamat.tools.BaseEditorFrame;
import com.huguesjohnson.tiamat.tools.ItemEditorFrame;
import com.huguesjohnson.tiamat.tools.adventureStudio.NavigationTreeNode;

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

	public ItemNavigationEventHandler(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(){ 
		ItemEditorFrame editor=new ItemEditorFrame(this.getFilePath());
		return(editor);
	}

	public boolean canAdd(){ return(true); }
	public NavigationTreeNode onAdd(){
		String name=EventUtilities.promptForName("Item");
		if(name==null){
			this.setCancel(true);
			return(null);
		}
		String itemFileName=new String(name);
		if(!itemFileName.endsWith(".item.xml")){
			itemFileName=itemFileName+".item.xml";
		}
		//create the item file
		Item item=new Item();
		try{
			XmlFormatter formatter=new XmlFormatter();
			formatter.saveTo(item,this.getTargetDir()+itemFileName);
		} 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()+itemFileName,new ItemNavigationEventHandler(this.getTargetDir(),itemFileName));
		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();
					Item item=(Item)formatter.loadFrom(this.getFilePath());
					formatter.saveTo(item,this.getTargetDir()+newName);
				} catch(Exception x){
					EventUtilities.showErrorDialog(x);
					this.setCancel(true);
				}				
			}
		}
	}
	
	public FileFilter getFilter(){
		return(new XmlFileFilter());
	}
}