PreparationPanel.java 4.5 KB

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