From 5713a1a69b28b3aaa9a11bd51b1247ad08ec5d91 Mon Sep 17 00:00:00 2001 From: Marius Klepp Date: Thu, 9 Apr 2026 09:16:33 +0200 Subject: [PATCH 1/6] feat: add shared Navbar with MainView and MainController for unified navigation --- src/main/java/app/Main.java | 6 +- src/main/java/app/SceneManager.java | 28 --------- src/main/java/ui/Page.java | 21 +++++++ .../java/ui/controller/MainController.java | 62 +++++++++++++++++++ .../java/ui/controller/NavbarController.java | 53 ++++++++++++++-- src/main/resources/view/MainView.fxml | 21 +++++++ src/main/resources/view/Navbar.fxml | 19 ++++++ 7 files changed, 172 insertions(+), 38 deletions(-) delete mode 100644 src/main/java/app/SceneManager.java create mode 100644 src/main/java/ui/Page.java create mode 100644 src/main/java/ui/controller/MainController.java create mode 100644 src/main/resources/view/MainView.fxml create mode 100644 src/main/resources/view/Navbar.fxml diff --git a/src/main/java/app/Main.java b/src/main/java/app/Main.java index 7bfc57c..0b94641 100644 --- a/src/main/java/app/Main.java +++ b/src/main/java/app/Main.java @@ -10,10 +10,6 @@ import persistence.OrganizationDao; import persistence.db.Database; -import javax.xml.crypto.Data; -import java.io.IOException; -import java.util.List; - public class Main extends Application { @Override @@ -28,7 +24,7 @@ public void start(Stage stage) throws Exception { SceneManager.setStage(stage); - Parent root = FXMLLoader.load(getClass().getResource("/fxml/Home.fxml")); + Parent root = FXMLLoader.load(getClass().getResource("/view/MainView.fxml")); Scene scene = new Scene(root, 1275, 725); stage.setTitle("GiveHope"); diff --git a/src/main/java/app/SceneManager.java b/src/main/java/app/SceneManager.java deleted file mode 100644 index 9632b35..0000000 --- a/src/main/java/app/SceneManager.java +++ /dev/null @@ -1,28 +0,0 @@ -package app; - -import javafx.fxml.FXMLLoader; -import javafx.scene.Parent; -import javafx.stage.Stage; - -import java.io.IOException; - -public class SceneManager { - - private static Stage stage; - - public static void setStage(Stage s) { - stage = s; - } - - public static void switchTo(String fxmlPath) throws IOException { - Parent root = FXMLLoader.load(SceneManager.class.getResource(fxmlPath)); - stage.getScene().setRoot(root); - } - - public static void switchTo(String fxmlPath, Object controller) throws IOException { - FXMLLoader loader = new FXMLLoader(SceneManager.class.getResource(fxmlPath)); - loader.setController(controller); - Parent root = loader.load(); - stage.getScene().setRoot(root); - } -} \ No newline at end of file diff --git a/src/main/java/ui/Page.java b/src/main/java/ui/Page.java new file mode 100644 index 0000000..e2066c8 --- /dev/null +++ b/src/main/java/ui/Page.java @@ -0,0 +1,21 @@ +package ui; + +public enum Page { + HOME("Home.fxml"), + PROFILE("MyProfile.fxml"), + SIGNIN("SignIn.fxml"), + REGISTER("Register.fxml"), + ORGANIZATIONS("OrganizationsView.fxml"), + MYPROFILE("MyProfile.fxml"); + + + private final String fileName; + + Page(String fileName) { + this.fileName = fileName; + } + + public String getFileName() { + return fileName; + } +} diff --git a/src/main/java/ui/controller/MainController.java b/src/main/java/ui/controller/MainController.java new file mode 100644 index 0000000..2bcc6cc --- /dev/null +++ b/src/main/java/ui/controller/MainController.java @@ -0,0 +1,62 @@ +package ui.controller; + +import application.user.UserRegister; +import application.user.UserSignIn; +import integration.security.Sha256PasswordHasher; +import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.layout.BorderPane; +import persistence.UserDao; +import ui.Page; + +import java.io.IOException; + +public class MainController { + + @FXML + private BorderPane mainPane; + + @FXML + private NavbarController navbarController; + + public void initialize() { + try { + navbarController.setOnNavigate(page -> { + try { + loadPage(page); + } catch (IOException e) { + + } + }); + loadPage(Page.HOME); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public void loadPage(Page page) throws IOException { + FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/" + page.getFileName())); + Parent root = loader.load(); + + Object controller = loader.getController(); + if (controller instanceof NavigationAware navigationAware) { + navigationAware.setOnNavigate(p -> { + try { + loadPage(p); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); + } + if (controller instanceof SignInController c) { + c.setUserSignIn(new UserSignIn(new Sha256PasswordHasher(), new UserDao())); + } + if (controller instanceof RegisterController c) { + c.setUserRegister(new UserRegister(new Sha256PasswordHasher(), new UserDao())); + } + + mainPane.setCenter(root); + navbarController.setActivePage(page); + } +} diff --git a/src/main/java/ui/controller/NavbarController.java b/src/main/java/ui/controller/NavbarController.java index 751de42..7523c33 100644 --- a/src/main/java/ui/controller/NavbarController.java +++ b/src/main/java/ui/controller/NavbarController.java @@ -1,12 +1,55 @@ package ui.controller; -import app.SceneManager; -import javafx.event.ActionEvent; +import java.util.function.Consumer; + +import javafx.fxml.FXML; +import javafx.scene.control.Button; +import ui.Page; public class NavbarController { - @javafx.fxml.FXML - private void handleGoHome(ActionEvent event) throws Exception { - SceneManager.switchTo("/fxml/Home.fxml"); + private Consumer onNavigate; + @FXML + private Button homeBtn; + @FXML + private Button orgBtn; + @FXML + private Button signInBtn; + + public void setOnNavigate(Consumer onNavigate) { + this.onNavigate = onNavigate; + } + + public void setActivePage(Page page) { + homeBtn.getStyleClass().remove("nav-button-active"); + orgBtn.getStyleClass().remove("nav-button-active"); + signInBtn.getStyleClass().remove("nav-button-active"); + + Button buttonForPage = switch (page) { + case HOME -> homeBtn; + case ORGANIZATIONS -> orgBtn; + case SIGNIN -> signInBtn; + default -> null; + }; + + if (buttonForPage != null) { + buttonForPage.getStyleClass().add("nav-button-active"); + } + } + + public void goHome() { + onNavigate.accept(Page.HOME); + } + + public void goToSignIn() { + onNavigate.accept(Page.SIGNIN); + } + + public void goToOrganizations() { + onNavigate.accept(Page.ORGANIZATIONS); + } + + public void goToMyProfile() { + onNavigate.accept(Page.PROFILE); } } diff --git a/src/main/resources/view/MainView.fxml b/src/main/resources/view/MainView.fxml new file mode 100644 index 0000000..6b655b0 --- /dev/null +++ b/src/main/resources/view/MainView.fxml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + +
+ +
+
diff --git a/src/main/resources/view/Navbar.fxml b/src/main/resources/view/Navbar.fxml new file mode 100644 index 0000000..21a512e --- /dev/null +++ b/src/main/resources/view/Navbar.fxml @@ -0,0 +1,19 @@ + + + + + + + + + +