package org.leumasjaffe.recipe.view; import javax.swing.JPanel; import java.awt.GridBagLayout; import javax.swing.JTextField; import java.awt.GridBagConstraints; import java.awt.Insets; import java.awt.event.ItemEvent; import java.text.NumberFormat; import java.util.Locale; import javax.swing.JToggleButton; import javax.swing.event.DocumentListener; import javax.swing.text.NumberFormatter; import org.leumasjaffe.recipe.model.Ingredient; import javax.swing.DefaultComboBoxModel; import javax.swing.JComboBox; import javax.swing.JFormattedTextField; import java.awt.Font; import javax.swing.JLabel; @SuppressWarnings("serial") public class IngredientPanel extends JPanel implements AutoGrowPanel.DocumentListenable { private JTextField txtName; public IngredientPanel() { GridBagLayout gridBagLayout = new GridBagLayout(); gridBagLayout.columnWidths = new int[]{0, 100, 40, 0, 0, 0}; gridBagLayout.rowHeights = new int[]{0, 0}; gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0, 0.0, 0.0, Double.MIN_VALUE}; gridBagLayout.rowWeights = new double[]{0.0, Double.MIN_VALUE}; setLayout(gridBagLayout); JLabel label = new JLabel("\u2022"); GridBagConstraints gbc_label = new GridBagConstraints(); gbc_label.insets = new Insets(0, 0, 0, 5); gbc_label.anchor = GridBagConstraints.EAST; gbc_label.gridx = 0; gbc_label.gridy = 0; add(label, gbc_label); txtName = new JTextField(); txtName.setFont(new Font("Source Code Pro", Font.PLAIN, 10)); GridBagConstraints gbc_txtName = new GridBagConstraints(); gbc_txtName.fill = GridBagConstraints.HORIZONTAL; gbc_txtName.insets = new Insets(0, 0, 0, 5); gbc_txtName.gridx = 1; gbc_txtName.gridy = 0; add(txtName, gbc_txtName); txtName.setColumns(10); NumberFormatter fmtDone = new NumberFormatter(NumberFormat.getNumberInstance(Locale.getDefault())); fmtDone.setMinimum(0.0); fmtDone.setCommitsOnValidEdit(true); JFormattedTextField txtAmount = new JFormattedTextField(fmtDone); txtAmount.setFont(new Font("Source Code Pro", Font.PLAIN, 10)); GridBagConstraints gbc_txtAmount = new GridBagConstraints(); gbc_txtAmount.fill = GridBagConstraints.HORIZONTAL; gbc_txtAmount.insets = new Insets(0, 0, 0, 5); gbc_txtAmount.gridx = 2; gbc_txtAmount.gridy = 0; add(txtAmount, gbc_txtAmount); txtAmount.setColumns(4); JToggleButton tglbtnUnits = new JToggleButton("vol"); tglbtnUnits.setFont(new Font("Source Code Pro", Font.PLAIN, 10)); tglbtnUnits.setMargin(new Insets(2, 5, 2, 5)); GridBagConstraints gbc_tglbtnUnits = new GridBagConstraints(); gbc_tglbtnUnits.insets = new Insets(0, 0, 0, 5); gbc_tglbtnUnits.gridx = 3; gbc_tglbtnUnits.gridy = 0; add(tglbtnUnits, gbc_tglbtnUnits); JComboBox weights = new JComboBox<>(new DefaultComboBoxModel<>(Ingredient.Weight.values())); weights.setFont(new Font("Source Code Pro", Font.PLAIN, 10)); JComboBox volumes = new JComboBox<>(new DefaultComboBoxModel<>(Ingredient.Volume.values())); volumes.setFont(new Font("Source Code Pro", Font.PLAIN, 10)); GridBagConstraints gbc_comboBox = new GridBagConstraints(); gbc_comboBox.fill = GridBagConstraints.HORIZONTAL; gbc_comboBox.gridx = 4; gbc_comboBox.gridy = 0; add(volumes, gbc_comboBox); // add(weights, gbc_comboBox); tglbtnUnits.addItemListener(e -> { if (e.getStateChange() == ItemEvent.SELECTED) { tglbtnUnits.setText("wgt"); remove(volumes); add(weights, gbc_comboBox); } else { tglbtnUnits.setText("vol"); remove(weights); add(volumes, gbc_comboBox); } repaint(); }); } @Override public void addDocumentListener(DocumentListener dl) { this.txtName.getDocument().addDocumentListener(dl); } @Override public void removeDocumentListener(DocumentListener dl) { this.txtName.getDocument().removeDocumentListener(dl); } }