IngredientPreparationPanel.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 javax.swing.JFormattedTextField;
  14. import java.awt.Font;
  15. import javax.swing.JLabel;
  16. @SuppressWarnings("serial")
  17. public class IngredientPreparationPanel extends JPanel {
  18. private ObservableListener<IngredientPreparationPanel, Ingredient> listener;
  19. public IngredientPreparationPanel(final Ingredient ingredient) {
  20. GridBagLayout gridBagLayout = new GridBagLayout();
  21. gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0};
  22. gridBagLayout.rowHeights = new int[]{0, 0};
  23. gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
  24. gridBagLayout.rowWeights = new double[]{0.0, Double.MIN_VALUE};
  25. setLayout(gridBagLayout);
  26. JLabel label = new JLabel("\u2022");
  27. GridBagConstraints gbc_label = new GridBagConstraints();
  28. gbc_label.insets = new Insets(0, 0, 0, 5);
  29. gbc_label.anchor = GridBagConstraints.EAST;
  30. gbc_label.gridx = 0;
  31. gbc_label.gridy = 0;
  32. add(label, gbc_label);
  33. JTextField txtName = new JTextField(ingredient.getName());
  34. txtName.setEditable(false);
  35. txtName.setFont(new Font("Source Code Pro", Font.PLAIN, 10));
  36. GridBagConstraints gbc_txtName = new GridBagConstraints();
  37. gbc_txtName.fill = GridBagConstraints.HORIZONTAL;
  38. gbc_txtName.insets = new Insets(0, 0, 0, 5);
  39. gbc_txtName.gridx = 1;
  40. gbc_txtName.gridy = 0;
  41. add(txtName, gbc_txtName);
  42. txtName.setColumns(10);
  43. NumberFormatter fmtDone = new NumberFormatter(NumberFormat.getNumberInstance(Locale.getDefault()));
  44. fmtDone.setMinimum(0.0);
  45. fmtDone.setCommitsOnValidEdit(true);
  46. JFormattedTextField txtAmount = new JFormattedTextField(fmtDone);
  47. txtAmount.setEditable(false);
  48. txtAmount.setValue(ingredient.getAmount().getValue());
  49. txtAmount.setFont(new Font("Source Code Pro", Font.PLAIN, 10));
  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(4);
  57. JTextField txtUnit = new JTextField(ingredient.getAmount().unitName());
  58. txtUnit.setEditable(false);
  59. txtUnit.setFont(new Font("Source Code Pro", Font.PLAIN, 10));
  60. GridBagConstraints gbc_txtUnit = new GridBagConstraints();
  61. gbc_txtUnit.insets = new Insets(0, 0, 0, 5);
  62. gbc_txtUnit.anchor = GridBagConstraints.ABOVE_BASELINE;
  63. gbc_txtUnit.fill = GridBagConstraints.HORIZONTAL;
  64. gbc_txtUnit.gridx = 3;
  65. gbc_txtUnit.gridy = 0;
  66. add(txtUnit, gbc_txtUnit);
  67. txtUnit.setColumns(6);
  68. JTextField txtPreparation = new JTextField(ingredient.getPreparation());
  69. txtPreparation.setEditable(false);
  70. txtPreparation.setFont(new Font("Source Code Pro", Font.PLAIN, 10));
  71. GridBagConstraints gbc_txtPreparation = new GridBagConstraints();
  72. gbc_txtPreparation.anchor = GridBagConstraints.ABOVE_BASELINE;
  73. gbc_txtPreparation.fill = GridBagConstraints.HORIZONTAL;
  74. gbc_txtPreparation.gridx = 4;
  75. gbc_txtPreparation.gridy = 0;
  76. add(txtPreparation, gbc_txtPreparation);
  77. txtPreparation.setColumns(10);
  78. listener = new ObservableListener<>(this, (c, t) -> {
  79. if (txtName.getText().equals(t.getName())) return;
  80. txtName.setText(t.getName());
  81. });
  82. listener.setObserved(ingredient);
  83. }
  84. @Override
  85. public void removeNotify() {
  86. super.removeNotify();
  87. ObserverDispatch.unsubscribeAll(listener);
  88. }
  89. }