IngredientPanel.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.event.DocumentListener;
  10. import javax.swing.text.NumberFormatter;
  11. import org.leumasjaffe.observer.ObservableController;
  12. import org.leumasjaffe.observer.ObserverDispatch;
  13. import org.leumasjaffe.recipe.model.Ingredient;
  14. import javax.swing.JFormattedTextField;
  15. import java.awt.Font;
  16. import javax.swing.JLabel;
  17. @SuppressWarnings("serial")
  18. public class IngredientPanel extends JPanel implements AutoGrowPanel.DocumentListenable {
  19. ObservableController<JTextField, Ingredient> controller;
  20. private JTextField txtName;
  21. public IngredientPanel(final Ingredient ingredient) {
  22. GridBagLayout gridBagLayout = new GridBagLayout();
  23. gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0};
  24. gridBagLayout.rowHeights = new int[]{0, 0};
  25. gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
  26. gridBagLayout.rowWeights = new double[]{0.0, Double.MIN_VALUE};
  27. setLayout(gridBagLayout);
  28. JLabel label = new JLabel("\u2022");
  29. GridBagConstraints gbc_label = new GridBagConstraints();
  30. gbc_label.insets = new Insets(0, 0, 0, 5);
  31. gbc_label.anchor = GridBagConstraints.EAST;
  32. gbc_label.gridx = 0;
  33. gbc_label.gridy = 0;
  34. add(label, gbc_label);
  35. txtName = new JTextField(ingredient.getName());
  36. txtName.setFont(new Font("Source Code Pro", Font.PLAIN, 10));
  37. GridBagConstraints gbc_txtName = new GridBagConstraints();
  38. gbc_txtName.fill = GridBagConstraints.HORIZONTAL;
  39. gbc_txtName.insets = new Insets(0, 0, 0, 5);
  40. gbc_txtName.gridx = 1;
  41. gbc_txtName.gridy = 0;
  42. add(txtName, gbc_txtName);
  43. txtName.setColumns(10);
  44. NumberFormatter fmtDone = new NumberFormatter(NumberFormat.getNumberInstance(Locale.getDefault()));
  45. fmtDone.setMinimum(0.0);
  46. fmtDone.setCommitsOnValidEdit(true);
  47. JFormattedTextField txtAmount = new JFormattedTextField(fmtDone);
  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.setFont(new Font("Source Code Pro", Font.PLAIN, 10));
  59. GridBagConstraints gbc_txtUnit = new GridBagConstraints();
  60. gbc_txtUnit.insets = new Insets(0, 0, 0, 5);
  61. gbc_txtUnit.anchor = GridBagConstraints.ABOVE_BASELINE;
  62. gbc_txtUnit.fill = GridBagConstraints.HORIZONTAL;
  63. gbc_txtUnit.gridx = 3;
  64. gbc_txtUnit.gridy = 0;
  65. add(txtUnit, gbc_txtUnit);
  66. txtUnit.setColumns(6);
  67. JTextField txtPreparation = new JTextField(ingredient.getPreparation());
  68. txtPreparation.setFont(new Font("Source Code Pro", Font.PLAIN, 10));
  69. GridBagConstraints gbc_txtPreparation = new GridBagConstraints();
  70. gbc_txtPreparation.anchor = GridBagConstraints.ABOVE_BASELINE;
  71. gbc_txtPreparation.fill = GridBagConstraints.HORIZONTAL;
  72. gbc_txtPreparation.gridx = 4;
  73. gbc_txtPreparation.gridy = 0;
  74. add(txtPreparation, gbc_txtPreparation);
  75. txtPreparation.setColumns(10);
  76. // I technically don't need to listen here as of this change,
  77. // but if I ever restore support for it, it will be convenient.
  78. controller = new ObservableController<>(txtName,
  79. Ingredient::getName, Ingredient::setName,
  80. JTextField::setText);
  81. controller.setObserved(ingredient);
  82. }
  83. @Override
  84. public void addDocumentListener(DocumentListener dl) {
  85. this.txtName.getDocument().addDocumentListener(dl);
  86. }
  87. @Override
  88. public void removeDocumentListener(DocumentListener dl) {
  89. this.txtName.getDocument().removeDocumentListener(dl);
  90. }
  91. @Override
  92. public void removeNotify() {
  93. super.removeNotify();
  94. ObserverDispatch.unsubscribeAll(controller);
  95. }
  96. }