package jp.ac.kyutech.mns.ist;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.geom.Point2D;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.KeyStroke;

import edu.umd.cs.piccolo.PCanvas;
import edu.umd.cs.piccolo.event.PInputEvent;

public class PopupMenu_forZoomRegion extends JPopupMenu {
	private static final long serialVersionUID = 7741402910818721975L;
	public static boolean isopening = false;
	
	Sheet sheet;
	PInputEvent ie;
	PCanvas canvas;
	ZoomRegionHandler zrh;

	public PopupMenu_forZoomRegion(Sheet aSheet, PInputEvent _ie, PCanvas _canvas , ZoomRegionHandler _zrh/*, JFrame _frame*/) {
		sheet = aSheet;
		ie = _ie;
		canvas = _canvas;
		zrh = _zrh;

		setLightWeightPopupEnabled(false);

		JMenuItem menuItem;

		menuItem = new JMenuItem("cancel");
		add(menuItem);
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0));//メニュー表示時のショートカットキー
		menuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
		    	isopening = false;
				zrh.dismissRegionLater();
			}
		});
		
		addSeparator();

		menuItem = new JMenuItem("選択部分を集約表示（全画面）");
		add(menuItem);
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));//メニュー表示時のショートカットキー
		menuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sheet.collect(zrh.selection, KeyEvent.VK_ENTER);
				isopening = false;
				zrh.dismissRegionLater();
			}
		});

		menuItem = new JMenuItem("選択部分を集約表示");
		add(menuItem);
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, 0));//メニュー表示時のショートカットキー
		menuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sheet.collect(zrh.selection, KeyEvent.VK_F);
				isopening = false;
				zrh.dismissRegionLater();
			}
		});

		menuItem = new JMenuItem("選択部分を参照表示");
		add(menuItem);
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, 0));//メニュー表示時のショートカットキー
		menuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sheet.collect(zrh.selection, KeyEvent.VK_R);
				isopening = false;
				zrh.dismissRegionLater();
			}
		});

		addSeparator(); //区切り線
		
		menuItem = new JMenuItem("選択部分を筆席マップとする");
		add(menuItem);
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M, 0));//メニュー表示時のショートカットキー
		menuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sheet.collect(zrh.selection, KeyEvent.VK_M);
				isopening = false;
				zrh.dismissRegionLater();
			}
		});
		
		//領域フィルタ用
		JMenu regionM = new JMenu("領域フィルタ");
		add(regionM);
		
		menuItem = new JMenuItem("選択部分を領域フィルタに追加(5)");
		regionM.add(menuItem);
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_5, 0));//メニュー表示時のショートカットキー
		menuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ConfigWindow.configWin.filter.addRegionFilter(sheet, zrh.selection, KeyEvent.VK_5);
				isopening = false;
				zrh.dismissRegionLater();
			}
		});
		menuItem = new JMenuItem("選択部分を領域フィルタに追加(3)");
		regionM.add(menuItem);
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_3, 0));//メニュー表示時のショートカットキー
		menuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ConfigWindow.configWin.filter.addRegionFilter(sheet, zrh.selection, KeyEvent.VK_3);
				isopening = false;
				zrh.dismissRegionLater();
			}
		});
		menuItem = new JMenuItem("選択部分を領域フィルタに追加(1)");
		regionM.add(menuItem);
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, 0));//メニュー表示時のショートカットキー
		menuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ConfigWindow.configWin.filter.addRegionFilter(sheet, zrh.selection, KeyEvent.VK_1);
				isopening = false;
				zrh.dismissRegionLater();
			}
		});

		showPopup();
	}

	public void showPopup() {
		Point2D cp = ie.getPositionRelativeTo(canvas.getCamera());

		show(canvas, (int)cp.getX(), (int)cp.getY());
		
		isopening = true;
	}
	
}
