Skip to content
Snippets Groups Projects
Commit b7cd7e46 authored by HSoreide's avatar HSoreide
Browse files

List of all recipes with navigation to selected recipe

parent f04f5058
No related branches found
No related tags found
2 merge requests!42Hs frontend recipes,!41Hs frontend recipes
......@@ -3,6 +3,7 @@ package no.ntnu.idatt1002.demo.controller;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
......@@ -10,13 +11,11 @@ import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import no.ntnu.idatt1002.demo.data.recipes.FileHandler;
import no.ntnu.idatt1002.demo.data.recipes.IngredientsAtHand;
import no.ntnu.idatt1002.demo.data.recipes.Recipe;
import no.ntnu.idatt1002.demo.data.recipes.RecipeRegister;
import no.ntnu.idatt1002.demo.data.recipes.*;
import javax.swing.text.html.ListView;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
......@@ -31,9 +30,12 @@ public class AllRecipesController implements Initializable {
private Button goBackBtn;
@FXML
// private ListView<String> recipeList;
private ListView<String> allList;
private ObservableList<Recipe> recipes;
private ObservableList<String> recipes;
private String selectedRecipeName;
@FXML
......@@ -48,6 +50,26 @@ public class AllRecipesController implements Initializable {
stage.show();
}
private void showRecipe(String recipeName) throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/view/Recipe.fxml"));
Recipe recipeOfInterest = recipeRegister.getRecipe(recipeName);
Parent root = loader.load();
RecipeController recipeController = loader.getController();
recipeController.setData(recipeOfInterest);
//Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
Stage stage = (Stage)allList.getParent().getScene().getWindow();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
......@@ -57,12 +79,33 @@ public class AllRecipesController implements Initializable {
int numberOfRecipes = recipeRegister.getRecipes().size();
ArrayList<Recipe> sortedRecipes = recipeRegister.pickBestFits(numberOfRecipes, ingredientsAtHand);
recipes = FXCollections.observableArrayList(sortedRecipes.stream().map(recipe -> {
return String.format("# %s - %d missing ingredients", recipe.getName(), recipe.getMissingIngredients());
}).toList());
allList.setItems(recipes);
// split("-").strip().
allList.setOnMouseClicked(new EventHandler<MouseEvent>() {
ArrayList<Recipe> recipesList = recipeRegister.pickBestFits(numberOfRecipes, ingredientsAtHand);
@Override
public void handle(MouseEvent mouseEvent) {
selectedRecipeName = allList.getSelectionModel()
.getSelectedItem().split("-|#")[1].strip();
/* recipes = FXCollections.observableArrayList(recipesList.stream().);
System.out.println(selectedRecipeName);
try {
showRecipe(selectedRecipeName);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
recipeList.setItems(recipes);*/
}
}
......@@ -42,6 +42,9 @@ public class RecipeController implements Initializable {
@FXML
private Pane ingredientPane;
@FXML
private Button allRecipesBtn;
private Recipe recipe;
......@@ -88,6 +91,18 @@ public class RecipeController implements Initializable {
stage.show();
}
@FXML
private void toAllRecipes(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/view/AllRecipes.fxml"));
Parent root = loader.load();
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
// ingredients = FXCollections.observableArrayList(recipe.getIngredientList());
......
......@@ -33,15 +33,11 @@
</children></HBox>
</top>
<center>
<ScrollPane prefHeight="514.0" prefWidth="830.0" BorderPane.alignment="CENTER">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="526.0" prefWidth="819.0">
<children>
<ListView fx:id="recipeList" prefHeight="526.0" prefWidth="819.0" styleClass="list-cell" stylesheets="@../style.css" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
</content>
</ScrollPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="526.0" prefWidth="819.0">
<children>
<ListView fx:id="allList" prefHeight="526.0" prefWidth="819.0" styleClass="list-cell" stylesheets="@../style.css" />
</children>
</AnchorPane>
</center>
<left>
<Pane prefHeight="595.0" prefWidth="148.0" BorderPane.alignment="CENTER" />
......
......@@ -16,17 +16,18 @@
<top>
<HBox prefHeight="136.0" prefWidth="1130.0" BorderPane.alignment="CENTER">
<children>
<Pane prefHeight="200.0" prefWidth="200.0">
<Pane prefHeight="136.0" prefWidth="411.0">
<children>
<Button id="button-stye" fx:id="goBackBtn" layoutX="76.0" layoutY="30.0" mnemonicParsing="false" onAction="#goBack" styleClass="button-style" stylesheets="@../style.css" text="Go Back">
<Button id="button-stye" fx:id="goBackBtn" layoutX="81.0" layoutY="30.0" mnemonicParsing="false" onAction="#goBack" styleClass="button-style" stylesheets="@../style.css" text="Recipe Suggestions">
<font>
<Font size="14.0" />
</font></Button>
<Button fx:id="allRecipesBtn" layoutX="81.0" layoutY="68.0" mnemonicParsing="false" onAction="#toAllRecipes" styleClass="button-style" stylesheets="@../style.css" text="All Recipes" />
</children>
</Pane>
<Pane prefHeight="103.0" prefWidth="853.0">
<children>
<Label fx:id="recipeName" alignment="CENTER" contentDisplay="CENTER" layoutX="198.0" layoutY="46.0" text="RecipeName" textAlignment="CENTER">
<Label fx:id="recipeName" alignment="CENTER" contentDisplay="CENTER" layoutX="107.0" layoutY="38.0" text="RecipeName" textAlignment="CENTER">
<font>
<Font size="36.0" />
</font>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment