package jp.ac.kyutech.mns.ist;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;

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

import jp.ac.kyutech.mns.ist.plugin.DefaultPlugin;
import jp.ac.kyutech.mns.ist.util.MultiDisplay;

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

public class InspectorPlugin extends DefaultPlugin implements ActionListener {
	JFrame srcjframe;
	InspectorHandler handler;
	InspectorPanel panel;
	PCanvas canvas;
	boolean isActive = false;
	public InspectorPlugin(PCanvas can, JFrame f){
		canvas = can;
		srcjframe = f;
		handler = new InspectorHandler(can, f);
		
	}
	public void setInspectorMode(boolean b) {
		if (b){
			handler.setEventFilter(new PInputEventFilter(InputEvent.BUTTON2_MASK));
			canvas.addInputEventListener(handler);
//			SimpleATN.openMemoryMonitor();
		} else {
			canvas.removeInputEventListener(handler);
		}
		handler.inspectorFrame.setVisible(b);
		handler.inspectorFrame.setSize(250,600);
		MultiDisplay.alineWindow(srcjframe, handler.inspectorFrame, 1, true);
		isActive = b;
	}

	public void actionPerformed(ActionEvent e){
		System.out.println(e.getActionCommand());
		if (e.getActionCommand().equals("disable")){
			setInspectorMode(false);
		} else if (e.getActionCommand().equals("enable")){
			setInspectorMode(true);
		} else if (e.getActionCommand().equals("reset inspector window position")){
			MultiDisplay.alineWindow(srcjframe, handler.inspectorFrame, 1, true);
		}
	}
	public void addMenu(JPopupMenu menu){
		JMenu insM;

		insM = new JMenu("Inspector");
		
		JMenuItem mi;

		mi = new JMenuItem("reset inspector window position");
		mi.addActionListener(this);
		insM.add(mi);

		if (isActive) mi = new JMenuItem("disable");
		else mi = new JMenuItem("enable");
		mi.addActionListener(this);
		insM.add(mi);
		
		
		menu.add(insM);
	}
}
