|
|
@@ -11,6 +11,8 @@ import javax.swing.JTextField;
|
|
|
|
|
|
import org.jdesktop.swingx.VerticalLayout;
|
|
|
import org.leumasjaffe.observer.ObservableListener;
|
|
|
+import org.leumasjaffe.observer.ObserverDispatch;
|
|
|
+import org.leumasjaffe.recipe.controller.ReplaceChildrenController;
|
|
|
import org.leumasjaffe.recipe.model.CollatedDuration;
|
|
|
import org.leumasjaffe.recipe.model.Element;
|
|
|
import org.leumasjaffe.recipe.model.RecipeCard;
|
|
|
@@ -22,12 +24,23 @@ import lombok.experimental.FieldDefaults;
|
|
|
import java.awt.Font;
|
|
|
|
|
|
@SuppressWarnings("serial")
|
|
|
-@FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
|
|
|
+@FieldDefaults(level=AccessLevel.PRIVATE)
|
|
|
public class SummaryPanel extends JPanel {
|
|
|
+ ReplaceChildrenController<RecipeCard, Element> controller;
|
|
|
ObservableListener<CollatedDurationPanel, RecipeCard> durationListener;
|
|
|
ObservableListener<JPanel, RecipeCard> childListener;
|
|
|
|
|
|
- public SummaryPanel(final RecipeCard card) {
|
|
|
+ JTextField txtTitle;
|
|
|
+ JTextArea txaDesription;
|
|
|
+
|
|
|
+ public SummaryPanel() {
|
|
|
+ controller = new ReplaceChildrenController<>(RecipeCard::getElements, element -> {
|
|
|
+ JPanel wrapper = new JPanel(new VerticalLayout());
|
|
|
+ wrapper.add(new ElementPanel(element));
|
|
|
+ wrapper.add(new JSeparator());
|
|
|
+ return wrapper;
|
|
|
+ });
|
|
|
+
|
|
|
GridBagLayout gridBagLayout = new GridBagLayout();
|
|
|
gridBagLayout.columnWidths = new int[]{0, 0, 0};
|
|
|
gridBagLayout.rowHeights = new int[]{0, 0, 0, 0};
|
|
|
@@ -50,8 +63,7 @@ public class SummaryPanel extends JPanel {
|
|
|
gbl_panelHeader.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
|
|
|
panelHeader.setLayout(gbl_panelHeader);
|
|
|
|
|
|
- JTextField txtTitle = new JTextField();
|
|
|
- txtTitle.setText(card.getTitle());
|
|
|
+ txtTitle = new JTextField();
|
|
|
GridBagConstraints gbc_txtTitle = new GridBagConstraints();
|
|
|
gbc_txtTitle.insets = new Insets(0, 0, 0, 5);
|
|
|
gbc_txtTitle.fill = GridBagConstraints.HORIZONTAL;
|
|
|
@@ -84,23 +96,23 @@ public class SummaryPanel extends JPanel {
|
|
|
add(panel, gbc_panel);
|
|
|
GridBagLayout gbl_panel = new GridBagLayout();
|
|
|
gbl_panel.columnWidths = new int[]{0, 0};
|
|
|
- gbl_panel.rowHeights = new int[]{0, 0, 0, 0};
|
|
|
+ gbl_panel.rowHeights = new int[]{0, 0, 0};
|
|
|
gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
|
|
|
- gbl_panel.rowWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE};
|
|
|
+ gbl_panel.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
|
|
|
panel.setLayout(gbl_panel);
|
|
|
|
|
|
JPanel panelPhoto = new ImagePanel();
|
|
|
GridBagConstraints gbc_panelPhoto = new GridBagConstraints();
|
|
|
+ gbc_panelPhoto.fill = GridBagConstraints.BOTH;
|
|
|
gbc_panelPhoto.insets = new Insets(0, 0, 5, 0);
|
|
|
gbc_panelPhoto.gridx = 0;
|
|
|
gbc_panelPhoto.gridy = 0;
|
|
|
panel.add(panelPhoto, gbc_panelPhoto);
|
|
|
|
|
|
- JTextArea txaDesription = new JTextArea(5, 20);
|
|
|
+ txaDesription = new JTextArea(5, 20);
|
|
|
txaDesription.setFont(new Font("Verdana", Font.PLAIN, 10));
|
|
|
txaDesription.setWrapStyleWord(true);
|
|
|
txaDesription.setLineWrap(true);
|
|
|
- txaDesription.setText(card.getDescription());
|
|
|
GridBagConstraints gbc_txaDesription = new GridBagConstraints();
|
|
|
gbc_txaDesription.insets = new Insets(0, 0, 5, 0);
|
|
|
gbc_txaDesription.fill = GridBagConstraints.BOTH;
|
|
|
@@ -111,21 +123,28 @@ public class SummaryPanel extends JPanel {
|
|
|
durationListener = new ObservableListener<>(panelDuration,
|
|
|
(c, v) -> c.setModel(v.getCollatedDuration()));
|
|
|
|
|
|
- childListener = new ObservableListener<>(panelIngredients, (c, v) -> {
|
|
|
- if (c.getComponents().length == v.getElements().size()) {
|
|
|
- return;
|
|
|
- }
|
|
|
- c.removeAll();
|
|
|
- for (final Element element : v.getElements()) {
|
|
|
- JPanel wrapper = new JPanel(new VerticalLayout());
|
|
|
- wrapper.add(new ElementPanel(element));
|
|
|
- wrapper.add(new JSeparator());
|
|
|
- c.add(wrapper);
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
+ // This indirection allows for testing of controller
|
|
|
+ childListener = new ObservableListener<>(panelIngredients,
|
|
|
+ (c, v) -> controller.accept(c, v));
|
|
|
+ }
|
|
|
+
|
|
|
+ public SummaryPanel(final RecipeCard card) {
|
|
|
+ this();
|
|
|
+ setModel(card);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setModel(final RecipeCard card) {
|
|
|
+ txtTitle.setText(card.getTitle());
|
|
|
+ txaDesription.setText(card.getDescription());
|
|
|
durationListener.setObserved(card);
|
|
|
childListener.setObserved(card);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public void removeNotify() {
|
|
|
+ super.removeNotify();
|
|
|
+ ObserverDispatch.unsubscribeAll(durationListener);
|
|
|
+ ObserverDispatch.unsubscribeAll(childListener);
|
|
|
+ }
|
|
|
+
|
|
|
}
|