IngredientPreparationPanel.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 java.text.NumberFormat;
  8. import java.util.Locale;
  9. import javax.swing.text.NumberFormatter;
  10. import org.leumasjaffe.observer.ObservableListener;
  11. import org.leumasjaffe.observer.ObserverDispatch;
  12. import org.leumasjaffe.recipe.model.Ingredient;
  13. import lombok.AccessLevel;
  14. import lombok.Getter;
  15. import lombok.experimental.FieldDefaults;
  16. import javax.swing.JFormattedTextField;
  17. import java.awt.Font;
  18. import javax.swing.JLabel;
  19. @SuppressWarnings("serial")
  20. @FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
  21. public class IngredientPreparationPanel extends JPanel {
  22. ObservableListener<IngredientPreparationPanel, Ingredient> listener;
  23. @Getter(AccessLevel.PACKAGE) JTextField txtName;
  24. @Getter(AccessLevel.PACKAGE) JFormattedTextField txtAmount;
  25. @Getter(AccessLevel.PACKAGE) JTextField txtUnit;
  26. @Getter(AccessLevel.PACKAGE) JTextField txtPreparation;
  27. public IngredientPreparationPanel(final Ingredient ingredient) {
  28. GridBagLayout gridBagLayout = new GridBagLayout();
  29. gridBagLayout.columnWidths = new int[]{0, 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, 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(ingredient.getName());
  42. txtName.setEditable(false);
  43. txtName.setFont(new Font("Source Code Pro", Font.PLAIN, 10));
  44. GridBagConstraints gbc_txtName = new GridBagConstraints();
  45. gbc_txtName.fill = GridBagConstraints.HORIZONTAL;
  46. gbc_txtName.insets = new Insets(0, 0, 0, 5);
  47. gbc_txtName.gridx = 1;
  48. gbc_txtName.gridy = 0;
  49. add(txtName, gbc_txtName);
  50. txtName.setColumns(10);
  51. NumberFormatter fmtDone = new NumberFormatter(NumberFormat.getNumberInstance(Locale.getDefault()));
  52. fmtDone.setMinimum(0.0);
  53. fmtDone.setCommitsOnValidEdit(true);
  54. txtAmount = new JFormattedTextField(fmtDone);
  55. txtAmount.setEditable(false);
  56. txtAmount.setValue(ingredient.getAmount().getValue());
  57. txtAmount.setFont(new Font("Source Code Pro", Font.PLAIN, 10));
  58. GridBagConstraints gbc_txtAmount = new GridBagConstraints();
  59. gbc_txtAmount.fill = GridBagConstraints.HORIZONTAL;
  60. gbc_txtAmount.insets = new Insets(0, 0, 0, 5);
  61. gbc_txtAmount.gridx = 2;
  62. gbc_txtAmount.gridy = 0;
  63. add(txtAmount, gbc_txtAmount);
  64. txtAmount.setColumns(4);
  65. txtUnit = new JTextField(ingredient.getAmount().unitName());
  66. txtUnit.setEditable(false);
  67. txtUnit.setFont(new Font("Source Code Pro", Font.PLAIN, 10));
  68. GridBagConstraints gbc_txtUnit = new GridBagConstraints();
  69. gbc_txtUnit.insets = new Insets(0, 0, 0, 5);
  70. gbc_txtUnit.anchor = GridBagConstraints.ABOVE_BASELINE;
  71. gbc_txtUnit.fill = GridBagConstraints.HORIZONTAL;
  72. gbc_txtUnit.gridx = 3;
  73. gbc_txtUnit.gridy = 0;
  74. add(txtUnit, gbc_txtUnit);
  75. txtUnit.setColumns(6);
  76. txtPreparation = new JTextField(ingredient.getPreparation());
  77. txtPreparation.setEditable(false);
  78. txtPreparation.setFont(new Font("Source Code Pro", Font.PLAIN, 10));
  79. GridBagConstraints gbc_txtPreparation = new GridBagConstraints();
  80. gbc_txtPreparation.anchor = GridBagConstraints.ABOVE_BASELINE;
  81. gbc_txtPreparation.fill = GridBagConstraints.HORIZONTAL;
  82. gbc_txtPreparation.gridx = 4;
  83. gbc_txtPreparation.gridy = 0;
  84. add(txtPreparation, gbc_txtPreparation);
  85. txtPreparation.setColumns(10);
  86. listener = new ObservableListener<>(this, (c, t) -> {
  87. if (txtName.getText().equals(t.getName())) return;
  88. txtName.setText(t.getName());
  89. });
  90. listener.setObserved(ingredient);
  91. }
  92. @Override
  93. public void removeNotify() {
  94. super.removeNotify();
  95. ObserverDispatch.unsubscribeAll(listener);
  96. }
  97. }