package parser.ServletParser.library;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.table.TableColumn;
import javax.swing.border.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.tree.*;

public class MetricsSelectDlg extends JDialog {
	private JTreeTable table;
	public boolean startPressed = false;
	private JButton startButton;
	private JButton cancelButton;
	private JButton selAllButton;
	private JButton unselAllButton;
	MetricsSelectionValues values;

	public MetricsSelectDlg(boolean colSel[])
	{
		super();
		startPressed = false;
		setModal(true);
		setTitle("QA Metrics");
		setLocation(100,100);
		setSize(new Dimension(500,400));

		values = new MetricsSelectionValues();
		values.initializeData(colSel);

		Container pane = getContentPane();
		pane.setLayout(new BorderLayout());

		table = new JTreeTable(values);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

		TableColumnModel tcm = table.getColumnModel();
		for (int i = 0; i < 3; i++) {
			TableColumn tc = tcm.getColumn(i);
			if (i == 0) tc.setPreferredWidth(250);
			else if (i == 1) tc.setPreferredWidth(150);
			MetricsSelectionHeaderRenderer hr = new MetricsSelectionHeaderRenderer();
			tc.setHeaderRenderer(hr);
		}

		table.setDefaultRenderer(String.class,new MetricsSelectItemRenderer());
		
		JScrollPane jsp = new JScrollPane(table);
		jsp.setPreferredSize(new Dimension(500,350));

		JPanel buttonPane = new JPanel();
		JPanel inButtonPane = new JPanel();
		inButtonPane.setLayout(new GridLayout(1,4,20,5));
		startButton = new JButton("Start");
		startButton.setPreferredSize(new Dimension(100,30));
		inButtonPane.add(startButton);
		cancelButton = new JButton("Cancel");
		cancelButton.setPreferredSize(new Dimension(100,30));
		inButtonPane.add(cancelButton);
		selAllButton = new JButton("Select All");
		selAllButton.setPreferredSize(new Dimension(100,30));
		inButtonPane.add(selAllButton);
		unselAllButton = new JButton("Unselect All");
		unselAllButton.setPreferredSize(new Dimension(100,30));
		inButtonPane.add(unselAllButton);
		inButtonPane.setPreferredSize(new Dimension(500,30));
		buttonPane.add(inButtonPane);
		buttonPane.setPreferredSize(new Dimension(500,50));

		JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,jsp,buttonPane);
		splitPane.setDividerLocation(330);
		pane.add(splitPane,BorderLayout.CENTER);

		startButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				startPressed = true;
				setVisible(false);}
		});
		cancelButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				startPressed = false;
				setVisible(false);}
		});
		selAllButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				values.setCheckAll();
				table.repaint();}
		});
		unselAllButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				values.setUncheckAll();
				table.repaint();}
		});

	}

	public void getSelectedItems(boolean items[]) {
		values.getSelectedItems(items);
	}
}