package extractor.ui.clusteringUI;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.io.*;

import java.util.*;

public class ClassClusteringResultUI extends JPanel implements ActionListener{
	
	private JPanel		clusterResultPane;	

	private JPanel		constraintMainPane;	
	private JPanel		constraintPane1;
	private JLabel		constraintLabel1;
	private JTextField	constraintField1;
	
	private JPanel		constraintPane2;
	private JLabel		constraintLabel2;
	private JTextField	constraintField2;
	
	private JPanel		constraintPane3;		
	private JLabel		constraintLabel3;
	private JTextField	constraintField3;
	
	private JTree		clusterTree;
	private JScrollPane	clusterTreeScroll;
	
	private JPanel 		clusterBtnPane;
	private JButton		clusterBtn;
	
	private DefaultTreeModel treeModel;
	private DefaultMutableTreeNode top;
	private Vector selectedClasses;
	private Vector constraints;
		
	public ClassClusteringResultUI(Vector classes)	
	{
		this.selectedClasses = classes;
		
		setLayout(new BorderLayout());
		setBorder(BorderFactory.createTitledBorder("클러스터링 화면 : "));
		
		addComponents();
		
		add(clusterResultPane, BorderLayout.CENTER);
		add(clusterBtnPane, BorderLayout.SOUTH);		
	}

	private void addComponents()
	{
		clusterResultPane = new JPanel();
		clusterResultPane.setLayout(new BoxLayout(clusterResultPane, BoxLayout.Y_AXIS));
		
		constraintMainPane = new JPanel(new GridLayout(3, 0, 10, 10));		
		constraintMainPane.setBorder(BorderFactory.createTitledBorder("클러스터링 제약조건 : "));
		
		constraintPane1 = new JPanel();
		constraintLabel1 = new JLabel("클러스터에 포함될 최소 클래스 개수 : ");		
		constraintField1 = new JTextField(10);
		constraintLabel1.setLabelFor(constraintField1);
		constraintPane1.add(constraintLabel1);
		constraintPane1.add(constraintField1);

		constraintPane2 = new JPanel();		
		constraintLabel2 = new JLabel("클러스터에 포함될 최소 클래스 개수 : ");
		constraintField2 = new JTextField(10);
		constraintLabel2.setLabelFor(constraintField2);
		constraintPane2.add(constraintLabel2);
		constraintPane2.add(constraintField2);
		
		constraintPane3 = new JPanel();		
		constraintLabel3 = new JLabel("클러스터에 포함될 최소 클래스 개수 : ");
		constraintField3 = new JTextField(10);
		constraintLabel3.setLabelFor(constraintField3);		
		constraintPane3.add(constraintLabel3);
		constraintPane3.add(constraintField3);

		constraintMainPane.add(constraintPane1);
		constraintMainPane.add(constraintPane2);
		constraintMainPane.add(constraintPane3);

		makeTree();		
		clusterTreeScroll = new JScrollPane(clusterTree);
		clusterTreeScroll.setPreferredSize(new Dimension(200, 500));
		clusterTreeScroll.setBorder(BorderFactory.createTitledBorder("클러스터 결과 : "));
		
		clusterResultPane.add(constraintMainPane);
		clusterResultPane.add(Box.createRigidArea(new Dimension(0,5)));
		clusterResultPane.add(clusterTreeScroll);

		clusterBtnPane = new JPanel();
		clusterBtn = new JButton("Clustering");		
		clusterBtn.addActionListener(this);
		clusterBtnPane.add(clusterBtn);	
	}

	private void makeTree()
	{	
		top = new DefaultMutableTreeNode("CLUSTER");
		treeModel = new DefaultTreeModel(top);
   		clusterTree = new JTree(treeModel);
   		clusterTree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
		DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
		renderer.setLeafIcon(new ImageIcon("images/class.gif"));
		clusterTree.setCellRenderer(renderer);		
	}
		
	
	public void actionPerformed(ActionEvent e) 
	{
		if(e.getSource() == clusterBtn)
		{
			String constraint1 = constraintField1.getText();
			String constraint2 = constraintField2.getText();
			String constraint3 = constraintField3.getText();
			
			constraints = new Vector();
			constraints.add(constraint1);
			constraints.add(constraint2);
			constraints.add(constraint3);
			
			performClustering(constraints);
		}		
	}		
	
	public void performClustering(Vector constraints)
	{
		// performing clustering processes
		//........
		//........

//for test
		Hashtable clusters = new Hashtable();
		Vector keys = new Vector();
		keys.add("cluster 1");
		keys.add("cluster 2");
		keys.add("cluster 3");
		
		clusters.put("cluster 1", selectedClasses);
		clusters.put("cluster 2", selectedClasses);
		clusters.put("cluster 3", selectedClasses);
		
		displayToTree(clusters, keys);
	}	
	
	private void displayToTree(Hashtable clusters, Vector keys)
	{
		int maxItem = treeModel.getChildCount((MutableTreeNode)top);
		if(maxItem != 0)
		{
			for(int j=maxItem-1; j >= 0; j--)
			{
				MutableTreeNode deletedItem = (MutableTreeNode)treeModel.getChild((MutableTreeNode)top, j);
				treeModel.removeNodeFromParent(deletedItem);
			}
		}
		
		DefaultMutableTreeNode clusterNode = null;
		DefaultMutableTreeNode classNode = null;
		
		int i = 0;		
  		for(Enumeration e=keys.elements();e.hasMoreElements();)
  		{
  			Object key = e.nextElement();
  			clusterNode = new DefaultMutableTreeNode(key);
  			treeModel.insertNodeInto((MutableTreeNode)clusterNode, (MutableTreeNode)top, i++);
  			
  			Vector values = (Vector)clusters.get(key);
  			int j = 0;
  			for(Enumeration enum=values.elements();enum.hasMoreElements();)
  			{
				classNode = new DefaultMutableTreeNode(enum.nextElement());
				treeModel.insertNodeInto((MutableTreeNode)classNode, (MutableTreeNode)clusterNode, j++);  				
  			}  				
  		}
  		clusterTree.expandRow(0);
  		clusterTree.setSelectionInterval(1, clusterTree.getRowCount());
 	}
	
	public static void main(String[] arg)
	{

// for test
		Vector classes = new Vector();
		classes.add("class A");
		classes.add("class B");
		classes.add("class C");				
		classes.add("class D");
		
		JFrame jf = new JFrame();
		jf.getContentPane().add(new ClassClusteringResultUI(classes));
		jf.pack();
		jf.setVisible(true);
	}
}