Open an independent FXML component in a managed view context

This commit is contained in:
2026-11-24 14:31:31 +01:00
parent ac992167bc
commit 514cc7fb8e
3 changed files with 48 additions and 0 deletions
+2
View File
@@ -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`. 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 ## Dependencies
These are the versions configured by this source snapshot, not a promise of compatibility with every newer release: These are the versions configured by this source snapshot, not a promise of compatibility with every newer release:
@@ -94,6 +94,32 @@ public final class ViewLoader {
return load(fxml, model, context, models.openView()); 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<? extends Parent> 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) { private static LoadedView load(URL fxml, Object model, ApplicationContext context, ViewLifetime lifetime) {
LoadSession session = LoadSession.nest(); LoadSession session = LoadSession.nest();
try { try {
@@ -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 @Test
void removingARepeatChildDetachesItsBindings() throws Exception { void removingARepeatChildDetachesItsBindings() throws Exception {
FxThread.onFx(() -> { FxThread.onFx(() -> {