IngredientPanel.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package org.leumasjaffe.recipe.view;
  2. import javax.swing.JPanel;
  3. import java.awt.GridBagLayout;
  4. import javax.swing.JTextField;
  5. import java.awt.GridBagConstraints;
  6. import java.awt.Insets;
  7. import javax.swing.event.DocumentListener;
  8. import org.leumasjaffe.observer.ObservableController;
  9. import org.leumasjaffe.observer.ObservableListener;
  10. import org.leumasjaffe.observer.ObserverDispatch;
  11. import org.leumasjaffe.recipe.model.Ingredient;
  12. import org.leumasjaffe.recipe.view.formatter.AmountFormatter;
  13. import lombok.AccessLevel;
  14. import lombok.Getter;
  15. import lombok.experimental.FieldDefaults;
  16. import javax.swing.JFormattedTextField;
  17. import javax.swing.JLabel;
  18. @SuppressWarnings("serial")
  19. @FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
  20. public class IngredientPanel extends JPanel implements AutoGrowPanel.ChildComponent {
  21. ObservableListener<JTextField, Ingredient> nameController;
  22. ObservableListener<JFormattedTextField, Ingredient> amountController;
  23. ObservableListener<JTextField, Ingredient> preparationController;
  24. @Getter(AccessLevel.PACKAGE) JTextField txtName;
  25. @Getter(AccessLevel.PACKAGE) JFormattedTextField txtAmount;
  26. @Getter(AccessLevel.PACKAGE) JTextField txtPreparation;
  27. public IngredientPanel() {
  28. GridBagLayout gridBagLayout = new GridBagLayout();
  29. gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0};
  30. gridBagLayout.rowHeights = new int[]{0, 0};
  31. gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
  32. gridBagLayout.rowWeights = new double[]{0.0, Double.MIN_VALUE};
  33. setLayout(gridBagLayout);
  34. JLabel label = new JLabel("\u2022");
  35. GridBagConstraints gbc_label = new GridBagConstraints();
  36. gbc_label.insets = new Insets(0, 0, 0, 5);
  37. gbc_label.anchor = GridBagConstraints.EAST;
  38. gbc_label.gridx = 0;
  39. gbc_label.gridy = 0;
  40. add(label, gbc_label);
  41. txtName = new JTextField();
  42. GridBagConstraints gbc_txtName = new GridBagConstraints();
  43. gbc_txtName.fill = GridBagConstraints.HORIZONTAL;
  44. gbc_txtName.insets = new Insets(0, 0, 0, 5);
  45. gbc_txtName.gridx = 1;
  46. gbc_txtName.gridy = 0;
  47. add(txtName, gbc_txtName);
  48. txtName.setColumns(10);
  49. txtAmount = new JFormattedTextField(new AmountFormatter());
  50. GridBagConstraints gbc_txtAmount = new GridBagConstraints();
  51. gbc_txtAmount.fill = GridBagConstraints.HORIZONTAL;
  52. gbc_txtAmount.insets = new Insets(0, 0, 0, 5);
  53. gbc_txtAmount.gridx = 2;
  54. gbc_txtAmount.gridy = 0;
  55. add(txtAmount, gbc_txtAmount);
  56. txtAmount.setColumns(11);
  57. txtPreparation = new JTextField();
  58. GridBagConstraints gbc_txtPreparation = new GridBagConstraints();
  59. gbc_txtPreparation.anchor = GridBagConstraints.ABOVE_BASELINE;
  60. gbc_txtPreparation.fill = GridBagConstraints.HORIZONTAL;
  61. gbc_txtPreparation.gridx = 3;
  62. gbc_txtPreparation.gridy = 0;
  63. add(txtPreparation, gbc_txtPreparation);
  64. txtPreparation.setColumns(10);
  65. // I technically don't need to listen here as of this change,
  66. // but if I ever restore support for it, it will be convenient.
  67. nameController = ObservableController.from(txtName,
  68. Ingredient::getName, Ingredient::setName);
  69. amountController = ObservableController.from(txtAmount,
  70. Ingredient::getAmount, Ingredient::setAmount);
  71. preparationController = ObservableController.from(txtPreparation,
  72. Ingredient::getPreparation, Ingredient::setPreparation,
  73. ing -> {
  74. ing.setPreparation("");
  75. ObserverDispatch.notifySubscribers(ing);
  76. });
  77. }
  78. public IngredientPanel(final Ingredient ingredient) {
  79. this();
  80. setModel(ingredient);
  81. }
  82. public void setModel(final Ingredient ingredient) {
  83. nameController.setObserved(ingredient);
  84. amountController.setObserved(ingredient);
  85. preparationController.setObserved(ingredient);
  86. }
  87. @Override
  88. public void removeNotify() {
  89. super.removeNotify();
  90. ObserverDispatch.unsubscribeAll(nameController);
  91. ObserverDispatch.unsubscribeAll(amountController);
  92. ObserverDispatch.unsubscribeAll(preparationController);
  93. }
  94. @Override
  95. public void addGrowShrinkListener(DocumentListener dl) {
  96. this.txtName.getDocument().addDocumentListener(dl);
  97. }
  98. @Override
  99. public void removeGrowShrinkListener(DocumentListener dl) {
  100. this.txtName.getDocument().removeDocumentListener(dl);
  101. }
  102. }