/*
 Tiamat - a role-playing game framework
 NavigationTreeCellRenderer.java - used to render cells in the navigation tree
 
 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;

import java.awt.Component;

import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;

/** NavigationTreeCellRenderer - used to render cells in the navigation tree
 * @author Hugues Johnson
 */
public class NavigationTreeCellRenderer extends DefaultTreeCellRenderer{
	/* 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=-1448477907188118796L;

	//icons
	private static ImageIcon openIcon;
	private static ImageIcon closedIcon;
	private static ImageIcon leafDefaultIcon;
	private static ImageIcon leafMidiIcon;
	private static ImageIcon leafFontIcon;
	private static boolean iconsCreated=false;
	
	public NavigationTreeCellRenderer(){
		super();
		//create icons
		if(!iconsCreated){
			openIcon=(new ImageIcon(this.getClass().getResource("/com/huguesjohnson/tiamat/tools/images/tree-folder-open.gif")));
			closedIcon=(new ImageIcon(this.getClass().getResource("/com/huguesjohnson/tiamat/tools/images/tree-folder-closed.gif")));
			leafDefaultIcon=(new ImageIcon(this.getClass().getResource("/com/huguesjohnson/tiamat/tools/images/tree-leaf-default.gif")));
			leafMidiIcon=(new ImageIcon(this.getClass().getResource("/com/huguesjohnson/tiamat/tools/images/tree-leaf-midi.gif")));
			leafFontIcon=(new ImageIcon(this.getClass().getResource("/com/huguesjohnson/tiamat/tools/images/tree-leaf-font.gif")));
			iconsCreated=true;
		}
	}
	
	//people I work with know that I lament how VB6 is probably the best programming language/IDE/environment ever
	//writing this method reminded me of that..
	//it took ~2 hours to figure this *%#@ out while in VB6 this would be done in 5 minutes
	//if VB6 runtime is supported on Vista (what a crappy name) then I'll be seriously tempted to just port Tiamat to it
	public Component getTreeCellRendererComponent(JTree tree,Object value,boolean sel,boolean expanded,boolean leaf,int row,boolean hasFocus){
    	super.getTreeCellRendererComponent(tree,value,sel,expanded,leaf,row,hasFocus);	
    	//apply custom open/close icons
    	this.setOpenIcon(openIcon);
    	this.setClosedIcon(closedIcon);
    	//set the leaf icon based on the extension
    	if(value instanceof NavigationTreeNode){
    		NavigationTreeNode node=(NavigationTreeNode)value;
    		String filePath=node.getFilePath();
    		if(filePath!=null){
    			int indexOf=filePath.indexOf(".");
    			if(indexOf>0){
    				String extension=filePath.substring(indexOf).toLowerCase();
    				if(extension.equals(".mid")||(extension.equals(".midi"))){
    					this.setIcon(leafMidiIcon);
    				} else if(extension.equals(".ttf")){
    					this.setIcon(leafFontIcon);
    				} else{
    					this.setIcon(leafDefaultIcon);
    				}
    			} else{
    		    	if(expanded){
        				this.setIcon(openIcon);
    		    	} else{
        				this.setIcon(closedIcon);
    		    	}
    			}
    		} 
    	}
    	return(this);
    }
	
}