/*
 Tiamat - a role-playing game framework
 ThemeNavigationEventHandler.java - handles Themes 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.graphics.Theme;
import com.huguesjohnson.tiamat.tools.BaseEditorFrame;
import com.huguesjohnson.tiamat.tools.ThemeEditorFrame;
import com.huguesjohnson.tiamat.tools.adventureStudio.NavigationTreeNode;

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

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

	public boolean canAdd(){ return(true); }
	public NavigationTreeNode onAdd(){
		String name=EventUtilities.promptForName("Theme");
		if(name==null){
			this.setCancel(true);
			return(null);
		}
		String themeFileName=new String(name);
		if(!themeFileName.endsWith(".theme.xml")){
			themeFileName=themeFileName+".theme.xml";
		}
		//create the theme file
		Theme theme=new Theme();
		try{
			XmlFormatter formatter=new XmlFormatter();
			formatter.saveTo(theme,this.getTargetDir()+themeFileName);
		} 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()+themeFileName,new ThemeNavigationEventHandler(this.getTargetDir(),themeFileName));
		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);
			}
		}
	}
	
	public FileFilter getFilter(){
		return(new XmlFileFilter());
	}
}