package extractor.ui.clusteringUI;

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

import java.util.*;

public class ClassMainUI extends JDialog implements ActionListener{
	
	private JPanel btnPane;
	private JButton okBtn;
	private JButton nextServletBtn;
	private JButton finishBtn;
	
	private int currentPane;
	private JDialog jd;
	Vector selected = new Vector();
	Dimension dim;
	//ClassWeightUI classWeightPane;
	ClusterTree clusterTree;
	
	
	
	public ClassMainUI(JDialog jd, Vector selected)
	{
		super(jd, "클러스터링 화면입니다.", true);	
		
		this.jd = jd;
		this.selected = selected;
		
		dim = Toolkit.getDefaultToolkit().getScreenSize();				

		btnPane = new JPanel(new FlowLayout(FlowLayout.CENTER, 50, 0));
		
		okBtn = new JButton("OK");
		okBtn.addActionListener(this);
		nextServletBtn = new JButton("Next Servlet");
		nextServletBtn.setEnabled(false);
		nextServletBtn.addActionListener(this);
		finishBtn = new JButton("Cancel");
		finishBtn.setEnabled(true);
		finishBtn.addActionListener(this);
		btnPane.add(okBtn);
		//btnPane.add(nextServletBtn);
		btnPane.add(finishBtn);
				
		if(jd != null)
			setLocation(jd.getLocation().x+200, jd.getLocation().y+100);  
		
		getContentPane().setLayout(new BorderLayout(30, 10));
		

	//for test
        final Object[] columnNames = {"", 
        							"# of method", 
                       				"# of variable",
                       				"# of message",
                       				};
        final Object[][] data = {
            {"class 1", new Double(3.4), new Double(5), new Double(5), new Double(2.3), new Double(3.7)},
            {"class 2", new Double(8.2), new Double(15), new Double(5.1), new Double(12), new Double(9.2)},
            {"class 3", new Double(3), new Double(5.9), new Double(9), new Double(12.1) , new Double(13)},
            {"class 4", new Double(3.4), new Double(5), new Double(5), new Double(2.3), new Double(3.7)},
            {"class 5", new Double(8.2), new Double(15), new Double(5.1), new Double(12), new Double(9.2)},
            {"class 6", new Double(3), new Double(5.9), new Double(9), new Double(12.1) , new Double(13)},
            {"class 7", new Double(3.4), new Double(5), new Double(5), new Double(2.3), new Double(3.7)},
            {"class 8", new Double(8.2), new Double(15), new Double(5.1), new Double(12), new Double(9.2)},
            {"class 9", new Double(3), new Double(5.9), new Double(9), new Double(12.1) , new Double(13)},
            {"class 10", new Double(3.4), new Double(5), new Double(5), new Double(2.3), new Double(3.7)},
            {"class 11", new Double(8.2), new Double(15), new Double(5.1), new Double(12), new Double(9.2)},
            {"class 12", new Double(3), new Double(5.9), new Double(9), new Double(12.1) , new Double(13)},
            {"class 13", new Double(3.4), new Double(5), new Double(5), new Double(2.3), new Double(3.7)},
            {"class 14", new Double(8.2), new Double(15), new Double(5.1), new Double(12), new Double(9.2)},
            {"class 15", new Double(3), new Double(5.9), new Double(9), new Double(12.1) , new Double(13)},
            {"class 16", new Double(3.4), new Double(5), new Double(5), new Double(2.3), new Double(3.7)},
            {"class 17", new Double(8.2), new Double(15), new Double(5.1), new Double(12), new Double(9.2)},
            {"class 18", new Double(3), new Double(5.9), new Double(9), new Double(12.1) , new Double(13)},
            {"class 19", new Double(3.4), new Double(5), new Double(5), new Double(2.3), new Double(3.7)},
            {"class 20", new Double(8.2), new Double(15), new Double(5.1), new Double(12), new Double(9.2)},
            {"class 21", new Double(3), new Double(5.9), new Double(9), new Double(12.1) , new Double(13)},
            {"class 22", new Double(3.4), new Double(5), new Double(5), new Double(2.3), new Double(3.7)},
            {"class 23", new Double(8.2), new Double(15), new Double(5.1), new Double(12), new Double(9.2)},
            {"class 24", new Double(3), new Double(5.9), new Double(9), new Double(12.1) , new Double(13)},
            };
        
        		
		//classWeightPane = new ClassWeightUI(selected);
		Vector rowVec = new Vector(); Vector colVec = new Vector();	
		Vector row1 = new Vector(); Vector row2 = new Vector();
		
		row1.add("row1");row1.add("row1");row1.add(new Boolean(true));
		row2.add("row2");row2.add("row2");row2.add(new Boolean(false));
		rowVec.add(row1);rowVec.add(row2);
		colVec.add("col1");colVec.add("col2");colVec.add("col3");

		clusterTree = new ClusterTree(this, rowVec, colVec);
		
		currentPane = 0;
		
		//getContentPane().add(classWeightPane, BorderLayout.CENTER);		
		getContentPane().add(clusterTree, BorderLayout.CENTER);
		getContentPane().add(btnPane, BorderLayout.SOUTH);
		
		setSize((int)dim.getWidth()*2/3, (int)dim.getHeight()*2/3);
		
		addWindowListener(
			new WindowAdapter()
			{
				public void windowClosing(WindowEvent e)
				{
					dispose();
				}
			}
		);		
		setVisible(true);
	}

	public void actionPerformed(ActionEvent e) 
	{
		Object obj = e.getSource();
		if(obj == okBtn)
		{
			/*switch (currentPane)
			{
				case 0:
					addClassClusteringResultUI();
					nextServletBtn.setEnabled(false);
					okBtn.setEnabled(false);
					finishBtn.setEnabled(true);
					break;
			}*/
			
		}
		else if(obj == finishBtn)
		{
			dispose();
		}

	}
				/*case 1:
					addServletMethodTFUI();
					nextServletBtn.setEnabled(false);
					break;
				case 2:
					addServletResultUI();
					if(selected.size() == 0)
					{
						okBtn.setEnabled(false);
						nextServletBtn.setEnabled(false);
						finishBtn.setEnabled(true);
					}
					else
						nextServletBtn.setEnabled(true);
					break;
			}
			
			//currentP = currentP >= 6 ? 6 : ++currentP;				


		}
		else if(obj == nextServletBtn)
		{
			System.out.println("I'm next servlet button.");
			if(selected.size() > 0)
			{
				getContentPane().remove(servletSplitPane);
				servletSplitPane = new ServletSplitPaneUI(selected);
				getContentPane().add(servletSplitPane, BorderLayout.CENTER);
				validate();
				currentPane = 0;
			}			
		}
		else if(obj == finishBtn)		
		{
			dispose();
		}			
	}
	
	private void addServletNameTFUI()
	{
		servletSplitPane.getRightPane().removeAll();
		servletSplitPane.getRightPane().add(new ServletNameTFUI(), BorderLayout.CENTER);
		servletSplitPane.getRightPane().validate();
		
		currentPane++;
	}	

	private void addServletVariableTFUI()
	{
		servletSplitPane.getRightPane().removeAll();
		servletSplitPane.getRightPane().add(new ServletVariableTFUI(), BorderLayout.CENTER);
		servletSplitPane.getRightPane().validate();
		
		currentPane++;
	}	

	private void addServletMethodTFUI()
	{
		servletSplitPane.getRightPane().removeAll();
		servletSplitPane.getRightPane().add(new ServletMethodTFUI(), BorderLayout.CENTER);
		servletSplitPane.getRightPane().validate();
		
		currentPane++;
	}	

	private void addServletResultUI()
	{
		servletSplitPane.getRightPane().removeAll();
		servletSplitPane.getRightPane().add(new ServletResultUI(jd), BorderLayout.CENTER);
		servletSplitPane.getRightPane().validate();
		
		currentPane++;
	}	
*/

	private void addClassClusteringResultUI()
	{
		//this.remove(classWeightPane);
		this.remove(clusterTree);
		getContentPane().add(new ClassClusteringResultUI(selected), BorderLayout.CENTER);
		getContentPane().validate();		
	}	
}