package extractor.ui.clusteringUI;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.tree.*;
import javax.swing.table.AbstractTableModel;


public class ClusterTree extends JPanel implements ActionListener {
	private boolean DEBUG = true;
	
	private JButton moveRightBtn;
	private JButton moveLeftBtn;
	private JButton newBtn;
	private JTree tree;
	private DefaultTreeModel treeModel;
	private JScrollPane tableScroll;
	private JTable table;
	private DefaultMutableTreeNode root;
	MyTableModel myTableModel;
	JDialog f = null;
	
	public ClusterTree(JDialog frame, Vector rowVec, Vector colVec)
	{
		f = frame;
		setLayout(new BorderLayout());
		setBorder(new EmptyBorder(10, 10, 10, 10));

		JPanel linePanel = new JPanel(new BorderLayout());
		linePanel.setBorder(new LineBorder(new Color(249, 249, 249)));

		JPanel boxPanel = new JPanel(new BorderLayout());
		boxPanel.setBorder(new EmptyBorder(10, 10, 10, 10));

		linePanel.add(boxPanel, BorderLayout.CENTER);

		GridPanel gridPanel = new GridPanel(13, 19, 0, 0);

		myTableModel = new MyTableModel(rowVec, colVec);	
		table = new JTable(myTableModel);
		tableScroll = new JScrollPane(table);
		tableScroll.setPreferredSize(new Dimension(500, 300));						
		gridPanel.add(0, 0, 13, 8, tableScroll);
		
		ImageIcon rightImg = new ImageIcon(ClassLoader.getSystemResource("images/lower.gif"));
		ImageIcon leftImg = new ImageIcon(ClassLoader.getSystemResource("images/upper.gif"));
		ImageIcon newImg = new ImageIcon(ClassLoader.getSystemResource("images/class.gif"));
		moveRightBtn = new JButton(rightImg);
		moveLeftBtn = new JButton(leftImg);
		newBtn = new JButton("New Cluster", newImg);

		gridPanel.add(3, 9, 1, 1, newBtn);
		gridPanel.add(5, 9, 1, 1, moveRightBtn);
		gridPanel.add(7, 9, 1, 1, moveLeftBtn);

		moveRightBtn.addActionListener(this);
		moveLeftBtn.addActionListener(this);
		newBtn.addActionListener(this);

		root = new DefaultMutableTreeNode("Cluster");
		treeModel = new DefaultTreeModel(root);
		tree = new JTree(treeModel);
		DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
		renderer.setLeafIcon(new ImageIcon("images/class.gif"));
		tree.setCellRenderer(renderer);
		JScrollPane listScrollPane = new JScrollPane(tree);
		
		listScrollPane.setPreferredSize(new Dimension(500, 300));
		gridPanel.add(0, 11, 13, 8, listScrollPane);
		
		boxPanel.add(gridPanel, BorderLayout.CENTER);

		add(linePanel, BorderLayout.CENTER);
	}


	public void actionPerformed(ActionEvent e) 
	{
		Vector listVal = new Vector();
		String className = null;
		if(e.getSource() == moveRightBtn)
		{
			for(int p=0;p<myTableModel.getRowCount();p++)
			{
				Boolean boo = (Boolean)myTableModel.getValueAt(p, myTableModel.getColumnCount()-1);
				if(boo.booleanValue() == true)
					listVal.add( ((Vector)myTableModel.rowVec.get(p)).get(0) );	
			}
			
			for(int j=0;j<listVal.size();j++)
			{
				className = (String)listVal.get(j);
				DefaultMutableTreeNode child = new DefaultMutableTreeNode(className);
		        DefaultMutableTreeNode parentNode = null;
		        boolean breakChk = false;
		        
		        TreePath parentPath = tree.getSelectionPath();
		
		        if (parentPath == null)
		        {
		            parentNode = root;
		        }
		        else
		        {
		            parentNode = (DefaultMutableTreeNode)(parentPath.getLastPathComponent());
		        }
		        
		        String childName=null;
		        
		        if(parentNode.getChildCount() > 0)
		        {
			        for(int k=0;k<parentNode.getChildCount();k++)
			        {
			        	childName = parentNode.getChildAt(k).toString();
			        	if(childName.equals(className))
			        	{
			        		breakChk = true;
			        		break;
			        	}
			        }
			        if(breakChk == false)
			       		treeModel.insertNodeInto(child, parentNode, j);
			    }
			    else if(parentNode.getChildCount() == 0)
			    	treeModel.insertNodeInto(child, parentNode, j);			   
		        
		        tree.expandRow(0);
		        tree.expandPath(parentPath);
			}
		}
		else if(e.getSource() == moveLeftBtn)
		{
			TreePath[] currentSelection = tree.getSelectionPaths();
			if (currentSelection != null)
			{
				for(int i=0;i<currentSelection.length;i++)
				{
				    DefaultMutableTreeNode currentNode = 
				    					(DefaultMutableTreeNode)currentSelection[i].getLastPathComponent();
				    MutableTreeNode parent = (MutableTreeNode)currentNode.getParent();
				    if (parent != null)
				    {
				        treeModel.removeNodeFromParent(currentNode);
				    }
				}
			}
		}
		else if(e.getSource() == newBtn)
		{
			JDialog jd = new JDialog(f, "Input the new cluster name", true);
			jd.setSize(new Dimension(350, 100));
			jd.getContentPane().add(new InputNode(tree));
			jd.setLocation(300, 200);
			jd.setVisible(true);
		}	
	}

    class MyTableModel extends AbstractTableModel
    {
		Vector rowVec = new Vector(); Vector colVec = new Vector();	
		
		public MyTableModel(Vector r, Vector c)		
		{
			this.rowVec = r;
			this.colVec = c;			
		}
				
        public int getColumnCount()
        {
            return colVec.size();
        }

        public String getColumnName(int col) {
            return (String)colVec.get(col);
	    }        
        
        public int getRowCount()
        {
            return rowVec.size();
        }

        public Object getValueAt(int row, int col)
        {
            return ((Vector)rowVec.get(row)).get(col);
        }

	    public Class getColumnClass(int c)
	    {
	    	return getValueAt(0, c).getClass();
    	}
    	
        public boolean isCellEditable(int row, int col) 
        {
            if(col == (colVec.size()-1) )
              	return true;
            else
            	return false;
        }
        
        public void setValueAt(Object value, int row, int col)
        {
            ((Vector)rowVec.get(row)).set(col, value);
        }
	}
}
