

package com.sixrr.metrics.ui.metricdisplay;

import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.table.JTableHeader;

import com.intellij.openapi.project.Project;
import com.sixrr.metrics.Metric;
import com.sixrr.metrics.MetricType;

class MetricTableHeaderMouseListener extends MouseAdapter
{
	private final Project project;
	private final JTable table;

	MetricTableHeaderMouseListener(Project project, JTable table)
	{
		super();
		this.project = project;
		this.table = table;
	}

	@Override
	public void mouseClicked(MouseEvent e)
	{
		super.mouseClicked(e);
		final int button = e.getButton();
		final int x = e.getX();
		final int y = e.getY();
		final JTableHeader tableHeader = table.getTableHeader();
		final Point point = new Point(x, y);
		final int column = tableHeader.columnAtPoint(point);
		if(column == -1)
		{
			return;
		}
		table.setColumnSelectionInterval(column, column);
		table.repaint();

		if(button == MouseEvent.BUTTON3 || e.isControlDown())
		{
			showPopupMenu(e);
		}
		else if(button == MouseEvent.BUTTON1)
		{
			final MetricTableModel model = (MetricTableModel) table.getModel();
			final int modelColumn = table.convertColumnIndexToModel(column);
			final int sortColumn = model.getSortColumn();
			if(sortColumn == modelColumn)
			{
				final boolean ascending = model.isAscending();
				model.changeSort(modelColumn, !ascending);
			}
			else
			{
				model.changeSort(modelColumn, true);
			}
			tableHeader.repaint();
			table.setColumnSelectionInterval(column, column);
			table.repaint();
		}
	}

	private void showPopupMenu(MouseEvent e)
	{
		final int selectedColumn = table.getSelectedColumn();
		if(selectedColumn == -1 || selectedColumn == 0)
		{
			return;
		}
		final JPopupMenu popup = new JPopupMenu();

		final MetricTableModel model = (MetricTableModel) table.getModel();
		final Metric metric = model.getMetricForColumn(selectedColumn).getMetric();
		final MetricType metricType = metric.getType();
		if(model.hasDiff())
		{
			popup.add(new JMenuItem(new ShowDiffDistributionAction(project, table)));
			popup.add(new JMenuItem(new ShowDiffHistogramAction(project, table)));
		}
		else
		{
			if(!metricType.equals(MetricType.RecursiveCount) && !metricType.equals(MetricType.RecursiveRatio))
			{
				popup.add(new JMenuItem(new ShowDistributionAction(project, table)));
				popup.add(new JMenuItem(new ShowHistogramAction(project, table)));
			}
			if(metricType.equals(MetricType.Count))
			{
				popup.add(new JMenuItem(new ShowPieChartAction(project, table)));
			}
			popup.add(new JMenuItem(new ShowExplanationAction(project, table)));
		}
		final int x = e.getX();
		final int y = e.getY();

		final JTableHeader tableHeader = table.getTableHeader();
		popup.show(tableHeader, x, y);
	}
}
