diff --git a/client/src/main/java/client/scenes/recipe/IngredientListCell.java b/client/src/main/java/client/scenes/recipe/IngredientListCell.java index ac317d8..f420bff 100644 --- a/client/src/main/java/client/scenes/recipe/IngredientListCell.java +++ b/client/src/main/java/client/scenes/recipe/IngredientListCell.java @@ -1,78 +1,18 @@ package client.scenes.recipe; -import javafx.scene.control.ListCell; import javafx.scene.control.TextField; -import javafx.scene.input.KeyCode; /** - * A custom ListCell for displaying and editing ingredients in an + * A custom {@link OrderedEditableListCell} for displaying and editing ingredients in an * IngredientList. Allows inline editing of ingredient names. * * @see IngredientListCtrl */ -public class IngredientListCell extends ListCell { +public class IngredientListCell extends OrderedEditableListCell { // TODO: change all this to use actual Ingredient objects // and all that :) - @Override - protected void updateItem(String item, boolean empty) { - super.updateItem(item, empty); - - if (empty || item == null) { - this.setText(null); - } else { - this.setText(item); - } - } - - @Override - public void startEdit() { - super.startEdit(); - - TextField textField = new TextField(this.getItem()); - - // Commit edit on Enter key press - textField.setOnAction(event -> { - this.commitEdit(textField.getText()); - }); - - // Cancel edit on Escape key press - textField.setOnKeyReleased(event -> { - if (event.getCode() == KeyCode.ESCAPE) { - this.cancelEdit(); - } - }); - - this.setText(null); - this.setGraphic(textField); - } - - /** - * Check if the input is valid (non-empty). - * - * @param input The input string to validate. - * @return true if valid, false otherwise. - */ - private boolean isInputValid(String input) { - return input != null && !input.trim().isEmpty(); - } - - @Override - public void cancelEdit() { - super.cancelEdit(); - - this.setText(this.getItem()); - this.setGraphic(null); - } - - @Override - public void commitEdit(String newValue) { - this.setGraphic(null); - - if (!isInputValid(newValue)) { - newValue = this.getItem(); // Revert to old value if input is invalid - } - - super.commitEdit(newValue); + protected String fromInput(TextField inputField) { + return inputField.getText(); } } diff --git a/client/src/main/java/client/scenes/recipe/IngredientListCtrl.java b/client/src/main/java/client/scenes/recipe/IngredientListCtrl.java index 6db8fb5..aa33ed2 100644 --- a/client/src/main/java/client/scenes/recipe/IngredientListCtrl.java +++ b/client/src/main/java/client/scenes/recipe/IngredientListCtrl.java @@ -137,7 +137,7 @@ public class IngredientListCtrl implements Initializable { this.ingredientListView.setEditable(true); this.ingredientListView.setCellFactory( list -> { - return new RecipeStepListCell(); + return new IngredientListCell(); }); this.ingredientListView.setOnEditCommit(this::handleIngredientEdit); diff --git a/client/src/main/java/client/scenes/recipe/OrderedEditableListCell.java b/client/src/main/java/client/scenes/recipe/OrderedEditableListCell.java new file mode 100644 index 0000000..8716c3a --- /dev/null +++ b/client/src/main/java/client/scenes/recipe/OrderedEditableListCell.java @@ -0,0 +1,78 @@ +package client.scenes.recipe; + +import javafx.scene.control.ListCell; +import javafx.scene.control.TextField; +import javafx.scene.input.KeyCode; + +public abstract class OrderedEditableListCell extends ListCell { + /** + * Get the display text for the given item, prefixed with its index. + * Looks like "1. description". + * + * @param item The description. + * @return The display text. + */ + private String getDisplayText(String item) { + return (this.getIndex() + 1) + ". " + item; + } + @Override + protected void updateItem(T item, boolean empty) { + super.updateItem(item, empty); + + if (empty || item == null) { + this.setText(null); + } else { + this.setText(this.getDisplayText(item.toString())); + } + } + public void startEdit() { + super.startEdit(); + + TextField textField = new TextField(this.getItem().toString()); + + // Commit edit on Enter key press + textField.setOnAction(event -> { + this.commitEdit(fromInput(textField)); + }); + + // Cancel edit on Escape key press + textField.setOnKeyReleased(event -> { + if (event.getCode() == KeyCode.ESCAPE) { + this.cancelEdit(); + } + }); + + this.setText(null); + this.setGraphic(textField); + } + + protected abstract T fromInput(TextField inputField); + /** + * Check if the input is valid (non-empty). + * + * @param input The input string to validate. + * @return true if valid, false otherwise. + */ + private boolean isInputValid(String input) { + return input != null && !input.trim().isEmpty(); + } + + @Override + public void cancelEdit() { + super.cancelEdit(); + + this.setText(this.getItem().toString()); + this.setGraphic(null); + } + + @Override + public void commitEdit(T newValue) { + this.setGraphic(null); + + if (!isInputValid(newValue.toString())) { + newValue = this.getItem(); // Revert to old value if input is invalid + } + + super.commitEdit(newValue); + } +} diff --git a/client/src/main/java/client/scenes/recipe/RecipeStepListCell.java b/client/src/main/java/client/scenes/recipe/RecipeStepListCell.java index 6d6e992..bf57c13 100644 --- a/client/src/main/java/client/scenes/recipe/RecipeStepListCell.java +++ b/client/src/main/java/client/scenes/recipe/RecipeStepListCell.java @@ -1,96 +1,15 @@ package client.scenes.recipe; -import javafx.scene.control.ListCell; import javafx.scene.control.TextField; -import javafx.scene.input.KeyCode; - /** * A custom ListCell for displaying and editing ingredients in an * RecipeStepList. Allows inline editing of ingredient names. * * @see RecipeStepListCtrl */ -public class RecipeStepListCell extends ListCell { - /** - * Get the display text for the given item, prefixed with its index. - * Looks like "1. Step description". - * - * @param item The step description. - * @return The display text. - */ - private String getDisplayText(String item) { - return this.getIndex() + ". " + item; - } - - /** - * Get the display text for the current item. - * Looks like "1. Step description". - * - * @return The display text. - */ - private String getDisplayText() { - return this.getDisplayText(this.getItem()); - } - +public class RecipeStepListCell extends OrderedEditableListCell { @Override - protected void updateItem(String item, boolean empty) { - super.updateItem(item, empty); - - if (empty || item == null) { - this.setText(null); - } else { - this.setText(this.getDisplayText(item)); - } - } - - @Override - public void startEdit() { - super.startEdit(); - - TextField textField = new TextField(this.getItem()); - - // Commit edit on Enter key press - textField.setOnAction(event -> { - this.commitEdit(textField.getText()); - }); - - // Cancel edit on Escape key press - textField.setOnKeyReleased(event -> { - if (event.getCode() == KeyCode.ESCAPE) { - this.cancelEdit(); - } - }); - - this.setText(null); - this.setGraphic(textField); - } - - /** - * Check if the input is valid (non-empty). - * - * @param input The input string to validate. - * @return true if valid, false otherwise. - */ - private boolean isInputValid(String input) { - return input != null && !input.trim().isEmpty(); - } - - @Override - public void cancelEdit() { - super.cancelEdit(); - - this.setText(this.getDisplayText()); - this.setGraphic(null); - } - - @Override - public void commitEdit(String newValue) { - this.setGraphic(null); - - if (!isInputValid(newValue)) { - newValue = this.getItem(); // Revert to old value if input is invalid - } - - super.commitEdit(newValue); + protected String fromInput(TextField inputField) { + return inputField.getText(); } }