IngredientPanel.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.awt.event.ItemEvent;
  8. import java.text.NumberFormat;
  9. import java.util.Locale;
  10. import javax.swing.JToggleButton;
  11. import javax.swing.event.DocumentListener;
  12. import javax.swing.text.NumberFormatter;
  13. import org.leumasjaffe.recipe.model.Ingredient;
  14. import javax.swing.DefaultComboBoxModel;
  15. import javax.swing.JComboBox;
  16. import javax.swing.JFormattedTextField;
  17. import java.awt.Font;
  18. import javax.swing.JLabel;
  19. @SuppressWarnings("serial")
  20. public class IngredientPanel extends JPanel implements AutoGrowPanel.DocumentListenable {
  21. private JTextField txtName;
  22. public IngredientPanel() {
  23. GridBagLayout gridBagLayout = new GridBagLayout();
  24. gridBagLayout.columnWidths = new int[]{0, 100, 40, 0, 0, 0};
  25. gridBagLayout.rowHeights = new int[]{0, 0};
  26. gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
  27. gridBagLayout.rowWeights = new double[]{0.0, Double.MIN_VALUE};
  28. setLayout(gridBagLayout);
  29. JLabel label = new JLabel("\u2022");
  30. GridBagConstraints gbc_label = new GridBagConstraints();
  31. gbc_label.insets = new Insets(0, 0, 0, 5);
  32. gbc_label.anchor = GridBagConstraints.EAST;
  33. gbc_label.gridx = 0;
  34. gbc_label.gridy = 0;
  35. add(label, gbc_label);
  36. txtName = new JTextField();
  37. txtName.setFont(new Font("Source Code Pro", Font.PLAIN, 10));
  38. GridBagConstraints gbc_txtName = new GridBagConstraints();
  39. gbc_txtName.fill = GridBagConstraints.HORIZONTAL;
  40. gbc_txtName.insets = new Insets(0, 0, 0, 5);
  41. gbc_txtName.gridx = 1;
  42. gbc_txtName.gridy = 0;
  43. add(txtName, gbc_txtName);
  44. txtName.setColumns(10);
  45. NumberFormatter fmtDone = new NumberFormatter(NumberFormat.getNumberInstance(Locale.getDefault()));
  46. fmtDone.setMinimum(0.0);
  47. fmtDone.setCommitsOnValidEdit(true);
  48. JFormattedTextField txtAmount = new JFormattedTextField(fmtDone);
  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. JToggleButton tglbtnUnits = new JToggleButton("vol");
  58. tglbtnUnits.setFont(new Font("Source Code Pro", Font.PLAIN, 10));
  59. tglbtnUnits.setMargin(new Insets(2, 5, 2, 5));
  60. GridBagConstraints gbc_tglbtnUnits = new GridBagConstraints();
  61. gbc_tglbtnUnits.insets = new Insets(0, 0, 0, 5);
  62. gbc_tglbtnUnits.gridx = 3;
  63. gbc_tglbtnUnits.gridy = 0;
  64. add(tglbtnUnits, gbc_tglbtnUnits);
  65. JComboBox<Ingredient.Weight> weights = new JComboBox<>(new DefaultComboBoxModel<>(Ingredient.Weight.values()));
  66. weights.setFont(new Font("Source Code Pro", Font.PLAIN, 10));
  67. JComboBox<Ingredient.Volume> volumes = new JComboBox<>(new DefaultComboBoxModel<>(Ingredient.Volume.values()));
  68. volumes.setFont(new Font("Source Code Pro", Font.PLAIN, 10));
  69. GridBagConstraints gbc_comboBox = new GridBagConstraints();
  70. gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
  71. gbc_comboBox.gridx = 4;
  72. gbc_comboBox.gridy = 0;
  73. add(volumes, gbc_comboBox);
  74. // add(weights, gbc_comboBox);
  75. tglbtnUnits.addItemListener(e -> {
  76. if (e.getStateChange() == ItemEvent.SELECTED) {
  77. tglbtnUnits.setText("wgt");
  78. remove(volumes);
  79. add(weights, gbc_comboBox);
  80. } else {
  81. tglbtnUnits.setText("vol");
  82. remove(weights);
  83. add(volumes, gbc_comboBox);
  84. }
  85. repaint();
  86. });
  87. }
  88. @Override
  89. public void addDocumentListener(DocumentListener dl) {
  90. this.txtName.getDocument().addDocumentListener(dl);
  91. }
  92. @Override
  93. public void removeDocumentListener(DocumentListener dl) {
  94. this.txtName.getDocument().removeDocumentListener(dl);
  95. }
  96. }