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;
|
package client.scenes.recipe;
|
||||||
|
|
||||||
import commons.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.FXCollections;
|
||||||
|
import javafx.collections.ObservableList;
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
import javafx.fxml.Initializable;
|
import javafx.fxml.Initializable;
|
||||||
|
import javafx.scene.control.Button;
|
||||||
import javafx.scene.control.ListView;
|
import javafx.scene.control.ListView;
|
||||||
|
|
||||||
import java.net.URL;
|
|
||||||
import java.util.ResourceBundle;
|
|
||||||
|
|
||||||
public class IngredientListCtrl implements Initializable {
|
public class IngredientListCtrl implements Initializable {
|
||||||
private Recipe recipe;
|
private Recipe recipe;
|
||||||
|
private ObservableList<String> ingredients;
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
ListView<String> ingredientListView;
|
ListView<String> ingredientListView;
|
||||||
|
|
||||||
public IngredientListCtrl() {}
|
@FXML
|
||||||
|
Button addIngredientButton;
|
||||||
|
@FXML
|
||||||
|
Button deleteIngredientButton;
|
||||||
|
|
||||||
public void setRecipe(Recipe recipe) {
|
public void setRecipe(Recipe recipe) {
|
||||||
this.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();
|
this.refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void refresh() {
|
private void updateRecipeIngredients() {
|
||||||
if (recipe == null) {
|
if (this.recipe != null) {
|
||||||
return;
|
List<String> updatedIngredients = new ArrayList<>(this.ingredients);
|
||||||
|
this.recipe.setIngredients(updatedIngredients);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var ingredients = recipe.getIngredients();
|
private void refresh() {
|
||||||
ingredientListView.setItems(FXCollections.observableList(ingredients));
|
|
||||||
ingredientListView.refresh();
|
ingredientListView.refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
public void initialize(URL location, ResourceBundle resources) {
|
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();
|
this.refresh();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,11 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
<?import javafx.geometry.Insets?>
|
<?import javafx.geometry.Insets?>
|
||||||
|
<?import javafx.scene.control.Button?>
|
||||||
<?import javafx.scene.control.Label?>
|
<?import javafx.scene.control.Label?>
|
||||||
<?import javafx.scene.control.ListView?>
|
<?import javafx.scene.control.ListView?>
|
||||||
<?import javafx.scene.layout.AnchorPane?>
|
<?import javafx.scene.layout.AnchorPane?>
|
||||||
|
<?import javafx.scene.layout.HBox?>
|
||||||
<?import javafx.scene.layout.VBox?>
|
<?import javafx.scene.layout.VBox?>
|
||||||
<?import javafx.scene.text.Font?>
|
<?import javafx.scene.text.Font?>
|
||||||
|
|
||||||
|
|
@ -11,14 +13,27 @@
|
||||||
<children>
|
<children>
|
||||||
<VBox minHeight="200.0" minWidth="200.0">
|
<VBox minHeight="200.0" minWidth="200.0">
|
||||||
<children>
|
<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>
|
<padding>
|
||||||
<Insets bottom="5.0" left="10.0" right="10.0" top="5.0" />
|
<Insets left="10.0" right="10.0" />
|
||||||
</padding>
|
</padding>
|
||||||
<font>
|
</HBox>
|
||||||
<Font name="System Bold" size="16.0" />
|
|
||||||
</font>
|
|
||||||
</Label>
|
|
||||||
<ListView fx:id="ingredientListView" prefHeight="200.0" prefWidth="200.0" />
|
<ListView fx:id="ingredientListView" prefHeight="200.0" prefWidth="200.0" />
|
||||||
</children>
|
</children>
|
||||||
</VBox>
|
</VBox>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue