فهرست منبع

Add separate versions of IngredientPanel for its three main use-cases:
- Preparation
- Cooking Steps
- Summarizing Ingredients Needed

Sam Jaffe 5 سال پیش
والد
کامیت
612f6c9d69

+ 4 - 21
src/main/lombok/org/leumasjaffe/recipe/view/IngredientPanel.java

@@ -12,7 +12,6 @@ import javax.swing.event.DocumentListener;
 import javax.swing.text.NumberFormatter;
 
 import org.leumasjaffe.event.AnyActionDocumentListener;
-import org.leumasjaffe.observer.ObservableListener;
 import org.leumasjaffe.observer.ObserverDispatch;
 import org.leumasjaffe.recipe.model.Ingredient;
 
@@ -23,11 +22,7 @@ import javax.swing.JLabel;
 @SuppressWarnings("serial")
 public class IngredientPanel extends JPanel implements AutoGrowPanel.DocumentListenable {
 	private JTextField txtName;
-	private JTextField txtUnit;
-	private JTextField txtPreparation;
-	
-	private ObservableListener<IngredientPanel, Ingredient> listener;
-	
+		
 	public IngredientPanel(final Ingredient ingredient) {
 		GridBagLayout gridBagLayout = new GridBagLayout();
 		gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0};
@@ -68,7 +63,7 @@ public class IngredientPanel extends JPanel implements AutoGrowPanel.DocumentLis
 		add(txtAmount, gbc_txtAmount);
 		txtAmount.setColumns(4);
 		
-		txtUnit = new JTextField(ingredient.getAmount().unitName());
+		JTextField txtUnit = new JTextField(ingredient.getAmount().unitName());
 		txtUnit.setFont(new Font("Source Code Pro", Font.PLAIN, 10));
 		GridBagConstraints gbc_txtUnit = new GridBagConstraints();
 		gbc_txtUnit.insets = new Insets(0, 0, 0, 5);
@@ -79,7 +74,7 @@ public class IngredientPanel extends JPanel implements AutoGrowPanel.DocumentLis
 		add(txtUnit, gbc_txtUnit);
 		txtUnit.setColumns(6);
 		
-		txtPreparation = new JTextField(ingredient.getPreparation());
+		JTextField txtPreparation = new JTextField(ingredient.getPreparation());
 		txtPreparation.setFont(new Font("Source Code Pro", Font.PLAIN, 10));
 		GridBagConstraints gbc_txtPreparation = new GridBagConstraints();
 		gbc_txtPreparation.anchor = GridBagConstraints.ABOVE_BASELINE;
@@ -91,14 +86,8 @@ public class IngredientPanel extends JPanel implements AutoGrowPanel.DocumentLis
 		
 		addDocumentListener((AnyActionDocumentListener) (e) -> {
 			ingredient.setName(txtName.getText());
-			listener.notifySubscribers(ingredient);
-		});
-		
-		listener = new ObservableListener<>(this, (c, t) -> {
-			if (txtName.getText().equals(t.getName())) return;
-			txtName.setText(t.getName());
+			ObserverDispatch.notifySubscribers(ingredient);
 		});
-		listener.setObserved(ingredient);
 	}
 
 	@Override
@@ -110,11 +99,5 @@ public class IngredientPanel extends JPanel implements AutoGrowPanel.DocumentLis
 	public void removeDocumentListener(DocumentListener dl) {
 		this.txtName.getDocument().removeDocumentListener(dl);
 	}
-	
-	@Override
-	public void removeNotify() {
-		super.removeNotify();
-		ObserverDispatch.unsubscribeAll(listener);
-	}
 
 }

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

@@ -0,0 +1,104 @@
+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 java.text.NumberFormat;
+import java.util.Locale;
+
+import javax.swing.text.NumberFormatter;
+
+import org.leumasjaffe.observer.ObservableListener;
+import org.leumasjaffe.observer.ObserverDispatch;
+import org.leumasjaffe.recipe.model.Ingredient;
+
+import javax.swing.JFormattedTextField;
+import java.awt.Font;
+import javax.swing.JLabel;
+
+@SuppressWarnings("serial")
+public class IngredientPreparationPanel extends JPanel {
+	
+	private ObservableListener<IngredientPreparationPanel, Ingredient> listener;
+	
+	public IngredientPreparationPanel(final Ingredient ingredient) {
+		GridBagLayout gridBagLayout = new GridBagLayout();
+		gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0};
+		gridBagLayout.rowHeights = new int[]{0, 0};
+		gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.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);
+		
+		JTextField txtName = new JTextField(ingredient.getName());
+		txtName.setEditable(false);
+		txtName.setFont(new Font("Source Code Pro", Font.PLAIN, 10));
+		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(10);
+		
+		NumberFormatter fmtDone = new NumberFormatter(NumberFormat.getNumberInstance(Locale.getDefault()));
+		fmtDone.setMinimum(0.0);
+		fmtDone.setCommitsOnValidEdit(true);
+		JFormattedTextField txtAmount = new JFormattedTextField(fmtDone);
+		txtAmount.setEditable(false);
+		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;
+		gbc_txtAmount.insets = new Insets(0, 0, 0, 5);
+		gbc_txtAmount.gridx = 2;
+		gbc_txtAmount.gridy = 0;
+		add(txtAmount, gbc_txtAmount);
+		txtAmount.setColumns(4);
+		
+		JTextField txtUnit = new JTextField(ingredient.getAmount().unitName());
+		txtUnit.setEditable(false);
+		txtUnit.setFont(new Font("Source Code Pro", Font.PLAIN, 10));
+		GridBagConstraints gbc_txtUnit = new GridBagConstraints();
+		gbc_txtUnit.insets = new Insets(0, 0, 0, 5);
+		gbc_txtUnit.anchor = GridBagConstraints.ABOVE_BASELINE;
+		gbc_txtUnit.fill = GridBagConstraints.HORIZONTAL;
+		gbc_txtUnit.gridx = 3;
+		gbc_txtUnit.gridy = 0;
+		add(txtUnit, gbc_txtUnit);
+		txtUnit.setColumns(6);
+		
+		JTextField txtPreparation = new JTextField(ingredient.getPreparation());
+		txtPreparation.setEditable(false);
+		txtPreparation.setFont(new Font("Source Code Pro", Font.PLAIN, 10));
+		GridBagConstraints gbc_txtPreparation = new GridBagConstraints();
+		gbc_txtPreparation.anchor = GridBagConstraints.ABOVE_BASELINE;
+		gbc_txtPreparation.fill = GridBagConstraints.HORIZONTAL;
+		gbc_txtPreparation.gridx = 4;
+		gbc_txtPreparation.gridy = 0;
+		add(txtPreparation, gbc_txtPreparation);
+		txtPreparation.setColumns(10);
+		
+		listener = new ObservableListener<>(this, (c, t) -> {
+			if (txtName.getText().equals(t.getName())) return;
+			txtName.setText(t.getName());
+		});
+		listener.setObserved(ingredient);
+	}
+	
+	@Override
+	public void removeNotify() {
+		super.removeNotify();
+		ObserverDispatch.unsubscribeAll(listener);
+	}
+
+}

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

@@ -2,6 +2,7 @@ package org.leumasjaffe.recipe.view;
 
 import javax.swing.JPanel;
 
+import org.jdesktop.swingx.VerticalLayout;
 import org.leumasjaffe.recipe.model.Preparation;
 
 import java.awt.GridBagLayout;
@@ -61,7 +62,8 @@ public class PreparationPanel extends JPanel {
 		gbc_lblDuration.gridy = 0;
 		panelLeft.add(lblDuration, gbc_lblDuration);
 		
-		AutoGrowPanel panelIngredients = new AutoGrowPanel(IngredientPanel::new, step.getIngredients());
+		JPanel panelIngredients = new JPanel();
+		panelIngredients.setLayout(new VerticalLayout(5));
 		GridBagConstraints gbc_panelIngredients = new GridBagConstraints();
 		gbc_panelIngredients.gridwidth = 3;
 		gbc_panelIngredients.insets = new Insets(0, 0, 0, 5);
@@ -69,6 +71,7 @@ public class PreparationPanel extends JPanel {
 		gbc_panelIngredients.gridx = 0;
 		gbc_panelIngredients.gridy = 1;
 		panelLeft.add(panelIngredients, gbc_panelIngredients);
+		step.getIngredients().stream().map(IngredientPreparationPanel::new).forEach(panelIngredients::add);
 	}
 
 }

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

@@ -43,7 +43,7 @@ public class ProductSummaryPanel extends JPanel {
 		
 		listener = new ObservableListener<>(panel, (c, t) -> {
 			c.removeAll();
-			product.getIngredients().stream().map(IngredientPanel::new).forEach(c::add);
+			product.getIngredients().stream().map(SummaryIngredientPanel::new).forEach(c::add);
 		});
 		listener.setObserved(product);
 	}

+ 91 - 0
src/main/lombok/org/leumasjaffe/recipe/view/SummaryIngredientPanel.java

@@ -0,0 +1,91 @@
+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 java.text.NumberFormat;
+import java.util.Locale;
+
+import javax.swing.text.NumberFormatter;
+
+import org.leumasjaffe.observer.ObservableListener;
+import org.leumasjaffe.observer.ObserverDispatch;
+import org.leumasjaffe.recipe.model.Ingredient;
+
+import javax.swing.JFormattedTextField;
+import java.awt.Font;
+import javax.swing.JLabel;
+
+@SuppressWarnings("serial")
+public class SummaryIngredientPanel extends JPanel {
+	private ObservableListener<SummaryIngredientPanel, Ingredient> listener;
+	
+	public SummaryIngredientPanel(final Ingredient ingredient) {
+		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);
+		
+		JTextField txtName = new JTextField(ingredient.getName());
+		txtName.setEditable(false);
+		txtName.setFont(new Font("Source Code Pro", Font.PLAIN, 10));
+		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(10);
+		
+		NumberFormatter fmtDone = new NumberFormatter(NumberFormat.getNumberInstance(Locale.getDefault()));
+		fmtDone.setMinimum(0.0);
+		fmtDone.setCommitsOnValidEdit(true);
+		JFormattedTextField txtAmount = new JFormattedTextField(fmtDone);
+		txtAmount.setEditable(false);
+		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;
+		gbc_txtAmount.insets = new Insets(0, 0, 0, 5);
+		gbc_txtAmount.gridx = 2;
+		gbc_txtAmount.gridy = 0;
+		add(txtAmount, gbc_txtAmount);
+		txtAmount.setColumns(4);
+		
+		JTextField txtUnit = new JTextField(ingredient.getAmount().unitName());
+		txtUnit.setEditable(false);
+		txtUnit.setFont(new Font("Source Code Pro", Font.PLAIN, 10));
+		GridBagConstraints gbc_txtUnit = new GridBagConstraints();
+		gbc_txtUnit.anchor = GridBagConstraints.ABOVE_BASELINE;
+		gbc_txtUnit.fill = GridBagConstraints.HORIZONTAL;
+		gbc_txtUnit.gridx = 3;
+		gbc_txtUnit.gridy = 0;
+		add(txtUnit, gbc_txtUnit);
+		txtUnit.setColumns(6);
+		
+		listener = new ObservableListener<>(this, (c, t) -> {
+			if (txtName.getText().equals(t.getName())) return;
+			txtName.setText(t.getName());
+		});
+		listener.setObserved(ingredient);
+	}
+	
+	@Override
+	public void removeNotify() {
+		super.removeNotify();
+		ObserverDispatch.unsubscribeAll(listener);
+	}
+
+}