feat(client/shopping): create custom editable shopping list cell element
This commit is contained in:
parent
f03c12cc0f
commit
795926298e
1 changed files with 67 additions and 0 deletions
|
|
@ -0,0 +1,67 @@
|
||||||
|
package client.scenes.shopping;
|
||||||
|
|
||||||
|
import client.scenes.recipe.OrderedEditableListCell;
|
||||||
|
import commons.FormalIngredient;
|
||||||
|
import javafx.scene.Node;
|
||||||
|
import javafx.scene.control.Label;
|
||||||
|
import javafx.scene.control.Spinner;
|
||||||
|
import javafx.scene.control.SpinnerValueFactory;
|
||||||
|
import javafx.scene.input.KeyCode;
|
||||||
|
import javafx.scene.input.KeyEvent;
|
||||||
|
import javafx.scene.layout.HBox;
|
||||||
|
import javafx.util.Pair;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public class ShoppingListCell extends OrderedEditableListCell<Pair<FormalIngredient, Optional<String>>> {
|
||||||
|
private Node makeEditor() {
|
||||||
|
HBox editor = new HBox();
|
||||||
|
Spinner<Double> amountInput = new Spinner<>();
|
||||||
|
FormalIngredient ingredient = getItem().getKey();
|
||||||
|
amountInput.setEditable(true);
|
||||||
|
amountInput.setValueFactory(new SpinnerValueFactory.DoubleSpinnerValueFactory(0, Double.MAX_VALUE, ingredient.getAmount()));
|
||||||
|
Label textLabel = new Label(getItem().getKey().getUnitSuffix() + " of " + getItem().getKey().getIngredient().getName());
|
||||||
|
editor.getChildren().addAll(amountInput, textLabel);
|
||||||
|
editor.addEventHandler(KeyEvent.KEY_RELEASED, e -> {
|
||||||
|
if (e.getCode() != KeyCode.ENTER) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Pair<FormalIngredient, Optional<String>> pair = getItem();
|
||||||
|
pair.getKey().setAmount(amountInput.getValue());
|
||||||
|
commitEdit(pair);
|
||||||
|
});
|
||||||
|
return editor;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void startEdit() {
|
||||||
|
super.startEdit();
|
||||||
|
this.setText("");
|
||||||
|
this.setGraphic(makeEditor());
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
protected void updateItem(Pair<FormalIngredient, Optional<String>> item, boolean empty) {
|
||||||
|
super.updateItem(item, empty);
|
||||||
|
|
||||||
|
if (empty) {
|
||||||
|
this.setText("");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String display = item.getKey().toString() +
|
||||||
|
item.getValue().map(recipe -> {
|
||||||
|
return " (" + recipe + ")";
|
||||||
|
}).orElse("");
|
||||||
|
|
||||||
|
this.setText(display);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void cancelEdit() {
|
||||||
|
super.cancelEdit();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void commitEdit(Pair<FormalIngredient, Optional<String>> newValue) {
|
||||||
|
super.commitEdit(newValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue