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

Import and search for Ingredients to add to fridge in pop-up window

parent 7db22c03
No related branches found
No related tags found
2 merge requests!42Hs frontend recipes,!41Hs frontend recipes
Pipeline #215591 failed
package no.ntnu.idatt1002.demo.controller;
public class AddIngredientController {
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.event.ActionEvent;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;
import java.util.stream.Collectors;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import no.ntnu.idatt1002.demo.data.recipes.FoodItem;
public class AddIngredientController implements Initializable {
/* ArrayList<String> testIngredients = new ArrayList<>(
Arrays.asList("Tester words", "Another tester word", "tomatoes that are canned", "minced meat", "blueberries")
*//*Arrays.asList(FoodItem.values().toString())*//*
);*/
/*List<String> testIngredients = Arrays.asList("Tester words", "Another tester word", "tomatoes that are canned", "minced meat", "blueberries");*/
String[] testIngredients = {"Tester words", "Another tester word", "tomatoes that are canned", "minced meat", "blueberries"};
private List<String> stringIngredients = Arrays.stream(FoodItem.values()).toList().stream().map(value -> value.label).toList();
private int noLengthOfList = stringIngredients.size();
String[] arrayIngredients = stringIngredients.stream().toArray(String[] ::new);
//private String[] arrayIngredients
private ObservableList<String> ingredients = FXCollections.observableArrayList(stringIngredients);
@FXML
private Button addBtn;
@FXML
private ListView<String> listView;
@FXML
private TextField searchBar;
@FXML
private Button searchBtn;
@FXML
void addToFridge(ActionEvent event) {
System.out.printf("Add ingredient to fridge");
String item = listView.getSelectionModel().getSelectedItem();
System.out.printf("The item was: %s", item);
// Add to existing at hand and write to file. if not null/blank
}
@FXML
void search(ActionEvent event) {
System.out.printf("Searching for ingredient to fridge");
listView.getItems().clear();
listView.getItems().addAll(searchList(searchBar.getText(), arrayIngredients));
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
/* ObservableList ingredients = FXCollections.observableArrayList(testIngredients);*/
/* ObservableList<FoodItem> ingredients = FXCollections.observableArrayList(
FoodItem.values());*/
// Fill list with ingredients
listView.setItems(ingredients);
}
private List<String> searchList(String searchWords, String[] listOfStrings) {
String[] searchWordsArray = searchWords.trim().split(" ");
return Arrays.stream(listOfStrings).filter((in) -> {
return Arrays.stream(searchWordsArray).allMatch((word) ->
in.toLowerCase().contains(word.toLowerCase()));
}).collect(Collectors.toList());
}
}
package no.ntnu.idatt1002.demo.controller;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.DialogPane;
import javafx.stage.Stage;
import no.ntnu.idatt1002.demo.data.Economics.FileHandling;
import java.io.IOException;
import java.net.URL;
import java.util.Optional;
import java.util.ResourceBundle;
public class SuggestRecipesController {
public class SuggestRecipesController implements Initializable {
FileHandling fileHandling;
@FXML
private Button addToFridgeBtn;
@FXML
private Button goBackBtn;
@FXML
private void addToFridge(ActionEvent event) throws IOException {
private void addIngredient(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader();
System.out.printf("Time to pop up the pop-up!");
System.out.println("Time to pop up the pop-up!");
loader.setLocation(getClass().getResource("/view/AddIngredient.fxml"));
DialogPane addIngredientPane = loader.load();
AddIngredientController addIngredientController = loader.getController();
Dialog<ButtonType> dialog = new Dialog<>();
dialog.setDialogPane(addIngredientPane);
dialog.setTitle("Hallays!");
Optional<ButtonType> clickedButton = dialog.showAndWait();
if (clickedButton.get() == ButtonType.OK) {
System.out.printf("Clickecked OK!");
}else if(clickedButton.get() == ButtonType.CANCEL) {
System.out.printf("Close the dialog");
}
}
@FXML
private void goBack(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader();
System.out.printf("Back button pressed");
loader.setLocation(getClass().getResource("/view/MainMenuNew.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) {
fileHandling = new FileHandling();
/*
//Initialize columns
setColumns();
//Initialize registers and tableview
incomeRegister = loadIncomeDataFromFile("Income");
income = FXCollections.observableArrayList(incomeRegister.getItems());
incomeTableView.setItems(income);
expenseRegister = loadExpenseDataFromFile("Expense");
expenses = FXCollections.observableArrayList(expenseRegister.getItems());
expenseTableView.setItems(expenses);*/
}
}
......@@ -15,3 +15,9 @@ amount=10.0
isRecurring=Not recurring
category=FOOD
date=2023-04-15
description=iphone
amount=5000.0
isRecurring=Not recurring
category=OTHER
......@@ -8,3 +8,15 @@ amount=500.0
isRecurring=Not recurring
category=SALARY
date=2023-04-15
description=bday
amount=1000.0
isRecurring=Not recurring
category=GIFT
date=2023-04-15
description=finn
amount=100.0
isRecurring=Not recurring
category=SALARY
.recipe-tile {
-fx-background-color: rgba(151, 175, 151, 0.8);
-fx-background-color: rgb(215, 153, 27);
-fx-background-radius: 25;
}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="no.ntnu.idatt1002.demo.controller.AddIngredientController">
<children>
<BorderPane prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<center>
<VBox prefHeight="200.0" prefWidth="100.0" BorderPane.alignment="CENTER" />
</center>
<top>
<HBox prefHeight="84.0" prefWidth="600.0" BorderPane.alignment="CENTER">
<DialogPane xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="no.ntnu.idatt1002.demo.controller.AddIngredientController">
<content>
<AnchorPane prefHeight="400.0" prefWidth="600.0">
<padding>
<Insets top="20.0" />
</padding>
<children>
<VBox layoutX="14.0" prefHeight="348.0" prefWidth="600.0">
<children>
<Pane prefHeight="84.0" prefWidth="0.0">
<HBox prefHeight="100.0" prefWidth="200.0" spacing="20.0">
<children>
<Label text="Add ingredients to the Fridge">
<TextField fx:id="searchBar" prefHeight="36.0" prefWidth="438.0" promptText="Search for ingredient">
<padding>
<Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
</padding>
<HBox.margin>
<Insets />
</HBox.margin>
</TextField>
<Button fx:id="searchBtn" alignment="CENTER" contentDisplay="CENTER" defaultButton="true" mnemonicParsing="false" onAction="#search" text="Search" textAlignment="CENTER">
<font>
<Font size="24.0" />
<Font size="14.0" />
</font>
</Label>
<HBox.margin>
<Insets />
</HBox.margin>
</Button>
</children>
</Pane>
<Pane prefHeight="84.0" prefWidth="93.0">
<VBox.margin>
<Insets left="25.0" right="25.0" />
</VBox.margin>
</HBox>
<ListView fx:id="listView" prefHeight="311.0" prefWidth="590.0">
<padding>
<Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
</padding>
<VBox.margin>
<Insets left="25.0" right="25.0" />
</VBox.margin>
</ListView>
<Pane prefHeight="81.0" prefWidth="600.0">
<children>
<Button layoutX="7.0" layoutY="28.0" mnemonicParsing="false" text="Close">
<Button fx:id="addBtn" layoutX="275.0" layoutY="7.0" mnemonicParsing="false" onAction="#addToFridge" text="ADD">
<font>
<Font size="14.0" />
</font>
......@@ -32,28 +55,21 @@
</children>
</Pane>
</children>
</HBox>
</top>
<bottom>
<Pane prefHeight="55.0" prefWidth="600.0" BorderPane.alignment="CENTER">
<children>
<HBox>
<children>
<Pane prefHeight="815.0" prefWidth="347.0">
<children>
<TextField layoutX="90.0" layoutY="8.0" prefHeight="31.0" prefWidth="198.0" />
</children>
</Pane>
<Pane prefHeight="815.0" prefWidth="269.0">
<children>
<Button layoutX="8.0" layoutY="20.0" mnemonicParsing="false" text="Add" />
</children>
</Pane>
</children>
</HBox>
</children>
</Pane>
</bottom>
</BorderPane>
</children>
</AnchorPane>
</VBox>
</children></AnchorPane>
</content>
<header>
<Label fx:id="addIngredientPane" alignment="CENTER" contentDisplay="CENTER" text="Add an ingredient to the fridge">
<font>
<Font size="24.0" />
</font>
<padding>
<Insets bottom="25.0" top="20.0" />
</padding>
</Label>
</header>
<buttonTypes>
<ButtonType fx:constant="OK" />
<ButtonType fx:constant="CANCEL" />
</buttonTypes>
</DialogPane>
......@@ -29,7 +29,7 @@
<ListView prefHeight="472.0" prefWidth="421.0" />
<Pane prefHeight="47.0" prefWidth="421.0">
<children>
<Button fx:id="addFridgeButton" layoutX="80.0" layoutY="9.0" mnemonicParsing="false" onAction="#addToFridge" text="Add another available ingredient">
<Button fx:id="addToFridgeBtn" layoutX="80.0" layoutY="9.0" mnemonicParsing="false" onAction="#addIngredient" text="Add another available ingredient">
<font>
<Font size="14.0" />
</font>
......@@ -49,7 +49,7 @@
<left>
<Pane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<children>
<Button alignment="CENTER" contentDisplay="CENTER" layoutX="62.0" layoutY="24.0" mnemonicParsing="false" text="Go Back" textAlignment="CENTER">
<Button fx:id="goBackBtn" alignment="CENTER" contentDisplay="CENTER" layoutX="62.0" layoutY="24.0" mnemonicParsing="false" onAction="#goBack" text="Go Back" textAlignment="CENTER">
<font>
<Font size="14.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