package mealplaner.recipes.gui.dialogs.recepies;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.util.Optional;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import mealplaner.BundleStore;
import mealplaner.commons.NonnegativeInteger;
import mealplaner.gui.commons.ButtonPanelBuilder;
import mealplaner.gui.commons.InputField;
import mealplaner.gui.commons.NonnegativeIntegerInputField;
import mealplaner.recipes.model.Recipe;
import mealplaner.recipes.provider.IngredientProvider;

public class RecipeInput extends JDialog {
	private static final long serialVersionUID = 1L;
	private JFrame parentFrame;
	private JPanel dataPanel;
	private RecipeTable recipeTable;
	private InputField<NonnegativeInteger> nonnegativeIntegerInputField;
	private Optional<Recipe> enteredRecipe;

	public RecipeInput(JFrame parentFrame, String label) {
		super(parentFrame, label, true);
		this.parentFrame = parentFrame;
		this.dataPanel = new JPanel();
		this.dataPanel.setLayout(new BorderLayout());
		recipeTable = new RecipeTable();
	}

	public Optional<Recipe> showDialog(Optional<Recipe> recipe,
			BundleStore bundles, IngredientProvider ingredients) {
		display(recipe, bundles, ingredients);
		dispose();
		return enteredRecipe;
	}

	private void display(Optional<Recipe> recipe,
			BundleStore bundles, IngredientProvider ingredients) {
		nonnegativeIntegerInputField = new NonnegativeIntegerInputField(
				bundles.message("recipeNumberOfPortionsField"),
				new NonnegativeInteger(4));
		JScrollPane tablescroll = new JScrollPane(
				recipeTable.setupTable(recipe, bundles, ingredients));
		JPanel buttonPanel = displayButtons(bundles);
		JPanel inputFieldPanel = new JPanel();
		inputFieldPanel.setLayout(new GridLayout(0, 2));
		nonnegativeIntegerInputField.addToPanel(inputFieldPanel);
		dataPanel.add(inputFieldPanel, BorderLayout.NORTH);
		dataPanel.add(tablescroll, BorderLayout.CENTER);
		dataPanel.add(buttonPanel, BorderLayout.SOUTH);
		getContentPane().add(dataPanel);

		setSize(300, 300);
		setLocationRelativeTo(parentFrame);
		setVisible(true);
	}

	private JPanel displayButtons(BundleStore bundles) {
		return new ButtonPanelBuilder(bundles)
				.addCancelDialogButton(this)
				.addOkButton(getSaveListener(recipeTable))
				.build();
	}

	private ActionListener getSaveListener(RecipeTable recipeTable) {
		return action -> {
			enteredRecipe = recipeTable
					.getRecipe(nonnegativeIntegerInputField.getUserInput().value);
			setVisible(false);
			dispose();
		};
	}
}

@@ -1266,16 +1266,42 @@
 ents) %7B%0A
+%09%09enteredRecipe = recipe;%0A
 %09%09displa
@@ -1637,306 +1637,306 @@
 %0A%09%09J
-ScrollPane tablescroll = new JScrollPane(%0A%09%09%09%09recipeTable.setupTable(recipe, bundles, ingredients));%0A%09%09JPanel buttonPanel = displayButtons(bundles);%0A%09%09JPanel inputFieldPanel = new JPanel();%0A%09%09inputFieldPanel.setLayout(new GridLayout(0, 2));%0A%09%09nonnegativeIntegerInputField.addToPanel(inputFieldPanel
+Panel inputFieldPanel = new JPanel();%0A%09%09inputFieldPanel.setLayout(new GridLayout(0, 2));%0A%09%09nonnegativeIntegerInputField.addToPanel(inputFieldPanel);%0A%09%09JScrollPane tablescroll = new JScrollPane(%0A%09%09%09%09recipeTable.setupTable(recipe, bundles, ingredients));%0A%09%09JPanel buttonPanel = displayButtons(bundles
 );%0A%09

Fix RecipeInput: when cancelled, there was a NullpointerException