import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.ActionEvent;
import javax.swing.KeyStroke;
import java.util.Map;
import java.util.HashMap;

/** 
 * The MainMenu class will serve as the menu bar for JPaint.
 * It will contain all basic functions, i.e. new file, etc. There are keyboard
 * shorts cuts to speed up the process.
 **/
public class MainMenu extends JMenuBar
{
	private Map JMenuItemsMap;
	private JMenu menuFile, menuEdit, menuView, menuHelp;
	private JMenuItem fileNew, fileOpen, fileClose, fileSave, fileSaveAs, 
	filePrint, fileExit, editCut, editCopy, editPaste, editDelete, viewToolBox, 
	viewColorBox, imageClear, imageRotate, helpAbout;
	
	/**
	 * Creates a default constructor for the MainMenu. Instantiates the menus
	 * and the menu items and the keys. Creates a maps to make accessors easier.
	 **/
	public MainMenu()
	{
		// instantiations for menus, menuitems and hotkeys
		initJMenus();
		initJMenuItems();
		initHotKeys();
		
		// creates a hashmap and then adds the items to the hashmap
		JMenuItemsMap = new HashMap(19);
		addJMenuItemsToMap();
		
		// adds everything onto the screen
		addAll();
	}
	
	/** Adds the menubar onto the screen. **/
	public void addAll()
	{
		add(menuFile);
		menuFile.add(fileNew);
		menuFile.add(fileOpen);
		menuFile.add(fileClose);
		menuFile.addSeparator();
		menuFile.add(fileSave);
		menuFile.add(fileSaveAs);
		menuFile.addSeparator();
		menuFile.add(filePrint);
		menuFile.addSeparator();
		menuFile.add(fileExit);
		
		add(menuEdit);
		
		menuEdit.add(editCut);
		menuEdit.add(editCopy);
		menuEdit.add(editPaste);
		menuEdit.addSeparator();
		menuEdit.add(editDelete);
		menuEdit.addSeparator();
		menuEdit.add(imageRotate);
		menuEdit.addSeparator();
		menuEdit.add(imageClear);
		
		add(menuView);
		
		menuView.add(viewToolBox);
		menuView.add(viewColorBox);
		
		add(menuHelp);
		
		menuHelp.add(helpAbout);
	}
	
	/** Adds actionlisteners to each of the menu items. **/
	public void addActionListener(ActionListener listener)
	{
		fileNew.addActionListener(listener);
		fileOpen.addActionListener(listener);
		fileClose.addActionListener(listener);
		fileSave.addActionListener(listener);
		fileSaveAs.addActionListener(listener);
		filePrint.addActionListener(listener);
		fileExit.addActionListener(listener);
		
		editCut.addActionListener(listener);
		editCopy.addActionListener(listener);
		editPaste.addActionListener(listener);
		editDelete.addActionListener(listener);
		
		viewToolBox.addActionListener(listener);
		viewColorBox.addActionListener(listener);
		
		imageRotate.addActionListener(listener);
		imageClear.addActionListener(listener);
		
		helpAbout.addActionListener(listener);
	}
	
	/** This method instantiates all the menus. **/
	public void initJMenus()
	{
		menuFile = new JMenu("File");
		menuEdit = new JMenu("Edit");
		menuView = new JMenu("View");
		menuHelp = new JMenu("About");
	}
	
	/** This method instantiates all the jmenu items. **/
	public void initJMenuItems()
	{
		fileNew = new JMenuItem("New");
		fileOpen = new JMenuItem("Open");
		fileClose = new JMenuItem("Close");
		fileSave = new JMenuItem("Save");
		fileSaveAs = new JMenuItem("Save As");
		filePrint = new JMenuItem("Print");
		fileExit = new JMenuItem("Exit");
		
		editCut = new JMenuItem("Cut");
	 	editCopy = new JMenuItem("Copy");
	 	editPaste = new JMenuItem("Paste");
	 	editDelete = new JMenuItem("Delete");
	
	 	viewToolBox = new JMenuItem("ToolBox");
	 	viewColorBox = new JMenuItem("ColorBox");
	
	 	imageClear = new JMenuItem("Clear Image");
	 	imageRotate = new JMenuItem("Rotate");
	
	 	helpAbout = new JMenuItem("About JPaint");	
	}
	
	/** This method sets all the hot keys for the menu items. **/
	public void initHotKeys()
	{
		fileNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
		fileOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
		fileSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));		
		fileSaveAs.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F12, ActionEvent.CTRL_MASK));
		filePrint.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.CTRL_MASK));
		fileClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, ActionEvent.ALT_MASK));
		fileExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
		viewToolBox.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, ActionEvent.CTRL_MASK));
		viewColorBox.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));
		editCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
		editCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
		editPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK));
		editDelete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, ActionEvent.CTRL_MASK));
		imageRotate.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.CTRL_MASK));
		imageClear.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.ALT_MASK));
		helpAbout.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, ActionEvent.ALT_MASK));
	}
	
	/** This method adds the menu items to the map. **/
	public void addJMenuItemsToMap()
	{
		JMenuItemsMap.put("fileNew", fileNew);
		JMenuItemsMap.put("fileOpen", fileOpen);
		JMenuItemsMap.put("fileClose", fileClose);
		JMenuItemsMap.put("fileSave", fileSave);
		JMenuItemsMap.put("fileSaveAs", fileSaveAs);
		JMenuItemsMap.put("filePrint", filePrint);
		JMenuItemsMap.put("fileExit", fileExit);
		JMenuItemsMap.put("editCut", editCut);
		JMenuItemsMap.put("editCopy", editCopy);
		JMenuItemsMap.put("editPaste", editPaste);
		JMenuItemsMap.put("editDelete", editDelete);
		JMenuItemsMap.put("viewToolBox", viewToolBox);
		JMenuItemsMap.put("viewColorBox", viewColorBox);
		JMenuItemsMap.put("imageRotate", imageRotate);
		JMenuItemsMap.put("imageClear", imageClear);
		JMenuItemsMap.put("helpAbout", helpAbout);
	}
	
	/** Returns the JMenuItem. **/
	public JMenuItem getJMenuItem(String str)
	{
		return (JMenuItem)JMenuItemsMap.get(str);
	}
}