From 23a0a20acb3df0cfd35fcbe05602f3197dfc7cd4 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Fri, 22 Dec 2023 12:07:52 -0500 Subject: [PATCH] Refactored to use the latest version of the scene router. --- pom.xml | 5 ++ .../com/andrewlalis/perfin/PerfinApp.java | 18 +++++-- .../perfin/control/MainViewController.java | 19 +++++-- .../andrewlalis/perfin/view/SceneRouter.java | 49 ------------------- src/main/java/module-info.java | 2 + src/main/resources/main-view.fxml | 2 + 6 files changed, 38 insertions(+), 57 deletions(-) delete mode 100644 src/main/java/com/andrewlalis/perfin/view/SceneRouter.java diff --git a/pom.xml b/pom.xml index 9745481..a74db60 100644 --- a/pom.xml +++ b/pom.xml @@ -26,6 +26,11 @@ javafx-fxml ${javafx.version} + + com.andrewlalis + javafx-scene-router + 1.1.0 + com.fasterxml.jackson.core diff --git a/src/main/java/com/andrewlalis/perfin/PerfinApp.java b/src/main/java/com/andrewlalis/perfin/PerfinApp.java index 918dd4a..3ad66d4 100644 --- a/src/main/java/com/andrewlalis/perfin/PerfinApp.java +++ b/src/main/java/com/andrewlalis/perfin/PerfinApp.java @@ -1,7 +1,7 @@ package com.andrewlalis.perfin; +import com.andrewlalis.javafx_scene_router.SceneRouter; import com.andrewlalis.perfin.control.MainViewController; -import com.andrewlalis.perfin.view.SceneRouter; import com.andrewlalis.perfin.view.SplashScreenStage; import javafx.application.Application; import javafx.scene.Scene; @@ -16,6 +16,11 @@ import java.util.function.Consumer; public class PerfinApp extends Application { public static final Path APP_DIR = Path.of(System.getProperty("user.home", "."), ".perfin"); + /** + * The router that's used for navigating between different "pages" in the application. + */ + public static final SceneRouter router = new SceneRouter(); + public static void main(String[] args) { launch(args); } @@ -24,6 +29,7 @@ public class PerfinApp extends Application { public void start(Stage stage) { SplashScreenStage splashStage = new SplashScreenStage("Loading", SceneUtil.load("/startup-splash-screen.fxml")); splashStage.show(); + defineRoutes(); initMainScreen(stage); splashStage.stateProperty().addListener((v, oldState, state) -> { if (state == SplashScreenStage.State.DONE) stage.show(); @@ -34,11 +40,15 @@ public class PerfinApp extends Application { private void initMainScreen(Stage stage) { stage.hide(); Scene mainViewScene = SceneUtil.load("/main-view.fxml", (Consumer) c -> { - c.router = new SceneRouter(c.mainContainer::setCenter) - .map("accounts", "/accounts-view.fxml") - .map("edit-account", "/edit-account.fxml"); + c.mainContainer.setCenter(router.getViewPane()); + router.navigate("accounts"); }); stage.setScene(mainViewScene); stage.setTitle("Perfin"); } + + private static void defineRoutes() { + router.map("accounts", SceneUtil.loadNode("/accounts-view.fxml")); + router.map("edit-account", SceneUtil.loadNode("/edit-account.fxml")); + } } \ No newline at end of file diff --git a/src/main/java/com/andrewlalis/perfin/control/MainViewController.java b/src/main/java/com/andrewlalis/perfin/control/MainViewController.java index af7fb5d..13363f8 100644 --- a/src/main/java/com/andrewlalis/perfin/control/MainViewController.java +++ b/src/main/java/com/andrewlalis/perfin/control/MainViewController.java @@ -1,12 +1,13 @@ package com.andrewlalis.perfin.control; -import com.andrewlalis.perfin.view.SceneRouter; +import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; +import static com.andrewlalis.perfin.PerfinApp.router; + public class MainViewController { - public SceneRouter router; @FXML public BorderPane mainContainer; @FXML @@ -14,11 +15,21 @@ public class MainViewController { @FXML public void goToAccounts() { - router.goTo("accounts"); + router.navigate("accounts"); } @FXML public void goToEditAccounts() { - router.goTo("edit-account"); + router.navigate("edit-account"); + } + + @FXML + public void goBack() { + router.navigateBack(); + } + + @FXML + public void goForward() { + router.navigateForward(); } } diff --git a/src/main/java/com/andrewlalis/perfin/view/SceneRouter.java b/src/main/java/com/andrewlalis/perfin/view/SceneRouter.java deleted file mode 100644 index 96a925c..0000000 --- a/src/main/java/com/andrewlalis/perfin/view/SceneRouter.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.andrewlalis.perfin.view; - -import com.andrewlalis.perfin.SceneUtil; -import javafx.application.Platform; -import javafx.scene.Parent; -import javafx.scene.layout.Pane; - -import java.util.HashMap; -import java.util.Map; -import java.util.function.Consumer; - -public class SceneRouter { - private final Consumer setter; - private final Map routeMap = new HashMap<>(); - public Object context = null; - - public SceneRouter(Pane pane) { - this(p -> pane.getChildren().setAll(p)); - } - - public SceneRouter(Consumer setter) { - this.setter = setter; - } - - public SceneRouter map(String route, Parent scene) { - routeMap.put(route, scene); - return this; - } - - public SceneRouter map(String route, String fxml) { - return map(route, SceneUtil.loadNode(fxml)); - } - - public void goTo(String route, Object context) { - Parent node = routeMap.get(route); - if (node == null) throw new IllegalArgumentException("Route " + route + " is not mapped to any node."); - this.context = context; - Platform.runLater(() -> setter.accept(node)); - } - - public void goTo(String route) { - goTo(route, null); - } - - @SuppressWarnings("unchecked") - public T getContext() { - return (T) context; - } -} diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 68f3184..f349b22 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -3,11 +3,13 @@ module com.andrewlalis.perfin { requires javafx.controls; requires javafx.fxml; requires javafx.graphics; + requires com.andrewlalis.javafx_scene_router; requires com.fasterxml.jackson.databind; requires java.sql; exports com.andrewlalis.perfin to javafx.graphics; + exports com.andrewlalis.perfin.view to javafx.graphics; opens com.andrewlalis.perfin.control to javafx.fxml; } \ No newline at end of file diff --git a/src/main/resources/main-view.fxml b/src/main/resources/main-view.fxml index b2c380e..3184886 100644 --- a/src/main/resources/main-view.fxml +++ b/src/main/resources/main-view.fxml @@ -13,6 +13,8 @@