PreparationPanel.java 3.7 KB

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