package gov.nih.nci.cbiit.cmps.ui.tree;

import gov.nih.nci.cbiit.cmps.ui.jgraph.MiddlePanelJGraphController;
import gov.nih.nci.cbiit.cmps.ui.mapping.ElementMetaLoader;

import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

public class TreeSelectionHandler implements TreeSelectionListener {

	private MiddlePanelJGraphController graphController;
	public TreeSelectionHandler(MiddlePanelJGraphController controller)
	{
		graphController=controller;
	}
	// TODO Auto-generated method stub
	/**
	 * Called whenever the value of the selection changes.
	 *
	 * @param e the event that characterizes the change.
	 */
	public void valueChanged(TreeSelectionEvent e)
	{
		if (graphController.isGraphSelected())
			return;
		
		TreePath newPath = e.getNewLeadSelectionPath();
		if(newPath==null)
		{
			graphController.getPropertiesSwitchController().setSelectedItem(null);
		}
		else
		{
			//clear graph selection
			graphController.getMiddlePanel().getGraph().clearSelection();
			DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) newPath.getLastPathComponent();
			//clear selection of "the other tree"
			if (treeNode instanceof DefaultSourceTreeNode)
				graphController.getMappingPanel().getTargetTree().clearSelection();
			else if (treeNode instanceof DefaultTargetTreeNode)
				graphController.getMappingPanel().getSourceTree().clearSelection();
			
			Object newSelection = treeNode.getUserObject();
			if(newSelection instanceof ElementMetaLoader.MyTreeObject)
				newSelection = ((ElementMetaLoader.MyTreeObject)newSelection).getObj();
			
			graphController.getPropertiesSwitchController().setSelectedItem(newSelection);
		}
		graphController.getPropertiesSwitchController().getPropertiesPage().updateProptiesDisplay(null);
	}

}

@@ -1267,32 +1267,103 @@
 SourceTreeNode)%0A
+%09%09%09%7B%0A%09%09%09%09if (graphController.getMappingPanel().getTargetTree()!=null)%0A%09
 %09%09%09%09graphControl
@@ -1418,16 +1418,21 @@
 tion();%0A
+%09%09%09%7D%0A
 %09%09%09else 
@@ -1478,16 +1478,88 @@
 eeNode)%0A
+%09%09%09%7B%0A%09%09%09%09if ( graphController.getMappingPanel().getSourceTree()!=null)%0A%09
 %09%09%09%09grap
@@ -1621,24 +1621,25 @@
 ction();%0A%09%09%09
+%7D
 %0A%09%09%09Object n

clear gaph and other tree selection