Refactored to use the latest version of the scene router.

This commit is contained in:
Andrew Lalis 2023-12-22 12:07:52 -05:00
parent 2959947acc
commit 23a0a20acb
6 changed files with 38 additions and 57 deletions

View File

@ -26,6 +26,11 @@
<artifactId>javafx-fxml</artifactId>
<version>${javafx.version}</version>
</dependency>
<dependency>
<groupId>com.andrewlalis</groupId>
<artifactId>javafx-scene-router</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>

View File

@ -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<MainViewController>) 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"));
}
}

View File

@ -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();
}
}

View File

@ -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<Parent> setter;
private final Map<String, Parent> routeMap = new HashMap<>();
public Object context = null;
public SceneRouter(Pane pane) {
this(p -> pane.getChildren().setAll(p));
}
public SceneRouter(Consumer<Parent> 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> T getContext() {
return (T) context;
}
}

View File

@ -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;
}

View File

@ -13,6 +13,8 @@
<top>
<HBox fx:id="mainHeader">
<Label text="TODO: Add breadcrumb navigation/header here!"/>
<Button text="Back" onAction="#goBack"/>
<Button text="Forward" onAction="#goForward"/>
<Button text="Go to accounts!" onAction="#goToAccounts"/>
<Button text="Go to edit accounts!" onAction="#goToEditAccounts"/>
</HBox>