import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.*;

public class Main extends JFrame {

	private MyTableModel tableModel;
	private JTable tableView;
	private String[] columnNames = { "Sudent ID", "Mark" };
	private String[][] records;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Main frame = new Main();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Main() {
		records = new String[20][2];

		BufferedReader input;
		try {
			// read in the marks from text file "data.txt"
			// Assume there are 20 lines in the file and the fields in a line is
			// separated by a tab.
			input = new BufferedReader(new FileReader("data.txt"));
			try {
				String line = null;
				/*
				 * readLine returns the content of a line. It returns null only
				 * for the END of the file.
				 */
				int i = 0;
				while ((line = input.readLine()) != null) {
					StringTokenizer st = new StringTokenizer(line, "\t");
					records[i][0] = st.nextToken(); // Extract the ID
					records[i][1] = st.nextToken(); // Extract the mark
					i++;
				}
			} finally {
				input.close();
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		}

		/*
		 * Create a JTable view and set its model. The JTable component is
		 * implicitly registered as a listener of tableModel.
		 */
		tableModel = new MyTableModel(records, columnNames);
		tableView = new JTable(tableModel);

		// Make each cell selectable.
		tableView.setRowSelectionAllowed(false);

		StatisticsPanel statsView = new StatisticsPanel(tableView);

		/*
		 * Register listeners (views) with the model.
		 */
		tableModel.addTableModelListener(statsView);

		/* Setup the GUI. */
		setupPane(tableView, statsView);

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 900, 300);
	}

	private void setupPane(JTable studentResultView, StatisticsPanel statsView) {
		/*
		 * Create a Panel to combine the distribution and statistics visual
		 * representations.
		 */
		JPanel right = new JPanel();
		right.setBorder(BorderFactory.createTitledBorder("Statistics"));
		right.setLayout(new BoxLayout(right, BoxLayout.Y_AXIS));
		right.add(statsView);

		JScrollPane left = new JScrollPane(studentResultView);
		left.setBorder(BorderFactory.createTitledBorder("Marks"));
		left.setPreferredSize(new Dimension(left.getPreferredSize().width,
				right.getSize().height));

		/* Create main pane for the application. */
		JPanel mainPane = new JPanel();
		mainPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.X_AXIS));
		mainPane.add(left);
		mainPane.add(Box.createRigidArea(new Dimension(10, 0)));
		mainPane.add(right);
		add(mainPane);
	}

}
