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.text.NumberFormat; import java.util.Locale; import javax.swing.event.DocumentListener; import javax.swing.text.NumberFormatter; import org.leumasjaffe.observer.ObservableController; import org.leumasjaffe.observer.ObserverDispatch; import org.leumasjaffe.recipe.model.Ingredient; import javax.swing.JFormattedTextField; import java.awt.Font; import javax.swing.JLabel; @SuppressWarnings("serial") public class IngredientPanel extends JPanel implements AutoGrowPanel.DocumentListenable { ObservableController controller; private JTextField txtName; public IngredientPanel(final Ingredient ingredient) { GridBagLayout gridBagLayout = new GridBagLayout(); gridBagLayout.columnWidths = new int[]{0, 0, 0, 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(ingredient.getName()); 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.setValue(ingredient.getAmount().getValue()); 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); JTextField txtUnit = new JTextField(ingredient.getAmount().unitName()); txtUnit.setFont(new Font("Source Code Pro", Font.PLAIN, 10)); GridBagConstraints gbc_txtUnit = new GridBagConstraints(); gbc_txtUnit.insets = new Insets(0, 0, 0, 5); gbc_txtUnit.anchor = GridBagConstraints.ABOVE_BASELINE; gbc_txtUnit.fill = GridBagConstraints.HORIZONTAL; gbc_txtUnit.gridx = 3; gbc_txtUnit.gridy = 0; add(txtUnit, gbc_txtUnit); txtUnit.setColumns(6); JTextField txtPreparation = new JTextField(ingredient.getPreparation()); txtPreparation.setFont(new Font("Source Code Pro", Font.PLAIN, 10)); GridBagConstraints gbc_txtPreparation = new GridBagConstraints(); gbc_txtPreparation.anchor = GridBagConstraints.ABOVE_BASELINE; gbc_txtPreparation.fill = GridBagConstraints.HORIZONTAL; gbc_txtPreparation.gridx = 4; gbc_txtPreparation.gridy = 0; add(txtPreparation, gbc_txtPreparation); txtPreparation.setColumns(10); // I technically don't need to listen here as of this change, // but if I ever restore support for it, it will be convenient. controller = new ObservableController<>(txtName, Ingredient::getName, Ingredient::setName, JTextField::setText); controller.setObserved(ingredient); } @Override public void addDocumentListener(DocumentListener dl) { this.txtName.getDocument().addDocumentListener(dl); } @Override public void removeDocumentListener(DocumentListener dl) { this.txtName.getDocument().removeDocumentListener(dl); } @Override public void removeNotify() { super.removeNotify(); ObserverDispatch.unsubscribeAll(controller); } }