瀏覽代碼

Fix ProductSummaryPanel to be less bad.

Sam Jaffe 5 年之前
父節點
當前提交
8e7cd06ffc
共有 1 個文件被更改,包括 23 次插入13 次删除
  1. 23 13
      src/main/lombok/org/leumasjaffe/recipe/view/ProductSummaryPanel.java

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

@@ -2,38 +2,48 @@ package org.leumasjaffe.recipe.view;
 
 import javax.swing.JPanel;
 
+import org.jdesktop.swingx.VerticalLayout;
+import org.leumasjaffe.observer.ObservableListener;
 import org.leumasjaffe.recipe.model.Product;
 import java.awt.GridBagLayout;
 import java.awt.Insets;
-import java.util.ArrayList;
 
 import javax.swing.JLabel;
 import java.awt.GridBagConstraints;
 
 @SuppressWarnings("serial")
 public class ProductSummaryPanel extends JPanel {
+	ObservableListener<JPanel, Product> listener;
+	
 	public ProductSummaryPanel(final Product product) {
 		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};
+		gridBagLayout.rowHeights = new int[]{0, 0, 0};
+		gridBagLayout.columnWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
+		gridBagLayout.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
 		setLayout(gridBagLayout);
 		
 		JLabel lblProductName = new JLabel(product.getName());
 		GridBagConstraints gbc_lblProductName = new GridBagConstraints();
+		gbc_lblProductName.insets = new Insets(0, 0, 5, 5);
 		gbc_lblProductName.gridx = 0;
 		gbc_lblProductName.gridy = 0;
 		add(lblProductName, gbc_lblProductName);
+				
+		JPanel panel = new JPanel();
+		panel.setLayout(new VerticalLayout());
+		GridBagConstraints gbc_panel = new GridBagConstraints();
+		gbc_panel.gridwidth = 2;
+		gbc_panel.insets = new Insets(0, 0, 0, 5);
+		gbc_panel.fill = GridBagConstraints.BOTH;
+		gbc_panel.gridx = 0;
+		gbc_panel.gridy = 1;
+		add(panel, gbc_panel);
 		
-		AutoGrowPanel panelIngredients = new AutoGrowPanel(IngredientPanel::new,
-				new ArrayList<>(product.getIngredients()));
-		GridBagConstraints gbc_panelIngredients = new GridBagConstraints();
-		gbc_panelIngredients.gridwidth = 2;
-		gbc_panelIngredients.insets = new Insets(0, 0, 0, 5);
-		gbc_panelIngredients.fill = GridBagConstraints.BOTH;
-		gbc_panelIngredients.gridx = 0;
-		gbc_panelIngredients.gridy = 1;
-		add(panelIngredients, gbc_panelIngredients);
+		listener = new ObservableListener<>(panel, (c, t) -> {
+			c.removeAll();
+			product.getIngredients().stream().map(IngredientPanel::new).forEach(c::add);
+		});
+		listener.setObserved(product);
 	}
 }