|
|
@@ -12,6 +12,7 @@ import javax.swing.event.DocumentListener;
|
|
|
import javax.swing.text.NumberFormatter;
|
|
|
|
|
|
import org.leumasjaffe.event.AnyActionDocumentListener;
|
|
|
+import org.leumasjaffe.observer.ObservableListener;
|
|
|
import org.leumasjaffe.observer.ObserverDispatch;
|
|
|
import org.leumasjaffe.recipe.model.Ingredient;
|
|
|
|
|
|
@@ -25,6 +26,8 @@ public class IngredientPanel extends JPanel implements AutoGrowPanel.DocumentLis
|
|
|
private JTextField txtUnit;
|
|
|
private JTextField txtPreparation;
|
|
|
|
|
|
+ private ObservableListener<IngredientPanel, Ingredient> listener;
|
|
|
+
|
|
|
public IngredientPanel(final Ingredient ingredient) {
|
|
|
GridBagLayout gridBagLayout = new GridBagLayout();
|
|
|
gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0};
|
|
|
@@ -88,8 +91,14 @@ public class IngredientPanel extends JPanel implements AutoGrowPanel.DocumentLis
|
|
|
|
|
|
addDocumentListener((AnyActionDocumentListener) (e) -> {
|
|
|
ingredient.setName(txtName.getText());
|
|
|
- ObserverDispatch.notifySubscribers(ingredient);
|
|
|
+ listener.notifySubscribers(ingredient);
|
|
|
+ });
|
|
|
+
|
|
|
+ listener = new ObservableListener<>(this, (c, t) -> {
|
|
|
+ if (txtName.getText().equals(t.getName())) return;
|
|
|
+ txtName.setText(t.getName());
|
|
|
});
|
|
|
+ listener.setObserved(ingredient);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -101,5 +110,11 @@ public class IngredientPanel extends JPanel implements AutoGrowPanel.DocumentLis
|
|
|
public void removeDocumentListener(DocumentListener dl) {
|
|
|
this.txtName.getDocument().removeDocumentListener(dl);
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void removeNotify() {
|
|
|
+ super.removeNotify();
|
|
|
+ ObserverDispatch.unsubscribeAll(listener);
|
|
|
+ }
|
|
|
|
|
|
}
|