// TableFrame.java
/*
 Demonstrate a couple tables using one table model.
*/

import java.awt.*;
import javax.swing.*;
import java.util.*;

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

import javax.swing.table.*;


class TableFrame extends JFrame {
	private BasicTableModel model;

	private JTable table;
	private JTable table2;
	
	JButton columnButton;
	JButton rowButton;
	JButton deleteButton;
	JButton loadButton;
	JButton saveButton;
	JComponent content;
		
	public TableFrame(String title) {
		super(title);
		content = (JComponent)getContentPane();
		content.setLayout(new BorderLayout(6,6));
		
		// Create a table model
		model = new BasicTableModel();
		
		// Create a table using that model
		table = new JTable(model);
		
		// there are many options for col resize strategy
		//table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		// JTable.AUTO_RESIZE_OFF
		
		// Create a scroll pane in the center, and put
		// the table in it
		JScrollPane scrollpane = new JScrollPane(table);
		scrollpane.setPreferredSize(new Dimension(300,200));
		content.add(scrollpane, BorderLayout.CENTER);
		
		// Create a second table using the same model, and put in the south
		JTable table2 = new JTable(model);
		scrollpane = new JScrollPane(table2);
		scrollpane.setPreferredSize(new Dimension(300,200));
		content.add(scrollpane, BorderLayout.SOUTH);
		
		// Create a bunch of controls in a box
		JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		content.add(panel, BorderLayout.EAST);

		rowButton = new JButton("Add Row");
		panel.add(rowButton);
		rowButton.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int i = model.addRow();
					table.clearSelection();
					table.addRowSelectionInterval(i, i);
				}
			}
		);
		
		columnButton = new JButton("Add Column");
		panel.add(columnButton);
		columnButton.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String result = JOptionPane.showInputDialog("What name for the new column?");
					if (result != null) {
						model.addColumn(result);
					}
				}
			}
		);
		
		deleteButton = new JButton("Delete Row");
		panel.add(deleteButton);
		deleteButton.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int row = table.getSelectedRow();
					if (row!=-1) model.deleteRow(row);
				}
			}
		);		
		
		loadButton = new JButton("Load File");
		panel.add(loadButton);
		loadButton.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JFileChooser chooser = new JFileChooser(".");
					int status = chooser.showOpenDialog(TableFrame.this);
					if (status == JFileChooser.APPROVE_OPTION) {
						model.loadFile(chooser.getSelectedFile());
					}
				}
			}
		);
		
		saveButton = new JButton("Save File");
		panel.add(saveButton);
		saveButton.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JFileChooser chooser = new JFileChooser(".");
					int status = chooser.showSaveDialog(TableFrame.this);
					if (status == JFileChooser.APPROVE_OPTION) {
						model.saveToFile(chooser.getSelectedFile());
					}
				}
			}
		);
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		pack();
		setVisible(true);
	}
	
	
	static public void main(String[] args) 
	{
		new TableFrame("TableFrame");
	}
}
