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;
|
package client.scenes.shopping;
|
||||||
|
|
||||||
|
import client.UI;
|
||||||
import client.service.ShoppingListService;
|
import client.service.ShoppingListService;
|
||||||
import client.utils.LocaleAware;
|
import client.utils.LocaleAware;
|
||||||
import client.utils.LocaleManager;
|
import client.utils.LocaleManager;
|
||||||
|
|
@ -7,8 +8,12 @@ import com.google.inject.Inject;
|
||||||
import commons.FormalIngredient;
|
import commons.FormalIngredient;
|
||||||
import javafx.event.ActionEvent;
|
import javafx.event.ActionEvent;
|
||||||
import javafx.fxml.FXML;
|
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.scene.control.ListView;
|
||||||
|
import javafx.stage.Modality;
|
||||||
|
import javafx.stage.Stage;
|
||||||
import javafx.util.Pair;
|
import javafx.util.Pair;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
@ -40,33 +45,37 @@ public class ShoppingListCtrl implements LocaleAware {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void initializeComponents() {
|
public void initializeComponents() {
|
||||||
this.shoppingListView.setCellFactory(l -> new ListCell<>() {
|
this.shoppingListView.setEditable(true);
|
||||||
@Override
|
this.shoppingListView.setCellFactory(l -> new ShoppingListCell());
|
||||||
protected void updateItem(Pair<FormalIngredient, Optional<String>> item, boolean empty) {
|
this.shoppingListView.getItems().setAll(
|
||||||
super.updateItem(item, empty);
|
this.shopping.getItems()
|
||||||
|
);
|
||||||
if (empty) {
|
}
|
||||||
this.setText("");
|
private void refreshList() {
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
String display = item.getKey().toString() +
|
|
||||||
item.getValue().map(recipe -> {
|
|
||||||
return " (" + recipe + ")";
|
|
||||||
}).orElse("");
|
|
||||||
|
|
||||||
this.setText(display);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
this.shoppingListView.getItems().setAll(
|
this.shoppingListView.getItems().setAll(
|
||||||
this.shopping.getItems()
|
this.shopping.getItems()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void handleAddItem(ActionEvent actionEvent) {
|
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) {
|
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