package jp.ac.kyutech.mns.ist.util;
import java.awt.BorderLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JScrollPane;

public class SingleTableSelector<E> extends JDialog {
	private static final long serialVersionUID = -5367037251692601443L;
	public E selectedObj = null;
	public SingleTableSelector(ArrayList<E> adata, JFrame parent, String title,
			String[] cn, String[] fn, String[] bn, int[] wary) {
		super(parent, title, true);
		setLayout(new BorderLayout());
		SelectableJTable<E> table = new SelectableJTable<E>(adata,
				cn,
				fn,
				bn, 
				wary,
				this);
		JScrollPane jsp = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		getContentPane().add(jsp);

		table.addKeyListener(new KeyAdapter(){
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == 27) {// ESC
					selectedObj = null;
					setVisible(false);
				}
			}
		});
		setSize(parent.getWidth()*5/6, parent.getHeight()*4/5);
//		setSize(table.getPreferredSize());
		setLocation(parent.getLocation().x+50, parent.getLocation().y+80/*(parent.getHeight()-this.getHeight())/2*/);
		setVisible(true);
	}
	public void notifySelected(E obj){
		selectedObj = obj;
		setVisible(false);
	}
}
