From 670de432c5d177e4f39ec3c0538bd908e2a614ca Mon Sep 17 00:00:00 2001 From: Steven Liu Date: Thu, 22 Jan 2026 19:54:33 +0100 Subject: [PATCH] feat(client/shopping): delegate list view cell rendering and make add element handler --- .../scenes/shopping/ShoppingListCtrl.java | 49 +++++++++++-------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/client/src/main/java/client/scenes/shopping/ShoppingListCtrl.java b/client/src/main/java/client/scenes/shopping/ShoppingListCtrl.java index 0e76b79..ec08c62 100644 --- a/client/src/main/java/client/scenes/shopping/ShoppingListCtrl.java +++ b/client/src/main/java/client/scenes/shopping/ShoppingListCtrl.java @@ -1,5 +1,6 @@ package client.scenes.shopping; +import client.UI; import client.service.ShoppingListService; import client.utils.LocaleAware; import client.utils.LocaleManager; @@ -7,8 +8,12 @@ import com.google.inject.Inject; import commons.FormalIngredient; import javafx.event.ActionEvent; import javafx.fxml.FXML; -import javafx.scene.control.ListCell; +import javafx.scene.Node; +import javafx.scene.Parent; +import javafx.scene.Scene; import javafx.scene.control.ListView; +import javafx.stage.Modality; +import javafx.stage.Stage; import javafx.util.Pair; import java.util.Optional; @@ -40,33 +45,37 @@ public class ShoppingListCtrl implements LocaleAware { } public void initializeComponents() { - this.shoppingListView.setCellFactory(l -> new ListCell<>() { - @Override - protected void updateItem(Pair> 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); - } - }); - + this.shoppingListView.setEditable(true); + this.shoppingListView.setCellFactory(l -> new ShoppingListCell()); + this.shoppingListView.getItems().setAll( + this.shopping.getItems() + ); + } + private void refreshList() { this.shoppingListView.getItems().setAll( this.shopping.getItems() ); } public void handleAddItem(ActionEvent actionEvent) { + Stage stage = new Stage(); + Pair root = UI.getFXML().load(ShoppingListNewItemPromptCtrl.class, + "client", "scenes", "shopping", "ShoppingListItemAddModal.fxml"); + root.getKey().setNewValueConsumer(fi -> { + this.shopping.putIngredient(fi); + refreshList(); + }); + stage.setScene(new Scene(root.getValue())); + stage.setTitle("My modal window"); + stage.initModality(Modality.WINDOW_MODAL); + stage.initOwner( + ((Node)actionEvent.getSource()).getScene().getWindow() ); + stage.show(); } public void handleRemoveItem(ActionEvent actionEvent) { + var x = this.shoppingListView.getSelectionModel().getSelectedItem(); + this.shopping.getItems().remove(x); + refreshList(); } }