Browse Source

Add duration hookups, minimally alter tests to pass.

Sam Jaffe 5 years ago
parent
commit
e7dda3b3c0

+ 5 - 2
src/main/lombok/org/leumasjaffe/recipe/model/Rest.java

@@ -1,14 +1,17 @@
 package org.leumasjaffe.recipe.model;
 
+import org.leumasjaffe.observer.Observable;
+
 import lombok.AccessLevel;
 import lombok.AllArgsConstructor;
 import lombok.Data;
+import lombok.EqualsAndHashCode;
 import lombok.Getter;
 import lombok.NoArgsConstructor;
 import lombok.experimental.FieldDefaults;
 
-@Data @AllArgsConstructor @NoArgsConstructor
-public class Rest {
+@Data @AllArgsConstructor @NoArgsConstructor @EqualsAndHashCode(callSuper=false)
+public class Rest extends Observable.Instance {
 	@AllArgsConstructor @Getter @FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
 	public enum Where {
 		FREEZER("in the freezer"), REFRIGERATOR("in the refrigerator"),

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

@@ -13,7 +13,7 @@ import org.leumasjaffe.recipe.view.formatter.DurationFormatter;
 
 @SuppressWarnings("serial")
 public class DurationPanel extends JPanel {
-	private JFormattedTextField txtTime;
+	JFormattedTextField txtTime;
 	
 	/**
 	 * @wbp.parser.constructor
@@ -40,7 +40,7 @@ public class DurationPanel extends JPanel {
 		add(txtTime, gbc_txtTime);
 	}
 
-	public DurationPanel(String name, Duration duration) {
+	public DurationPanel(String name, final Duration duration) {
 		this(name);
 		setModel(duration);
 	}

+ 22 - 5
src/main/lombok/org/leumasjaffe/recipe/view/ElementPanel.java

@@ -4,6 +4,7 @@ import javax.swing.JPanel;
 import javax.swing.JScrollPane;
 
 import org.leumasjaffe.observer.ForwardingObservableListener;
+import org.leumasjaffe.observer.ObservableListener;
 import org.leumasjaffe.observer.ObserverDispatch;
 import org.leumasjaffe.recipe.model.Phase;
 import org.leumasjaffe.recipe.model.Element;
@@ -28,10 +29,13 @@ import javax.swing.Box;
 @SuppressWarnings("serial")
 @FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
 public class ElementPanel extends JScrollPane {
-	JPanel panelViewPort;
+	ObservableListener<CollatedDurationPanel, Element> durationListener;
 	ForwardingObservableListener<Element> listener = new ForwardingObservableListener<>();
 
-	public ElementPanel(Element element) {
+	JLabel lblName;
+	JPanel panelViewPort;
+
+	public ElementPanel() {
 		setPreferredSize(new Dimension(500, 450));
 
 		setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
@@ -46,7 +50,7 @@ public class ElementPanel extends JScrollPane {
 		gbl_panelColumnHeader.rowWeights = new double[]{0.0, Double.MIN_VALUE};
 		panelColumnHeader.setLayout(gbl_panelColumnHeader);
 		
-		JLabel lblName = new JLabel(element.getName());
+		lblName = new JLabel();
 		GridBagConstraints gbc_lblName = new GridBagConstraints();
 		gbc_lblName.insets = new Insets(0, 0, 0, 5);
 		gbc_lblName.gridx = 0;
@@ -60,8 +64,7 @@ public class ElementPanel extends JScrollPane {
 		gbc_horizontalGlue.gridy = 0;
 		panelColumnHeader.add(horizontalGlue, gbc_horizontalGlue);
 		
-		CollatedDurationPanel panelDuration =
-				new CollatedDurationPanel(element.getCollatedDuration());
+		CollatedDurationPanel panelDuration = new CollatedDurationPanel();
 		GridBagConstraints gbc_panelDuration = new GridBagConstraints();
 		gbc_panelDuration.gridx = 2;
 		gbc_panelDuration.gridy = 0;
@@ -71,12 +74,26 @@ public class ElementPanel extends JScrollPane {
 		setViewportView(panelViewPort);
 		panelViewPort.setLayout(new VerticalLayout(5));
 				
+		durationListener = new ObservableListener<>(panelDuration,
+				(c, v) -> c.setModel(v.getCollatedDuration()));
+	}
+	
+	public ElementPanel(final Element element) {
+		this();
+		setModel(element);
+	}
+	
+	public void setModel(final Element element) {
+		lblName.setText(element.getName());
+		
+		panelViewPort.removeAll();
 		for (final Phase phase : element.getPhases()) {
 			panelViewPort.add(new PhasePanel(phase));
 			panelViewPort.add(new JSeparator());
 		}
 		
 		listener.setObserved(element, element.getPhases());
+		durationListener.setObserved(element);
 	}
 	
 	@Override

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

@@ -1,8 +1,12 @@
 package org.leumasjaffe.recipe.view;
 
+import java.util.ArrayList;
+import java.util.List;
+
 import javax.swing.JPanel;
 
 import org.leumasjaffe.observer.ForwardingObservableListener;
+import org.leumasjaffe.observer.Observable;
 import org.leumasjaffe.observer.ObserverDispatch;
 import org.leumasjaffe.recipe.model.Phase;
 import org.leumasjaffe.recipe.model.Preparation;
@@ -27,7 +31,11 @@ public class PhasePanel extends JPanel {
 		phase.getPreparation().ifPresent(this::addPrep);
 		phase.getCooking().forEach(this::addStep);
 		phase.getRest().ifPresent(this::addRest);
-		listener.setObserved(phase, phase.getCooking());
+
+		List<Observable> children = new ArrayList<>(phase.getCooking());
+		phase.getPreparation().ifPresent(children::add);
+		phase.getRest().ifPresent(children::add);
+		listener.setObserved(phase, children);
 	}
 	
 	void addPrep(final Preparation step) {

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

@@ -3,6 +3,7 @@ package org.leumasjaffe.recipe.view;
 import javax.swing.JPanel;
 
 import org.jdesktop.swingx.VerticalLayout;
+import org.leumasjaffe.observer.ObservableController;
 import org.leumasjaffe.observer.ObservableListener;
 import org.leumasjaffe.recipe.controller.ReplaceChildrenController;
 import org.leumasjaffe.recipe.model.Duration;
@@ -20,15 +21,15 @@ import java.awt.Insets;
 import javax.swing.JLabel;
 import java.awt.Component;
 import javax.swing.Box;
+import javax.swing.JFormattedTextField;
 
 @SuppressWarnings("serial")
 @FieldDefaults(level=AccessLevel.PRIVATE)
 public class PreparationPanel extends JPanel {
 	ReplaceChildrenController<Preparation, Ingredient> controller;
 	ObservableListener<JPanel, Preparation> childListener;
+	ObservableListener<JFormattedTextField, Preparation> durationListener;
 	
-	DurationPanel panelDuration;
-		
 	public PreparationPanel() {
 		controller = new ReplaceChildrenController<>(Preparation::getIngredients,
 				IngredientPreparationPanel::new);
@@ -69,7 +70,7 @@ public class PreparationPanel extends JPanel {
 		gbc_horizontalGlue.gridy = 0;
 		panelLeft.add(horizontalGlue, gbc_horizontalGlue);
 		
-		panelDuration = new DurationPanel("Requires", Duration.ZERO);
+		DurationPanel panelDuration = new DurationPanel("Requires", Duration.ZERO);
 		GridBagConstraints gbc_panelDuration = new GridBagConstraints();
 		gbc_panelDuration.insets = new Insets(0, 0, 5, 0);
 		gbc_panelDuration.gridx = 2;
@@ -89,6 +90,8 @@ public class PreparationPanel extends JPanel {
 		// This indirection allows for testing of controller
 		childListener = new ObservableListener<>(panelIngredients,
 				(c, v) -> controller.accept(c, v));
+		durationListener = ObservableController.from(panelDuration.txtTime,
+				Preparation::getDuration, Preparation::setDuration);
 	}
 	
 	public PreparationPanel(final Preparation preparation) {
@@ -97,7 +100,7 @@ public class PreparationPanel extends JPanel {
 	}
 	
 	public void setModel(final Preparation preparation) {
-		panelDuration.setModel(preparation.getDuration());
+		durationListener.setObserved(preparation);
 		childListener.setObserved(preparation);
 	}
 

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

@@ -5,7 +5,6 @@ import javax.swing.JSplitPane;
 import org.jdesktop.swingx.VerticalLayout;
 import org.leumasjaffe.observer.ForwardingObservableListener;
 import org.leumasjaffe.observer.ObservableListener;
-import org.leumasjaffe.recipe.model.Element;
 import org.leumasjaffe.recipe.model.RecipeCard;
 import org.leumasjaffe.recipe.view.summary.SummaryPanel;
 

+ 13 - 2
src/main/lombok/org/leumasjaffe/recipe/view/RestPanel.java

@@ -2,6 +2,8 @@ package org.leumasjaffe.recipe.view;
 
 import javax.swing.JPanel;
 
+import org.leumasjaffe.observer.ObservableController;
+import org.leumasjaffe.observer.ObservableListener;
 import org.leumasjaffe.recipe.model.Rest;
 
 import lombok.AccessLevel;
@@ -9,6 +11,8 @@ import lombok.Getter;
 import lombok.experimental.FieldDefaults;
 
 import java.awt.GridBagLayout;
+
+import javax.swing.JFormattedTextField;
 import javax.swing.JLabel;
 import java.awt.GridBagConstraints;
 import java.awt.Insets;
@@ -16,8 +20,10 @@ import java.awt.Insets;
 @SuppressWarnings("serial")
 @FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
 public class RestPanel extends JPanel {
+	ObservableListener<JFormattedTextField, Rest> durationListener;
+
 	@Getter(AccessLevel.PACKAGE) JLabel lblLocation;
-	@Getter(AccessLevel.PACKAGE) JLabel lblDuration;
+	@Getter(AccessLevel.PACKAGE) DurationPanel lblDuration;
 
 	public RestPanel(Rest rest) {
 		GridBagLayout gridBagLayout = new GridBagLayout();
@@ -41,11 +47,16 @@ public class RestPanel extends JPanel {
 		gbc_lblLocation.gridy = 0;
 		add(lblLocation, gbc_lblLocation);
 		
-		lblDuration = new JLabel(rest.getDuration().toString());
+		lblDuration = new DurationPanel("");
 		GridBagConstraints gbc_lblDuration = new GridBagConstraints();
 		gbc_lblDuration.gridx = 2;
 		gbc_lblDuration.gridy = 0;
 		add(lblDuration, gbc_lblDuration);
+		
+		durationListener = ObservableController.from(lblDuration.txtTime,
+				Rest::getDuration, Rest::setDuration);
+		
+		durationListener.setObserved(rest);
 	}
 
 }

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

@@ -4,6 +4,8 @@ import javax.swing.JPanel;
 import javax.swing.event.DocumentListener;
 
 import org.leumasjaffe.observer.ForwardingObservableListener;
+import org.leumasjaffe.observer.ObservableController;
+import org.leumasjaffe.observer.ObservableListener;
 import org.leumasjaffe.observer.ObserverDispatch;
 import org.leumasjaffe.recipe.model.Ingredient;
 import org.leumasjaffe.recipe.model.Step;
@@ -22,15 +24,19 @@ import javax.swing.JLabel;
 import javax.swing.JTextPane;
 import java.awt.Component;
 import javax.swing.Box;
+import javax.swing.JFormattedTextField;
+
 import java.awt.Dimension;
 
 @SuppressWarnings("serial")
 @FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
 public class StepPanel extends JPanel implements AutoGrowPanel.DocumentListenable {
+	ForwardingObservableListener<Step> listener = new ForwardingObservableListener<>();
+	ObservableListener<JFormattedTextField, Step> durationListener;
+
 	@Getter(AccessLevel.PACKAGE) JLabel lblIndex;
 	@Getter(AccessLevel.PACKAGE) JTextPane txtpnInstructions;
 	@Getter(AccessLevel.PACKAGE) AutoGrowPanel panelIngredients;
-	ForwardingObservableListener<Step> listener = new ForwardingObservableListener<>();
 		
 	public StepPanel(int zeroIndex, Step step) {
 		GridBagLayout gridBagLayout = new GridBagLayout();
@@ -103,8 +109,12 @@ public class StepPanel extends JPanel implements AutoGrowPanel.DocumentListenabl
 		gbc_txtpnInstructions.gridy = 0;
 		add(txtpnInstructions, gbc_txtpnInstructions);
 		
+		durationListener = ObservableController.from(panelDuration.txtTime,
+				Step::getDuration, Step::setDuration);
+		
 		setListPosition(zeroIndex);
 		listener.setObserved(step, ingredients);
+		durationListener.setObserved(step);
 	}
 
 	@Override

+ 1 - 2
src/main/lombok/org/leumasjaffe/recipe/view/summary/SummaryPanel.java

@@ -13,7 +13,6 @@ import org.jdesktop.swingx.VerticalLayout;
 import org.leumasjaffe.observer.ObservableListener;
 import org.leumasjaffe.observer.ObserverDispatch;
 import org.leumasjaffe.recipe.controller.ReplaceChildrenController;
-import org.leumasjaffe.recipe.model.CollatedDuration;
 import org.leumasjaffe.recipe.model.Element;
 import org.leumasjaffe.recipe.model.RecipeCard;
 import org.leumasjaffe.recipe.view.CollatedDurationPanel;
@@ -72,7 +71,7 @@ public class SummaryPanel extends JPanel {
 		panelHeader.add(txtTitle, gbc_txtTitle);
 		txtTitle.setColumns(10);
 		
-		CollatedDurationPanel panelDuration = new CollatedDurationPanel(CollatedDuration.ZERO);
+		CollatedDurationPanel panelDuration = new CollatedDurationPanel();
 		GridBagConstraints gbc_panelDuration = new GridBagConstraints();
 		gbc_panelDuration.gridx = 0;
 		gbc_panelDuration.gridy = 1;

+ 2 - 2
src/test/java/org/leumasjaffe/recipe/model/PhaseTest.java

@@ -37,12 +37,12 @@ class PhaseTest {
 	}
 	
 	@Test
-	void testDoesNotAddPrepDurationIfPresent() {
+	void testAddsPrepDurationIfPresent() {
 		final Phase phase = new Phase();
 		final Preparation prep = new Preparation();
 		prep.setDuration(dur);
 		phase.setPreparation(prep);
-		assertEquals(Duration.ZERO, phase.getDuration());
+		assertNotEquals(Duration.ZERO, phase.getDuration());
 	}
 	
 	@Test

+ 0 - 11
src/test/java/org/leumasjaffe/recipe/view/PreparationPanelTest.java

@@ -17,7 +17,6 @@ import org.mockito.junit.jupiter.MockitoExtension;
 @ExtendWith(MockitoExtension.class)
 class PreparationPanelTest extends SwingTestCase {
 	
-	@Mock DurationPanel panelDuration;
 	@Mock ReplaceChildrenController<Preparation, Ingredient> controller;
 	Preparation stuff;
 	@InjectMocks PreparationPanel panel = new PreparationPanel();
@@ -33,19 +32,9 @@ class PreparationPanelTest extends SwingTestCase {
 
 	@Test
 	void testHasContent() {
-		verify(panelDuration).setModel(any());
 		verify(controller, times(1)).accept(any(), same(stuff));
 	}
 
-	@Test
-	void testDoesNotUpdateDurationWhenNotified() {
-		clearInvocations(panelDuration);
-
-		ObserverDispatch.notifySubscribers(stuff);		
-
-		verify(panelDuration, never()).setModel(any());
-	}
-
 	@Test
 	void testUpdatesNumberOfChildrenWhenNotified() {
 		clearInvocations((Object) controller);

+ 1 - 1
src/test/java/org/leumasjaffe/recipe/view/RestPanelTest.java

@@ -30,7 +30,7 @@ class RestPanelTest extends SwingTestCase {
 
 	@Test
 	void testHasContent() {
-		assertThat(panel.getLblDuration().getText(),
+		assertThat(panel.getLblDuration().txtTime.getText(),
 				containsString(dur.toString()));
 		assertEquals(stuff.getWhere().getHumanReadable(),
 				panel.getLblLocation().getText());