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

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import jp.ac.kyutech.mns.ist.Note;
import edu.umd.cs.piccolo.nodes.PText;

public class LayerSeatmapButtonMenu extends JPopupMenu implements FramePopup {
	LayerSeatmapButton v;
	PText jt;
	JFrame frame;
	private static final long serialVersionUID = -3662668322301800275L;

	public LayerSeatmapButtonMenu(LayerSeatmapButton _v, PText _jt) {
		this.v = _v;
		this.jt = _jt;
		JMenuItem menuItem;

		setLightWeightPopupEnabled(false);

		menuItem = new JMenuItem("cancel");
		add(menuItem);

		addSeparator();

		menuItem = new JMenuItem(jt.getText()+"番を拡大");
		add(menuItem);
		menuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				v.jumpByNumber(Integer.parseInt(jt.getText()));
			}
		});

		if (jt.getTextPaint() == Color.red){ //コンフリクト
			menuItem = new JMenuItem(jt.getText()+"番を座席から外す");
			add(menuItem);
			menuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Note stu = (Note)jt.getAttribute("student");
					if (stu != null){
						v.removeJump(stu);
						v.addLeave(stu);
					}
				}
			});
		}
		if (jt.getTextPaint() == Color.red){ //コンフリクト
			menuItem = new JMenuItem(jt.getText()+"番以外を座席から外す");
			add(menuItem);
			menuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Note mystu = (Note)jt.getAttribute("student");
					ArrayList<Note> pts = new ArrayList<Note>(v.jumps);
					pts.remove(mystu);
					for(Note stu: pts){
						v.removeJump(stu);
						v.addLeave(stu);
					}
				}
			});
		}

	}

	public void showWithFrame(Component c, int x, int y, JFrame _f) {
		frame = _f;
		show(c, x, y);
	}

}
