package j3deditor.bin.hierarchy.view;

import j3deditor.bin.hierarchy.HierarchyManager;
import j3deditor.bin.hierarchy.J3DeCamera;
import j3deditor.bin.hierarchy.J3DeLight;
import j3deditor.bin.hierarchy.J3DeMaterial;
import j3deditor.bin.hierarchy.J3DePrimitive;
import j3deditor.bin.hierarchy.J3DeScene;

import j3deditor.bin.hierarchy.J3DeShape3D;

import j3deditor.bin.hierarchy.J3DeTexture;

import j3deditor.bin.hierarchy.J3DeTextureAttributes;

import j3deditor.bin.util.Utilities;

import java.awt.Component;

import java.awt.Dimension;

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


/**
 * Custom tree cell renderer.
 * 
 * @author Risto Seene
 */
public class TreeRenderer extends DefaultTreeCellRenderer{
	
	private ImageIcon hierarchy = Utilities.image.getImageIcon("icons/hierarchy.png");
	private ImageIcon scene = Utilities.image.getImageIcon("icons/translate.png");
	private ImageIcon camera = Utilities.image.getImageIcon("icons/camera.png");
	private ImageIcon primitive = Utilities.image.getImageIcon("icons/primitive.png");
	private ImageIcon shape3d = Utilities.image.getImageIcon("icons/geometry.png");
	private ImageIcon light = Utilities.image.getImageIcon("icons/light.png");
	private ImageIcon material = Utilities.image.getImageIcon("icons/material.png");
	private ImageIcon texture = Utilities.image.getImageIcon("icons/texture.png");
	private ImageIcon textureAttributes = Utilities.image.getImageIcon("icons/tex_attr.png");


	/**
	 * Configures the renderer based on the passed in components.
	 * 
	 * @return the Component that the renderer uses to draw the value
	 * @see javax.swing.tree.DefaultTreeCellRenderer
	 */
	@Override
	public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus){
		Component c = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
		try{
			if(value instanceof HierarchyManager){
				this.setIcon(hierarchy);
			}
			else if(value instanceof J3DeScene){
				this.setIcon(scene);
			}
			else if(value instanceof J3DeCamera){
				this.setIcon(camera);
			}
			else if(value instanceof J3DePrimitive){
				this.setIcon(primitive);
			}
		          else if(value instanceof J3DeShape3D){
		                    this.setIcon(shape3d);
		          }
			else if(value instanceof J3DeLight){
				this.setIcon(light);
			}
		          else if(value instanceof J3DeMaterial){
		                    this.setIcon(material);
		          }
		          else if(value instanceof J3DeTexture){
		                    this.setIcon(texture);
		          }
		          else if(value instanceof J3DeTextureAttributes){
		                    this.setIcon(textureAttributes);
		          }
			
			return this;
		} catch(Throwable t){
			Utilities.out.throwWarning(t);
		}
		return c;
	}

	/**
	 * Returns the preferred size of this tree cell.
	 * Height is always 20.
	 * 
	 * @return Returns the preferred size of this tree cell
	 */
	@Override
	public Dimension getPreferredSize(){
		Dimension dim = super.getPreferredSize();
		dim.height = 20;
		return dim;
	}
}
