浏览代码

Add DurationPanel, CollatedDuration+Panel

Sam Jaffe 5 年之前
父节点
当前提交
f600e5e9f8

+ 17 - 0
src/main/lombok/org/leumasjaffe/recipe/model/CollatedDuration.java

@@ -0,0 +1,17 @@
+package org.leumasjaffe.recipe.model;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+
+@Data @AllArgsConstructor
+public class CollatedDuration {
+	public static final CollatedDuration ZERO =
+			new CollatedDuration(Duration.ZERO, Duration.ZERO, Duration.ZERO);
+	
+	Duration prepTime, cookingTime, totalTime;
+	
+	public CollatedDuration plus(CollatedDuration rhs) {
+		return new CollatedDuration(prepTime.plus(rhs.prepTime),
+				cookingTime.plus(rhs.cookingTime), totalTime.plus(rhs.totalTime));
+	}
+}

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

@@ -7,10 +7,13 @@ import java.util.stream.Stream;
 
 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
 
-@JsonIgnoreProperties({"duration", "ingredients", "components", "ingredientsAsStream"})
+@JsonIgnoreProperties({
+	"duration", "ingredients", "components", "ingredientsAsStream", "collatedDuration"
+})
 interface CompoundRecipeComponent extends RecipeComponent {
 	Stream<? extends RecipeComponent> getComponents();
 	Stream<Ingredient> getIngredientsAsStream();
+	CollatedDuration getCollatedDuration();
 	
 	@Override
 	default Duration getDuration() {

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

@@ -23,5 +23,10 @@ public class Element extends Observable.Instance implements CompoundRecipeCompon
 	@Override
 	public Stream<Phase> getComponents() {
 		return phases.stream();
-	}	
+	}
+
+	public CollatedDuration getCollatedDuration() {
+		return getComponents().map(Phase::getCollatedDuration)
+				.reduce(CollatedDuration.ZERO, CollatedDuration::plus);
+	}
 }

+ 8 - 0
src/main/lombok/org/leumasjaffe/recipe/model/Phase.java

@@ -29,6 +29,14 @@ public class Phase extends Observable.Instance implements CompoundRecipeComponen
 		return cooking.stream();
 	}
 	
+	public CollatedDuration getCollatedDuration() {
+		final Duration prep = preparation.map(Preparation::getDuration).orElse(Duration.ZERO);
+		final Duration rest = this.rest.map(Rest::getDuration).orElse(Duration.ZERO);
+		final Duration cooking = this.cooking.stream().map(Step::getDuration)
+				.reduce(Duration.ZERO, Duration::plus);
+		return new CollatedDuration(prep, cooking, prep.plus(cooking).plus(rest));
+	}
+	
 	public void setPreparation(final @NonNull Preparation p) {
 		preparation = Optional.of(new Preparation(p.duration, this::getIngredients));
 	}

+ 5 - 0
src/main/lombok/org/leumasjaffe/recipe/model/RecipeCard.java

@@ -27,4 +27,9 @@ public class RecipeCard implements CompoundRecipeComponent {
 	public Stream<Ingredient> getIngredientsAsStream() {
 		return getComponents().flatMap(Element::getIngredientsAsStream);
 	}
+
+	public CollatedDuration getCollatedDuration() {
+		return getComponents().map(Element::getCollatedDuration)
+				.reduce(CollatedDuration.ZERO, CollatedDuration::plus);
+	}
 }

+ 28 - 0
src/main/lombok/org/leumasjaffe/recipe/view/CollatedDurationPanel.java

@@ -0,0 +1,28 @@
+package org.leumasjaffe.recipe.view;
+
+import javax.swing.JPanel;
+import javax.swing.JSeparator;
+import javax.swing.SwingConstants;
+
+import org.jdesktop.swingx.HorizontalLayout;
+import org.leumasjaffe.recipe.model.CollatedDuration;
+
+@SuppressWarnings("serial")
+public class CollatedDurationPanel extends JPanel {
+	
+	public CollatedDurationPanel(CollatedDuration duration) {
+		setLayout(new HorizontalLayout(5));
+		
+		DurationPanel panelPrepTime = new DurationPanel("Prep", duration.getPrepTime());
+		add(panelPrepTime);
+		add(new JSeparator(SwingConstants.VERTICAL));
+		
+		DurationPanel panelCookingTime = new DurationPanel("Cooking", duration.getCookingTime());
+		add(panelCookingTime);
+		add(new JSeparator(SwingConstants.VERTICAL));
+		
+		DurationPanel panelTotalTime = new DurationPanel("Total", duration.getTotalTime());
+		add(panelTotalTime);
+	}
+
+}

+ 35 - 0
src/main/lombok/org/leumasjaffe/recipe/view/DurationPanel.java

@@ -0,0 +1,35 @@
+package org.leumasjaffe.recipe.view;
+
+import java.awt.GridBagConstraints;
+import java.awt.GridBagLayout;
+import java.awt.Insets;
+
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+
+import org.leumasjaffe.recipe.model.Duration;
+
+@SuppressWarnings("serial")
+public class DurationPanel extends JPanel {
+	public DurationPanel(String name, Duration duration) {
+		GridBagLayout gridBagLayout = new GridBagLayout();
+		gridBagLayout.columnWidths = new int[]{0, 0, 0};
+		gridBagLayout.rowHeights = new int[]{0, 0};
+		gridBagLayout.columnWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
+		gridBagLayout.rowWeights = new double[]{0.0, Double.MIN_VALUE};
+		setLayout(gridBagLayout);
+		
+		JLabel lblPrep = new JLabel(name + ": ");
+		GridBagConstraints gbc_lblPrep = new GridBagConstraints();
+		gbc_lblPrep.insets = new Insets(0, 0, 0, 5);
+		gbc_lblPrep.gridx = 0;
+		gbc_lblPrep.gridy = 0;
+		add(lblPrep, gbc_lblPrep);
+		
+		JLabel lblPrepTime = new JLabel(duration.toString());
+		GridBagConstraints gbc_lblPrepTime = new GridBagConstraints();
+		gbc_lblPrepTime.gridx = 1;
+		gbc_lblPrepTime.gridy = 0;
+		add(lblPrepTime, gbc_lblPrepTime);
+	}
+}

+ 32 - 3
src/main/lombok/org/leumasjaffe/recipe/view/ElementPanel.java

@@ -13,8 +13,13 @@ import lombok.experimental.FieldDefaults;
 
 import org.jdesktop.swingx.VerticalLayout;
 
-import javax.swing.JButton;
 import javax.swing.JSeparator;
+import java.awt.GridBagLayout;
+import javax.swing.JLabel;
+import java.awt.GridBagConstraints;
+import java.awt.Insets;
+import java.awt.Component;
+import javax.swing.Box;
 
 @SuppressWarnings("serial")
 @FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
@@ -25,10 +30,34 @@ public class ElementPanel extends JScrollPane {
 	public ElementPanel(Element element) {
 		JPanel panelColumnHeader = new JPanel();
 		setColumnHeaderView(panelColumnHeader);
+		GridBagLayout gbl_panelColumnHeader = new GridBagLayout();
+		gbl_panelColumnHeader.columnWidths = new int[]{0, 0, 0, 0};
+		gbl_panelColumnHeader.rowHeights = new int[]{0, 0};
+		gbl_panelColumnHeader.columnWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE};
+		gbl_panelColumnHeader.rowWeights = new double[]{0.0, Double.MIN_VALUE};
+		panelColumnHeader.setLayout(gbl_panelColumnHeader);
 		
-		JButton btnAddStep = new JButton("Add Phase");
-		panelColumnHeader.add(btnAddStep);
+		JLabel lblName = new JLabel(element.getName());
+		GridBagConstraints gbc_lblName = new GridBagConstraints();
+		gbc_lblName.insets = new Insets(0, 0, 0, 5);
+		gbc_lblName.gridx = 0;
+		gbc_lblName.gridy = 0;
+		panelColumnHeader.add(lblName, gbc_lblName);
 		
+		Component horizontalGlue = Box.createHorizontalGlue();
+		GridBagConstraints gbc_horizontalGlue = new GridBagConstraints();
+		gbc_horizontalGlue.insets = new Insets(0, 0, 0, 5);
+		gbc_horizontalGlue.gridx = 1;
+		gbc_horizontalGlue.gridy = 0;
+		panelColumnHeader.add(horizontalGlue, gbc_horizontalGlue);
+		
+		CollatedDurationPanel panelDuration =
+				new CollatedDurationPanel(element.getCollatedDuration());
+		GridBagConstraints gbc_panelDuration = new GridBagConstraints();
+		gbc_panelDuration.gridx = 2;
+		gbc_panelDuration.gridy = 0;
+		panelColumnHeader.add(panelDuration, gbc_panelDuration);
+
 		panelViewPort = new JPanel();
 		setViewportView(panelViewPort);
 		panelViewPort.setLayout(new VerticalLayout(5));