/*
 Tiamat - a role-playing game framework
 SequenceNavigationEventHandler.java - handles Sequences 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.Character;
import com.huguesjohnson.tiamat.Event;
import com.huguesjohnson.tiamat.graphics.Sequence;
import com.huguesjohnson.tiamat.graphics.SequenceFrame;
import com.huguesjohnson.tiamat.tools.BaseEditorFrame;
import com.huguesjohnson.tiamat.tools.SequenceEditorFrame;
import com.huguesjohnson.tiamat.tools.adventureStudio.NavigationTreeNode;

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

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

	public boolean canAdd(){ return(true); }
	public NavigationTreeNode onAdd(){
		String name=EventUtilities.promptForName("Sequence");
		if(name==null){
			this.setCancel(true);
			return(null);
		}
		String sequenceFileName=new String(name);
		if(!sequenceFileName.endsWith(".sequence.xml")){
			sequenceFileName=sequenceFileName+".sequence.xml";
		}
		//create the character file
		Sequence sequence=new Sequence(new SequenceFrame[0],new Event[0]);
		try{
			XmlFormatter formatter=new XmlFormatter();
			formatter.saveTo(sequence,this.getTargetDir()+sequenceFileName);
		} 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()+sequenceFileName,new SequenceNavigationEventHandler(this.getTargetDir(),sequenceFileName));
		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 Character
				try{
					XmlFormatter formatter=new XmlFormatter();
					Character character=(Character)formatter.loadFrom(this.getFilePath());
					character.getProfile().set("name",newName.substring(0,(newName.indexOf(".character.xml"))));
					formatter.saveTo(character,this.getTargetDir()+newName);
				} catch(Exception x){
					EventUtilities.showErrorDialog(x);
					this.setCancel(true);
				}				
			}
		}
	}

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