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.text.NumberFormatter; import org.leumasjaffe.observer.ObservableListener; 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 IngredientPreparationPanel extends JPanel { private ObservableListener listener; public IngredientPreparationPanel(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); JTextField txtName = new JTextField(ingredient.getName()); txtName.setEditable(false); 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.setEditable(false); 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.setEditable(false); 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.setEditable(false); 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); listener = new ObservableListener<>(this, (c, t) -> { if (txtName.getText().equals(t.getName())) return; txtName.setText(t.getName()); }); listener.setObserved(ingredient); } @Override public void removeNotify() { super.removeNotify(); ObserverDispatch.unsubscribeAll(listener); } }