Adding ingredients is possible, but no automatically refresh

This commit is contained in:
Mei Chang van der Werff 2025-12-02 16:25:19 +01:00
commit f93cf8f0aa
11 changed files with 181 additions and 41 deletions

View file

@ -20,12 +20,9 @@ import static com.google.inject.Guice.createInjector;
import java.io.IOException; import java.io.IOException;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import client.scenes.FoodpalApplicationCtrl; import client.scenes.*;
import com.google.inject.Injector; import com.google.inject.Injector;
import client.scenes.AddQuoteCtrl;
import client.scenes.MainCtrl;
import client.scenes.QuoteOverviewCtrl;
import client.utils.ServerUtilsExample; import client.utils.ServerUtilsExample;
import javafx.application.Application; import javafx.application.Application;
import javafx.stage.Stage; import javafx.stage.Stage;
@ -50,10 +47,12 @@ public class Main extends Application {
} }
var overview = FXML.load(QuoteOverviewCtrl.class, "client", "scenes", "QuoteOverview.fxml"); var overview = FXML.load(QuoteOverviewCtrl.class, "client", "scenes", "QuoteOverview.fxml");
var add = FXML.load(AddQuoteCtrl.class, "client", "scenes", "AddQuote.fxml"); var addName = FXML.load(AddNameCtrl.class, "client", "scenes", "AddName.fxml");
var foodpal = FXML.load(FoodpalApplicationCtrl.class, "client", "scenes", "FoodpalApplication.fxml"); var foodpal = FXML.load(FoodpalApplicationCtrl.class, "client", "scenes", "FoodpalApplication.fxml");
var addIngredient = FXML.load(AddIngredientCtrl.class, "client", "scenes", "AddIngredient.fxml");
var mainCtrl = INJECTOR.getInstance(MainCtrl.class); var mainCtrl = INJECTOR.getInstance(MainCtrl.class);
mainCtrl.initialize(primaryStage, overview, add, foodpal); mainCtrl.initialize(primaryStage, overview, addName, foodpal, addIngredient);
} }
} }

View file

@ -15,11 +15,12 @@
*/ */
package client; package client;
import client.scenes.FoodpalApplicationCtrl;
import com.google.inject.Binder; import com.google.inject.Binder;
import com.google.inject.Module; import com.google.inject.Module;
import com.google.inject.Scopes; import com.google.inject.Scopes;
import client.scenes.AddQuoteCtrl; import client.scenes.AddNameCtrl;
import client.scenes.MainCtrl; import client.scenes.MainCtrl;
import client.scenes.QuoteOverviewCtrl; import client.scenes.QuoteOverviewCtrl;
@ -28,7 +29,8 @@ public class MyModule implements Module {
@Override @Override
public void configure(Binder binder) { public void configure(Binder binder) {
binder.bind(MainCtrl.class).in(Scopes.SINGLETON); binder.bind(MainCtrl.class).in(Scopes.SINGLETON);
binder.bind(AddQuoteCtrl.class).in(Scopes.SINGLETON); binder.bind(AddNameCtrl.class).in(Scopes.SINGLETON);
binder.bind(FoodpalApplicationCtrl.class).in(Scopes.SINGLETON);
binder.bind(QuoteOverviewCtrl.class).in(Scopes.SINGLETON); binder.bind(QuoteOverviewCtrl.class).in(Scopes.SINGLETON);
} }
} }

View file

@ -0,0 +1,85 @@
/*
* Copyright 2021 Delft University of Technology
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package client.scenes;
import client.utils.ServerUtils;
import com.google.inject.Inject;
import commons.Recipe;
import jakarta.ws.rs.WebApplicationException;
import javafx.scene.control.Alert;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.stage.Modality;
import java.io.IOException;
import java.util.List;
public class AddIngredientCtrl {
private final ServerUtils server;
private final MainCtrl mainCtrl;
private final FoodpalApplicationCtrl foodpalCtrl;
public TextField recipeName;
@Inject
public AddIngredientCtrl(ServerUtils server, MainCtrl mainCtrl, FoodpalApplicationCtrl foodpalCtrl) {
this.mainCtrl = mainCtrl;
this.server = server;
this.foodpalCtrl = foodpalCtrl;
}
public void cancel() throws IOException, InterruptedException {
clearFields();
mainCtrl.showFoodpal();
}
public void ok() throws IOException, InterruptedException {
try {
Recipe selected = foodpalCtrl.getSelectedRecipe();
server.addRecipeIngredient(selected, recipeName.getText());
} catch (WebApplicationException e) {
var alert = new Alert(Alert.AlertType.ERROR);
alert.initModality(Modality.APPLICATION_MODAL);
alert.setContentText(e.getMessage());
alert.showAndWait();
return;
}
clearFields();
mainCtrl.showFoodpal();
}
private void clearFields() {
recipeName.clear();
}
public void keyPressed(KeyEvent e) throws IOException, InterruptedException {
switch (e.getCode()) {
case ENTER:
ok();
break;
case ESCAPE:
cancel();
break;
default:
break;
}
}
}

View file

@ -19,7 +19,6 @@ import client.utils.ServerUtils;
import com.google.inject.Inject; import com.google.inject.Inject;
import jakarta.ws.rs.WebApplicationException; import jakarta.ws.rs.WebApplicationException;
import javafx.fxml.FXML;
import javafx.scene.control.Alert; import javafx.scene.control.Alert;
import javafx.scene.control.TextField; import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent; import javafx.scene.input.KeyEvent;
@ -27,29 +26,22 @@ import javafx.stage.Modality;
import java.io.IOException; import java.io.IOException;
public class AddQuoteCtrl { public class AddNameCtrl {
private final ServerUtils server; private final ServerUtils server;
private final MainCtrl mainCtrl; private final MainCtrl mainCtrl;
public TextField recipeName; public TextField recipeName;
@FXML
private TextField firstName;
@FXML
private TextField lastName;
@FXML
private TextField quote;
@Inject @Inject
public AddQuoteCtrl(ServerUtils server, MainCtrl mainCtrl) { public AddNameCtrl(ServerUtils server, MainCtrl mainCtrl) {
this.mainCtrl = mainCtrl; this.mainCtrl = mainCtrl;
this.server = server; this.server = server;
} }
public void cancel() { public void cancel() throws IOException, InterruptedException {
clearFields(); clearFields();
mainCtrl.showOverview(); mainCtrl.showFoodpal();
} }
public void ok() throws IOException, InterruptedException { public void ok() throws IOException, InterruptedException {

View file

@ -107,6 +107,10 @@ public class FoodpalApplicationCtrl {
// till the all the code from everyone is implemented for now to not have errors // till the all the code from everyone is implemented for now to not have errors
private void showRecipeDetails(Recipe newRecipe) { private void showRecipeDetails(Recipe newRecipe) {
recipeNameLabel.setText(newRecipe.getName());
ingredientsListView.getItems().setAll(newRecipe.getIngredients());
preparationListView.getItems().setAll(newRecipe.getPreparationSteps());
} }
// Button handlers // Button handlers
@ -124,23 +128,25 @@ public class FoodpalApplicationCtrl {
} }
/**
* Adds a recipe, by going to a different scene, there you insert the title and bam recipe created
*/
@FXML @FXML
private void addRecipe(ActionEvent e) { private void addRecipe() {
// Navigate to "create recipe" screen (should be like a pop-up or new screen or just another place in the app) // Navigate to "create recipe" screen (should be like a pop-up or new screen or just another place in the app)
mainCtrl.showAdd(); mainCtrl.showAddName();
} }
@FXML @FXML
private void removeSelectedRecipe() { private void removeSelectedRecipe() throws IOException, InterruptedException {
Recipe selected = recipeList.getSelectionModel().getSelectedItem(); Recipe selected = recipeList.getSelectionModel().getSelectedItem();
if (selected == null) { if (selected == null) {
return; return;
} }
// TODO: someone else todo server.deleteRecipe(selected.getId());
recipeList.getItems().remove(selected); recipeList.getItems().remove(selected);
showRecipeDetails(null);
} }
@FXML @FXML
@ -150,7 +156,7 @@ public class FoodpalApplicationCtrl {
return; return;
} }
// Let MainCtrl open the full detail screen // Let MainCtrl open the full detail screen
mainCtrl.showAdd(); //I had showrecipedetail but intelij says showoverview mainCtrl.showAddName(); //I had showrecipedetail but intelij says showoverview
} }
@FXML @FXML
@ -160,10 +166,15 @@ public class FoodpalApplicationCtrl {
openSelectedRecipe(); openSelectedRecipe();
} }
/**
* You can add ingredients. you get send to a different scene that creates the ingredient
* It doesn't automaticcaly refresh yet so before you can see the new ingredient you have to first click on a
* different recipe and come back after
*/
@FXML @FXML
private void addIngredient() { private void addIngredient() {
// TODO: make it possible to add ingredient to current recipe
System.out.println("Add ingredient clicked"); System.out.println("Add ingredient clicked");
mainCtrl.showAddIngredient();
} }
@FXML @FXML
@ -193,6 +204,10 @@ public class FoodpalApplicationCtrl {
private void MakePrintable() { private void MakePrintable() {
System.out.println("Recipe printed"); System.out.println("Recipe printed");
} }
public Recipe getSelectedRecipe() {
return recipeList.getSelectionModel().getSelectedItem();
}
} }

View file

@ -15,6 +15,7 @@
*/ */
package client.scenes; package client.scenes;
import commons.Recipe;
import javafx.scene.Parent; import javafx.scene.Parent;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.stage.Stage; import javafx.stage.Stage;
@ -29,28 +30,37 @@ public class MainCtrl {
private QuoteOverviewCtrl overviewCtrl; private QuoteOverviewCtrl overviewCtrl;
private Scene overview; private Scene overview;
private AddQuoteCtrl addCtrl; private AddNameCtrl addNameCtrl;
private Scene add; private Scene addName;
private FoodpalApplicationCtrl foodpalCtrl; private FoodpalApplicationCtrl foodpalCtrl;
private Scene foodpal; private Scene foodpal;
private AddIngredientCtrl AddIngredientCtrl;
private Scene addIngredient;
public void initialize(Stage primaryStage, public void initialize(Stage primaryStage,
Pair<QuoteOverviewCtrl, Parent> overview, Pair<QuoteOverviewCtrl, Parent> overview,
Pair<AddQuoteCtrl, Parent> add, Pair<AddNameCtrl, Parent> addName,
Pair<FoodpalApplicationCtrl, Parent> foodpal) throws IOException, InterruptedException { Pair<FoodpalApplicationCtrl, Parent> foodpal,
Pair<AddIngredientCtrl, Parent> addIngredient
) throws IOException, InterruptedException {
this.primaryStage = primaryStage; this.primaryStage = primaryStage;
this.overviewCtrl = overview.getKey(); this.overviewCtrl = overview.getKey();
this.overview = new Scene(overview.getValue()); this.overview = new Scene(overview.getValue());
this.addCtrl = add.getKey(); this.addNameCtrl = addName.getKey();
this.add = new Scene(add.getValue()); this.addName = new Scene(addName.getValue());
this.foodpalCtrl = foodpal.getKey(); this.foodpalCtrl = foodpal.getKey();
this.foodpal = new Scene(foodpal.getValue()); this.foodpal = new Scene(foodpal.getValue());
this.AddIngredientCtrl = addIngredient.getKey();
this.addIngredient = new Scene(addIngredient.getValue());
showFoodpal(); showFoodpal();
primaryStage.show(); primaryStage.show();
} }
@ -61,12 +71,12 @@ public class MainCtrl {
overviewCtrl.refresh(); overviewCtrl.refresh();
} }
public void showAdd() { public void showAddName() {
primaryStage.setTitle("Naming recipes"); primaryStage.setTitle("Naming recipes");
primaryStage.setScene(add); primaryStage.setScene(addName);
add.setOnKeyPressed(e -> { addName.setOnKeyPressed(e -> {
try { try {
addCtrl.keyPressed(e); addNameCtrl.keyPressed(e);
} catch (IOException | InterruptedException ex) { } catch (IOException | InterruptedException ex) {
throw new RuntimeException(ex); throw new RuntimeException(ex);
} }
@ -78,4 +88,17 @@ public class MainCtrl {
primaryStage.setScene(foodpal); primaryStage.setScene(foodpal);
foodpalCtrl.refresh(); foodpalCtrl.refresh();
} }
public void showAddIngredient() {
primaryStage.setTitle("adding ingredients");
primaryStage.setScene(addIngredient);
addName.setOnKeyPressed(e -> {
try {
AddIngredientCtrl.keyPressed(e);
} catch (IOException | InterruptedException ex) {
throw new RuntimeException(ex);
}
});
}
} }

View file

@ -60,7 +60,7 @@ public class QuoteOverviewCtrl implements Initializable {
} }
public void addQuote() { public void addQuote() {
mainCtrl.showAdd(); mainCtrl.showAddName();
} }
public void refresh() { public void refresh() {

View file

@ -16,6 +16,7 @@ import java.net.URI;
import java.net.http.HttpClient; import java.net.http.HttpClient;
import java.net.http.HttpRequest; import java.net.http.HttpRequest;
import java.net.http.HttpResponse; import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON; import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;
@ -165,4 +166,12 @@ public class ServerUtils {
return addRecipe(newRecipe); return addRecipe(newRecipe);
} }
public Recipe addRecipeIngredient(Recipe recipe, String ingredient) throws IOException, InterruptedException {
List<String> ingredients = new ArrayList<>(recipe.getIngredients());
ingredients.add(ingredient);
recipe.setIngredients(ingredients);
return addRecipe(recipe);
}
} }

View file

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="94.0" prefWidth="470.0" xmlns="http://javafx.com/javafx/23.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="client.scenes.AddIngredientCtrl">
<children>
<Button layoutX="417.0" layoutY="54.0" mnemonicParsing="false" onAction="#ok" text="Ok" />
<Button layoutX="349.0" layoutY="54.0" mnemonicParsing="false" onAction="#cancel" text="Cancel" />
<TextField fx:id="recipeName" layoutX="109.0" layoutY="24.0" prefHeight="18.0" prefWidth="292.0" />
<Label layoutX="28.0" layoutY="29.0" text="Recipe Name" />
</children>
</AnchorPane>

View file

@ -5,7 +5,7 @@
<?import javafx.scene.control.TextField?> <?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?> <?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="94.0" prefWidth="470.0" xmlns="http://javafx.com/javafx/23.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="client.scenes.AddQuoteCtrl"> <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="94.0" prefWidth="470.0" xmlns="http://javafx.com/javafx/23.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="client.scenes.AddNameCtrl">
<children> <children>
<Button layoutX="417.0" layoutY="54.0" mnemonicParsing="false" onAction="#ok" text="Ok" /> <Button layoutX="417.0" layoutY="54.0" mnemonicParsing="false" onAction="#ok" text="Ok" />
<Button layoutX="349.0" layoutY="54.0" mnemonicParsing="false" onAction="#cancel" text="Cancel" /> <Button layoutX="349.0" layoutY="54.0" mnemonicParsing="false" onAction="#cancel" text="Cancel" />

View file

@ -108,7 +108,7 @@
<!-- Recipe title row --> <!-- Recipe title row -->
<HBox spacing="10"> <HBox spacing="10">
<Label fx:id="replaceNameLabel" text="Recipe Name"> <Label fx:id="recipeNameLabel" text="Recipe Name">
<font> <font>
<Font name="System Bold" size="14.0" /> <Font name="System Bold" size="14.0" />
</font></Label> </font></Label>