package beaform.gui.formulaeditor;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;

import beaform.Ingredient;

/**
 * This class houses all the components for the Ingredients in the formula editor.
 *
 * @author Steven Post
 *
 */
public class IngredientPane extends JPanel {

	/**
	 * a serial
	 */
	private static final long serialVersionUID = 2342490028064073798L;

	/** A label for the list of ingredients */
	private static final JLabel LBL_INGREDIENTS = new JLabel("Ingredients");

	/** A label for the name of ingredients */
	private static final JLabel LBL_INGREDIENT_NAME = new JLabel("Name");

	/** A label for the amount in an ingredient */
	private static final JLabel LBL_AMOUNT = new JLabel("Amount");

	/** Dimensions for the amount field */
	private static final Dimension DIM_AMOUNT = new Dimension(60, 30);

	/** Dimensions for lists */
	private static final Dimension DIM_LISTS = new Dimension(200, 100);

	/** Dimensions for most text fields */
	private static final Dimension DIM_TEXTFIELDS = new Dimension(100, 30);

	/** A list model to get the list of formulas to the screen */
	private final transient DefaultListModel<Ingredient> ingredients = new DefaultListModel<Ingredient>();

	/**
	 * Constructor
	 */
	public IngredientPane() {
		super(new GridBagLayout());
		init();
	}

	private void init() {
		final GridBagConstraints constraints = new GridBagConstraints();

		int gridy = 0;
		final JList<Ingredient> lstFormulas = new JList<Ingredient>(this.ingredients);
		final JTextField txtNewIngredient = new JTextField();
		final JTextField txtNewIngredientAmount = new JTextField();
		final JButton btnAddIngredient = new JButton("Add ingedrient");
		final JButton btnDelIngredient = new JButton("Del ingedrient");

		constraints.gridx = 0;
		constraints.gridy = gridy;
		constraints.gridwidth = 2;
		this.add(LBL_INGREDIENTS, constraints);
		constraints.gridwidth = 1;

		gridy++;
		constraints.gridx = 0;
		constraints.gridy = gridy;
		constraints.gridheight = 4;
		lstFormulas.setMinimumSize(DIM_LISTS);
		lstFormulas.setPreferredSize(DIM_LISTS);
		lstFormulas.setMaximumSize(DIM_LISTS);
		this.add(lstFormulas, constraints);
		constraints.gridheight = 1;

		constraints.gridx = 1;
		constraints.gridy = gridy;
		LBL_INGREDIENT_NAME.setMinimumSize(DIM_TEXTFIELDS);
		LBL_INGREDIENT_NAME.setPreferredSize(DIM_TEXTFIELDS);
		LBL_INGREDIENT_NAME.setMaximumSize(DIM_TEXTFIELDS);
		this.add(LBL_INGREDIENT_NAME, constraints);

		constraints.gridx = 2;
		constraints.gridy = gridy;
		LBL_AMOUNT.setMinimumSize(DIM_AMOUNT);
		LBL_AMOUNT.setPreferredSize(DIM_AMOUNT);
		LBL_AMOUNT.setMaximumSize(DIM_AMOUNT);
		this.add(LBL_AMOUNT, constraints);

		gridy++;
		constraints.gridx = 1;
		constraints.gridy = gridy;
		txtNewIngredient.setMinimumSize(DIM_TEXTFIELDS);
		txtNewIngredient.setPreferredSize(DIM_TEXTFIELDS);
		txtNewIngredient.setMaximumSize(DIM_TEXTFIELDS);
		this.add(txtNewIngredient, constraints);

		constraints.gridx = 2;
		constraints.gridy = gridy;
		txtNewIngredientAmount.setMinimumSize(DIM_AMOUNT);
		txtNewIngredientAmount.setPreferredSize(DIM_AMOUNT);
		txtNewIngredientAmount.setMaximumSize(DIM_AMOUNT);
		this.add(txtNewIngredientAmount, constraints);

		gridy++;
		constraints.gridx = 1;
		constraints.gridy = gridy;
		btnAddIngredient.addActionListener(new AddIngredientAction(txtNewIngredient, txtNewIngredientAmount, this.ingredients));
		this.add(btnAddIngredient, constraints);

		gridy++;
		constraints.gridx = 1;
		constraints.gridy = gridy;
		btnDelIngredient.addActionListener(new DelIngredientAction(lstFormulas, this.ingredients));
		this.add(btnDelIngredient, constraints);
	}

	/**
	 * Add an ingredient to the list of ingredients.
	 *
	 * @param ingredient The ingredient to add
	 */
	public void addIngredient(final Ingredient ingredient) {
		this.ingredients.addElement(ingredient);
	}

	public Iterator<Ingredient> getIngredients() {
		final List<Ingredient> ingredients = new ArrayList<Ingredient>();
		final int nrOfIngredients = this.ingredients.getSize();
		for (int i = 0; i < nrOfIngredients; i++) {
			ingredients.add(this.ingredients.getElementAt(i));
		}
		return ingredients.iterator();
	}

}

@@ -857,35 +857,24 @@
  JLabel LBL_
-INGREDIENT_
 NAME = new J
@@ -1801,28 +1801,19 @@
 eld txtN
-ewIngredient
+ame
  = new J
@@ -1843,37 +1843,24 @@
 extField txt
-NewIngredient
 Amount = new
@@ -2507,35 +2507,24 @@
 ridy;%0A%09%09LBL_
-INGREDIENT_
 NAME.setMini
@@ -2550,35 +2550,24 @@
 LDS);%0A%09%09LBL_
-INGREDIENT_
 NAME.setPref
@@ -2595,35 +2595,24 @@
 LDS);%0A%09%09LBL_
-INGREDIENT_
 NAME.setMaxi
@@ -2655,19 +2655,8 @@
 LBL_
-INGREDIENT_
 NAME
@@ -2956,36 +2956,27 @@
 ridy;%0A%09%09txtN
-ewIngredient
+ame
 .setMinimumS
@@ -2998,36 +2998,27 @@
 LDS);%0A%09%09txtN
-ewIngredient
+ame
 .setPreferre
@@ -3046,28 +3046,19 @@
 ;%0A%09%09txtN
-ewIngredient
+ame
 .setMaxi
@@ -3097,28 +3097,19 @@
 add(txtN
-ewIngredient
+ame
 , constr
@@ -3172,37 +3172,24 @@
 gridy;%0A%09%09txt
-NewIngredient
 Amount.setMi
@@ -3212,37 +3212,24 @@
 OUNT);%0A%09%09txt
-NewIngredient
 Amount.setPr
@@ -3258,29 +3258,16 @@
 );%0A%09%09txt
-NewIngredient
 Amount.s
@@ -3307,29 +3307,16 @@
 .add(txt
-NewIngredient
 Amount, 
@@ -3464,37 +3464,15 @@
 txtN
-ewIngredient, txtNewIngredien
+ame, tx
 tAmo
@@ -3959,16 +3959,88 @@
 t);%0A%09%7D%0A%0A
+%09/**%0A%09 * Gets all the ingredients.%0A%09 * @return all the ingredients%0A%09 */%0A
 %09public 

Rename some long variables