Naming recipes on a different scene

This commit is contained in:
Mei Chang van der Werff 2025-12-02 13:08:23 +01:00
commit 236718fa6e
6 changed files with 54 additions and 62 deletions

View file

@ -15,11 +15,9 @@
*/ */
package client.scenes; package client.scenes;
import client.utils.ServerUtils;
import com.google.inject.Inject; import com.google.inject.Inject;
import client.utils.ServerUtilsExample;
import commons.Person;
import commons.Quote;
import jakarta.ws.rs.WebApplicationException; import jakarta.ws.rs.WebApplicationException;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.Alert; import javafx.scene.control.Alert;
@ -27,11 +25,13 @@ import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent; import javafx.scene.input.KeyEvent;
import javafx.stage.Modality; import javafx.stage.Modality;
import java.io.IOException;
public class AddQuoteCtrl { public class AddQuoteCtrl {
private final ServerUtilsExample server; private final ServerUtils server;
private final MainCtrl mainCtrl; private final MainCtrl mainCtrl;
public TextField recipeName;
@FXML @FXML
private TextField firstName; private TextField firstName;
@ -42,10 +42,9 @@ public class AddQuoteCtrl {
private TextField quote; private TextField quote;
@Inject @Inject
public AddQuoteCtrl(ServerUtilsExample server, MainCtrl mainCtrl) { public AddQuoteCtrl(ServerUtils server, MainCtrl mainCtrl) {
this.mainCtrl = mainCtrl; this.mainCtrl = mainCtrl;
this.server = server; this.server = server;
} }
public void cancel() { public void cancel() {
@ -53,9 +52,9 @@ public class AddQuoteCtrl {
mainCtrl.showOverview(); mainCtrl.showOverview();
} }
public void ok() { public void ok() throws IOException, InterruptedException {
try { try {
server.addQuote(getQuote()); server.addRecipeName(recipeName.getText());
} catch (WebApplicationException e) { } catch (WebApplicationException e) {
var alert = new Alert(Alert.AlertType.ERROR); var alert = new Alert(Alert.AlertType.ERROR);
@ -63,25 +62,19 @@ public class AddQuoteCtrl {
alert.setContentText(e.getMessage()); alert.setContentText(e.getMessage());
alert.showAndWait(); alert.showAndWait();
return; return;
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
} }
clearFields(); clearFields();
mainCtrl.showOverview(); mainCtrl.showFoodpal();
}
private Quote getQuote() {
var p = new Person(firstName.getText(), lastName.getText());
var q = quote.getText();
return new Quote(p, q);
} }
private void clearFields() { private void clearFields() {
firstName.clear(); recipeName.clear();
lastName.clear();
quote.clear();
} }
public void keyPressed(KeyEvent e) { public void keyPressed(KeyEvent e) throws IOException, InterruptedException {
switch (e.getCode()) { switch (e.getCode()) {
case ENTER: case ENTER:
ok(); ok();

View file

@ -1,8 +1,11 @@
package client.scenes; package client.scenes;
import javafx.event.ActionEvent;
import java.io.IOException;
import java.util.List; import java.util.List;
import client.utils.ServerUtils;
import commons.Recipe; import commons.Recipe;
import jakarta.inject.Inject; import jakarta.inject.Inject;
@ -14,6 +17,8 @@ import javafx.scene.control.ListView;
public class FoodpalApplicationCtrl { public class FoodpalApplicationCtrl {
private final MainCtrl mainCtrl; private final MainCtrl mainCtrl;
private final ServerUtils server;
// all of these aren't used with only my part of the code // all of these aren't used with only my part of the code
// everything in the top bar === // everything in the top bar ===
@ -68,12 +73,13 @@ public class FoodpalApplicationCtrl {
private Button addPreparationStepButton; private Button addPreparationStepButton;
@Inject @Inject
public FoodpalApplicationCtrl(MainCtrl mainCtrl) { public FoodpalApplicationCtrl(MainCtrl mainCtrl, ServerUtils server) {
this.mainCtrl = mainCtrl; this.mainCtrl = mainCtrl;
this.server = server;
} }
@FXML @FXML
private void initialize() { private void initialize() throws IOException, InterruptedException {
// Show recipe name in the list // Show recipe name in the list
recipeList.setCellFactory(list -> new ListCell<>() { recipeList.setCellFactory(list -> new ListCell<>() {
@Override @Override
@ -106,9 +112,9 @@ public class FoodpalApplicationCtrl {
// Button handlers // Button handlers
@FXML @FXML
public void refresh() { public void refresh() throws IOException, InterruptedException {
// TODO: someone else doing this // TODO: someone else doing this
List<Recipe> recipes = showRecipeDetails(); List<Recipe> recipes = server.getRecipes();
recipeList.getItems().setAll(recipes); recipeList.getItems().setAll(recipes);
// Select first recipe in the list by default // Select first recipe in the list by default
@ -117,15 +123,12 @@ public class FoodpalApplicationCtrl {
} }
} }
// to remove error till everything else is implemented
private List<Recipe> showRecipeDetails() {
return List.of();
}
@FXML @FXML
private void addRecipe() { private void addRecipe(ActionEvent e) {
// 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.showOverview(); mainCtrl.showAdd();
} }
@FXML @FXML
@ -147,7 +150,7 @@ public class FoodpalApplicationCtrl {
return; return;
} }
// Let MainCtrl open the full detail screen // Let MainCtrl open the full detail screen
mainCtrl.showOverview(); //I had showrecipedetail but intelij says showoverview mainCtrl.showAdd(); //I had showrecipedetail but intelij says showoverview
} }
@FXML @FXML
@ -185,20 +188,6 @@ public class FoodpalApplicationCtrl {
System.out.println("Switch language to PL"); System.out.println("Switch language to PL");
} }
@FXML
private void closeWindow() {
System.out.println("Close window");
}
@FXML
private void maximizeWindow() {
System.out.println("Maximize window");
}
@FXML
private void minimizeWindow() {
System.out.println("Minimize window");
}
@FXML @FXML
private void MakePrintable() { private void MakePrintable() {

View file

@ -20,6 +20,8 @@ import javafx.scene.Scene;
import javafx.stage.Stage; import javafx.stage.Stage;
import javafx.util.Pair; import javafx.util.Pair;
import java.io.IOException;
public class MainCtrl { public class MainCtrl {
private Stage primaryStage; private Stage primaryStage;
@ -36,7 +38,7 @@ public class MainCtrl {
public void initialize(Stage primaryStage, public void initialize(Stage primaryStage,
Pair<QuoteOverviewCtrl, Parent> overview, Pair<QuoteOverviewCtrl, Parent> overview,
Pair<AddQuoteCtrl, Parent> add, Pair<AddQuoteCtrl, Parent> add,
Pair<FoodpalApplicationCtrl, Parent> foodpal){ Pair<FoodpalApplicationCtrl, Parent> foodpal) throws IOException, InterruptedException {
this.primaryStage = primaryStage; this.primaryStage = primaryStage;
@ -60,12 +62,18 @@ public class MainCtrl {
} }
public void showAdd() { public void showAdd() {
primaryStage.setTitle("Quotes: Adding Quote"); primaryStage.setTitle("Naming recipes");
primaryStage.setScene(add); primaryStage.setScene(add);
add.setOnKeyPressed(e -> addCtrl.keyPressed(e)); add.setOnKeyPressed(e -> {
try {
addCtrl.keyPressed(e);
} catch (IOException | InterruptedException ex) {
throw new RuntimeException(ex);
}
});
} }
public void showFoodpal(){ public void showFoodpal() throws IOException, InterruptedException {
primaryStage.setTitle("FoodPal"); primaryStage.setTitle("FoodPal");
primaryStage.setScene(foodpal); primaryStage.setScene(foodpal);
foodpalCtrl.refresh(); foodpalCtrl.refresh();

View file

@ -2,9 +2,11 @@ package client.utils;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import commons.Quote;
import commons.Recipe; import commons.Recipe;
import jakarta.ws.rs.ProcessingException; import jakarta.ws.rs.ProcessingException;
import jakarta.ws.rs.client.ClientBuilder; import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.Entity;
import org.glassfish.jersey.client.ClientConfig; import org.glassfish.jersey.client.ClientConfig;
@ -156,4 +158,11 @@ public class ServerUtils {
} }
return true; return true;
} }
public Recipe addRecipeName(String name) throws IOException, InterruptedException {
Recipe newRecipe = new Recipe();
newRecipe.setName(name);
return addRecipe(newRecipe);
}
} }

View file

@ -5,15 +5,11 @@
<?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="146.0" prefWidth="470.0" xmlns="http://javafx.com/javafx/17" 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.AddQuoteCtrl">
<children> <children>
<Button layoutX="419.0" layoutY="105.0" mnemonicParsing="false" onAction="#ok" text="Ok" /> <Button layoutX="417.0" layoutY="54.0" mnemonicParsing="false" onAction="#ok" text="Ok" />
<Button layoutX="350.0" layoutY="105.0" mnemonicParsing="false" onAction="#cancel" text="Cancel" /> <Button layoutX="349.0" layoutY="54.0" mnemonicParsing="false" onAction="#cancel" text="Cancel" />
<TextField fx:id="firstName" layoutX="109.0" layoutY="24.0" prefHeight="26.0" prefWidth="79.0" /> <TextField fx:id="recipeName" layoutX="109.0" layoutY="24.0" prefHeight="18.0" prefWidth="292.0" />
<TextField fx:id="lastName" layoutX="286.0" layoutY="24.0" /> <Label layoutX="28.0" layoutY="29.0" text="Recipe Name" />
<TextField fx:id="quote" layoutX="109.0" layoutY="61.0" prefHeight="26.0" prefWidth="345.0" />
<Label layoutX="216.0" layoutY="29.0" text="Last Name" />
<Label layoutX="28.0" layoutY="29.0" text="First Name" />
<Label layoutX="28.0" layoutY="66.0" text="Quote" />
</children> </children>
</AnchorPane> </AnchorPane>

View file

@ -70,9 +70,6 @@
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints> </rowConstraints>
<children> <children>
<Button fx:id="closeButton" onAction="#closeWindow" text="X" GridPane.columnIndex="3" />
<Button fx:id="maximizeButton" onAction="#maximizeWindow" text="□" GridPane.columnIndex="2" />
<Button fx:id="minimizeButton" onAction="#minimizeWindow" text="-" GridPane.columnIndex="1" />
<Button fx:id="refreshButton" onAction="#refresh" prefHeight="25.0" prefWidth="34.0" text="⟳" GridPane.columnIndex="3" GridPane.rowIndex="2" /> <Button fx:id="refreshButton" onAction="#refresh" prefHeight="25.0" prefWidth="34.0" text="⟳" GridPane.columnIndex="3" GridPane.rowIndex="2" />
</children> </children>