feat: ingredient adding, deletion and editing
This commit is contained in:
parent
b1af6c1ef9
commit
bdf6f71c01
2 changed files with 81 additions and 15 deletions
|
|
@ -1,39 +1,90 @@
|
|||
package client.scenes.recipe;
|
||||
|
||||
import commons.Recipe;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.ResourceBundle;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.fxml.Initializable;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.ListView;
|
||||
|
||||
import java.net.URL;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
public class IngredientListCtrl implements Initializable {
|
||||
private Recipe recipe;
|
||||
private ObservableList<String> ingredients;
|
||||
|
||||
@FXML
|
||||
ListView<String> ingredientListView;
|
||||
|
||||
public IngredientListCtrl() {}
|
||||
@FXML
|
||||
Button addIngredientButton;
|
||||
@FXML
|
||||
Button deleteIngredientButton;
|
||||
|
||||
public void setRecipe(Recipe recipe) {
|
||||
this.recipe = recipe;
|
||||
this.fetchFromRecipe();
|
||||
}
|
||||
|
||||
private void fetchFromRecipe() {
|
||||
if (recipe == null) {
|
||||
this.ingredients = FXCollections.observableArrayList(new ArrayList<>());
|
||||
} else {
|
||||
List<String> ingredientList = recipe.getIngredients();
|
||||
this.ingredients = FXCollections.observableArrayList(ingredientList);
|
||||
}
|
||||
|
||||
this.ingredientListView.setItems(this.ingredients);
|
||||
this.refresh();
|
||||
}
|
||||
|
||||
private void refresh() {
|
||||
if (recipe == null) {
|
||||
return;
|
||||
private void updateRecipeIngredients() {
|
||||
if (this.recipe != null) {
|
||||
List<String> updatedIngredients = new ArrayList<>(this.ingredients);
|
||||
this.recipe.setIngredients(updatedIngredients);
|
||||
}
|
||||
}
|
||||
|
||||
var ingredients = recipe.getIngredients();
|
||||
ingredientListView.setItems(FXCollections.observableList(ingredients));
|
||||
private void refresh() {
|
||||
ingredientListView.refresh();
|
||||
}
|
||||
|
||||
@FXML
|
||||
public void initialize(URL location, ResourceBundle resources) {
|
||||
this.ingredientListView.setEditable(true);
|
||||
this.ingredientListView.setCellFactory(
|
||||
list -> {
|
||||
return new IngredientListCell();
|
||||
});
|
||||
|
||||
this.ingredientListView.setOnEditCommit(event -> {
|
||||
int index = event.getIndex();
|
||||
String newValue = event.getNewValue();
|
||||
this.ingredients.set(index, newValue);
|
||||
this.updateRecipeIngredients();
|
||||
});
|
||||
|
||||
// TODO: actually communicate with the server :)
|
||||
this.addIngredientButton.setOnAction(event -> {
|
||||
this.ingredients.add("Ingredient " + (this.ingredients.size() + 1));
|
||||
this.updateRecipeIngredients();
|
||||
|
||||
this.ingredientListView.getSelectionModel().select(
|
||||
this.ingredients.size() - 1);
|
||||
});
|
||||
|
||||
this.deleteIngredientButton.setOnAction(event -> {
|
||||
int selectedIndex = this.ingredientListView.getSelectionModel().getSelectedIndex();
|
||||
if (selectedIndex >= 0) {
|
||||
this.ingredients.remove(selectedIndex);
|
||||
}
|
||||
|
||||
this.updateRecipeIngredients();
|
||||
});
|
||||
|
||||
this.refresh();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<?import javafx.geometry.Insets?>
|
||||
<?import javafx.scene.control.Button?>
|
||||
<?import javafx.scene.control.Label?>
|
||||
<?import javafx.scene.control.ListView?>
|
||||
<?import javafx.scene.layout.AnchorPane?>
|
||||
<?import javafx.scene.layout.HBox?>
|
||||
<?import javafx.scene.layout.VBox?>
|
||||
<?import javafx.scene.text.Font?>
|
||||
|
||||
|
|
@ -11,14 +13,27 @@
|
|||
<children>
|
||||
<VBox minHeight="200.0" minWidth="200.0">
|
||||
<children>
|
||||
<Label text="Ingredients">
|
||||
<HBox alignment="CENTER_LEFT" spacing="20.0">
|
||||
<children>
|
||||
<Label text="Ingredients">
|
||||
<padding>
|
||||
<Insets bottom="5.0" top="5.0" />
|
||||
</padding>
|
||||
<font>
|
||||
<Font name="System Bold" size="16.0" />
|
||||
</font>
|
||||
</Label>
|
||||
<HBox alignment="CENTER" spacing="10.0">
|
||||
<children>
|
||||
<Button fx:id="addIngredientButton" mnemonicParsing="false" text="Add" />
|
||||
<Button fx:id="deleteIngredientButton" mnemonicParsing="false" text="Delete" />
|
||||
</children>
|
||||
</HBox>
|
||||
</children>
|
||||
<padding>
|
||||
<Insets bottom="5.0" left="10.0" right="10.0" top="5.0" />
|
||||
<Insets left="10.0" right="10.0" />
|
||||
</padding>
|
||||
<font>
|
||||
<Font name="System Bold" size="16.0" />
|
||||
</font>
|
||||
</Label>
|
||||
</HBox>
|
||||
<ListView fx:id="ingredientListView" prefHeight="200.0" prefWidth="200.0" />
|
||||
</children>
|
||||
</VBox>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue