|
|
@@ -9,11 +9,13 @@ import java.awt.event.FocusListener;
|
|
|
|
|
|
import javax.swing.event.DocumentListener;
|
|
|
|
|
|
+import org.leumasjaffe.observer.ForwardingObservableListener;
|
|
|
import org.leumasjaffe.observer.ObservableController;
|
|
|
import org.leumasjaffe.observer.ObservableListener;
|
|
|
import org.leumasjaffe.observer.ObserverDispatch;
|
|
|
import org.leumasjaffe.recipe.model.Ingredient;
|
|
|
import org.leumasjaffe.recipe.view.formatter.AmountFormatter;
|
|
|
+import org.leumasjaffe.recipe.viewmodel.ScaleFactor;
|
|
|
|
|
|
import lombok.AccessLevel;
|
|
|
import lombok.Getter;
|
|
|
@@ -28,11 +30,15 @@ public class IngredientPanel extends JPanel implements AutoGrowPanel.ChildCompon
|
|
|
ObservableListener<JTextField, Ingredient> nameController;
|
|
|
ObservableListener<JFormattedTextField, Ingredient> amountController;
|
|
|
ObservableListener<JTextField, Ingredient> preparationController;
|
|
|
+ ForwardingObservableListener<Ingredient> forward = new ForwardingObservableListener<>();
|
|
|
+
|
|
|
@Getter(AccessLevel.PACKAGE) JTextField txtName;
|
|
|
@Getter(AccessLevel.PACKAGE) JFormattedTextField txtAmount;
|
|
|
@Getter(AccessLevel.PACKAGE) JTextField txtPreparation;
|
|
|
+ ScaleFactor scale;
|
|
|
|
|
|
- public IngredientPanel() {
|
|
|
+ public IngredientPanel(final ScaleFactor scale, boolean editable) {
|
|
|
+ this.scale = scale;
|
|
|
GridBagLayout gridBagLayout = new GridBagLayout();
|
|
|
gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0};
|
|
|
gridBagLayout.rowHeights = new int[]{0, 0};
|
|
|
@@ -49,6 +55,7 @@ public class IngredientPanel extends JPanel implements AutoGrowPanel.ChildCompon
|
|
|
add(label, gbc_label);
|
|
|
|
|
|
txtName = new JTextField();
|
|
|
+ txtName.setEditable(editable);
|
|
|
GridBagConstraints gbc_txtName = new GridBagConstraints();
|
|
|
gbc_txtName.fill = GridBagConstraints.HORIZONTAL;
|
|
|
gbc_txtName.insets = new Insets(0, 0, 0, 5);
|
|
|
@@ -58,6 +65,7 @@ public class IngredientPanel extends JPanel implements AutoGrowPanel.ChildCompon
|
|
|
txtName.setColumns(15);
|
|
|
|
|
|
txtAmount = new JFormattedTextField(new AmountFormatter());
|
|
|
+ txtAmount.setEditable(editable);
|
|
|
GridBagConstraints gbc_txtAmount = new GridBagConstraints();
|
|
|
gbc_txtAmount.fill = GridBagConstraints.HORIZONTAL;
|
|
|
gbc_txtAmount.insets = new Insets(0, 0, 0, 5);
|
|
|
@@ -67,6 +75,7 @@ public class IngredientPanel extends JPanel implements AutoGrowPanel.ChildCompon
|
|
|
txtAmount.setColumns(6);
|
|
|
|
|
|
txtPreparation = new JTextField();
|
|
|
+ txtPreparation.setEditable(editable);
|
|
|
GridBagConstraints gbc_txtPreparation = new GridBagConstraints();
|
|
|
gbc_txtPreparation.anchor = GridBagConstraints.ABOVE_BASELINE;
|
|
|
gbc_txtPreparation.fill = GridBagConstraints.HORIZONTAL;
|
|
|
@@ -75,22 +84,20 @@ public class IngredientPanel extends JPanel implements AutoGrowPanel.ChildCompon
|
|
|
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.
|
|
|
nameController = ObservableController.from(txtName,
|
|
|
Ingredient::getName, Ingredient::setName);
|
|
|
amountController = ObservableController.from(txtAmount,
|
|
|
- Ingredient::getAmount, Ingredient::setAmount);
|
|
|
+ i -> i.getAmount().scale(scale.getScale()), Ingredient::setAmount);
|
|
|
preparationController = ObservableController.from(txtPreparation,
|
|
|
Ingredient::getPreparation, Ingredient::setPreparation,
|
|
|
ing -> {
|
|
|
ing.setPreparation("");
|
|
|
ObserverDispatch.notifySubscribers(ing);
|
|
|
- });
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
- public IngredientPanel(final Ingredient ingredient) {
|
|
|
- this();
|
|
|
+ public IngredientPanel(final Ingredient ingredient, final ScaleFactor scale, boolean editable) {
|
|
|
+ this(scale, editable);
|
|
|
setModel(ingredient);
|
|
|
}
|
|
|
|
|
|
@@ -98,6 +105,15 @@ public class IngredientPanel extends JPanel implements AutoGrowPanel.ChildCompon
|
|
|
nameController.setObserved(ingredient);
|
|
|
amountController.setObserved(ingredient);
|
|
|
preparationController.setObserved(ingredient);
|
|
|
+ forward.setObserved(ingredient, scale);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void setEnabled(boolean enabled) {
|
|
|
+ super.setEnabled(enabled);
|
|
|
+ txtName.setEditable(enabled);
|
|
|
+ txtAmount.setEditable(enabled);
|
|
|
+ txtPreparation.setEditable(enabled);
|
|
|
}
|
|
|
|
|
|
@Override
|