fix(logic): immigrates LangSelectMenu to own controller

fix(println): also disappears the debug println statement from prod code
TODO: in the distant future, consider using a nice logging framework
(log4j?? or whatever spring boot is using..)
This commit is contained in:
Zhongheng Liu 2025-12-19 23:29:27 +02:00
commit ca7c5cbfb9
Signed by: steven
GPG key ID: F69B980899C1C09D
4 changed files with 117 additions and 54 deletions

View file

@ -2,10 +2,8 @@ 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 client.exception.UpdateException; import client.exception.UpdateException;
import client.scenes.recipe.IngredientListCtrl; import client.scenes.recipe.IngredientListCtrl;
@ -18,7 +16,6 @@ import client.utils.ServerUtils;
import commons.Recipe; import commons.Recipe;
import jakarta.inject.Inject; import jakarta.inject.Inject;
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;
@ -27,13 +24,10 @@ 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;
@ -141,44 +135,7 @@ public class FoodpalApplicationCtrl implements LocaleAware {
@Override @Override
public void initializeComponents() { public void initializeComponents() {
// TODO Reduce code duplication?? // TODO Reduce code duplication??
langSelectMenu.getItems().setAll("en", "pl", "nl");
langSelectMenu.setConverter(new StringConverter<String>() {
@Override
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);
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(); initStepsIngredientsList();
initRecipeList(); initRecipeList();
refresh(); refresh();
@ -331,15 +288,6 @@ public class FoodpalApplicationCtrl implements LocaleAware {
edit.requestFocus(); edit.requestFocus();
} }
// Language buttons
@FXML
private void switchLocale(ActionEvent event) {
System.out.println("switching");
String lang = langSelectMenu.getSelectionModel().getSelectedItem();
localeManager.setLocale(Locale.of(lang));
}
@FXML @FXML
private void makePrintable() { private void makePrintable() {
System.out.println("Recipe printed"); System.out.println("Recipe printed");

View file

@ -0,0 +1,100 @@
package client.scenes;
import client.utils.LocaleAware;
import client.utils.LocaleManager;
import com.google.inject.Inject;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.util.StringConverter;
import java.io.InputStream;
import java.util.Locale;
/**
* The language selection menu controller.
* This needs to implement {@link LocaleAware LocaleAware} so that the
* <code>getLocaleString(String)</code> function is available.
*/
public class LangSelectMenuCtrl implements LocaleAware {
public ComboBox<String> langSelectMenu;
private final LocaleManager manager;
@Inject
public LangSelectMenuCtrl(LocaleManager manager) {
this.manager = manager;
}
/**
* Switches the locale of the application depending on which button the user selects in the list.
* @param event The action event triggered by the user.
*/
@FXML
private void switchLocale(ActionEvent event) {
String lang = langSelectMenu.getSelectionModel().getSelectedItem();
manager.setLocale(Locale.of(lang));
}
@Override
public void initializeComponents() {
langSelectMenu.getItems().setAll("en", "pl", "nl");
langSelectMenu.setConverter(new StringConverter<String>() {
@Override
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);
if (item == null || empty) {
setText(null);
setGraphic(null);
return;
}
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"))
)
);
}
});
}
@Override
public void updateText() {
// doesn't do anything; the text doesn't need to be updated.
}
@Override
public LocaleManager getLocaleManager() {
return manager;
}
}

View file

@ -38,8 +38,7 @@
<padding> <padding>
<Insets bottom="10" left="10" right="10" top="10" /> <Insets bottom="10" left="10" right="10" top="10" />
</padding> </padding>
<ComboBox fx:id="langSelectMenu" onAction="#switchLocale"> <fx:include source="LangSelect.fxml" />
</ComboBox>
<Button fx:id="refreshButton" onAction="#refresh" prefHeight="25.0" prefWidth="34.0" text="⟳" GridPane.columnIndex="3" GridPane.rowIndex="2" /> <Button fx:id="refreshButton" onAction="#refresh" prefHeight="25.0" prefWidth="34.0" text="⟳" GridPane.columnIndex="3" GridPane.rowIndex="2" />
<Label fx:id="recipesLabel" text="Recipes"> <Label fx:id="recipesLabel" text="Recipes">
<font> <font>

View file

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<ComboBox
fx:id="langSelectMenu"
xmlns="http://javafx.com/javafx/25"
xmlns:fx="http://javafx.com/fxml/1"
onAction="#switchLocale"
fx:controller="client.scenes.LangSelectMenuCtrl"
>
</ComboBox>