|
|
@@ -10,6 +10,7 @@ import javax.swing.event.DocumentListener;
|
|
|
|
|
|
import org.leumasjaffe.observer.ObservableController;
|
|
|
import org.leumasjaffe.observer.ObserverDispatch;
|
|
|
+import org.leumasjaffe.recipe.model.Amount;
|
|
|
import org.leumasjaffe.recipe.model.Ingredient;
|
|
|
import org.leumasjaffe.recipe.view.formatter.AmountFormatter;
|
|
|
|
|
|
@@ -25,12 +26,13 @@ import javax.swing.JLabel;
|
|
|
@FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
|
|
|
public class IngredientPanel extends JPanel implements AutoGrowPanel.DocumentListenable {
|
|
|
ObservableController<JTextField, Ingredient> nameController;
|
|
|
+ ObservableController<JFormattedTextField, Ingredient> amountController;
|
|
|
ObservableController<JTextField, Ingredient> preparationController;
|
|
|
@Getter(AccessLevel.PACKAGE) JTextField txtName;
|
|
|
@Getter(AccessLevel.PACKAGE) JFormattedTextField txtAmount;
|
|
|
@Getter(AccessLevel.PACKAGE) JTextField txtPreparation;
|
|
|
|
|
|
- public IngredientPanel(final Ingredient ingredient) {
|
|
|
+ public IngredientPanel() {
|
|
|
GridBagLayout gridBagLayout = new GridBagLayout();
|
|
|
gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0};
|
|
|
gridBagLayout.rowHeights = new int[]{0, 0};
|
|
|
@@ -46,7 +48,7 @@ public class IngredientPanel extends JPanel implements AutoGrowPanel.DocumentLis
|
|
|
gbc_label.gridy = 0;
|
|
|
add(label, gbc_label);
|
|
|
|
|
|
- txtName = new JTextField(ingredient.getName());
|
|
|
+ txtName = new JTextField();
|
|
|
txtName.setFont(new Font("Source Code Pro", Font.PLAIN, 10));
|
|
|
GridBagConstraints gbc_txtName = new GridBagConstraints();
|
|
|
gbc_txtName.fill = GridBagConstraints.HORIZONTAL;
|
|
|
@@ -57,7 +59,6 @@ public class IngredientPanel extends JPanel implements AutoGrowPanel.DocumentLis
|
|
|
txtName.setColumns(10);
|
|
|
|
|
|
txtAmount = new JFormattedTextField(new AmountFormatter());
|
|
|
- txtAmount.setValue(ingredient.getAmount());
|
|
|
txtAmount.setFont(new Font("Source Code Pro", Font.PLAIN, 10));
|
|
|
GridBagConstraints gbc_txtAmount = new GridBagConstraints();
|
|
|
gbc_txtAmount.fill = GridBagConstraints.HORIZONTAL;
|
|
|
@@ -67,7 +68,7 @@ public class IngredientPanel extends JPanel implements AutoGrowPanel.DocumentLis
|
|
|
add(txtAmount, gbc_txtAmount);
|
|
|
txtAmount.setColumns(11);
|
|
|
|
|
|
- txtPreparation = new JTextField(ingredient.getPreparation());
|
|
|
+ txtPreparation = new JTextField();
|
|
|
txtPreparation.setFont(new Font("Source Code Pro", Font.PLAIN, 10));
|
|
|
GridBagConstraints gbc_txtPreparation = new GridBagConstraints();
|
|
|
gbc_txtPreparation.anchor = GridBagConstraints.ABOVE_BASELINE;
|
|
|
@@ -82,14 +83,26 @@ public class IngredientPanel extends JPanel implements AutoGrowPanel.DocumentLis
|
|
|
nameController = new ObservableController<>(txtName,
|
|
|
Ingredient::getName, Ingredient::setName,
|
|
|
JTextField::setText);
|
|
|
+ amountController = new ObservableController<>(txtAmount,
|
|
|
+ ing -> ing.getAmount().toString(),
|
|
|
+ (ing, str) -> ing.setAmount(new Amount(str)),
|
|
|
+ (c, str) -> c.setValue(new Amount(str)));
|
|
|
preparationController = new ObservableController<>(txtPreparation,
|
|
|
Ingredient::getPreparation, Ingredient::setPreparation,
|
|
|
JTextField::setText);
|
|
|
+ }
|
|
|
|
|
|
+ public IngredientPanel(final Ingredient ingredient) {
|
|
|
+ this();
|
|
|
+ setModel(ingredient);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setModel(final Ingredient ingredient) {
|
|
|
nameController.setObserved(ingredient);
|
|
|
+ amountController.setObserved(ingredient);
|
|
|
preparationController.setObserved(ingredient);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Override
|
|
|
public void addDocumentListener(DocumentListener dl) {
|
|
|
this.txtName.getDocument().addDocumentListener(dl);
|