diff --git a/src/main/java/no/ntnu/idatt1005/group03/UI/AboutUsScreen.java b/src/main/java/no/ntnu/idatt1005/group03/UI/AboutUsScreen.java index 48803c7..5f24826 100644 --- a/src/main/java/no/ntnu/idatt1005/group03/UI/AboutUsScreen.java +++ b/src/main/java/no/ntnu/idatt1005/group03/UI/AboutUsScreen.java @@ -1,7 +1,6 @@ package no.ntnu.idatt1005.group03.UI; import javafx.geometry.Pos; -import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.ScrollPane; import javafx.scene.image.Image; @@ -50,12 +49,12 @@ public void start(Stage stage) { ScrollPane scrollPane = wrapInScrollPane(content); layout.setCenter(scrollPane); - Scene scene = new Scene(layout, 625, 550); - scene.getStylesheets().add(getClass().getResource("/styles/app.css").toExternalForm()); - - stage.setTitle("About Us"); - stage.setScene(scene); - stage.show(); + ScreenManager.showRoot( + stage, + layout, + "About Us", + getClass().getResource("/styles/app.css").toExternalForm() + ); } // --- UI COMPONENTS --- @@ -91,4 +90,4 @@ private ScrollPane wrapInScrollPane(VBox content) { scrollPane.getStyleClass().add("dark-scroll-pane"); return scrollPane; } -} \ No newline at end of file +} diff --git a/src/main/java/no/ntnu/idatt1005/group03/UI/AccountScreen.java b/src/main/java/no/ntnu/idatt1005/group03/UI/AccountScreen.java index 7d864d6..5013d8d 100644 --- a/src/main/java/no/ntnu/idatt1005/group03/UI/AccountScreen.java +++ b/src/main/java/no/ntnu/idatt1005/group03/UI/AccountScreen.java @@ -1,7 +1,6 @@ package no.ntnu.idatt1005.group03.UI; import javafx.geometry.Pos; -import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.BorderPane; import javafx.stage.Stage; @@ -62,13 +61,12 @@ public void start(Stage stage) { accountLayout.setCenter(mainContent); - Scene scene = new Scene(accountLayout, 450, 600); - // CONNECT TO CSS - scene.getStylesheets().add(getClass().getResource("/styles/app.css").toExternalForm()); - - stage.setTitle("Account"); - stage.setScene(scene); - stage.show(); + ScreenManager.showRoot( + stage, + accountLayout, + "Account", + getClass().getResource("/styles/app.css").toExternalForm() + ); } private VBox createProfileSection() { @@ -145,4 +143,4 @@ private VBox createLogoutSection() { VBox.setVgrow(logoutBox, Priority.ALWAYS); return logoutBox; } -} \ No newline at end of file +} diff --git a/src/main/java/no/ntnu/idatt1005/group03/UI/DonationScreen.java b/src/main/java/no/ntnu/idatt1005/group03/UI/DonationScreen.java index dd2cee9..e7d1eac 100644 --- a/src/main/java/no/ntnu/idatt1005/group03/UI/DonationScreen.java +++ b/src/main/java/no/ntnu/idatt1005/group03/UI/DonationScreen.java @@ -1,7 +1,6 @@ package no.ntnu.idatt1005.group03.UI; import javafx.geometry.Insets; -import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; @@ -91,14 +90,12 @@ public void start(Stage stage) { donateButton, backButton); - Scene scene = new Scene(layout, 400, 330); - - // CONNECT TO CSS - scene.getStylesheets().add(getClass().getResource("/styles/app.css").toExternalForm()); - - stage.setTitle("Donation"); - stage.setScene(scene); - stage.show(); + ScreenManager.showRoot( + stage, + layout, + "Donation", + getClass().getResource("/styles/app.css").toExternalForm() + ); } private Label createOrgNameLabel() { @@ -203,4 +200,4 @@ private Button createBackButton() { backButton.setOnAction(e -> ScreenManager.showHomeScreen()); return backButton; } -} \ No newline at end of file +} diff --git a/src/main/java/no/ntnu/idatt1005/group03/UI/LoginScreen.java b/src/main/java/no/ntnu/idatt1005/group03/UI/LoginScreen.java index cf7bfae..a3bbd21 100644 --- a/src/main/java/no/ntnu/idatt1005/group03/UI/LoginScreen.java +++ b/src/main/java/no/ntnu/idatt1005/group03/UI/LoginScreen.java @@ -1,6 +1,7 @@ package no.ntnu.idatt1005.group03.UI; import javafx.application.Application; +import javafx.application.Platform; import javafx.geometry.HPos; import javafx.geometry.Insets; import javafx.geometry.Pos; @@ -32,8 +33,7 @@ */ public class LoginScreen extends Application { // homepage - private Scene scene1; // Sign Up - private Scene scene2; // Log In + private Scene authScene; private AuthController authController; private Stage stage; private Label signUpErrLabel; @@ -51,6 +51,9 @@ public class LoginScreen extends Application { // homepage */ @Override public void start(Stage primaryStage) { + boolean MaximizeOnStartup = primaryStage.getScene() == null; + String stylesheet = getClass().getResource("/styles/app.css").toExternalForm(); + this.stage = primaryStage; this.authController = new AuthController(new UserRepositoryDAO()); this.favoriteController = new FavoriteController(); @@ -61,16 +64,27 @@ public void start(Stage primaryStage) { loginErrLabel = new Label(); loginErrLabel.setTextFill(Color.RED); - scene1 = createScene1(); - scene2 = createScene2(); + if (primaryStage.getScene() == null) { + authScene = new Scene(createSignUpRoot()); + authScene.getStylesheets().add(stylesheet); + primaryStage.setTitle("Log in screen"); + primaryStage.setScene(authScene); + primaryStage.show(); + } else { + authScene = primaryStage.getScene(); + authScene.setRoot(createSignUpRoot()); + authScene.getStylesheets().setAll(stylesheet); + primaryStage.setTitle("Log in screen"); + primaryStage.show(); + } - primaryStage.setTitle("Log in screen"); - primaryStage.setScene(scene1); - primaryStage.show(); + if (MaximizeOnStartup) { + Platform.runLater(() -> primaryStage.setMaximized(true)); + } } // -------- SCENE 1 (Sign Up) -------- - private Scene createScene1() { + private GridPane createSignUpRoot() { GridPane root1 = createBaseGrid(); Label nameLabel = new Label("Email address"); @@ -113,11 +127,11 @@ private Scene createScene1() { accountBox.setAlignment(Pos.CENTER); root1.add(accountBox, 1, 9, 3, 1); - return wrapInScene(root1); + return root1; } // -------- SCENE 2 (Log In) -------- - private Scene createScene2() { + private GridPane createLoginRoot() { GridPane root2 = createBaseGrid(); Label usernameLabel2 = new Label("Email address or username"); @@ -156,7 +170,7 @@ private Scene createScene2() { GridPane.setHalignment(forgot, HPos.CENTER); - return wrapInScene(root2); + return root2; } // -------- REUSABLE COMPONENTS -------- @@ -186,14 +200,14 @@ private void addHeader(GridPane grid, boolean isScene2) { signloginButton.getStyleClass().addAll("btn", isScene2 ? "btn-outline" : "btn-secondary", "btn-toggle"); signloginButton.setOnAction(e -> { signUpErrLabel.setText(""); - stage.setScene(scene1); + authScene.setRoot(createSignUpRoot()); }); Button loginButton = new Button("Log in"); loginButton.getStyleClass().addAll("btn", isScene2 ? "btn-secondary" : "btn-outline", "btn-toggle"); loginButton.setOnAction(e -> { loginErrLabel.setText(""); - stage.setScene(scene2); + authScene.setRoot(createLoginRoot()); }); HBox buttonBox = new HBox(0); // 0px spacing @@ -204,12 +218,6 @@ private void addHeader(GridPane grid, boolean isScene2) { grid.add(buttonBox, 1, 0, 3, 1); } - private Scene wrapInScene(GridPane root) { - Scene scene = new Scene(root, 450, 350); - scene.getStylesheets().add(getClass().getResource("/styles/app.css").toExternalForm()); - return scene; - } - // -------- ACTION LOGIC -------- private void handleSignUp(String username, String email, String password, String confirmPassword) { @@ -238,4 +246,4 @@ private void handleLogin(String usernameOrEmail, String password) { loginErrLabel.setText("Wrong username, email, or password."); } } -} \ No newline at end of file +} diff --git a/src/main/java/no/ntnu/idatt1005/group03/UI/OrganizationsScreen.java b/src/main/java/no/ntnu/idatt1005/group03/UI/OrganizationsScreen.java index 95fe06f..4091ef0 100644 --- a/src/main/java/no/ntnu/idatt1005/group03/UI/OrganizationsScreen.java +++ b/src/main/java/no/ntnu/idatt1005/group03/UI/OrganizationsScreen.java @@ -1,7 +1,6 @@ package no.ntnu.idatt1005.group03.UI; import javafx.geometry.Insets; -import javafx.scene.Scene; import javafx.geometry.Pos; import javafx.scene.control.Button; import javafx.scene.control.Hyperlink; @@ -87,12 +86,12 @@ public void start(Stage stage) { ScrollPane scrollPane = wrapInScrollPane(content); mainLayout.setCenter(scrollPane); - Scene scene = new Scene(mainLayout, 625, 550); - scene.getStylesheets().add(getClass().getResource("/styles/app.css").toExternalForm()); - - stage.setTitle("Organizations"); - stage.setScene(scene); - stage.show(); + ScreenManager.showRoot( + stage, + mainLayout, + "Organizations", + getClass().getResource("/styles/app.css").toExternalForm() + ); } private Text createHeaderSection() { @@ -211,4 +210,4 @@ private ScrollPane wrapInScrollPane(VBox content) { scrollPane.getStyleClass().add("transparent-scroll-pane"); return scrollPane; } -} \ No newline at end of file +} diff --git a/src/main/java/no/ntnu/idatt1005/group03/UI/ScreenManager.java b/src/main/java/no/ntnu/idatt1005/group03/UI/ScreenManager.java index 4b76259..da2795f 100644 --- a/src/main/java/no/ntnu/idatt1005/group03/UI/ScreenManager.java +++ b/src/main/java/no/ntnu/idatt1005/group03/UI/ScreenManager.java @@ -1,5 +1,7 @@ package no.ntnu.idatt1005.group03.UI; +import javafx.scene.Parent; +import javafx.scene.Scene; import javafx.stage.Stage; import no.ntnu.idatt1005.group03.Model.Organization; import no.ntnu.idatt1005.group03.Model.User; @@ -55,6 +57,40 @@ public static User getCurrentUser() { return currentUser; } + public static void showScene(Stage stage, Scene scene, String title) { + Scene currentScene = stage.getScene(); + stage.setTitle(title); + if (currentScene == null) { + stage.setScene(scene); + stage.show(); + } else { + currentScene.setRoot(scene.getRoot()); + currentScene.getStylesheets().setAll(scene.getStylesheets()); + } + } + + /** + * Shows a new root on the given stage while reusing the existing scene when possible. + * + * @param stage the stage to update + * @param root the root node to display + * @param title the window title + * @param stylesheet the stylesheet URL to apply + */ + public static void showRoot(Stage stage, Parent root, String title, String stylesheet) { + Scene currentScene = stage.getScene(); + stage.setTitle(title); + if (currentScene == null) { + Scene scene = new Scene(root); + scene.getStylesheets().add(stylesheet); + stage.setScene(scene); + stage.show(); + } else { + currentScene.setRoot(root); + currentScene.getStylesheets().setAll(stylesheet); + } + } + // Navigation Methods /** * Displays the home screen for the current user. @@ -95,4 +131,4 @@ public static void logout() { currentUser = null; new LoginScreen().start(mainStage); } -} \ No newline at end of file +}