StepPanel.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package org.leumasjaffe.recipe.view;
  2. import javax.swing.JPanel;
  3. import javax.swing.event.DocumentListener;
  4. import org.leumasjaffe.observer.ForwardingObservableListener;
  5. import org.leumasjaffe.observer.ObservableController;
  6. import org.leumasjaffe.observer.ObservableListener;
  7. import org.leumasjaffe.observer.ObserverDispatch;
  8. import org.leumasjaffe.recipe.controller.TextBinding;
  9. import org.leumasjaffe.recipe.model.Ingredient;
  10. import org.leumasjaffe.recipe.model.Step;
  11. import lombok.AccessLevel;
  12. import lombok.Getter;
  13. import lombok.experimental.FieldDefaults;
  14. import java.awt.GridBagLayout;
  15. import java.awt.GridBagConstraints;
  16. import java.awt.Insets;
  17. import java.awt.event.FocusListener;
  18. import javax.swing.JLabel;
  19. import javax.swing.JTextPane;
  20. import java.awt.Component;
  21. import javax.swing.Box;
  22. import javax.swing.JFormattedTextField;
  23. import java.awt.Dimension;
  24. @SuppressWarnings("serial")
  25. @FieldDefaults(level=AccessLevel.PRIVATE)
  26. public class StepPanel extends JPanel implements AutoGrowPanel.ChildComponent {
  27. ForwardingObservableListener<Step> listener = new ForwardingObservableListener<>();
  28. TextBinding<Step> instructionBinding;
  29. ObservableListener<JFormattedTextField, Step> durationController;
  30. JLabel lblIndex;
  31. @Getter(AccessLevel.PACKAGE) JTextPane txtpnInstructions;
  32. AutoGrowPanel<IngredientPanel, Ingredient> panelIngredients;
  33. public StepPanel() {
  34. GridBagLayout gridBagLayout = new GridBagLayout();
  35. gridBagLayout.columnWidths = new int[]{0, 0, 0};
  36. gridBagLayout.rowHeights = new int[]{0, 0};
  37. gridBagLayout.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
  38. gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
  39. setLayout(gridBagLayout);
  40. JPanel panelLeft = new JPanel();
  41. GridBagConstraints gbc_panelLeft = new GridBagConstraints();
  42. gbc_panelLeft.insets = new Insets(0, 0, 0, 5);
  43. gbc_panelLeft.fill = GridBagConstraints.BOTH;
  44. gbc_panelLeft.gridx = 0;
  45. gbc_panelLeft.gridy = 0;
  46. add(panelLeft, gbc_panelLeft);
  47. GridBagLayout gbl_panelLeft = new GridBagLayout();
  48. gbl_panelLeft.columnWidths = new int[]{0, 0, 0, 0};
  49. gbl_panelLeft.rowHeights = new int[]{0, 0, 0};
  50. gbl_panelLeft.columnWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE};
  51. gbl_panelLeft.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
  52. panelLeft.setLayout(gbl_panelLeft);
  53. lblIndex = new JLabel("");
  54. GridBagConstraints gbc_lblIndex = new GridBagConstraints();
  55. gbc_lblIndex.fill = GridBagConstraints.HORIZONTAL;
  56. gbc_lblIndex.insets = new Insets(0, 0, 0, 5);
  57. gbc_lblIndex.gridx = 0;
  58. gbc_lblIndex.gridy = 0;
  59. panelLeft.add(lblIndex, gbc_lblIndex);
  60. Component horizontalGlue = Box.createHorizontalGlue();
  61. GridBagConstraints gbc_horizontalGlue = new GridBagConstraints();
  62. gbc_horizontalGlue.insets = new Insets(0, 0, 0, 5);
  63. gbc_horizontalGlue.gridx = 1;
  64. gbc_horizontalGlue.gridy = 0;
  65. panelLeft.add(horizontalGlue, gbc_horizontalGlue);
  66. DurationPanel panelDuration = new DurationPanel("Requires");
  67. GridBagConstraints gbc_panelDuration = new GridBagConstraints();
  68. gbc_panelDuration.gridx = 2;
  69. gbc_panelDuration.gridy = 0;
  70. panelLeft.add(panelDuration, gbc_panelDuration);
  71. panelIngredients = new AutoGrowPanel<>(Ingredient::new, IngredientPanel::new);
  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. txtpnInstructions = new JTextPane();
  80. txtpnInstructions.setPreferredSize(new Dimension(200, 30));
  81. GridBagConstraints gbc_txtpnInstructions = new GridBagConstraints();
  82. gbc_txtpnInstructions.fill = GridBagConstraints.BOTH;
  83. gbc_txtpnInstructions.gridx = 1;
  84. gbc_txtpnInstructions.gridy = 0;
  85. add(txtpnInstructions, gbc_txtpnInstructions);
  86. instructionBinding = new TextBinding<>(txtpnInstructions,
  87. Step::getInstruction, Step::setInstruction);
  88. durationController = ObservableController.from(panelDuration.txtTime,
  89. Step::getDuration, Step::setDuration);
  90. setListPosition(0);
  91. }
  92. public StepPanel(final Step step) {
  93. this();
  94. setModel(step);
  95. }
  96. void setModel(final Step step) {
  97. panelIngredients.setModel(step.getIngredients(), added -> {
  98. listener.setObserved(step, step.getIngredients());
  99. if (!added) {
  100. ObserverDispatch.notifySubscribers(step);
  101. }
  102. });
  103. listener.setObserved(step, step.getIngredients());
  104. instructionBinding.setModel(step);
  105. durationController.setObserved(step);
  106. }
  107. @Override
  108. public void removeNotify() {
  109. super.removeNotify();
  110. ObserverDispatch.unsubscribeAll(listener);
  111. ObserverDispatch.unsubscribeAll(instructionBinding);
  112. ObserverDispatch.unsubscribeAll(durationController);
  113. }
  114. @Override
  115. public void setListPosition(int zeroIndex) {
  116. this.lblIndex.setText("Step " + Integer.toString(zeroIndex + 1));
  117. repaint();
  118. }
  119. @Override
  120. public void addGrowListener(DocumentListener dl) {
  121. this.txtpnInstructions.getDocument().addDocumentListener(dl);
  122. }
  123. @Override
  124. public void removeGrowListener(DocumentListener dl) {
  125. this.txtpnInstructions.getDocument().removeDocumentListener(dl);
  126. }
  127. @Override
  128. public void addShrinkListener(FocusListener fl) {
  129. this.txtpnInstructions.addFocusListener(fl);
  130. }
  131. @Override
  132. public void removeShrinkListener(FocusListener fl) {
  133. this.txtpnInstructions.removeFocusListener(fl);
  134. }
  135. }