package gov.va.med.esr.ui.util;

import java.util.List;

import com.jenkov.prizetags.tree.itf.ISelectListener;
import com.jenkov.prizetags.tree.itf.ITree;
import com.jenkov.prizetags.tree.itf.ITreeNode;

public class TreeNodeSelectListener implements ISelectListener {

	public void nodeSelected(ITreeNode treenode, ITree tree) {
		// TODO Auto-generated method stub
		System.out.println("selected Node :" + treenode);
		
		if ( treenode == null ) return;
		ITreeNode parentNode = treenode.getParent();
		processParent(parentNode, tree);
	}

	private void processParent(ITreeNode parentNode, ITree tree) {
		
		if (parentNode == null  ) return;
		
		
		System.out.println("inspecting :" + parentNode.getId());
		
		List childNodes = parentNode.getChildren();
		boolean allSelected = true;
		
		for ( int i=0; i<childNodes.size();i++){
			
			ITreeNode currChild = (ITreeNode)childNodes.get(i);
			if ( ! tree.isSelected(currChild.getId()))
			{
				allSelected = false;
				break;
			}
		}
		
		if ( allSelected)
		{
			tree.select(parentNode.getId());
			System.out.println("selectecting :" + parentNode.getId());
		}
		
		processParent(parentNode.getParent(), tree);
	}
}
