Browse Source

Don't duplicate ingredientpanel code for a read-only version.

Sam Jaffe 5 years ago
parent
commit
dbe25274fb

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

@@ -32,7 +32,7 @@ public class IngredientPanel extends JPanel implements AutoGrowPanel.ChildCompon
 	@Getter(AccessLevel.PACKAGE) JFormattedTextField txtAmount;
 	@Getter(AccessLevel.PACKAGE) JTextField txtPreparation;
 		
-	public IngredientPanel() {
+	public IngredientPanel(boolean editable) {
 		GridBagLayout gridBagLayout = new GridBagLayout();
 		gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0};
 		gridBagLayout.rowHeights = new int[]{0, 0};
@@ -49,6 +49,7 @@ public class IngredientPanel extends JPanel implements AutoGrowPanel.ChildCompon
 		add(label, gbc_label);
 		
 		txtName = new JTextField();
+		txtName.setEditable(editable);
 		GridBagConstraints gbc_txtName = new GridBagConstraints();
 		gbc_txtName.fill = GridBagConstraints.HORIZONTAL;
 		gbc_txtName.insets = new Insets(0, 0, 0, 5);
@@ -58,6 +59,7 @@ public class IngredientPanel extends JPanel implements AutoGrowPanel.ChildCompon
 		txtName.setColumns(15);
 		
 		txtAmount = new JFormattedTextField(new AmountFormatter());
+		txtAmount.setEditable(editable);
 		GridBagConstraints gbc_txtAmount = new GridBagConstraints();
 		gbc_txtAmount.fill = GridBagConstraints.HORIZONTAL;
 		gbc_txtAmount.insets = new Insets(0, 0, 0, 5);
@@ -67,6 +69,7 @@ public class IngredientPanel extends JPanel implements AutoGrowPanel.ChildCompon
 		txtAmount.setColumns(6);
 		
 		txtPreparation = new JTextField();
+		txtPreparation.setEditable(editable);
 		GridBagConstraints gbc_txtPreparation = new GridBagConstraints();
 		gbc_txtPreparation.anchor = GridBagConstraints.ABOVE_BASELINE;
 		gbc_txtPreparation.fill = GridBagConstraints.HORIZONTAL;
@@ -75,8 +78,6 @@ public class IngredientPanel extends JPanel implements AutoGrowPanel.ChildCompon
 		add(txtPreparation, gbc_txtPreparation);
 		txtPreparation.setColumns(10);
 		
-		// I technically don't need to listen here as of this change,
-		// but if I ever restore support for it, it will be convenient.
 		nameController = ObservableController.from(txtName,
 				Ingredient::getName, Ingredient::setName);
 		amountController = ObservableController.from(txtAmount,
@@ -89,8 +90,8 @@ public class IngredientPanel extends JPanel implements AutoGrowPanel.ChildCompon
 				});
 	}
 
-	public IngredientPanel(final Ingredient ingredient) {
-		this();
+	public IngredientPanel(final Ingredient ingredient, boolean editable) {
+		this(editable);
 		setModel(ingredient);
 	}
 	

+ 0 - 98
src/main/lombok/org/leumasjaffe/recipe/view/IngredientPreparationPanel.java

@@ -1,98 +0,0 @@
-package org.leumasjaffe.recipe.view;
-
-import javax.swing.JPanel;
-import java.awt.GridBagLayout;
-import javax.swing.JTextField;
-import java.awt.GridBagConstraints;
-import java.awt.Insets;
-
-import org.leumasjaffe.observer.ObservableListener;
-import org.leumasjaffe.observer.ObserverDispatch;
-import org.leumasjaffe.recipe.model.Ingredient;
-import org.leumasjaffe.recipe.view.formatter.AmountFormatter;
-
-import lombok.AccessLevel;
-import lombok.Getter;
-import lombok.experimental.FieldDefaults;
-
-import javax.swing.JFormattedTextField;
-import javax.swing.JLabel;
-
-@SuppressWarnings("serial")
-@FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
-public class IngredientPreparationPanel extends JPanel {
-	
-	ObservableListener<IngredientPreparationPanel, Ingredient> listener;
-	@Getter(AccessLevel.PACKAGE) JTextField txtName;
-	@Getter(AccessLevel.PACKAGE) JFormattedTextField txtAmount;
-	@Getter(AccessLevel.PACKAGE) JTextField txtPreparation;
-	
-	public IngredientPreparationPanel() {
-		GridBagLayout gridBagLayout = new GridBagLayout();
-		gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0};
-		gridBagLayout.rowHeights = new int[]{0, 0};
-		gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
-		gridBagLayout.rowWeights = new double[]{0.0, Double.MIN_VALUE};
-		setLayout(gridBagLayout);
-		
-		JLabel label = new JLabel("\u2022");
-		GridBagConstraints gbc_label = new GridBagConstraints();
-		gbc_label.insets = new Insets(0, 0, 0, 5);
-		gbc_label.anchor = GridBagConstraints.EAST;
-		gbc_label.gridx = 0;
-		gbc_label.gridy = 0;
-		add(label, gbc_label);
-		
-		txtName = new JTextField();
-		txtName.setEditable(false);
-		GridBagConstraints gbc_txtName = new GridBagConstraints();
-		gbc_txtName.fill = GridBagConstraints.HORIZONTAL;
-		gbc_txtName.insets = new Insets(0, 0, 0, 5);
-		gbc_txtName.gridx = 1;
-		gbc_txtName.gridy = 0;
-		add(txtName, gbc_txtName);
-		txtName.setColumns(15);
-		
-		txtAmount = new JFormattedTextField(new AmountFormatter());
-		txtAmount.setEditable(false);
-		GridBagConstraints gbc_txtAmount = new GridBagConstraints();
-		gbc_txtAmount.fill = GridBagConstraints.HORIZONTAL;
-		gbc_txtAmount.insets = new Insets(0, 0, 0, 5);
-		gbc_txtAmount.gridx = 2;
-		gbc_txtAmount.gridy = 0;
-		add(txtAmount, gbc_txtAmount);
-		txtAmount.setColumns(6);
-		
-		txtPreparation = new JTextField();
-		txtPreparation.setEditable(false);
-		GridBagConstraints gbc_txtPreparation = new GridBagConstraints();
-		gbc_txtPreparation.anchor = GridBagConstraints.ABOVE_BASELINE;
-		gbc_txtPreparation.fill = GridBagConstraints.HORIZONTAL;
-		gbc_txtPreparation.gridx = 3;
-		gbc_txtPreparation.gridy = 0;
-		add(txtPreparation, gbc_txtPreparation);
-		txtPreparation.setColumns(10);
-		
-		listener = new ObservableListener<>(this, (c, v) -> {
-			c.txtName.setText(v.getName());
-			c.txtAmount.setValue(v.getAmount());
-			c.txtPreparation.setText(v.getPreparation());
-		});
-	}
-	
-	public IngredientPreparationPanel(final Ingredient ingredient) {
-		this();
-		setModel(ingredient);
-	}
-	
-	public void setModel(final Ingredient ingredient) {
-		listener.setObserved(ingredient);
-	}
-	
-	@Override
-	public void removeNotify() {
-		super.removeNotify();
-		ObserverDispatch.unsubscribeAll(listener);
-	}
-
-}

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

@@ -35,7 +35,7 @@ public class PreparationPanel extends JPanel {
 	
 	public PreparationPanel() {
 		controller = new ReplaceChildrenAction<>(Preparation::getIngredients,
-				IngredientPreparationPanel::new);
+				ing -> new IngredientPanel(ing, false));
 		
 		GridBagLayout gridBagLayout = new GridBagLayout();
 		gridBagLayout.columnWidths = new int[]{0, 0, 0};

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

@@ -83,7 +83,7 @@ public class StepPanel extends JPanel implements AutoGrowPanel.ChildComponent {
 		gbc_panelDuration.gridy = 0;
 		panelLeft.add(panelDuration, gbc_panelDuration);
 		
-		panelIngredients = new AutoGrowPanel<>(Ingredient::new, IngredientPanel::new);
+		panelIngredients = new AutoGrowPanel<>(Ingredient::new, ing -> new IngredientPanel(ing, true));
 		GridBagConstraints gbc_panelIngredients = new GridBagConstraints();
 		gbc_panelIngredients.gridwidth = 3;
 		gbc_panelIngredients.insets = new Insets(0, 0, 0, 5);

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

@@ -22,7 +22,7 @@ class IngredientPanelTest extends SwingTestCase {
 	@BeforeEach
 	void setUp() {
 		stuff = new Ingredient("Onions", "Sliced", new Amount("100 g"));
-		panel = new IngredientPanel(stuff);
+		panel = new IngredientPanel(stuff, true);
 		listener.setObserved(stuff);
 		// setObserved invokes our callback.
 		clearInvocations(listener);

+ 0 - 51
src/test/java/org/leumasjaffe/recipe/view/IngredientPreparationPanelTest.java

@@ -1,51 +0,0 @@
-package org.leumasjaffe.recipe.view;
-
-import static org.junit.jupiter.api.Assertions.*;
-
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.leumasjaffe.observer.ObserverDispatch;
-import org.leumasjaffe.recipe.model.Amount;
-import org.leumasjaffe.recipe.model.Ingredient;
-
-class IngredientPreparationPanelTest extends SwingTestCase {
-
-	Ingredient stuff;
-	IngredientPreparationPanel panel;
-	
-	@BeforeEach
-	void setUp() {
-		stuff = new Ingredient("Onions", "Sliced", new Amount("100 g"));
-		panel = new IngredientPreparationPanel(stuff);
-	}
-
-	@Test
-	void testFilledOutWithContent() {
-		assertEquals("Onions", panel.getTxtName().getText());
-		assertEquals("Sliced", panel.getTxtPreparation().getText());
-		assertEquals(new Amount("100 g"), panel.getTxtAmount().getValue());
-	}
-	
-	@Test
-	void testCannotEditContent() {
-		assertFalse(panel.getTxtName().isEditable());
-		assertFalse(panel.getTxtPreparation().isEditable());
-		assertFalse(panel.getTxtAmount().isEditable());
-	}
-
-	@Test
-	void testIsSubscribedToUpdates() {
-		stuff.setName("Bacon");
-		stuff.setPreparation("Cut into Lardons");
-		stuff.setAmount(new Amount("50 g"));
-		
-		assertEquals("Onions", panel.getTxtName().getText());
-		
-		ObserverDispatch.notifySubscribers(stuff);
-		
-		assertEquals("Bacon", panel.getTxtName().getText());
-		assertEquals("Cut into Lardons", panel.getTxtPreparation().getText());
-		assertEquals(new Amount("50 g"), panel.getTxtAmount().getValue());
-	}
-
-}

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

@@ -29,7 +29,7 @@ class StepPanelTest extends SwingTestCase {
 	
 	@Spy JLabel lblIndex;
 	@Spy AutoGrowPanel<IngredientPanel, Ingredient> panelIngredients =
-			new AutoGrowPanel<>(Ingredient::new, IngredientPanel::new);
+			new AutoGrowPanel<>(Ingredient::new, ing -> new IngredientPanel(ing, true));
 	@InjectMocks StepPanel panel = new StepPanel();
 
 	@BeforeEach