diff --git a/src/main/java/app/Main.java b/src/main/java/app/Main.java index 8683e16..aed51cc 100644 --- a/src/main/java/app/Main.java +++ b/src/main/java/app/Main.java @@ -24,6 +24,8 @@ import persistence.OrganizationDao; import persistence.db.Database; +public class Main extends Application { + import javax.xml.crypto.Data; import java.io.IOException; import java.util.List; @@ -72,7 +74,6 @@ public static void main(String[] args) { } catch (Exception e) { e.printStackTrace(); } - @Override public void start(Stage stage) throws Exception { @@ -86,7 +87,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/HomeController.java b/src/main/java/ui/controller/HomeController.java index a56fd9c..c65c6a4 100644 --- a/src/main/java/ui/controller/HomeController.java +++ b/src/main/java/ui/controller/HomeController.java @@ -7,24 +7,29 @@ import javafx.fxml.FXML; import javafx.scene.control.Button; import persistence.UserDao; +import ui.Page; + import java.io.IOException; +import java.util.function.Consumer; + +public class HomeController implements NavigationAware { -public class HomeController { + private Consumer onNavigate; + + public void setOnNavigate(Consumer onNavigate) { + this.onNavigate = onNavigate; + } public Button signInButton; @FXML private void handleSignIn() throws IOException { - UserDao userDao = new UserDao(); - PasswordHasher hasher = new Sha256PasswordHasher(); - UserSignIn userSignIn = new UserSignIn(hasher, userDao); - - SceneManager.switchTo("/fxml/signIn.fxml", new SignInController(userSignIn)); + onNavigate.accept(Page.SIGNIN); } @FXML - private void handleBrowseOrganizations() throws IOException { - SceneManager.switchTo("/fxml/OrganizationsView.fxml"); + private void handleBrowseOrganizations() { + onNavigate.accept(Page.ORGANIZATIONS); } } \ No newline at end of file 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/java/ui/controller/NavigationAware.java b/src/main/java/ui/controller/NavigationAware.java new file mode 100644 index 0000000..a04623e --- /dev/null +++ b/src/main/java/ui/controller/NavigationAware.java @@ -0,0 +1,9 @@ +package ui.controller; + +import ui.Page; + +import java.util.function.Consumer; + +public interface NavigationAware { + void setOnNavigate(Consumer onNavigate); +} \ No newline at end of file diff --git a/src/main/java/ui/controller/OrganizationController.java b/src/main/java/ui/controller/OrganizationController.java index 076d35d..a0b85d9 100644 --- a/src/main/java/ui/controller/OrganizationController.java +++ b/src/main/java/ui/controller/OrganizationController.java @@ -14,12 +14,14 @@ import javafx.scene.layout.VBox; import persistence.OrganizationDao; import persistence.UserDao; +import ui.Page; import util.SessionManager; import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; +import java.util.function.Consumer; import java.util.stream.Collectors; public class OrganizationController { @@ -42,31 +44,14 @@ public class OrganizationController { private int page = 0; private final int PER_PAGE = 15; + private Consumer onNavigate; + + public void setOnNavigate(Consumer onNavigate) { + this.onNavigate = onNavigate; + } + public void initialize() { try { - if (SessionManager.isSignedIn()) { - signInAndMyProfileBtn.setText("My profile"); - signInAndMyProfileBtn.setOnAction(e -> { - try { - SceneManager.switchTo("/fxml/MyProfile.fxml"); - } catch (IOException ex) { - throw new RuntimeException(ex); - } - }); - } else { - signInAndMyProfileBtn.setText("Sign In"); - UserDao userDao = new UserDao(); - PasswordHasher hasher = new Sha256PasswordHasher(); - UserSignIn userSignIn = new UserSignIn(hasher, userDao); - signInAndMyProfileBtn.setOnAction(e -> { - try { - SceneManager.switchTo("/fxml/signIn.fxml", new SignInController(userSignIn)); - } catch (IOException ex) { - throw new RuntimeException(ex); - } - }); - } - OrganizationDao dao = new OrganizationDao(); allOrgs = dao.getAll(); System.out.println("Fetched: " + allOrgs.size() + " organizations"); @@ -154,17 +139,13 @@ private void nextPage() { } @FXML - private void goHome() throws IOException { - SceneManager.switchTo("/fxml/Home.fxml"); + private void handleGoHome() { + onNavigate.accept(Page.HOME); } @FXML - private void goToSignIn() throws IOException { - UserDao userDao = new UserDao(); - PasswordHasher hasher = new Sha256PasswordHasher(); - UserSignIn userSignIn = new UserSignIn(hasher, userDao); - - SceneManager.switchTo("/fxml/signIn.fxml", new SignInController(userSignIn)); + private void handleSignIn() { + onNavigate.accept(Page.SIGNIN); } } diff --git a/src/main/java/ui/controller/RegisterController.java b/src/main/java/ui/controller/RegisterController.java index 075e642..681b353 100644 --- a/src/main/java/ui/controller/RegisterController.java +++ b/src/main/java/ui/controller/RegisterController.java @@ -10,10 +10,12 @@ import javafx.scene.control.PasswordField; import javafx.scene.control.TextField; import persistence.UserDao; +import ui.Page; import java.io.IOException; +import java.util.function.Consumer; -public class RegisterController { +public class RegisterController implements NavigationAware { @FXML private TextField usernameField; @FXML private TextField emailField; @@ -22,12 +24,17 @@ public class RegisterController { @FXML private PasswordField confirmPasswordField; @FXML private Label errorLabel; - private final UserRegister userRegister; + private UserRegister userRegister; + private Consumer onNavigate; - public RegisterController(UserRegister userRegister) { + public void setUserRegister(UserRegister userRegister) { this.userRegister = userRegister; } + public void setOnNavigate(Consumer onNavigate) { + this.onNavigate = onNavigate; + } + @FXML private void handleRegister() { String username = usernameField.getText().trim(); @@ -50,25 +57,19 @@ private void handleRegister() { try { userRegister.execute(username, phoneNr, password, email); - SceneManager.switchTo("/fxml/signIn.fxml", new SignInController( - new UserSignIn(new Sha256PasswordHasher(), new UserDao()) - )); + onNavigate.accept(Page.SIGNIN); } catch (IllegalArgumentException e) { errorLabel.setText(e.getMessage()); - } catch (IOException e) { - errorLabel.setText("Something went wrong, please try again."); } } @FXML - private void handleSignIn() throws IOException { - PasswordHasher hasher = new Sha256PasswordHasher(); - UserSignIn userSignIn = new UserSignIn(hasher, new UserDao()); - SceneManager.switchTo("/fxml/signIn.fxml", new SignInController(userSignIn)); + private void handleSignIn() { + onNavigate.accept(Page.SIGNIN); } @FXML - private void handleGoHome() throws IOException { - SceneManager.switchTo("/fxml/Home.fxml"); + private void handleGoHome() { + onNavigate.accept(Page.HOME); } } diff --git a/src/main/java/ui/controller/SignInController.java b/src/main/java/ui/controller/SignInController.java index ac0b314..f8ebe4c 100644 --- a/src/main/java/ui/controller/SignInController.java +++ b/src/main/java/ui/controller/SignInController.java @@ -16,22 +16,29 @@ import javafx.scene.control.TextField; import javafx.stage.Stage; import persistence.UserDao; +import ui.Page; import util.SessionManager; import java.io.IOException; +import java.util.function.Consumer; -public class SignInController { +public class SignInController implements NavigationAware { @FXML private TextField loginField; @FXML private PasswordField passwordField; @FXML private Label errorLabel; - private final UserSignIn userSignIn; + private UserSignIn userSignIn; + private Consumer onNavigate; - public SignInController(UserSignIn userSignIn) { + public void setUserSignIn(UserSignIn userSignIn) { this.userSignIn = userSignIn; } + public void setOnNavigate(Consumer onNavigate) { + this.onNavigate = onNavigate; + } + @FXML private void handleSignIn() { @@ -49,32 +56,14 @@ private void handleSignIn() { try { User user = userSignIn.execute(login, password); SessionManager.signIn(user); - SceneManager.switchTo("/fxml/Home.fxml"); + onNavigate.accept(Page.HOME); } catch (IllegalArgumentException e) { errorLabel.setText(e.getMessage()); - } catch (IOException e) { - errorLabel.setText("Noe gikk galt, prøv igjen."); } - - // Her skal senere login service kalles - } - - @FXML - private void handleGoHome(ActionEvent event) throws Exception { - - FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/Home.fxml")); - Scene scene = new Scene(loader.load()); - - Stage stage = (Stage)((Node)event.getSource()).getScene().getWindow(); - stage.setScene(scene); } @FXML private void handleRegister() throws IOException { - UserDao userDao = new UserDao(); - PasswordHasher hasher = new Sha256PasswordHasher(); - UserRegister userRegister = new UserRegister(hasher, userDao); - - SceneManager.switchTo("/fxml/Register.fxml", new RegisterController(userRegister)); + onNavigate.accept(Page.REGISTER); } } \ No newline at end of file diff --git a/src/main/resources/css/Global.css b/src/main/resources/css/Global.css index 816b1f1..c39569e 100644 --- a/src/main/resources/css/Global.css +++ b/src/main/resources/css/Global.css @@ -3,13 +3,7 @@ -fx-font-family: "Segoe UI"; } -.nav-button { - -fx-background-color: #d9d9d9; - -fx-background-radius: 20; - -fx-font-size: 22px; - -fx-cursor: hand; - -fx-padding: 10 20 10 20; -} + .nav-button:hover { -fx-background-color: #bfc3c7; -fx-scale-x: 1.05; @@ -53,11 +47,6 @@ -fx-padding: 8px 15px; } -.logo-label { - -fx-font-size: 26px; - -fx-font-family: "Times New Roman"; - -fx-text-fill: #1a1a1a; -} .page-title { -fx-font-size: 36px; @@ -103,4 +92,35 @@ .section-title { -fx-font-size: 22px; -fx-text-fill: #1a1a1a; +} + + +/*NavigationBar*/ +.navbar { + -fx-background-color: #1e3a8a; + -fx-padding: 12 20 12 20; +} + +.nav-button { + -fx-background-color: transparent; + -fx-border-color: rgba(255,255,255,0.4); + -fx-border-radius: 20; + -fx-background-radius: 20; + -fx-text-fill: white; + -fx-font-size: 13px; + -fx-cursor: hand; + -fx-padding: 6 16 6 16; +} + +.nav-button-active { + -fx-background-color: white; + -fx-text-fill: #1a1a2e; + -fx-font-weight: bold; +} + +.logo-label { + -fx-font-size: 22px; + -fx-font-family: "Times New Roman"; + -fx-text-fill: white; + -fx-font-weight: bold; } \ No newline at end of file diff --git a/src/main/resources/css/OrganizationView.css b/src/main/resources/css/OrganizationView.css index b756b98..6904bfd 100644 --- a/src/main/resources/css/OrganizationView.css +++ b/src/main/resources/css/OrganizationView.css @@ -15,7 +15,7 @@ } .donate-button { - -fx-background-color: #3a86ff; + -fx-background-color: #1e3a8a; -fx-text-fill: white; -fx-font-size: 14px; -fx-font-weight: bold; @@ -25,12 +25,12 @@ } .donate-button:hover { - -fx-background-color: #266ddf; + -fx-background-color: #2649a8; -fx-scale-x: 1.05; -fx-scale-y: 1.05; } .donate-button:pressed { - -fx-background-color: #1a5bbf; + -fx-background-color: #162d6e; -fx-scale-x: 0.97; -fx-scale-y: 0.97; } diff --git a/src/main/resources/fxml/Navbar.fxml b/src/main/resources/fxml/Navbar.fxml deleted file mode 100644 index af9d6cd..0000000 --- a/src/main/resources/fxml/Navbar.fxml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - - -