/**
 * The main GUI for the project.
 * 
 * @author Julia McClellan, Luke Giacalone, Hyun Choi
 * @version 05/18/2016
 */

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;

public class GUI extends JFrame
{
	private ArrayList<Inventory> inventories;
	private Inventory selected;
	private JPanel display, options;
	
	/**
	 * Constructs the GUI for the given inventories.
	 * 
	 * @param i The inventories in the program.
	 */
	public GUI(ArrayList<Inventory> i)
	{
		try
		{
			UIManager.setLookAndFeel(new NimbusLookAndFeel());
		}
		catch(Throwable e){}
		
		addMenu();
		
		inventories = i;
		JPanel panel = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.gridy = 0;
		c.anchor = GridBagConstraints.NORTHWEST;
		
		options = new JPanel();
		panel.add(options, c);
		c.gridy++;
		display = new JPanel();
		panel.add(display, c);
		
		if(inventories != null && inventories.size() != 0)
		{
			for(Inventory inventory: inventories)
			{
				options.add(new InventoryButton(inventory));
			}
			updateSelected(inventories.get(0));
		}
		
		add(panel);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		pack();
		setVisible(true);
		this.setResizable(false);
	}
	
	/**
	 * Replaces the inventory in the central panel with the new inventory.
	 * 
	 * @param i The inventory to display.
	 */
	private void updateSelected(Inventory i)
	{
		display.setVisible(false);
		selected = i;
		display.removeAll();
		display.add(i.getGUI());
		display.setVisible(true);
	}
	
	private void addMenu() {
		JMenuBar menu = new JMenuBar();
		
		JMenu file = new JMenu("File");
		JMenu newMenu = new JMenu("New");
		newMenu.add(new JMenuItem("Inventory"));
		newMenu.add(new JMenuItem("Item"));
		file.add(newMenu);
		file.add(new JMenuItem("Export"));
		file.add(new JMenuItem("Print"));
		menu.add(file);
		
		JMenu edit = new JMenu("Edit");
		edit.add(new JMenuItem("Undo"));
		edit.add(new JMenuItem("Redo"));
		menu.add(edit);
		
		this.setJMenuBar(menu);
	}
	
	/**
	 * A button for the menu bar to select an inventory.
	 */
	private class InventoryButton extends JPanel
	{
		private Inventory inventory;
		
		/**
		 * Constructs the panel.
		 * 
		 * @param i The inventory to be selected with this panel.
		 */
		public InventoryButton(Inventory i)
		{
			inventory = i;
			setBorder(BorderFactory.createCompoundBorder(new LineBorder(Color.BLACK, 1, true), new EmptyBorder(2, 2, 2, 2)));
			setBackground(Color.WHITE);
			add(new JLabel(i.getName()));
			addMouseListener(new MouseListener()
			{
				public void mouseClicked(MouseEvent arg0)
				{
					updateSelected(inventory);
				}
				
				public void mouseEntered(MouseEvent arg0)
				{
					setBackground(Color.LIGHT_GRAY);
				}
				
				public void mouseExited(MouseEvent arg0)
				{
					setBackground(Color.WHITE);
				}
				
				public void mousePressed(MouseEvent arg0){}
				public void mouseReleased(MouseEvent arg0){}
			});
		}
	}
}
@@ -206,16 +206,89 @@
 Layout;%0A
+import java.awt.event.ActionEvent;%0Aimport java.awt.event.ActionListener;%0A
 import j
@@ -2205,44 +2205,206 @@
 ;%0A%09%09
+JMenuItem inventory = 
 new
+ J
 Menu
-.add(new JMenuItem(%22I
+Item(%22Inventory%22);%0A%09%09inventory.addActionListener(new ActionListener()%0A%09%09%7B%0A%09%09%09public void actionPerformed(ActionEvent arg0)%0A%09%09%09%7B%0A%09%09%09%09%0A%09%09%09%7D%0A%09%09%7D);%0A%09%09newMenu.add(i
 nventory
 %22));
@@ -2399,18 +2399,16 @@
 nventory
-%22)
 );%0A%09%09new
@@ -2587,33 +2587,41 @@
 (%22Edit%22);%0A%09%09
-edit.add(
+JMenuItem undo = 
 new JMenuIte
@@ -2633,43 +2633,415 @@
 do%22)
-);%0A%09%09edit.add(new JMenuItem(%22Redo%22)
+;%0A%09%09undo.addActionListener(new ActionListener()%0A%09%09%7B%0A%09%09%09public void actionPerformed(ActionEvent arg0)%0A%09%09%09%7B%0A%09%09%09%09if(Operation.canUndo()) Operation.undoLast();%0A%09%09%09%7D%0A%09%09%7D);%0A%09%09edit.add(undo);%0A%09%09JMenuItem redo = new JMenuItem(%22Redo%22);%0A%09%09redo.addActionListener(new ActionListener()%0A%09%09%7B%0A%09%09%09public void actionPerformed(ActionEvent arg0)%0A%09%09%09%7B%0A%09%09%09%09if(Operation.canRedo()) Operation.redoLast();%0A%09%09%09%7D%0A%09%09%7D);%0A%09%09edit.add(redo
 );%0A%09

Undo and redo functions