package j3deditor.bin.hierarchy.view;


import j3deditor.bin.hierarchy.HierarchyNode;

import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;


/**
 * Custom <code>TreeModel</code> for <code>JTree</code>
 * that visualizes hierarchy.
 *
 * @author Risto Seene
 */
public class HTreeModel implements TreeModel{

	/** root node */
	private Object root;


	/**
	 * Creates an instance of <code>HTreeModel</code>
	 * with the given root node.
	 *
	 * @param root
	 *	root node for this tree model
	 */
	public HTreeModel(Object root){
		this.root = root;
	}


	/**
	 * Not in use.
	 * @param tml
	 * 	doesn't do anything
	 */
	public void addTreeModelListener(TreeModelListener tml){
	}

	/**
	 * Returns the child of the given node
	 * at the specified index.
	 *
	 * @param node
	 * 	node which child will be returned
	 * @param index
	 * 	index of the requested child
	 * @return Returns the child of the given node
	 *	at the specified index
	 */
	public Object getChild(Object node, int index){
		return ((HierarchyNode)node).getChild(index);
	}

	/**
	 * Returns the number of children the given
	 * node has.
	 *
	 * @param node
	 * 	node which child count is requested
	 * @return Returns the number of children the given
	 *	node has
	 */
	public int getChildCount(Object node){
		return ((HierarchyNode)node).getNumberOfChildren();
	}

	/**
	 * Returns the index of the given child of the given node.
	 *
	 * @param parent
	 * 	node which childs index is requested
	 * @param child
	 * 	node which index is requested
	 * @return Returns the index of the given child of the given node
	 */
	public int getIndexOfChild(Object parent, Object child){
		return ((HierarchyNode)parent).getIndexOfChild((HierarchyNode)child);
	}

	/**
	 * Returns the root node of this tree model.
	 *
	 * @return Returns the root node of this tree model
	 */
	public Object getRoot(){
		return root;
	}

	/**
	 * Tests if the given node has children.
	 *
	 * @param node
	 * 	node that that will be tested
	 * @return Returns <code>true</code> if the given
	 *	node has no children,
	 *	otherwise <code>false</code>
	 */
	public boolean isLeaf(Object node){
		if(((HierarchyNode)node).getNumberOfChildren() != 0){
			return false;
		}
		return true;
	}

	/**
	 * Not in use.
	 * @param tml
	 * 	doesn't do anything
	 */
	public void removeTreeModelListener(TreeModelListener tml){
	}

	/**
	 * Not in use.
	 * @param tp
	 * 	doesn't do anything
	 * @param ob
	 * 	doesn't do anything
	 */
	public void valueForPathChanged(TreePath tp, Object ob){
	}

}
