浏览代码

Move StepPanel to use BetterAutoGrowPanel.

Sam Jaffe 5 年之前
父节点
当前提交
de406008fc

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

@@ -23,7 +23,7 @@ import javax.swing.JLabel;
 
 @SuppressWarnings("serial")
 @FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
-public class IngredientPanel extends JPanel implements AutoGrowPanel.DocumentListenable {
+public class IngredientPanel extends JPanel implements BetterAutoGrowPanel.ChildComponent {
 	ObservableListener<JTextField, Ingredient> nameController;
 	ObservableListener<JFormattedTextField, Ingredient> amountController;
 	ObservableListener<JTextField, Ingredient> preparationController;
@@ -96,19 +96,19 @@ public class IngredientPanel extends JPanel implements AutoGrowPanel.DocumentLis
 	}
 	
 	@Override
-	public void addDocumentListener(DocumentListener dl) {
-		this.txtName.getDocument().addDocumentListener(dl);
+	public void removeNotify() {
+		super.removeNotify();
+		ObserverDispatch.unsubscribeAll(nameController);
 	}
 
 	@Override
-	public void removeDocumentListener(DocumentListener dl) {
-		this.txtName.getDocument().removeDocumentListener(dl);
+	public void addGrowShrinkListener(DocumentListener dl) {
+		this.txtName.getDocument().addDocumentListener(dl);
 	}
-	
+
 	@Override
-	public void removeNotify() {
-		super.removeNotify();
-		ObserverDispatch.unsubscribeAll(nameController);
+	public void removeGrowShrinkListener(DocumentListener dl) {
+		this.txtName.getDocument().removeDocumentListener(dl);
 	}
 
 }

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

@@ -37,7 +37,7 @@ public class StepPanel extends JPanel implements AutoGrowPanel.DocumentListenabl
 
 	@Getter(AccessLevel.PACKAGE) JLabel lblIndex;
 	@Getter(AccessLevel.PACKAGE) JTextPane txtpnInstructions;
-	@Getter(AccessLevel.PACKAGE) AutoGrowPanel panelIngredients;
+	@Getter(AccessLevel.PACKAGE) BetterAutoGrowPanel<IngredientPanel, Ingredient> panelIngredients;
 		
 	public StepPanel(final Step step) {
 		GridBagLayout gridBagLayout = new GridBagLayout();
@@ -76,22 +76,14 @@ public class StepPanel extends JPanel implements AutoGrowPanel.DocumentListenabl
 		gbc_horizontalGlue.gridy = 0;
 		panelLeft.add(horizontalGlue, gbc_horizontalGlue);
 		
-		DurationPanel panelDuration = new DurationPanel("Requires", step.getDuration());
+		DurationPanel panelDuration = new DurationPanel("Requires");
 		GridBagConstraints gbc_panelDuration = new GridBagConstraints();
 		gbc_panelDuration.gridx = 2;
 		gbc_panelDuration.gridy = 0;
 		panelLeft.add(panelDuration, gbc_panelDuration);
 		
 		final List<Ingredient> ingredients = step.getIngredients();
-		panelIngredients = new AutoGrowPanel(IngredientPanel::new,
-				Ingredient::new, ing -> {
-					ingredients.add(ing);
-					listener.setObserved(step, ingredients);
-				}, i -> {
-					ingredients.remove(i);
-					listener.setObserved(step, ingredients);
-					ObserverDispatch.notifySubscribers(step);
-				}, 0, ingredients.toArray(new Ingredient[0]));
+		panelIngredients = new BetterAutoGrowPanel<>(Ingredient::new, IngredientPanel::new);
 		GridBagConstraints gbc_panelIngredients = new GridBagConstraints();
 		gbc_panelIngredients.gridwidth = 3;
 		gbc_panelIngredients.insets = new Insets(0, 0, 0, 5);
@@ -113,6 +105,12 @@ public class StepPanel extends JPanel implements AutoGrowPanel.DocumentListenabl
 		durationListener = ObservableController.from(panelDuration.txtTime,
 				Step::getDuration, Step::setDuration);
 		
+		panelIngredients.setModel(step.getIngredients(), added -> {
+			listener.setObserved(step, step.getIngredients());
+			if (!added) {
+				ObserverDispatch.notifySubscribers(step);
+			}
+		});
 		listener.setObserved(step, ingredients);
 		intructionListener.setObserved(step);
 		durationListener.setObserved(step);