feat: controller for language list control
This commit is contained in:
parent
6b0d728ad3
commit
7e69976e5e
1 changed files with 73 additions and 36 deletions
|
|
@ -2,6 +2,7 @@ package client.scenes;
|
|||
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
|
@ -9,11 +10,11 @@ import java.util.Locale;
|
|||
import client.exception.UpdateException;
|
||||
import client.scenes.recipe.IngredientListCtrl;
|
||||
import client.scenes.recipe.RecipeStepListCtrl;
|
||||
|
||||
import client.utils.DefaultRecipeFactory;
|
||||
import client.utils.LocaleAware;
|
||||
import client.utils.LocaleManager;
|
||||
import client.utils.ServerUtils;
|
||||
|
||||
import commons.Recipe;
|
||||
|
||||
import jakarta.inject.Inject;
|
||||
|
|
@ -21,14 +22,18 @@ import javafx.event.ActionEvent;
|
|||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.Alert;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.ComboBox;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.ListCell;
|
||||
import javafx.scene.control.ListView;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.image.Image;
|
||||
import javafx.scene.image.ImageView;
|
||||
import javafx.scene.input.KeyCode;
|
||||
import javafx.scene.layout.HBox;
|
||||
import javafx.scene.layout.VBox;
|
||||
import javafx.scene.text.Font;
|
||||
import javafx.util.StringConverter;
|
||||
|
||||
public class FoodpalApplicationCtrl implements LocaleAware {
|
||||
private final ServerUtils server;
|
||||
|
|
@ -39,21 +44,10 @@ 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;
|
||||
public ComboBox<String> langSelectMenu;
|
||||
|
||||
@FXML
|
||||
private ListView<Recipe> recipeList;
|
||||
|
|
@ -90,9 +84,30 @@ public class FoodpalApplicationCtrl implements LocaleAware {
|
|||
this.ingredientListCtrl = ingredientListCtrl;
|
||||
this.stepListCtrl = stepListCtrl;
|
||||
}
|
||||
@Override
|
||||
public void initializeComponents() {
|
||||
// TODO Reduce code duplication??
|
||||
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)
|
||||
);
|
||||
|
||||
// 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
|
||||
this.ingredientListCtrl.setUpdateCallback(newList -> {
|
||||
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);
|
||||
}
|
||||
});
|
||||
// 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
|
||||
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);
|
||||
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"))));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// 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();
|
||||
}
|
||||
});
|
||||
|
||||
initStepsIngredientsList();
|
||||
initRecipeList();
|
||||
refresh();
|
||||
}
|
||||
private void showName(String name) {
|
||||
|
|
@ -298,8 +335,8 @@ public class FoodpalApplicationCtrl implements LocaleAware {
|
|||
// Language buttons
|
||||
@FXML
|
||||
private void switchLocale(ActionEvent event) {
|
||||
Button button = (Button)event.getSource();
|
||||
String lang = (String)button.getUserData();
|
||||
System.out.println("switching");
|
||||
String lang = langSelectMenu.getSelectionModel().getSelectedItem();
|
||||
localeManager.setLocale(Locale.of(lang));
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue