feat: controller for language list control

This commit is contained in:
Zhongheng Liu 2025-12-16 13:09:03 +01:00
commit 7e69976e5e
Signed by: steven
GPG key ID: F69B980899C1C09D

View file

@ -2,6 +2,7 @@ package client.scenes;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -9,11 +10,11 @@ import java.util.Locale;
import client.exception.UpdateException; import client.exception.UpdateException;
import client.scenes.recipe.IngredientListCtrl; import client.scenes.recipe.IngredientListCtrl;
import client.scenes.recipe.RecipeStepListCtrl; import client.scenes.recipe.RecipeStepListCtrl;
import client.utils.DefaultRecipeFactory; import client.utils.DefaultRecipeFactory;
import client.utils.LocaleAware; import client.utils.LocaleAware;
import client.utils.LocaleManager; import client.utils.LocaleManager;
import client.utils.ServerUtils; import client.utils.ServerUtils;
import commons.Recipe; import commons.Recipe;
import jakarta.inject.Inject; import jakarta.inject.Inject;
@ -21,14 +22,18 @@ import javafx.event.ActionEvent;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.Alert; import javafx.scene.control.Alert;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.control.ListCell; import javafx.scene.control.ListCell;
import javafx.scene.control.ListView; import javafx.scene.control.ListView;
import javafx.scene.control.TextField; import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode; import javafx.scene.input.KeyCode;
import javafx.scene.layout.HBox; import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
import javafx.scene.text.Font; import javafx.scene.text.Font;
import javafx.util.StringConverter;
public class FoodpalApplicationCtrl implements LocaleAware { public class FoodpalApplicationCtrl implements LocaleAware {
private final ServerUtils server; private final ServerUtils server;
@ -39,21 +44,10 @@ public class FoodpalApplicationCtrl implements LocaleAware {
public VBox detailsScreen; public VBox detailsScreen;
public HBox editableTitleArea; public HBox editableTitleArea;
// all of these aren't used with only my part of the code
// everything in the top bar ===
@FXML
private Button flagEnButton; //already here for advanced stuff
@FXML
private Button flagNlButton; //already here for advanced stuff
@FXML
private Button flagPlButton; //already here for advanced stuff
// everything in the left lane // everything in the left lane
@FXML @FXML
public Label recipesLabel; public Label recipesLabel;
public ComboBox<String> langSelectMenu;
@FXML @FXML
private ListView<Recipe> recipeList; private ListView<Recipe> recipeList;
@ -90,9 +84,30 @@ public class FoodpalApplicationCtrl implements LocaleAware {
this.ingredientListCtrl = ingredientListCtrl; this.ingredientListCtrl = ingredientListCtrl;
this.stepListCtrl = stepListCtrl; this.stepListCtrl = stepListCtrl;
} }
@Override private void initRecipeList() {
public void initializeComponents() { // Show recipe name in the list
// TODO Reduce code duplication?? recipeList.setCellFactory(list -> new ListCell<>() {
@Override
protected void updateItem(Recipe item, boolean empty) {
super.updateItem(item, empty);
setText(empty || item == null ? "" : item.getName());
}
});
// When your selection changes, update details in the panel
recipeList.getSelectionModel().selectedItemProperty().addListener(
(obs, oldRecipe, newRecipe) -> showRecipeDetails(newRecipe)
);
// Double-click to go to detail screen
recipeList.setOnMouseClicked(event -> {
final int DOUBLE_CLICK = 2; //to not get magic number:P
if (event.getClickCount() == DOUBLE_CLICK) {
openSelectedRecipe();
}
});
}
private void initStepsIngredientsList() {
// Initialize callback for ingredient list updates // Initialize callback for ingredient list updates
this.ingredientListCtrl.setUpdateCallback(newList -> { this.ingredientListCtrl.setUpdateCallback(newList -> {
Recipe selectedRecipe = recipeList.getSelectionModel().getSelectedItem(); Recipe selectedRecipe = recipeList.getSelectionModel().getSelectedItem();
@ -122,28 +137,50 @@ public class FoodpalApplicationCtrl implements LocaleAware {
throw new UpdateException("Unable to update recipe to server for " + selectedRecipe); throw new UpdateException("Unable to update recipe to server for " + selectedRecipe);
} }
}); });
// Show recipe name in the list }
recipeList.setCellFactory(list -> new ListCell<>() { @Override
public void initializeComponents() {
// TODO Reduce code duplication??
langSelectMenu.getItems().setAll("en", "pl", "nl");
langSelectMenu.setConverter(new StringConverter<String>() {
@Override @Override
protected void updateItem(Recipe item, boolean empty) { public String toString(String s) {
if (s == null) {
return "";
}
return getLocaleString("lang." + s + ".display");
}
@Override
public String fromString(String s) {
return s;
}
});
langSelectMenu.setCellFactory(list -> new ListCell<>() {
@Override
protected void updateItem(String item, boolean empty) {
final int IMAGE_HEIGHT = 32;
final int HBOX_SPACING = 10;
super.updateItem(item, empty); super.updateItem(item, empty);
setText(empty || item == null ? "" : item.getName()); if (item == null || empty) {
setText(null);
setGraphic(null);
} else {
InputStream imageStream = getClass().getResourceAsStream("/flag_" + item + ".png");
if (imageStream == null) {
setGraphic(new HBox(new Label(getLocaleString("lang." + item + ".display"))));
return;
}
Image img = new Image(imageStream);
ImageView imageView = new ImageView(img);
imageView.setFitHeight(IMAGE_HEIGHT);
imageView.setFitWidth(IMAGE_HEIGHT);
setGraphic(new HBox(HBOX_SPACING, imageView, new Label(getLocaleString("lang." + item + ".display"))));
}
} }
}); });
initStepsIngredientsList();
// When your selection changes, update details in the panel initRecipeList();
recipeList.getSelectionModel().selectedItemProperty().addListener(
(obs, oldRecipe, newRecipe) -> showRecipeDetails(newRecipe)
);
// Double-click to go to detail screen
recipeList.setOnMouseClicked(event -> {
final int DOUBLE_CLICK = 2; //to not get magic number:P
if (event.getClickCount() == DOUBLE_CLICK) {
openSelectedRecipe();
}
});
refresh(); refresh();
} }
private void showName(String name) { private void showName(String name) {
@ -298,8 +335,8 @@ public class FoodpalApplicationCtrl implements LocaleAware {
// Language buttons // Language buttons
@FXML @FXML
private void switchLocale(ActionEvent event) { private void switchLocale(ActionEvent event) {
Button button = (Button)event.getSource(); System.out.println("switching");
String lang = (String)button.getUserData(); String lang = langSelectMenu.getSelectionModel().getSelectedItem();
localeManager.setLocale(Locale.of(lang)); localeManager.setLocale(Locale.of(lang));
} }