Selaa lähdekoodia

Associate RecipeCards with their own Panel type.

Sam Jaffe 5 vuotta sitten
vanhempi
commit
a75417c0d9

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

@@ -0,0 +1,43 @@
+package org.leumasjaffe.recipe.view;
+
+import javax.swing.JSplitPane;
+
+import org.jdesktop.swingx.VerticalLayout;
+import org.leumasjaffe.recipe.controller.FileController;
+import org.leumasjaffe.recipe.model.Element;
+import org.leumasjaffe.recipe.model.RecipeCard;
+import org.leumasjaffe.recipe.view.summary.SummaryPanel;
+
+import lombok.AccessLevel;
+import lombok.Getter;
+import lombok.experimental.FieldDefaults;
+
+import javax.swing.JPanel;
+
+@SuppressWarnings("serial")
+@FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
+public class RecipeCardPanel extends JSplitPane implements FileController.ViewModel {
+	@Getter(AccessLevel.PACKAGE) SummaryPanel summaryPanel;
+	@Getter(AccessLevel.PACKAGE) JPanel rightPanel;
+	
+	public RecipeCardPanel() {
+		rightPanel = new JPanel();
+		rightPanel.setLayout(new VerticalLayout(5));
+		setRightComponent(rightPanel);
+		
+		summaryPanel = new SummaryPanel();
+		setLeftComponent(summaryPanel);
+	}
+
+	@Override
+	public void setModel(final RecipeCard card) {
+		rightPanel.removeAll();
+		summaryPanel.removeElements();
+		card.getElements().forEach(this::addElement);
+	}
+	
+	private void addElement(final Element comp) {
+		summaryPanel.addElement(comp);
+		rightPanel.add(new ElementPanel(comp));
+	}
+}

+ 4 - 24
src/main/lombok/org/leumasjaffe/recipe/view/RecipeManagerFrame.java

@@ -1,12 +1,8 @@
 package org.leumasjaffe.recipe.view;
 
 import javax.swing.JFrame;
-import javax.swing.JTabbedPane;
 
 import org.leumasjaffe.recipe.controller.FileController;
-import org.leumasjaffe.recipe.model.Element;
-import org.leumasjaffe.recipe.model.RecipeCard;
-import org.leumasjaffe.recipe.view.summary.SummaryPanel;
 
 import lombok.AccessLevel;
 import lombok.experimental.FieldDefaults;
@@ -16,12 +12,11 @@ import javax.swing.JMenu;
 
 @SuppressWarnings("serial")
 @FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
-public class RecipeManagerFrame extends JFrame implements FileController.ViewModel {
-	SummaryPanel summaryPanel;
-	JTabbedPane tabbedPane;
+public class RecipeManagerFrame extends JFrame {
 	
 	public RecipeManagerFrame() {
-		FileController fileController = new FileController(this);
+		RecipeCardPanel panel = new RecipeCardPanel();
+		FileController fileController = new FileController(panel);
 
 		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 		
@@ -31,12 +26,8 @@ public class RecipeManagerFrame extends JFrame implements FileController.ViewMod
 		JMenu mnFile = new FileMenu(this, fileController);
 		menuBar.add(mnFile);
 				
-		tabbedPane = new JTabbedPane();
-		setContentPane(tabbedPane);
+		setContentPane(panel);
 
-		summaryPanel = new SummaryPanel();
-		tabbedPane.addTab("Summary", summaryPanel);
-		
 //		fileController.create();
 		fileController.open("src/test/resources/example.json");
 
@@ -44,15 +35,4 @@ public class RecipeManagerFrame extends JFrame implements FileController.ViewMod
 		repaint();
 		setVisible(true);
 	}
-	
-	@Override
-	public void setModel(final RecipeCard card) {
-		// TODO Clear underlying panels
-		card.getElements().forEach(this::addElements);
-	}
-	
-	private void addElements(final Element comp) {
-		summaryPanel.addProduct(comp);
-		tabbedPane.addTab(comp.getName(), new ElementPanel(comp));
-	}
 }

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

@@ -90,8 +90,12 @@ public class SummaryPanel extends JPanel {
 		add(txtpnDescription, gbc_txtpnDescription);
 	}
 
-	public void addProduct(final Element comp) {
+	public void addElement(final Element comp) {
 		panelIngredients.add(new ElementPanel(comp));
 		panelIngredients.add(new JSeparator());
 	}
+	
+	public void removeElements() {
+		panelIngredients.removeAll();
+	}
 }