feat(client/shopping): delegate list view cell rendering and make add element handler
This commit is contained in:
parent
795926298e
commit
670de432c5
1 changed files with 29 additions and 20 deletions
|
|
@ -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<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);
|
||||
}
|
||||
});
|
||||
|
||||
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<ShoppingListNewItemPromptCtrl, Parent> 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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue