docs: add notes to makeMenuItems

This commit is contained in:
Zhongheng Liu 2026-01-17 13:37:24 +01:00
commit 5942250f11
Signed by: steven
GPG key ID: F69B980899C1C09D

View file

@ -82,6 +82,18 @@ public class IngredientListCell extends OrderedEditableListCell<RecipeIngredient
this.setText(null); this.setText(null);
this.setGraphic(container); this.setGraphic(container);
} }
/**
* Creates a menu of ingredients from the provided iterable list of items.
* Modifies the provided <code>menu</code> reference.
* @param menu The MenuButton to add items to.
* @param items The items to add to the dropdown menu.
* @param labelMapper A function that outputs a string from the object to display on the label.
* @param onSelect Callback that triggers upon selection.
* @param container Parent container of the menu.
* Required since the UI logic needs to append a field to the end of the HBox upon selection
* @param <T> The type this menu concerns itself with.
*/
private <T> void makeMenuItems( private <T> void makeMenuItems(
MenuButton menu, MenuButton menu,
Iterable<T> items, Iterable<T> items,
@ -90,15 +102,21 @@ public class IngredientListCell extends OrderedEditableListCell<RecipeIngredient
HBox container) { HBox container) {
MenuItem newIngredientItem = new MenuItem(); MenuItem newIngredientItem = new MenuItem();
newIngredientItem.setText("Something new..."); newIngredientItem.setText("Something new...");
// on new ingredient click
newIngredientItem.setOnAction(_ -> { newIngredientItem.setOnAction(_ -> {
menu.setText("New Ingredient"); menu.setText("New Ingredient"); // otherwise the text label on menu refuses to update
selectedIngredient.setValue(null); selectedIngredient.setValue(null); // indicate null to signal a new ingredient creation
logger.info("Making new ingredient"); logger.info("Making new ingredient");
// TODO printError() integration // TODO printError() integration
TextField newIngredientNameInput = makeNewIngredientNameField(selectedIngredient::set); TextField newIngredientNameInput = makeNewIngredientNameField(selectedIngredient::set);
container.getChildren().add(newIngredientNameInput); container.getChildren().add(newIngredientNameInput);
}); });
// Puts the add ingredient button and a separator on top of the dropdown menu.
menu.getItems().addAll(newIngredientItem, new SeparatorMenuItem()); menu.getItems().addAll(newIngredientItem, new SeparatorMenuItem());
// Iterates over the list of items and applies the label and onSelect handlers.
for (T item : items) { for (T item : items) {
MenuItem mi = new MenuItem(); MenuItem mi = new MenuItem();
mi.setText(labelMapper.apply(item)); mi.setText(labelMapper.apply(item));
@ -127,8 +145,6 @@ public class IngredientListCell extends OrderedEditableListCell<RecipeIngredient
Unit unit = unitChoice.getValue(); Unit unit = unitChoice.getValue();
Ingredient i = selectedIngredient.getValue(); Ingredient i = selectedIngredient.getValue();
if (i == null) { if (i == null) {
return; // The user is forced to kindly try again until something valid comes up. return; // The user is forced to kindly try again until something valid comes up.
} }
if (unit == null || !unit.isFormal()) { if (unit == null || !unit.isFormal()) {