/*
Tiamat - a role-playing game framework
ScriptTablePopupMenu.java - popup menu for ScriptTable
 
 Copyright  (C) 2006 Hugues Johnson
 
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
the GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package com.huguesjohnson.tiamat.tools.scriptTable;

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

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

/**
 * @author Hugues Johnson
 * ScriptTablePopupMenu - popup menu for ScriptTable
 */
public class ScriptTablePopupMenu extends JPopupMenu implements ActionListener{
	/* Determines if a de-serialized file is compatible with this class. See <a href="http://java.sun.com/j2se/1.5.0/docs/guide/serialization/spec/class.html">Java Object Serialization Specifications</a>. */
	private static final long serialVersionUID=6481775396218824747L;

	private ScriptTable parent;
	private JMenuItem expandMenuItem;
	private JMenuItem collapseMenuItem;
	private int row;
	
	public ScriptTablePopupMenu(ScriptTable parent){
		super();
		//set the parent
		this.parent=parent;
		//add menu items
	    JMenuItem addMenuItem=new JMenuItem("Add a new script entry..");
	    addMenuItem.setMnemonic('A');
	    addMenuItem.setActionCommand("add");
	    addMenuItem.addActionListener(this);
	    this.add(addMenuItem);
	    JMenuItem editMenuItem=new JMenuItem("Edit selected script entry..");
	    editMenuItem.setMnemonic('E');
	    editMenuItem.setActionCommand("edit");
	    editMenuItem.addActionListener(this);
	    this.add(editMenuItem);
	    JMenuItem deleteMenuItem=new JMenuItem("Delete selected script entry");
	    deleteMenuItem.setMnemonic('D');
	    deleteMenuItem.setActionCommand("delete");
	    deleteMenuItem.addActionListener(this);
	    this.add(deleteMenuItem);
	    this.addSeparator();
	    this.expandMenuItem=new JMenuItem("Expand this row");
	    this.expandMenuItem.setMnemonic('x');
	    this.expandMenuItem.setActionCommand("expand");
	    this.expandMenuItem.addActionListener(this);
	    this.add(this.expandMenuItem);
	    this.collapseMenuItem=new JMenuItem("Collapse this row");
	    this.collapseMenuItem.setMnemonic('C');
	    this.collapseMenuItem.setActionCommand("collapse");
	    this.collapseMenuItem.addActionListener(this);
	    this.add(this.collapseMenuItem);
	}
	
	public void show(Point point){
		//determine whether expand or collapse should be enabled
		this.row=this.parent.rowAtPoint(point);
		int rowHeight=this.parent.getRowHeight(this.row);
		int defaultRowHeight=this.parent.getDefaultRowHeight();
		if(rowHeight>defaultRowHeight){
			this.collapseMenuItem.setEnabled(true);
			this.expandMenuItem.setEnabled(false);
		} else{
			this.collapseMenuItem.setEnabled(false);
			this.expandMenuItem.setEnabled(true);
		}		
		//now show the menu
		this.show(this.parent,(int)point.getX(),(int)point.getY());
	}

	public void actionPerformed(ActionEvent actionEvent){
		String actionCommand=actionEvent.getActionCommand();
		if(actionCommand.equals("add")){
			this.parent.onAdd();
		} else if(actionCommand.equals("edit")){
			this.parent.onEdit(this.row);
		} else if(actionCommand.equals("delete")){
			this.parent.onDelete(this.row);
		} else if(actionCommand.equals("expand")){
			this.parent.onExpandRow(this.row);
		} else if(actionCommand.equals("collapse")){
			this.parent.onCollapseRow(this.row);
		}
	}
}