PreparationPanel.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package org.leumasjaffe.recipe.view;
  2. import javax.swing.JPanel;
  3. import org.jdesktop.swingx.VerticalLayout;
  4. import org.leumasjaffe.observer.IndirectObservableListener;
  5. import org.leumasjaffe.observer.ObservableController;
  6. import org.leumasjaffe.observer.ObservableListener;
  7. import org.leumasjaffe.observer.ObserverDispatch;
  8. import org.leumasjaffe.recipe.controller.ReplaceChildrenController;
  9. import org.leumasjaffe.recipe.model.Duration;
  10. import org.leumasjaffe.recipe.model.Ingredient;
  11. import org.leumasjaffe.recipe.model.Phase;
  12. import org.leumasjaffe.recipe.model.Preparation;
  13. import lombok.AccessLevel;
  14. import lombok.experimental.FieldDefaults;
  15. import java.awt.GridBagLayout;
  16. import java.awt.GridBagConstraints;
  17. import java.awt.Insets;
  18. import javax.swing.JLabel;
  19. import java.awt.Component;
  20. import javax.swing.Box;
  21. import javax.swing.JFormattedTextField;
  22. @SuppressWarnings("serial")
  23. @FieldDefaults(level=AccessLevel.PRIVATE)
  24. public class PreparationPanel extends JPanel {
  25. ReplaceChildrenController<Preparation, Ingredient> controller;
  26. IndirectObservableListener<JPanel, Preparation> childListener;
  27. ObservableListener<JFormattedTextField, Preparation> durationController;
  28. public PreparationPanel() {
  29. controller = new ReplaceChildrenController<>(Preparation::getIngredients,
  30. IngredientPreparationPanel::new);
  31. GridBagLayout gridBagLayout = new GridBagLayout();
  32. gridBagLayout.columnWidths = new int[]{0, 0, 0};
  33. gridBagLayout.rowHeights = new int[]{0, 0};
  34. gridBagLayout.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
  35. gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
  36. setLayout(gridBagLayout);
  37. JPanel panelLeft = new JPanel();
  38. GridBagConstraints gbc_panelLeft = new GridBagConstraints();
  39. gbc_panelLeft.insets = new Insets(0, 0, 0, 5);
  40. gbc_panelLeft.fill = GridBagConstraints.BOTH;
  41. gbc_panelLeft.gridx = 0;
  42. gbc_panelLeft.gridy = 0;
  43. add(panelLeft, gbc_panelLeft);
  44. GridBagLayout gbl_panelLeft = new GridBagLayout();
  45. gbl_panelLeft.columnWidths = new int[]{0, 0, 0, 0};
  46. gbl_panelLeft.rowHeights = new int[]{0, 0, 0};
  47. gbl_panelLeft.columnWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE};
  48. gbl_panelLeft.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
  49. panelLeft.setLayout(gbl_panelLeft);
  50. JLabel lblIndex = new JLabel("Prep");
  51. GridBagConstraints gbc_lblIndex = new GridBagConstraints();
  52. gbc_lblIndex.fill = GridBagConstraints.HORIZONTAL;
  53. gbc_lblIndex.insets = new Insets(0, 0, 0, 5);
  54. gbc_lblIndex.gridx = 0;
  55. gbc_lblIndex.gridy = 0;
  56. panelLeft.add(lblIndex, gbc_lblIndex);
  57. Component horizontalGlue = Box.createHorizontalGlue();
  58. GridBagConstraints gbc_horizontalGlue = new GridBagConstraints();
  59. gbc_horizontalGlue.insets = new Insets(0, 0, 0, 5);
  60. gbc_horizontalGlue.gridx = 1;
  61. gbc_horizontalGlue.gridy = 0;
  62. panelLeft.add(horizontalGlue, gbc_horizontalGlue);
  63. DurationPanel panelDuration = new DurationPanel("Requires", Duration.ZERO);
  64. GridBagConstraints gbc_panelDuration = new GridBagConstraints();
  65. gbc_panelDuration.gridx = 2;
  66. gbc_panelDuration.gridy = 0;
  67. panelLeft.add(panelDuration, gbc_panelDuration);
  68. JPanel panelIngredients = new JPanel();
  69. panelIngredients.setLayout(new VerticalLayout());
  70. GridBagConstraints gbc_panelIngredients = new GridBagConstraints();
  71. gbc_panelIngredients.gridwidth = 3;
  72. gbc_panelIngredients.insets = new Insets(0, 0, 0, 5);
  73. gbc_panelIngredients.fill = GridBagConstraints.BOTH;
  74. gbc_panelIngredients.gridx = 0;
  75. gbc_panelIngredients.gridy = 1;
  76. panelLeft.add(panelIngredients, gbc_panelIngredients);
  77. // This indirection allows for testing of controller
  78. childListener = new IndirectObservableListener<>(panelIngredients,
  79. (c, v) -> controller.accept(c, v));
  80. durationController = ObservableController.from(panelDuration.txtTime,
  81. Preparation::getDuration, Preparation::setDuration);
  82. }
  83. public PreparationPanel(final Phase phase) {
  84. this();
  85. setModel(phase);
  86. }
  87. public void setModel(final Phase phase) {
  88. childListener.setObserved(phase.getPreparation().get(), phase);
  89. durationController.setObserved(phase.getPreparation().get());
  90. }
  91. @Override
  92. public void removeNotify() {
  93. super.removeNotify();
  94. ObserverDispatch.unsubscribeAll(childListener);
  95. ObserverDispatch.unsubscribeAll(durationController);
  96. }
  97. }