Переглянути джерело

Add ingredient data into the IngredientPanel.

Sam Jaffe 5 роки тому
батько
коміт
ffb04b803d

+ 1 - 1
src/main/lombok/org/leumasjaffe/recipe/model/Amount.java

@@ -73,7 +73,7 @@ public class Amount {
 		return build.toString();
 	}
 	
-	private String unitName() {
+	public String unitName() {
 		switch (unit) {
 		case WEIGHT:
 			return wgt.displayName;

+ 3 - 3
src/main/lombok/org/leumasjaffe/recipe/model/Preparation.java

@@ -1,12 +1,12 @@
 package org.leumasjaffe.recipe.model;
 
-import java.util.HashSet;
-import java.util.Set;
+import java.util.ArrayList;
+import java.util.List;
 
 import lombok.Data;
 
 @Data
 public class Preparation implements RecipeComponent {
-	Set<Ingredient> ingredients = new HashSet<>();
+	List<Ingredient> ingredients = new ArrayList<>();
 	Duration duration;
 }

+ 3 - 3
src/main/lombok/org/leumasjaffe/recipe/model/Step.java

@@ -1,13 +1,13 @@
 package org.leumasjaffe.recipe.model;
 
-import java.util.HashSet;
-import java.util.Set;
+import java.util.ArrayList;
+import java.util.List;
 
 import lombok.Data;
 
 @Data
 public class Step implements RecipeComponent {
-	Set<Ingredient> ingredients = new HashSet<>();
+	List<Ingredient> ingredients = new ArrayList<>();
 	Duration duration;
 	String instruction;
 }

+ 11 - 3
src/main/lombok/org/leumasjaffe/recipe/view/AutoGrowPanel.java

@@ -3,6 +3,7 @@ package org.leumasjaffe.recipe.view;
 import java.awt.Component;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.function.Function;
 import java.util.function.IntFunction;
 import java.util.function.Supplier;
 
@@ -48,10 +49,10 @@ public class AutoGrowPanel extends JPanel {
 	 * @wbp.parser.constructor
 	 */
 	public AutoGrowPanel(Supplier<DocumentListenable> prod) {
-		this(i -> prod.get());
+		this(i -> prod.get(), 1);
 	}
 	
-	public AutoGrowPanel(IntFunction<DocumentListenable> prod) {
+	public AutoGrowPanel(IntFunction<DocumentListenable> prod, int create) {
 		this.prod = prod;
 		
 		this.grow = new AnyActionDocumentListener() {
@@ -64,7 +65,14 @@ public class AutoGrowPanel extends JPanel {
 		};
 
 		setLayout(new VerticalLayout(5));
-		extend();
+		while (create-- > 0) {
+			extend();
+		}
+	}
+
+	public <T> AutoGrowPanel(Function<T, DocumentListenable> function, List<T> ingredients) {
+		this(i -> function.apply(ingredients.get(i)), ingredients.size());
+		
 	}
 
 	private void extend() {

+ 6 - 3
src/main/lombok/org/leumasjaffe/recipe/view/IngredientPanel.java

@@ -11,6 +11,8 @@ import java.util.Locale;
 import javax.swing.event.DocumentListener;
 import javax.swing.text.NumberFormatter;
 
+import org.leumasjaffe.recipe.model.Ingredient;
+
 import javax.swing.JFormattedTextField;
 import java.awt.Font;
 import javax.swing.JLabel;
@@ -20,7 +22,7 @@ public class IngredientPanel extends JPanel implements AutoGrowPanel.DocumentLis
 	private JTextField txtName;
 	private JTextField txtUnit;
 	
-	public IngredientPanel() {
+	public IngredientPanel(final Ingredient ingredient) {
 		GridBagLayout gridBagLayout = new GridBagLayout();
 		gridBagLayout.columnWidths = new int[]{0, 100, 40, 40, 0};
 		gridBagLayout.rowHeights = new int[]{0, 0};
@@ -36,7 +38,7 @@ public class IngredientPanel extends JPanel implements AutoGrowPanel.DocumentLis
 		gbc_label.gridy = 0;
 		add(label, gbc_label);
 		
-		txtName = new JTextField();
+		txtName = new JTextField(ingredient.getName());
 		txtName.setFont(new Font("Source Code Pro", Font.PLAIN, 10));
 		GridBagConstraints gbc_txtName = new GridBagConstraints();
 		gbc_txtName.fill = GridBagConstraints.HORIZONTAL;
@@ -50,6 +52,7 @@ public class IngredientPanel extends JPanel implements AutoGrowPanel.DocumentLis
 		fmtDone.setMinimum(0.0);
 		fmtDone.setCommitsOnValidEdit(true);
 		JFormattedTextField txtAmount = new JFormattedTextField(fmtDone);
+		txtAmount.setValue(ingredient.getAmount().getValue());
 		txtAmount.setFont(new Font("Source Code Pro", Font.PLAIN, 10));
 		GridBagConstraints gbc_txtAmount = new GridBagConstraints();
 		gbc_txtAmount.fill = GridBagConstraints.HORIZONTAL;
@@ -59,7 +62,7 @@ public class IngredientPanel extends JPanel implements AutoGrowPanel.DocumentLis
 		add(txtAmount, gbc_txtAmount);
 		txtAmount.setColumns(4);
 		
-		txtUnit = new JTextField();
+		txtUnit = new JTextField(ingredient.getAmount().unitName());
 		txtUnit.setFont(new Font("Source Code Pro", Font.PLAIN, 10));
 		GridBagConstraints gbc_txtUnit = new GridBagConstraints();
 		gbc_txtUnit.anchor = GridBagConstraints.ABOVE_BASELINE;

+ 1 - 1
src/main/lombok/org/leumasjaffe/recipe/view/PreparationPanel.java

@@ -66,7 +66,7 @@ public class PreparationPanel extends JPanel implements AutoGrowPanel.DocumentLi
 		gbc_lblDuration.gridy = 0;
 		panelLeft.add(lblDuration, gbc_lblDuration);
 		
-		AutoGrowPanel panelIngredients = new AutoGrowPanel(IngredientPanel::new);
+		AutoGrowPanel panelIngredients = new AutoGrowPanel(IngredientPanel::new, step.getIngredients());
 		GridBagConstraints gbc_panelIngredients = new GridBagConstraints();
 		gbc_panelIngredients.gridwidth = 3;
 		gbc_panelIngredients.insets = new Insets(0, 0, 0, 5);

+ 1 - 1
src/main/lombok/org/leumasjaffe/recipe/view/StepPanel.java

@@ -66,7 +66,7 @@ public class StepPanel extends JPanel implements AutoGrowPanel.DocumentListenabl
 		gbc_lblDuration.gridy = 0;
 		panelLeft.add(lblDuration, gbc_lblDuration);
 		
-		AutoGrowPanel panelIngredients = new AutoGrowPanel(IngredientPanel::new);
+		AutoGrowPanel panelIngredients = new AutoGrowPanel(IngredientPanel::new, step.getIngredients());
 		GridBagConstraints gbc_panelIngredients = new GridBagConstraints();
 		gbc_panelIngredients.gridwidth = 3;
 		gbc_panelIngredients.insets = new Insets(0, 0, 0, 5);