package jp.ac.kyutech.mns.ist.sheetmap;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class SingleSelector4SeatmapController<E> extends JDialog {
	private static final long serialVersionUID = 5168438833044266747L;
	ArrayList<E> elements;
	ArrayList<JButton> buttons;
	public E selectedObj = null;
	public SingleSelector4SeatmapController(ArrayList<E> elelist, JFrame parent, String title){
		super(parent, title, true);
		elements = elelist;
		
		buttons = new ArrayList<JButton>();
		
		JPanel jp = new JPanel();
		jp.setLayout(new BoxLayout(jp, BoxLayout.Y_AXIS));
		for(E ele: elements){
			ObjectHoldingJButton<E> jb = new ObjectHoldingJButton<E>(ele, this);
			jp.add(jb);
		}
		getContentPane().add(new JScrollPane(jp), BorderLayout.CENTER);
		
		JButton cancel = new JButton("cancel");
		cancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				selectedObj = null;
				setVisible(false);
			}
		});
		getContentPane().add(cancel, BorderLayout.SOUTH);
		pack();
		setLocation(parent.getLocation().x+30, parent.getLocation().y+30);
	}
	public void notifySelected(E obj){
		selectedObj = obj;
//		System.out.println("notify "+selectedObj.toString());
		setVisible(false);
	}
}

class ObjectHoldingJButton<E> extends JButton {
	private static final long serialVersionUID = -2622093728012785495L;
	E object;
	SingleSelector4SeatmapController<E> sel;
	public ObjectHoldingJButton(E o, SingleSelector4SeatmapController<E> _sel){
		super(o.toString());
		object = o;
		sel = _sel;
		
		addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				sel.notifySelected(object);
			}
		});
	}
}