package extractor.ui.clusteringUI;

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

import java.util.*;
import java.lang.*;


public class ClassWeightUI extends JSplitPane implements ActionListener{
	
	
	//left panel
	private JPanel		leftPane;
	private JPanel 		sliderPane;
	private JSlider 	methodSlider;
	private JSlider 	variableSlider;
	private JSlider 	messageSlider;
	private JPanel 		applyBtnPane;
	private JButton 	applyBtn;
	
	//right panel
	private JScrollPane		rightScrollPane;	
	private JTable 			rightTable;
	
	static final int FPS_INIT = 15;
	Dimension dim;

	DefaultTableModel tableModel;
	Object[] columns = null;
	static Object[][] data = null;
	Vector classes;
	
	public ClassWeightUI(Vector selectedClasses)
	{
		this.classes = selectedClasses;
		
		dim = Toolkit.getDefaultToolkit().getScreenSize();
		
		makeLeft();
		makeInitialRight();

		setLeftComponent(leftPane);
		setRightComponent(rightScrollPane);		
		setOneTouchExpandable(true);
		setDividerLocation(((int)dim.getWidth()*2/3)*1/4);
		
	}

	private void makeLeft()
	{
		leftPane = new JPanel(new BorderLayout());
		sliderPane = new JPanel(new GridLayout(3, 0, 10, 30));
		applyBtnPane = new JPanel();
		
        methodSlider = new JSlider(JSlider.HORIZONTAL, 0, 30, FPS_INIT);
        //methodSlider.addChangeListener(this);
        methodSlider.setMajorTickSpacing(10);
        methodSlider.setMinorTickSpacing(1);
        methodSlider.setPaintTicks(true);
        methodSlider.setPaintLabels(true);
        methodSlider.setBorder(BorderFactory.createTitledBorder("메소드의 가중치 : "));

        variableSlider = new JSlider(JSlider.HORIZONTAL, 0, 30, FPS_INIT);
        //variableSlider.addChangeListener(this);
        variableSlider.setMajorTickSpacing(10);
        variableSlider.setMinorTickSpacing(1);
        variableSlider.setPaintTicks(true);
        variableSlider.setPaintLabels(true);
        variableSlider.setBorder(BorderFactory.createTitledBorder("변수의 가중치 : "));

        messageSlider = new JSlider(JSlider.HORIZONTAL, 0, 30, FPS_INIT);
        //messageSlider.addChangeListener(this);
        messageSlider.setMajorTickSpacing(10);
        messageSlider.setMinorTickSpacing(1);
        messageSlider.setPaintTicks(true);
        messageSlider.setPaintLabels(true);
        messageSlider.setBorder(BorderFactory.createTitledBorder("메시지의 가중치 : "));		

		sliderPane.add(methodSlider);
        sliderPane.add(variableSlider);      
        sliderPane.add(messageSlider);       


		applyBtn = new JButton("Apply");
		applyBtn.addActionListener(this);
		applyBtnPane.add(applyBtn);
		
		leftPane.add(sliderPane, BorderLayout.CENTER);		
        leftPane.add(applyBtnPane, BorderLayout.SOUTH);		        
	}
		
	private void makeInitialRight()
	{
		
//for test
		makeData();
		makeColumns();
		
		tableModel = new DefaultTableModel(data, columns);
        rightTable = new JTable(tableModel);
        rightTable.setPreferredScrollableViewportSize(new Dimension(500, 100));
        
        rightScrollPane = new JScrollPane(rightTable);
        rightScrollPane.setBorder(BorderFactory.createTitledBorder("메트릭스 적용결과 : "));				
	}
	
	private void makeData()
	{
		
        data = new Object[24][4];
		
		for(int i=0;i<data.length;i++)
		{
			for(int j=0;j<data[i].length;j++)
			{
				if(j==0)
					data[i][j] = "class " + (i+1);
				else 
					data[i][j] = "";
			}
		}
	}
	
	private void makeColumns()
	{
		columns = new Object[4];
	    columns[0] = "";
		columns[1] = "# of method";
		columns[2] = "# of variable";
	    columns[3] = "# of message";
	}
	
	private void changeData()
	{
		for(int i=0;i<data.length;i++)
		{
			for(int j=0;j<data[i].length;j++)
			{
				if(j!=0)
				{ 
					double random = Math.random();
					data[i][j] = "" + random;
					tableModel.setValueAt(data[i][j], i, j);
				}
			}
		}		
	}
	
	public void actionPerformed(ActionEvent e) 
	{
		if(e.getSource() == applyBtn)
		{
			System.out.println("applyBtn pushed");
			//Performing clustering processes
			//...
			//...
			
			changeData();
		}		
	}		
	/*public static void main(String[] arg)
	{
        final Object[] columnNames = {"", 
        							"# of method", 
                       				"# of variable",
                       				"# of message",
                       				"# of xxx",
                       				"# of yyy"};
        final Object[][] data = {
            {"class A", new Double(3.4), new Double(5), new Double(5), new Double(2.3), new Double(3.7)},
            {"class B", new Double(8.2), new Double(15), new Double(5.1), new Double(12), new Double(9.2)},
            {"class c", new Double(3), new Double(5.9), new Double(9), new Double(12.1) , new Double(13)},
            {"class D", new Double(3.4), new Double(5), new Double(5), new Double(2.3), new Double(3.7)},
            {"class E", new Double(8.2), new Double(15), new Double(5.1), new Double(12), new Double(9.2)},
            {"class F", new Double(3), new Double(5.9), new Double(9), new Double(12.1) , new Double(13)},
            {"class G", new Double(3.4), new Double(5), new Double(5), new Double(2.3), new Double(3.7)},
            {"class H", new Double(8.2), new Double(15), new Double(5.1), new Double(12), new Double(9.2)},
            {"class I", new Double(3), new Double(5.9), new Double(9), new Double(12.1) , new Double(13)},
            {"class J", new Double(3.4), new Double(5), new Double(5), new Double(2.3), new Double(3.7)},
            {"class K", new Double(8.2), new Double(15), new Double(5.1), new Double(12), new Double(9.2)},
            {"class L", new Double(3), new Double(5.9), new Double(9), new Double(12.1) , new Double(13)},
            };

		JFrame jf = new JFrame();
		jf.getContentPane().add(new ClassWeightUI(data, columnNames));
		jf.pack();
		jf.setVisible(true);
	}*/
}