Pārlūkot izejas kodu

Add a separate preparation panel

Sam Jaffe 5 gadi atpakaļ
vecāks
revīzija
c4cc35bd88

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

@@ -2,7 +2,6 @@ package org.leumasjaffe.recipe.model;
 
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.List;
 import java.util.Optional;
 import java.util.stream.Collectors;
@@ -17,7 +16,7 @@ public class Card {
 	int id = 0; // TODO Fix this
 	int[] dependsOn = {}; // decltype(id)[]
 	String vessel = "";
-	Optional<Step> preparation = Optional.empty();
+	Optional<Preparation> preparation = Optional.empty();
 	List<Step> cooking = new ArrayList<>();
 	Optional<Rest> rest = Optional.empty();
 	
@@ -28,7 +27,7 @@ public class Card {
 	
 	@JsonIgnore
 	Stream<Ingredient> getIngredientsAsStream() {
-		return preparation.map(Collections::singletonList).orElse(cooking).stream()
-				.flatMap(s -> s.getIngredients().stream());
+		return preparation.map(p -> p.getIngredients().stream())
+				.orElse(cooking.stream().flatMap(s -> s.getIngredients().stream()));
 	}
 }

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

@@ -0,0 +1,15 @@
+package org.leumasjaffe.recipe.model;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import lombok.Data;
+
+@Data
+public class Preparation {
+	Set<Ingredient> ingredients = new HashSet<>();
+	Map<String, String> instructions = new HashMap<>();
+	Duration duration;
+}

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

@@ -3,6 +3,7 @@ package org.leumasjaffe.recipe.view;
 import javax.swing.JPanel;
 
 import org.leumasjaffe.recipe.model.Card;
+import org.leumasjaffe.recipe.model.Preparation;
 import org.leumasjaffe.recipe.model.Rest;
 import org.leumasjaffe.recipe.model.Step;
 import org.jdesktop.swingx.VerticalLayout;
@@ -14,11 +15,15 @@ public class CardPanel extends JPanel {
 	public CardPanel(final Card card) {		
 		setLayout(new VerticalLayout(5));
 		
-		card.getPreparation().ifPresent(this::addStep);
+		card.getPreparation().ifPresent(this::addPrep);
 		card.getCooking().forEach(this::addStep);
 		card.getRest().ifPresent(this::addRest);
 	}
 	
+	void addPrep(final Preparation step) {
+		add(new PreparationPanel(step));
+	}
+	
 	void addStep(final Step step) {
 		add(new StepPanel(steps++, step));
 	}

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

@@ -0,0 +1,97 @@
+package org.leumasjaffe.recipe.view;
+
+import javax.swing.JPanel;
+import javax.swing.event.DocumentListener;
+
+import org.leumasjaffe.recipe.model.Preparation;
+
+import java.awt.GridBagLayout;
+
+import java.awt.GridBagConstraints;
+import java.awt.Insets;
+
+import javax.swing.JLabel;
+import javax.swing.JTextPane;
+import java.awt.Component;
+import javax.swing.Box;
+import java.awt.Dimension;
+
+@SuppressWarnings("serial")
+public class PreparationPanel extends JPanel implements AutoGrowPanel.DocumentListenable {
+	private JLabel lblIndex;
+	private JTextPane txtpnInstructions;
+		
+	public PreparationPanel(Preparation step) {
+		GridBagLayout gridBagLayout = new GridBagLayout();
+		gridBagLayout.columnWidths = new int[]{0, 0, 0};
+		gridBagLayout.rowHeights = new int[]{0, 0};
+		gridBagLayout.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
+		gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
+		setLayout(gridBagLayout);
+		
+		JPanel panelLeft = new JPanel();
+		panelLeft.setPreferredSize(new Dimension(200, 50));
+		GridBagConstraints gbc_panelLeft = new GridBagConstraints();
+		gbc_panelLeft.insets = new Insets(0, 0, 0, 5);
+		gbc_panelLeft.fill = GridBagConstraints.BOTH;
+		gbc_panelLeft.gridx = 0;
+		gbc_panelLeft.gridy = 0;
+		add(panelLeft, gbc_panelLeft);
+		GridBagLayout gbl_panelLeft = new GridBagLayout();
+		gbl_panelLeft.columnWidths = new int[]{0, 0, 0, 0};
+		gbl_panelLeft.rowHeights = new int[]{0, 0, 0};
+		gbl_panelLeft.columnWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE};
+		gbl_panelLeft.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
+		panelLeft.setLayout(gbl_panelLeft);
+		
+		lblIndex = new JLabel("");
+		GridBagConstraints gbc_lblIndex = new GridBagConstraints();
+		gbc_lblIndex.fill = GridBagConstraints.HORIZONTAL;
+		gbc_lblIndex.insets = new Insets(0, 0, 5, 5);
+		gbc_lblIndex.gridx = 0;
+		gbc_lblIndex.gridy = 0;
+		panelLeft.add(lblIndex, gbc_lblIndex);
+		
+		Component horizontalGlue = Box.createHorizontalGlue();
+		GridBagConstraints gbc_horizontalGlue = new GridBagConstraints();
+		gbc_horizontalGlue.insets = new Insets(0, 0, 5, 5);
+		gbc_horizontalGlue.gridx = 1;
+		gbc_horizontalGlue.gridy = 0;
+		panelLeft.add(horizontalGlue, gbc_horizontalGlue);
+		
+		JLabel lblDuration = new JLabel("Duration");
+		GridBagConstraints gbc_lblDuration = new GridBagConstraints();
+		gbc_lblDuration.insets = new Insets(0, 0, 5, 0);
+		gbc_lblDuration.gridx = 2;
+		gbc_lblDuration.gridy = 0;
+		panelLeft.add(lblDuration, gbc_lblDuration);
+		
+		AutoGrowPanel panelIngredients = new AutoGrowPanel(IngredientPanel::new);
+		GridBagConstraints gbc_panelIngredients = new GridBagConstraints();
+		gbc_panelIngredients.gridwidth = 3;
+		gbc_panelIngredients.insets = new Insets(0, 0, 0, 5);
+		gbc_panelIngredients.fill = GridBagConstraints.BOTH;
+		gbc_panelIngredients.gridx = 0;
+		gbc_panelIngredients.gridy = 1;
+		panelLeft.add(panelIngredients, gbc_panelIngredients);
+		
+		txtpnInstructions = new JTextPane();
+		txtpnInstructions.setPreferredSize(new Dimension(200, 30));
+		txtpnInstructions.setText("Instructions");
+		GridBagConstraints gbc_txtpnInstructions = new GridBagConstraints();
+		gbc_txtpnInstructions.fill = GridBagConstraints.BOTH;
+		gbc_txtpnInstructions.gridx = 1;
+		gbc_txtpnInstructions.gridy = 0;
+		add(txtpnInstructions, gbc_txtpnInstructions);
+	}
+
+	@Override
+	public void addDocumentListener(DocumentListener dl) {
+		this.txtpnInstructions.getDocument().addDocumentListener(dl);
+	}
+
+	@Override
+	public void removeDocumentListener(DocumentListener dl) {
+		this.txtpnInstructions.getDocument().removeDocumentListener(dl);		
+	}
+}

+ 3 - 1
src/test/resources/example.json

@@ -26,7 +26,9 @@
               "minSeconds": 300,
               "maxSeconds": 600
             },
-            "instruction": "Expode"
+            "instructions": {
+              
+            }
           }
         }
       ]