diff --git a/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java b/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java index b80404a..b260357 100644 --- a/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java +++ b/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java @@ -4,7 +4,6 @@ package client.scenes; import java.io.IOException; import java.util.Collections; import java.util.List; -import java.util.Locale; import client.exception.UpdateException; import client.scenes.recipe.IngredientListCtrl; @@ -23,7 +22,6 @@ import commons.ws.Topics; import commons.ws.messages.Message; import jakarta.inject.Inject; import javafx.application.Platform; -import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.Alert; import javafx.scene.control.Button; @@ -48,18 +46,6 @@ public class FoodpalApplicationCtrl implements LocaleAware { public VBox detailsScreen; 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 @FXML public Label recipesLabel; @@ -113,6 +99,23 @@ public class FoodpalApplicationCtrl implements LocaleAware { initializeWebSocket(); } + private void initRecipeList() { + // Show recipe name in the list + 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); + updateFavouriteButton(newRecipe); + } + ); + } @Inject void setSearchBarCtrl(SearchBarCtrl searchBarCtrl) { @@ -165,11 +168,7 @@ public class FoodpalApplicationCtrl implements LocaleAware { }); }); } - - @Override - public void initializeComponents() { - config = configService.getConfig(); - // TODO Reduce code duplication?? + private void initStepsIngredientsList() { // Initialize callback for ingredient list updates this.ingredientListCtrl.setUpdateCallback(newList -> { Recipe selectedRecipe = recipeList.getSelectionModel().getSelectedItem(); @@ -199,21 +198,12 @@ public class FoodpalApplicationCtrl implements LocaleAware { throw new UpdateException("Unable to update recipe to server for " + selectedRecipe); } }); - // Show recipe name in the list - 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); - updateFavouriteButton(newRecipe); - } - ); + } + @Override + public void initializeComponents() { + config = configService.getConfig(); + initStepsIngredientsList(); + initRecipeList(); // Double-click to go to detail screen recipeList.setOnMouseClicked(event -> { @@ -222,7 +212,6 @@ public class FoodpalApplicationCtrl implements LocaleAware { openSelectedRecipe(); } }); - this.initializeSearchBar(); refresh(); updateFavouriteButton(recipeList.getSelectionModel().getSelectedItem()); @@ -247,13 +236,6 @@ public class FoodpalApplicationCtrl implements LocaleAware { printRecipeButton.setText(getLocaleString("menu.button.print")); recipesLabel.setText(getLocaleString("menu.label.recipes")); - - // TODO propagate ResourceBundle lang changes to nested controller - // ingredientsLabel.setText(getLocaleString("menu.label.ingredients")); - // preparationLabel.setText(getLocaleString("menu.label.preparation")); - - // addIngredientButton.setText(getLocaleString("menu.button.add.ingredient")); - // addPreparationStepButton.setText(getLocaleString("menu.button.add.step")); } @Override @@ -378,15 +360,6 @@ public class FoodpalApplicationCtrl implements LocaleAware { editableTitleArea.getChildren().add(edit); edit.requestFocus(); } - - // Language buttons - @FXML - private void switchLocale(ActionEvent event) { - Button button = (Button)event.getSource(); - String lang = (String)button.getUserData(); - localeManager.setLocale(Locale.of(lang)); - } - @FXML private void makePrintable() { System.out.println("Recipe printed"); diff --git a/client/src/main/java/client/scenes/LangSelectMenuCtrl.java b/client/src/main/java/client/scenes/LangSelectMenuCtrl.java new file mode 100644 index 0000000..1df3e54 --- /dev/null +++ b/client/src/main/java/client/scenes/LangSelectMenuCtrl.java @@ -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 + * getLocaleString(String) function is available. + */ +public class LangSelectMenuCtrl implements LocaleAware { + public ComboBox 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() { + @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; + } +} diff --git a/client/src/main/java/client/utils/PrintExportService.java b/client/src/main/java/client/utils/PrintExportService.java new file mode 100644 index 0000000..624d4d1 --- /dev/null +++ b/client/src/main/java/client/utils/PrintExportService.java @@ -0,0 +1,67 @@ +package client.utils; + +import commons.Recipe; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +public class PrintExportService { + + /** + * Builds the String with all the recipe data in a human-readable format and returns said string. + * @param recipe - Recipe Object that needs to be converted + * @return - String Result that is the converted recipe object + */ + public static String buildRecipeText(Recipe recipe){ + String result = "Title: " + recipe.getName() + "\nRecipe ID: " + recipe.getId() + "\n" + + "Ingredients: "; //Starts the string with name and recipe ID + for(int i =0; i + @@ -23,58 +24,12 @@ - - - - - - - - - - - - - - - - - -