From 514cc7fb8ec85824aa4f62adecd52585fe8a9093 Mon Sep 17 00:00:00 2001 From: Konrad Neitzel Date: Tue, 24 Nov 2026 14:31:31 +0100 Subject: [PATCH] Open an independent FXML component in a managed view context --- fx/README.md | 2 ++ .../java/de/neitzel/fx/view/ViewLoader.java | 26 +++++++++++++++++++ .../de/neitzel/fx/view/ManagedViewTest.java | 20 ++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/fx/README.md b/fx/README.md index acf22ce..0c9767c 100644 --- a/fx/README.md +++ b/fx/README.md @@ -48,6 +48,8 @@ The launch class is `de.neitzel.fx.example.Main`. `Main` does not extend `Applic Depend on `de.neitzel.lib:fx`. That module depends on `core`, `injection` and on JavaFX base, controls, graphics, fxml, and web. Application types the container constructs use `jakarta.inject.Singleton` or `jakarta.inject.Named`. +Use `ViewLoader.loadComponent(type, model, context, models)` to open a reusable Component directly from its own FXML. It returns a managed `LoadedView` with the same Component, ViewModel selection and binding lifetime as an embedded instance. + ## Dependencies These are the versions configured by this source snapshot, not a promise of compatibility with every newer release: diff --git a/fx/src/main/java/de/neitzel/fx/view/ViewLoader.java b/fx/src/main/java/de/neitzel/fx/view/ViewLoader.java index 6ada1fb..8f2d174 100644 --- a/fx/src/main/java/de/neitzel/fx/view/ViewLoader.java +++ b/fx/src/main/java/de/neitzel/fx/view/ViewLoader.java @@ -94,6 +94,32 @@ public final class ViewLoader { return load(fxml, model, context, models.openView()); } + /** + * Opens a Component's own fx:root document with the supplied Model. The Component selects + * its ViewModel in the same context as its nested Views. Closing the result detaches its + * bindings and releases its controller and children. + * + * @param type the injected Component class with a sibling ClassName.fxml + * @param model the initial non-null Model + * @param context the application container + * @param models the owner of the View and its Model identity + * @return the initialized Component and its effective ViewModel + */ + public static LoadedView loadComponent(Class type, Object model, + ApplicationContext context, ViewModelContext models) { + java.util.Objects.requireNonNull(model, "Component Model"); + ViewLifetime lifetime = models.openView(); + try { + Parent root = type.cast(Components.fresh(type, context, lifetime)); + Components.setModel(root, model, lifetime); + Object viewModel = ViewModels.of(models, model, ViewModels.namedOn(type)); + return new LoadedView(root, viewModel, lifetime); + } catch (RuntimeException | Error failure) { + lifetime.rollback(failure); + throw failure; + } + } + private static LoadedView load(URL fxml, Object model, ApplicationContext context, ViewLifetime lifetime) { LoadSession session = LoadSession.nest(); try { diff --git a/fx/src/test/java/de/neitzel/fx/view/ManagedViewTest.java b/fx/src/test/java/de/neitzel/fx/view/ManagedViewTest.java index c596f92..ea5ae58 100644 --- a/fx/src/test/java/de/neitzel/fx/view/ManagedViewTest.java +++ b/fx/src/test/java/de/neitzel/fx/view/ManagedViewTest.java @@ -93,6 +93,26 @@ class ManagedViewTest { }); } + @Test + void opensAComponentDirectlyAndDetachesOnlyItsOwnBindings() throws Exception { + FxThread.onFx(() -> { + var model = new Address("Main"); + var services = new ApplicationContext("de.neitzel.fx.view.component"); + var type = de.neitzel.fx.view.component.AddressForm.class; + try (var models = new ViewModelContext()) { + var first = ViewLoader.loadComponent(type, model, services, models); + var second = ViewLoader.loadComponent(type, model, services, models); + assertSame(first.getViewModel(), second.getViewModel()); + var input = field(first.getRoot(), "#streetField"); + input.setText("Shared"); + assertEquals("Shared", field(second.getRoot(), "#streetField").getText()); + first.close(); + input.setText("Detached"); + assertEquals("Shared", model.getStreet()); + } + }); + } + @Test void removingARepeatChildDetachesItsBindings() throws Exception { FxThread.onFx(() -> {