Jelajahi Sumber

Make PhasePanel use AutoGrowPanel

Sam Jaffe 5 tahun lalu
induk
melakukan
0a2f732249

+ 6 - 2
src/main/lombok/org/leumasjaffe/recipe/view/AutoGrowPanel.java

@@ -77,8 +77,8 @@ public class AutoGrowPanel extends JPanel {
 	
 	@SafeVarargs
 	public <T, C extends Component & DocumentListenable> AutoGrowPanel(Function<T, C> function,
-			Supplier<T> newItem, Consumer<? super T> onData, IntConsumer onDelete, T...ts) {
-		setLayout(new VerticalLayout());
+			Supplier<T> newItem, Consumer<? super T> onData, IntConsumer onDelete, int gap, T...ts) {
+		setLayout(new VerticalLayout(gap));
 		
 		T next = newItem.get();
 		this.onDelete = onDelete;
@@ -95,6 +95,10 @@ public class AutoGrowPanel extends JPanel {
 		members.add(empty);
 		add(empty);
 		empty.addDocumentListener(this.grow);
+		
+		for (int i = 0; i < members.size(); ++i) {
+			members.get(i).setListPosition(i);
+		}
 	}
 
 	private DocumentListenable getBack() {

+ 25 - 19
src/main/lombok/org/leumasjaffe/recipe/view/PhasePanel.java

@@ -10,44 +10,50 @@ import org.leumasjaffe.observer.Observable;
 import org.leumasjaffe.observer.ObserverDispatch;
 import org.leumasjaffe.recipe.model.Phase;
 import org.leumasjaffe.recipe.model.Preparation;
-import org.leumasjaffe.recipe.model.Rest;
 import org.leumasjaffe.recipe.model.Step;
 
 import lombok.AccessLevel;
 import lombok.experimental.FieldDefaults;
-import lombok.experimental.NonFinal;
 
 import org.jdesktop.swingx.VerticalLayout;
 
 @SuppressWarnings("serial")
 @FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
 public class PhasePanel extends JPanel {
-	@NonFinal int steps = 0;
 	ForwardingObservableListener<Phase> listener = new ForwardingObservableListener<>();
+	ForwardingObservableListener<Preparation> linkBack = new ForwardingObservableListener<>();
 
+	AutoGrowPanel panelSteps;
+	
 	public PhasePanel(final Phase phase) {		
 		setLayout(new VerticalLayout(5));
 		
-		phase.getPreparation().ifPresent(this::addPrep);
-		phase.getCooking().forEach(this::addStep);
-		phase.getRest().ifPresent(this::addRest);
+		phase.getPreparation().ifPresent(prep -> add(new PreparationPanel(prep)));
+		
+		final List<Step> steps = phase.getCooking();
+		panelSteps = new AutoGrowPanel(StepPanel::new, Step::new,
+				step -> {
+					steps.add(step);
+					listener.setObserved(phase, allChildren(phase));
+				},
+				i -> {
+					steps.remove(i);
+					listener.setObserved(phase, allChildren(phase));
+					ObserverDispatch.notifySubscribers(phase);
+				}, 5, steps.toArray(new Step[0]));
+		add(panelSteps);
+		
+		phase.getRest().ifPresent(rest -> add(new RestPanel(rest)));
 
+		listener.setObserved(phase, allChildren(phase));
+		phase.getPreparation().ifPresent(prep -> linkBack.setObserved(prep, phase));
+	}
+	
+	private static List<Observable> allChildren(Phase phase) {
 		List<Observable> children = new ArrayList<>(phase.getCooking());
 		phase.getPreparation().ifPresent(children::add);
 		phase.getRest().ifPresent(children::add);
-		listener.setObserved(phase, children);
-	}
-	
-	void addPrep(final Preparation step) {
-		add(new PreparationPanel(step));
-	}
-	
-	void addStep(final Step step) {
-		add(new StepPanel(steps++, step));
-	}
-	
-	void addRest(final Rest rest) {
-		add(new RestPanel(rest));
+		return children;
 	}
 	
 	@Override

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

@@ -38,7 +38,7 @@ public class StepPanel extends JPanel implements AutoGrowPanel.DocumentListenabl
 	@Getter(AccessLevel.PACKAGE) JTextPane txtpnInstructions;
 	@Getter(AccessLevel.PACKAGE) AutoGrowPanel panelIngredients;
 		
-	public StepPanel(int zeroIndex, Step step) {
+	public StepPanel(final Step step) {
 		GridBagLayout gridBagLayout = new GridBagLayout();
 		gridBagLayout.columnWidths = new int[]{0, 0, 0};
 		gridBagLayout.rowHeights = new int[]{0, 0};
@@ -90,7 +90,7 @@ public class StepPanel extends JPanel implements AutoGrowPanel.DocumentListenabl
 					ingredients.remove(i);
 					listener.setObserved(step, ingredients);
 					ObserverDispatch.notifySubscribers(step);
-				}, ingredients.toArray(new Ingredient[0]));
+				}, 0, ingredients.toArray(new Ingredient[0]));
 		GridBagConstraints gbc_panelIngredients = new GridBagConstraints();
 		gbc_panelIngredients.gridwidth = 3;
 		gbc_panelIngredients.insets = new Insets(0, 0, 0, 5);
@@ -111,11 +111,15 @@ public class StepPanel extends JPanel implements AutoGrowPanel.DocumentListenabl
 		durationListener = ObservableController.from(panelDuration.txtTime,
 				Step::getDuration, Step::setDuration);
 		
-		setListPosition(zeroIndex);
 		listener.setObserved(step, ingredients);
 		durationListener.setObserved(step);
 	}
 
+	public StepPanel(int zeroIndex, Step step) {
+		this(step);
+		setListPosition(zeroIndex);
+	}
+	
 	@Override
 	public void addDocumentListener(DocumentListener dl) {
 		this.txtpnInstructions.getDocument().addDocumentListener(dl);

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

@@ -55,7 +55,7 @@ class AutoGrowPanelTest extends SwingTestCase {
 	}
 	
 	private AutoGrowPanel create(MockComponent... mocks) {
-		return new AutoGrowPanel(m -> m, this::mocked, add, remove, mocks);
+		return new AutoGrowPanel(m -> m, this::mocked, add, remove, 0, mocks);
 	}
 	
 	@Test