|
@@ -5,15 +5,14 @@ import java.awt.GridBagLayout;
|
|
|
import javax.swing.JTextField;
|
|
import javax.swing.JTextField;
|
|
|
import java.awt.GridBagConstraints;
|
|
import java.awt.GridBagConstraints;
|
|
|
import java.awt.Insets;
|
|
import java.awt.Insets;
|
|
|
-import java.text.NumberFormat;
|
|
|
|
|
-import java.util.Locale;
|
|
|
|
|
|
|
|
|
|
import javax.swing.event.DocumentListener;
|
|
import javax.swing.event.DocumentListener;
|
|
|
-import javax.swing.text.NumberFormatter;
|
|
|
|
|
|
|
|
|
|
import org.leumasjaffe.observer.ObservableController;
|
|
import org.leumasjaffe.observer.ObservableController;
|
|
|
|
|
+import org.leumasjaffe.observer.ObservableListener;
|
|
|
import org.leumasjaffe.observer.ObserverDispatch;
|
|
import org.leumasjaffe.observer.ObserverDispatch;
|
|
|
import org.leumasjaffe.recipe.model.Ingredient;
|
|
import org.leumasjaffe.recipe.model.Ingredient;
|
|
|
|
|
+import org.leumasjaffe.recipe.view.formatter.AmountFormatter;
|
|
|
|
|
|
|
|
import lombok.AccessLevel;
|
|
import lombok.AccessLevel;
|
|
|
import lombok.Getter;
|
|
import lombok.Getter;
|
|
@@ -26,18 +25,18 @@ import javax.swing.JLabel;
|
|
|
@SuppressWarnings("serial")
|
|
@SuppressWarnings("serial")
|
|
|
@FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
|
|
@FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
|
|
|
public class IngredientPanel extends JPanel implements AutoGrowPanel.DocumentListenable {
|
|
public class IngredientPanel extends JPanel implements AutoGrowPanel.DocumentListenable {
|
|
|
- ObservableController<JTextField, Ingredient> nameController;
|
|
|
|
|
- ObservableController<JTextField, Ingredient> preparationController;
|
|
|
|
|
|
|
+ ObservableListener<JTextField, Ingredient> nameController;
|
|
|
|
|
+ ObservableListener<JFormattedTextField, Ingredient> amountController;
|
|
|
|
|
+ ObservableListener<JTextField, Ingredient> preparationController;
|
|
|
@Getter(AccessLevel.PACKAGE) JTextField txtName;
|
|
@Getter(AccessLevel.PACKAGE) JTextField txtName;
|
|
|
@Getter(AccessLevel.PACKAGE) JFormattedTextField txtAmount;
|
|
@Getter(AccessLevel.PACKAGE) JFormattedTextField txtAmount;
|
|
|
- @Getter(AccessLevel.PACKAGE) JTextField txtUnit;
|
|
|
|
|
@Getter(AccessLevel.PACKAGE) JTextField txtPreparation;
|
|
@Getter(AccessLevel.PACKAGE) JTextField txtPreparation;
|
|
|
|
|
|
|
|
- public IngredientPanel(final Ingredient ingredient) {
|
|
|
|
|
|
|
+ public IngredientPanel() {
|
|
|
GridBagLayout gridBagLayout = new GridBagLayout();
|
|
GridBagLayout gridBagLayout = new GridBagLayout();
|
|
|
- gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0};
|
|
|
|
|
|
|
+ gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0};
|
|
|
gridBagLayout.rowHeights = new int[]{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.columnWeights = new double[]{0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
|
|
|
gridBagLayout.rowWeights = new double[]{0.0, Double.MIN_VALUE};
|
|
gridBagLayout.rowWeights = new double[]{0.0, Double.MIN_VALUE};
|
|
|
setLayout(gridBagLayout);
|
|
setLayout(gridBagLayout);
|
|
|
|
|
|
|
@@ -49,7 +48,7 @@ public class IngredientPanel extends JPanel implements AutoGrowPanel.DocumentLis
|
|
|
gbc_label.gridy = 0;
|
|
gbc_label.gridy = 0;
|
|
|
add(label, gbc_label);
|
|
add(label, gbc_label);
|
|
|
|
|
|
|
|
- txtName = new JTextField(ingredient.getName());
|
|
|
|
|
|
|
+ txtName = new JTextField();
|
|
|
txtName.setFont(new Font("Source Code Pro", Font.PLAIN, 10));
|
|
txtName.setFont(new Font("Source Code Pro", Font.PLAIN, 10));
|
|
|
GridBagConstraints gbc_txtName = new GridBagConstraints();
|
|
GridBagConstraints gbc_txtName = new GridBagConstraints();
|
|
|
gbc_txtName.fill = GridBagConstraints.HORIZONTAL;
|
|
gbc_txtName.fill = GridBagConstraints.HORIZONTAL;
|
|
@@ -59,11 +58,7 @@ public class IngredientPanel extends JPanel implements AutoGrowPanel.DocumentLis
|
|
|
add(txtName, gbc_txtName);
|
|
add(txtName, gbc_txtName);
|
|
|
txtName.setColumns(10);
|
|
txtName.setColumns(10);
|
|
|
|
|
|
|
|
- NumberFormatter fmtDone = new NumberFormatter(NumberFormat.getNumberInstance(Locale.getDefault()));
|
|
|
|
|
- fmtDone.setMinimum(0.0);
|
|
|
|
|
- fmtDone.setCommitsOnValidEdit(true);
|
|
|
|
|
- txtAmount = new JFormattedTextField(fmtDone);
|
|
|
|
|
- txtAmount.setValue(ingredient.getAmount().getValue());
|
|
|
|
|
|
|
+ txtAmount = new JFormattedTextField(new AmountFormatter());
|
|
|
txtAmount.setFont(new Font("Source Code Pro", Font.PLAIN, 10));
|
|
txtAmount.setFont(new Font("Source Code Pro", Font.PLAIN, 10));
|
|
|
GridBagConstraints gbc_txtAmount = new GridBagConstraints();
|
|
GridBagConstraints gbc_txtAmount = new GridBagConstraints();
|
|
|
gbc_txtAmount.fill = GridBagConstraints.HORIZONTAL;
|
|
gbc_txtAmount.fill = GridBagConstraints.HORIZONTAL;
|
|
@@ -71,42 +66,39 @@ public class IngredientPanel extends JPanel implements AutoGrowPanel.DocumentLis
|
|
|
gbc_txtAmount.gridx = 2;
|
|
gbc_txtAmount.gridx = 2;
|
|
|
gbc_txtAmount.gridy = 0;
|
|
gbc_txtAmount.gridy = 0;
|
|
|
add(txtAmount, gbc_txtAmount);
|
|
add(txtAmount, gbc_txtAmount);
|
|
|
- txtAmount.setColumns(4);
|
|
|
|
|
-
|
|
|
|
|
- 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);
|
|
|
|
|
|
|
+ txtAmount.setColumns(11);
|
|
|
|
|
|
|
|
- txtPreparation = new JTextField(ingredient.getPreparation());
|
|
|
|
|
|
|
+ txtPreparation = new JTextField();
|
|
|
txtPreparation.setFont(new Font("Source Code Pro", Font.PLAIN, 10));
|
|
txtPreparation.setFont(new Font("Source Code Pro", Font.PLAIN, 10));
|
|
|
GridBagConstraints gbc_txtPreparation = new GridBagConstraints();
|
|
GridBagConstraints gbc_txtPreparation = new GridBagConstraints();
|
|
|
gbc_txtPreparation.anchor = GridBagConstraints.ABOVE_BASELINE;
|
|
gbc_txtPreparation.anchor = GridBagConstraints.ABOVE_BASELINE;
|
|
|
gbc_txtPreparation.fill = GridBagConstraints.HORIZONTAL;
|
|
gbc_txtPreparation.fill = GridBagConstraints.HORIZONTAL;
|
|
|
- gbc_txtPreparation.gridx = 4;
|
|
|
|
|
|
|
+ gbc_txtPreparation.gridx = 3;
|
|
|
gbc_txtPreparation.gridy = 0;
|
|
gbc_txtPreparation.gridy = 0;
|
|
|
add(txtPreparation, gbc_txtPreparation);
|
|
add(txtPreparation, gbc_txtPreparation);
|
|
|
txtPreparation.setColumns(10);
|
|
txtPreparation.setColumns(10);
|
|
|
|
|
|
|
|
// I technically don't need to listen here as of this change,
|
|
// I technically don't need to listen here as of this change,
|
|
|
// but if I ever restore support for it, it will be convenient.
|
|
// but if I ever restore support for it, it will be convenient.
|
|
|
- nameController = new ObservableController<>(txtName,
|
|
|
|
|
- Ingredient::getName, Ingredient::setName,
|
|
|
|
|
- JTextField::setText);
|
|
|
|
|
- preparationController = new ObservableController<>(txtPreparation,
|
|
|
|
|
- Ingredient::getPreparation, Ingredient::setPreparation,
|
|
|
|
|
- JTextField::setText);
|
|
|
|
|
|
|
+ nameController = ObservableController.from(txtName,
|
|
|
|
|
+ Ingredient::getName, Ingredient::setName);
|
|
|
|
|
+ amountController = ObservableController.from(txtAmount,
|
|
|
|
|
+ Ingredient::getAmount, Ingredient::setAmount);
|
|
|
|
|
+ preparationController = ObservableController.from(txtPreparation,
|
|
|
|
|
+ Ingredient::getPreparation, Ingredient::setPreparation);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
+ public IngredientPanel(final Ingredient ingredient) {
|
|
|
|
|
+ this();
|
|
|
|
|
+ setModel(ingredient);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void setModel(final Ingredient ingredient) {
|
|
|
nameController.setObserved(ingredient);
|
|
nameController.setObserved(ingredient);
|
|
|
|
|
+ amountController.setObserved(ingredient);
|
|
|
preparationController.setObserved(ingredient);
|
|
preparationController.setObserved(ingredient);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
public void addDocumentListener(DocumentListener dl) {
|
|
public void addDocumentListener(DocumentListener dl) {
|
|
|
this.txtName.getDocument().addDocumentListener(dl);
|
|
this.txtName.getDocument().addDocumentListener(dl);
|