StepPanel.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.ObserverDispatch;
  6. import org.leumasjaffe.recipe.model.Ingredient;
  7. import org.leumasjaffe.recipe.model.Step;
  8. import lombok.AccessLevel;
  9. import lombok.Getter;
  10. import lombok.experimental.FieldDefaults;
  11. import java.awt.GridBagLayout;
  12. import java.awt.GridBagConstraints;
  13. import java.awt.Insets;
  14. import java.util.List;
  15. import javax.swing.JLabel;
  16. import javax.swing.JTextPane;
  17. import java.awt.Component;
  18. import javax.swing.Box;
  19. import java.awt.Dimension;
  20. @SuppressWarnings("serial")
  21. @FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
  22. public class StepPanel extends JPanel implements AutoGrowPanel.DocumentListenable {
  23. @Getter(AccessLevel.PACKAGE) JLabel lblIndex;
  24. @Getter(AccessLevel.PACKAGE) JLabel lblDuration;
  25. @Getter(AccessLevel.PACKAGE) JTextPane txtpnInstructions;
  26. @Getter(AccessLevel.PACKAGE) JPanel panelIngredients;
  27. ForwardingObservableListener<Step> listener = new ForwardingObservableListener<>();
  28. public StepPanel(int zeroIndex, Step step) {
  29. GridBagLayout gridBagLayout = new GridBagLayout();
  30. gridBagLayout.columnWidths = new int[]{0, 0, 0};
  31. gridBagLayout.rowHeights = new int[]{0, 0};
  32. gridBagLayout.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
  33. gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
  34. setLayout(gridBagLayout);
  35. JPanel panelLeft = new JPanel();
  36. GridBagConstraints gbc_panelLeft = new GridBagConstraints();
  37. gbc_panelLeft.insets = new Insets(0, 0, 0, 5);
  38. gbc_panelLeft.fill = GridBagConstraints.BOTH;
  39. gbc_panelLeft.gridx = 0;
  40. gbc_panelLeft.gridy = 0;
  41. add(panelLeft, gbc_panelLeft);
  42. GridBagLayout gbl_panelLeft = new GridBagLayout();
  43. gbl_panelLeft.columnWidths = new int[]{0, 0, 0, 0};
  44. gbl_panelLeft.rowHeights = new int[]{0, 0, 0};
  45. gbl_panelLeft.columnWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE};
  46. gbl_panelLeft.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
  47. panelLeft.setLayout(gbl_panelLeft);
  48. lblIndex = new JLabel("");
  49. GridBagConstraints gbc_lblIndex = new GridBagConstraints();
  50. gbc_lblIndex.fill = GridBagConstraints.HORIZONTAL;
  51. gbc_lblIndex.insets = new Insets(0, 0, 5, 5);
  52. gbc_lblIndex.gridx = 0;
  53. gbc_lblIndex.gridy = 0;
  54. panelLeft.add(lblIndex, gbc_lblIndex);
  55. Component horizontalGlue = Box.createHorizontalGlue();
  56. GridBagConstraints gbc_horizontalGlue = new GridBagConstraints();
  57. gbc_horizontalGlue.insets = new Insets(0, 0, 5, 5);
  58. gbc_horizontalGlue.gridx = 1;
  59. gbc_horizontalGlue.gridy = 0;
  60. panelLeft.add(horizontalGlue, gbc_horizontalGlue);
  61. lblDuration = new JLabel("Requires: " + step.getDuration().toString());
  62. GridBagConstraints gbc_lblDuration = new GridBagConstraints();
  63. gbc_lblDuration.insets = new Insets(0, 0, 5, 0);
  64. gbc_lblDuration.gridx = 2;
  65. gbc_lblDuration.gridy = 0;
  66. panelLeft.add(lblDuration, gbc_lblDuration);
  67. final List<Ingredient> ingredients = step.getIngredients();
  68. panelIngredients = new AutoGrowPanel(IngredientPanel::new,
  69. Ingredient::new, ingredients::add, i -> {
  70. ingredients.remove((int) i);
  71. ObserverDispatch.notifySubscribers(step);
  72. }, ingredients.toArray(new Ingredient[0]));
  73. GridBagConstraints gbc_panelIngredients = new GridBagConstraints();
  74. gbc_panelIngredients.gridwidth = 3;
  75. gbc_panelIngredients.insets = new Insets(0, 0, 0, 5);
  76. gbc_panelIngredients.fill = GridBagConstraints.BOTH;
  77. gbc_panelIngredients.gridx = 0;
  78. gbc_panelIngredients.gridy = 1;
  79. panelLeft.add(panelIngredients, gbc_panelIngredients);
  80. txtpnInstructions = new JTextPane();
  81. txtpnInstructions.setPreferredSize(new Dimension(200, 30));
  82. txtpnInstructions.setText(step.getInstruction());
  83. GridBagConstraints gbc_txtpnInstructions = new GridBagConstraints();
  84. gbc_txtpnInstructions.fill = GridBagConstraints.BOTH;
  85. gbc_txtpnInstructions.gridx = 1;
  86. gbc_txtpnInstructions.gridy = 0;
  87. add(txtpnInstructions, gbc_txtpnInstructions);
  88. setListPosition(zeroIndex);
  89. listener.setObserved(step, step.getIngredients());
  90. }
  91. @Override
  92. public void addDocumentListener(DocumentListener dl) {
  93. this.txtpnInstructions.getDocument().addDocumentListener(dl);
  94. }
  95. @Override
  96. public void removeDocumentListener(DocumentListener dl) {
  97. this.txtpnInstructions.getDocument().removeDocumentListener(dl);
  98. }
  99. @Override
  100. public void setListPosition(int zeroIndex) {
  101. this.lblIndex.setText("Step " + Integer.toString(zeroIndex + 1));
  102. repaint();
  103. }
  104. @Override
  105. public void removeNotify() {
  106. super.removeNotify();
  107. ObserverDispatch.unsubscribeAll(listener);
  108. }
  109. }