Separate JavaFX loading from the Neitzel injection container
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
package de.neitzel.fx.view;
|
||||
|
||||
import de.neitzel.injection.ApplicationContext;
|
||||
import javafx.beans.DefaultProperty;
|
||||
import javafx.scene.Parent;
|
||||
import lombok.Getter;
|
||||
@@ -42,7 +41,7 @@ public class BindingList {
|
||||
* @param context the container that creates a named Converter
|
||||
*/
|
||||
void apply(Map<String, Object> namespace, Object viewModel, Object controller, Parent root,
|
||||
ApplicationContext context, ViewLifetime lifetime) {
|
||||
ViewInstances context, ViewLifetime lifetime) {
|
||||
for (Object element : elements) {
|
||||
if (element instanceof DirectBinding directBinding) {
|
||||
DirectBindingApplier.apply(namespace, viewModel, directBinding, context, lifetime);
|
||||
|
||||
@@ -48,9 +48,6 @@ final class CommandBindingApplier {
|
||||
|
||||
static void apply(Map<String, Object> namespace, Object viewModel, CommandBinding binding,
|
||||
ViewLifetime lifetime) {
|
||||
if (!lifetime.managed()) {
|
||||
throw invalid(namespace, binding, "CommandBinding requires a ViewModelContext");
|
||||
}
|
||||
var target = target(namespace, binding);
|
||||
var command = command(namespace, binding, viewModel);
|
||||
try {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package de.neitzel.fx.view;
|
||||
|
||||
import de.neitzel.injection.ApplicationContext;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
@@ -37,35 +36,9 @@ final class Components {
|
||||
private Components() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a builder for a type the container knows.
|
||||
* <p>
|
||||
* An unknown type, such as a JavaFX control, makes {@link ApplicationContext#getComponent}
|
||||
* throw {@link IllegalArgumentException}. This method returns null so FXML constructs that
|
||||
* type itself. A Component that extends a JavaFX class is also left to FXML when the document
|
||||
* names that JavaFX class: the container would otherwise hand back the Component. A failure
|
||||
* while creating a known Component is not caught. The first Component of a class in one load
|
||||
* is the cached singleton. A second Component of that class is a new injected instance, so the
|
||||
* document can show two of them.
|
||||
*
|
||||
* @param type the type FXML is about to construct
|
||||
* @param context the container that creates Components
|
||||
* @return a builder for a Component, or {@code null} when the type is not a Component
|
||||
*/
|
||||
static Builder<?> builder(Class<?> type, ApplicationContext context, ViewLifetime owner) {
|
||||
Object component;
|
||||
try {
|
||||
component = owner.managed() ? context.createComponent(type) : context.getComponent(type);
|
||||
} catch (IllegalArgumentException notAComponent) {
|
||||
return null;
|
||||
}
|
||||
if (component.getClass() != type) {
|
||||
return null;
|
||||
}
|
||||
if (!owner.managed() && !LoadSession.claimFirst(type)) {
|
||||
component = context.createComponent(type.asSubclass(Object.class));
|
||||
}
|
||||
return new ComponentBuilder(component, context, owner.child());
|
||||
/** Returns a Component builder or leaves the requested type to JavaFX. */
|
||||
static Builder<?> builder(Class<?> type, ViewInstances context, ViewLifetime owner) {
|
||||
return context.isComponent(type) ? new ComponentBuilder(type, context, owner.child()) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,10 +47,10 @@ final class Components {
|
||||
*/
|
||||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
private static final class ComponentBuilder extends AbstractMap<String, Object> implements Builder<Object> {
|
||||
/** The injected instance. */
|
||||
private final Object component;
|
||||
/** The exact Component class declared by FXML. */
|
||||
private final Class<?> type;
|
||||
/** The context used to load its document. */
|
||||
private final ApplicationContext context;
|
||||
private final ViewInstances context;
|
||||
private final ViewLifetime lifetime;
|
||||
/** Values already resolved by the containing FXMLLoader. */
|
||||
private final Map<String, Object> values = new LinkedHashMap<>();
|
||||
@@ -108,7 +81,7 @@ final class Components {
|
||||
*/
|
||||
@Override
|
||||
public Object build() {
|
||||
Object result = load(component, context, lifetime);
|
||||
Object result = load(context.createComponent(type, lifetime), context, lifetime);
|
||||
if (!values.isEmpty()) {
|
||||
// A second fx:root load uses the public FXMLLoader API for bean setters,
|
||||
// enum/numeric coercion and diagnostics, without depending on BeanAdapter
|
||||
@@ -137,19 +110,9 @@ final class Components {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new Component and loads its document.
|
||||
* <p>
|
||||
* The instance is not the cached singleton. Its dependencies still come from
|
||||
* {@link ApplicationContext#getComponent}. Bindings wait until {@link #setModel} runs.
|
||||
*
|
||||
* @param type the Component class
|
||||
* @param context the container that creates the instance and its dependencies
|
||||
* @return the loaded Component
|
||||
*/
|
||||
static Object fresh(Class<?> type, ApplicationContext context, ViewLifetime lifetime) {
|
||||
Object component = context.createComponent(type.asSubclass(Object.class));
|
||||
return load(component, context, lifetime);
|
||||
/** Creates a fresh Component and loads its document before setting its Model. */
|
||||
static Object fresh(Class<?> type, ViewInstances context, ViewLifetime lifetime) {
|
||||
return load(context.component(type, lifetime), context, lifetime);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -161,10 +124,6 @@ final class Components {
|
||||
* @param model the Model instance
|
||||
*/
|
||||
static void setModel(Object component, Object model, ViewLifetime lifetime) {
|
||||
if (!lifetime.managed()) {
|
||||
modelProperty(component).setValue(model);
|
||||
return;
|
||||
}
|
||||
LoadSession session = LoadSession.nest();
|
||||
try {
|
||||
modelProperty(component).setValue(model);
|
||||
@@ -181,8 +140,7 @@ final class Components {
|
||||
* @param context the container that creates nested Components
|
||||
* @return the loaded Component
|
||||
*/
|
||||
private static Object load(Object component, ApplicationContext context, ViewLifetime lifetime) {
|
||||
lifetime.ownController(component);
|
||||
private static Object load(Object component, ViewInstances context, ViewLifetime lifetime) {
|
||||
Class<?> type = component.getClass();
|
||||
String document = type.getSimpleName() + ".fxml";
|
||||
URL fxml = type.getResource(document);
|
||||
@@ -195,11 +153,11 @@ final class Components {
|
||||
FxmlHandlers.observe(loader, lifetime);
|
||||
loader.setRoot(component);
|
||||
loader.setController(component);
|
||||
loader.setControllerFactory(nestedType -> ViewLoader.createController(context, nestedType, lifetime));
|
||||
loader.setControllerFactory(nestedType -> context.controller(nestedType, lifetime));
|
||||
loader.setBuilderFactory(nestedType -> builder(nestedType, context, lifetime));
|
||||
loader.load();
|
||||
nested.checkFailure();
|
||||
if (lifetime.managed() && component instanceof Node node) {
|
||||
if (component instanceof Node node) {
|
||||
node.getProperties().put(Components.class, Boolean.TRUE);
|
||||
lifetime.own(() -> node.getProperties().remove(Components.class));
|
||||
}
|
||||
@@ -221,7 +179,7 @@ final class Components {
|
||||
* @param context the container that creates a named Converter
|
||||
*/
|
||||
private static void watchModel(Object component, List<BindingList> lists, Map<String, Object> namespace,
|
||||
ApplicationContext context, ViewLifetime lifetime) {
|
||||
ViewInstances context, ViewLifetime lifetime) {
|
||||
Property<Object> model = modelProperty(component);
|
||||
boolean[] applied = {false};
|
||||
lifetime.listen(model, (observable, oldValue, newValue) -> {
|
||||
@@ -239,10 +197,10 @@ final class Components {
|
||||
for (BindingList list : lists) {
|
||||
list.apply(namespace, viewModel, component, root, context, lifetime);
|
||||
}
|
||||
if (lifetime.managed()) Table.openAll(root, viewModel, context, lifetime);
|
||||
Table.openAll(root, viewModel, context, lifetime);
|
||||
} catch (RuntimeException | Error failure) {
|
||||
lifetime.rollback(failure);
|
||||
if (!lifetime.managed() || !LoadSession.recordFailure(failure)) {
|
||||
if (!LoadSession.recordFailure(failure)) {
|
||||
throw failure;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package de.neitzel.fx.view;
|
||||
|
||||
import de.neitzel.injection.ApplicationContext;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
@@ -33,7 +32,7 @@ final class DirectBindingApplier {
|
||||
* @param context the container that creates a named Converter
|
||||
*/
|
||||
static void apply(Map<String, Object> namespace, Object viewModel, DirectBinding binding,
|
||||
ApplicationContext context, ViewLifetime lifetime) {
|
||||
ViewInstances context, ViewLifetime lifetime) {
|
||||
Object control = namespace.get(binding.getControl());
|
||||
if (control == null) {
|
||||
throw new IllegalArgumentException("No control \"" + binding.getControl() + "\"");
|
||||
@@ -82,7 +81,7 @@ final class DirectBindingApplier {
|
||||
|
||||
/** Connects a named observable to the View when its accessor exposes no writable property. */
|
||||
private static void alignReadOnly(Map<String, Object> namespace, Property<?> control, Resolved resolved,
|
||||
DirectBinding binding, ApplicationContext context, ViewLifetime lifetime) {
|
||||
DirectBinding binding, ViewInstances context, ViewLifetime lifetime) {
|
||||
if (!"view".equals(binding.getDirection())) {
|
||||
throw new IllegalArgumentException("Read-only source for path \"" + binding.getPath()
|
||||
+ "\" requires direction=\"view\" on " + binding.getControl() + "." + binding.getProperty()
|
||||
@@ -292,10 +291,10 @@ final class DirectBindingApplier {
|
||||
* @param className the Converter class name
|
||||
* @return the Converter
|
||||
*/
|
||||
private static Converter converter(ApplicationContext context, String className) {
|
||||
private static Converter converter(ViewInstances context, String className) {
|
||||
try {
|
||||
Class<?> type = Class.forName(className);
|
||||
Object instance = context.getComponent(type.asSubclass(Object.class));
|
||||
Object instance = context.requiredService(type);
|
||||
if (!(instance instanceof Converter created)) {
|
||||
throw new IllegalArgumentException("Not a converter: " + className);
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ final class FxmlHandlers implements LoadListener {
|
||||
}
|
||||
|
||||
static void observe(FXMLLoader loader, ViewLifetime lifetime) {
|
||||
if (lifetime.managed()) loader.setLoadListener(new FxmlHandlers(lifetime));
|
||||
loader.setLoadListener(new FxmlHandlers(lifetime));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -3,9 +3,7 @@ package de.neitzel.fx.view;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Deque;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Holds the binding lists instantiated while one View is loading.
|
||||
@@ -27,11 +25,6 @@ final class LoadSession {
|
||||
*/
|
||||
private final List<BindingList> bindingLists = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Component classes already built during the outermost load.
|
||||
*/
|
||||
private final Set<Class<?>> built = new HashSet<>();
|
||||
|
||||
private Throwable bindingFailure;
|
||||
|
||||
// JavaFX reports exceptions from a property listener to the thread's uncaught handler.
|
||||
@@ -105,23 +98,6 @@ final class LoadSession {
|
||||
stack.peek().bindingLists.add(bindingList);
|
||||
}
|
||||
|
||||
/**
|
||||
* Records the first Component of {@code type} built during this load.
|
||||
* <p>
|
||||
* Nested documents share the outermost session, so a second Component of the same class
|
||||
* anywhere in the load is not the first.
|
||||
*
|
||||
* @param type the Component class
|
||||
* @return {@code true} when this is the first time that class is built during the load
|
||||
*/
|
||||
static boolean claimFirst(Class<?> type) {
|
||||
Deque<LoadSession> stack = STACK.get();
|
||||
if (stack == null || stack.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
return stack.peekLast().built.add(type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the lists registered during this session, in document order.
|
||||
*
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package de.neitzel.fx.view;
|
||||
|
||||
import de.neitzel.injection.ApplicationContext;
|
||||
import javafx.beans.property.Property;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.scene.Node;
|
||||
@@ -24,10 +23,10 @@ final class RepeatApplier {
|
||||
/**
|
||||
* Fills the named pane with one new Component for each element, in list order.
|
||||
* <p>
|
||||
* Each Component is a new injected instance. The cached singleton of that class is left
|
||||
* in place. The child's model property is the element. Adding or removing an element
|
||||
* adds or removes the matching child. The list is the one the entity holds, so a change
|
||||
* on either side is visible in the pane.
|
||||
* The adapter creates a fresh Component for each occurrence. Its model property is the
|
||||
* element. Adding an element loads a child. Removing one releases that child's View.
|
||||
* The list is the one the entity holds, so a change on either side is visible in the pane.
|
||||
* The parent's View lifetime also owns the remaining children.
|
||||
*
|
||||
* @param namespace objects addressed by {@code fx:id}
|
||||
* @param viewModel the ViewModel for this View, or a replacement that exposes the collection by name
|
||||
@@ -35,7 +34,7 @@ final class RepeatApplier {
|
||||
* @param context the container that creates each Component
|
||||
*/
|
||||
static void apply(Map<String, Object> namespace, Object viewModel, Repeat repeat,
|
||||
ApplicationContext context, ViewLifetime lifetime) {
|
||||
ViewInstances context, ViewLifetime lifetime) {
|
||||
Object named = namespace.get(repeat.getPane());
|
||||
if (!(named instanceof Pane pane)) {
|
||||
throw new IllegalArgumentException("No pane \"" + repeat.getPane() + "\"");
|
||||
@@ -82,7 +81,7 @@ final class RepeatApplier {
|
||||
* @param values the collection
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private static void listen(Pane pane, Class<?> type, ApplicationContext context, ObservableList<?> values,
|
||||
private static void listen(Pane pane, Class<?> type, ViewInstances context, ObservableList<?> values,
|
||||
ViewLifetime lifetime, int offset, List<RepeatedChild> children) {
|
||||
ObservableList<Object> elements = (ObservableList<Object>) values;
|
||||
lifetime.listen(elements, change -> {
|
||||
@@ -113,7 +112,7 @@ final class RepeatApplier {
|
||||
* @param element the Model instance
|
||||
* @return the loaded Component
|
||||
*/
|
||||
private static RepeatedChild child(Class<?> type, ApplicationContext context, Object element,
|
||||
private static RepeatedChild child(Class<?> type, ViewInstances context, Object element,
|
||||
ViewLifetime owner) {
|
||||
ViewLifetime lifetime = owner.child();
|
||||
try {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package de.neitzel.fx.view;
|
||||
|
||||
import de.neitzel.injection.ApplicationContext;
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
@@ -232,7 +231,7 @@ public class Table extends VBox {
|
||||
/**
|
||||
* The container that creates Rows.
|
||||
*/
|
||||
private ApplicationContext context;
|
||||
private ViewInstances context;
|
||||
|
||||
/**
|
||||
* The Row class.
|
||||
@@ -363,7 +362,7 @@ public class Table extends VBox {
|
||||
* @param viewModel the ViewModel for this View
|
||||
* @param context the container that creates each Row
|
||||
*/
|
||||
static void openAll(Parent root, Object viewModel, ApplicationContext context, ViewLifetime lifetime) {
|
||||
static void openAll(Parent root, Object viewModel, ViewInstances context, ViewLifetime lifetime) {
|
||||
walk(root, root, viewModel, context, lifetime);
|
||||
}
|
||||
|
||||
@@ -373,7 +372,7 @@ public class Table extends VBox {
|
||||
* @param viewModel the ViewModel for this View
|
||||
* @param context the container that creates each Row
|
||||
*/
|
||||
void open(Object viewModel, ApplicationContext context, ViewLifetime owner) {
|
||||
void open(Object viewModel, ViewInstances context, ViewLifetime owner) {
|
||||
if (resultMode) {
|
||||
return;
|
||||
}
|
||||
@@ -1339,7 +1338,7 @@ public class Table extends VBox {
|
||||
node.addEventFilter(MouseEvent.MOUSE_CLICKED, select);
|
||||
rowLifetime.own(() -> node.removeEventFilter(MouseEvent.MOUSE_CLICKED, select));
|
||||
watchEdits(node, rowLifetime);
|
||||
if (rowLifetime.managed()) rowLifetimes.put(element, rowLifetime);
|
||||
rowLifetimes.put(element, rowLifetime);
|
||||
return node;
|
||||
} catch (RuntimeException | Error failure) {
|
||||
rowLifetime.rollback(failure);
|
||||
@@ -1566,7 +1565,7 @@ public class Table extends VBox {
|
||||
* @param viewModel the ViewModel for this View
|
||||
* @param context the container that creates each Row
|
||||
*/
|
||||
private static void walk(Node node, Parent root, Object viewModel, ApplicationContext context,
|
||||
private static void walk(Node node, Parent root, Object viewModel, ViewInstances context,
|
||||
ViewLifetime lifetime) {
|
||||
if (node != root && Components.ownsBindings(node)) return;
|
||||
if (node instanceof Table table) {
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package de.neitzel.fx.view;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/** Supplies a View's objects from the application's dependency injection configuration. */
|
||||
public interface ViewDependencies {
|
||||
|
||||
/** Checks whether the exact FXML type is a Component, without creating an instance. */
|
||||
boolean isComponent(Class<?> type);
|
||||
|
||||
/**
|
||||
* Looks up an application-owned service such as a Converter or Localizer.
|
||||
* Report lookup and creation failures as exceptions. An empty result means the service is absent.
|
||||
*/
|
||||
<T> Optional<T> findService(Class<T> type);
|
||||
|
||||
/**
|
||||
* Creates a fresh Controller or Component and supplies its release action.
|
||||
* The adapter owns any partial allocations until it returns the object.
|
||||
*/
|
||||
<T> ViewObject<T> createViewObject(Class<T> type);
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package de.neitzel.fx.view;
|
||||
|
||||
import de.neitzel.fx.i18n.Localizer;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
/** Instance selection shared by the FXML factories and the binding appliers. */
|
||||
record ViewInstances(ViewDependencies dependencies) {
|
||||
|
||||
ViewInstances {
|
||||
Objects.requireNonNull(dependencies, "View dependencies");
|
||||
}
|
||||
|
||||
Object controller(Class<?> type, ViewLifetime lifetime) {
|
||||
return create(type, lifetime);
|
||||
}
|
||||
|
||||
boolean isComponent(Class<?> type) {
|
||||
return dependencies.isComponent(type);
|
||||
}
|
||||
|
||||
Object component(Class<?> type, ViewLifetime lifetime) {
|
||||
if (!dependencies.isComponent(type)) {
|
||||
throw new IllegalArgumentException("No Component registered for " + type.getName());
|
||||
}
|
||||
return createComponent(type, lifetime);
|
||||
}
|
||||
|
||||
Object createComponent(Class<?> type, ViewLifetime lifetime) {
|
||||
Object instance = create(type, lifetime);
|
||||
if (instance.getClass() != type) {
|
||||
throw new IllegalArgumentException("Expected exact Component type " + type.getName()
|
||||
+ ", got " + instance.getClass().getName());
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
private Object create(Class<?> type, ViewLifetime lifetime) {
|
||||
try {
|
||||
ViewObject<?> view = dependencies.createViewObject(type);
|
||||
// Each instance owns a distinct cleanup entry even if actions are shared.
|
||||
lifetime.own(() -> view.release().run());
|
||||
return type.cast(view.instance());
|
||||
} catch (RuntimeException failure) {
|
||||
throw new IllegalStateException("Could not create View object " + type.getName(), failure);
|
||||
}
|
||||
}
|
||||
|
||||
Object requiredService(Class<?> type) {
|
||||
return service(type)
|
||||
.orElseThrow(() -> new IllegalArgumentException("No View service for " + type.getName()));
|
||||
}
|
||||
|
||||
Localizer localizer() {
|
||||
return service(Localizer.class).orElse(null);
|
||||
}
|
||||
|
||||
private <T> Optional<T> service(Class<T> type) {
|
||||
try {
|
||||
return dependencies.findService(type);
|
||||
} catch (RuntimeException failure) {
|
||||
throw new IllegalStateException("Could not resolve View service " + type.getName(), failure);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,31 +22,11 @@ final class ViewLifetime implements AutoCloseable {
|
||||
return models;
|
||||
}
|
||||
|
||||
boolean managed() {
|
||||
return models != ViewModels.LEGACY;
|
||||
}
|
||||
|
||||
void own(Runnable removal) {
|
||||
if (managed()) {
|
||||
if (closed) {
|
||||
removal.run();
|
||||
} else {
|
||||
cleanup.addLast(removal);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ownController(Object controller) {
|
||||
if (controller instanceof AutoCloseable closeable) {
|
||||
own(() -> {
|
||||
try {
|
||||
closeable.close();
|
||||
} catch (RuntimeException failure) {
|
||||
throw failure;
|
||||
} catch (Exception failure) {
|
||||
throw new IllegalStateException("Could not close " + controller.getClass().getName(), failure);
|
||||
}
|
||||
});
|
||||
if (closed) {
|
||||
removal.run();
|
||||
} else {
|
||||
cleanup.addLast(removal);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,12 +2,12 @@ package de.neitzel.fx.view;
|
||||
|
||||
import de.neitzel.fx.i18n.Localizer;
|
||||
import de.neitzel.fx.i18n.Messages;
|
||||
import de.neitzel.injection.ApplicationContext;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Parent;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Loads a View for one Model instance.
|
||||
@@ -21,93 +21,35 @@ public final class ViewLoader {
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a View for one Model instance.
|
||||
* <p>
|
||||
* Reuses the ViewModel already built for {@code model}, or builds one, then loads {@code fxml}
|
||||
* and applies binding elements declared in {@code fx:define}. A Computed binding writes a
|
||||
* Controller method result to a control and does not write the Model. Direction {@code both}
|
||||
* keeps the control and the Attribute
|
||||
* aligned, so editing the control writes the Attribute immediately. Direction {@code view}
|
||||
* shows the Attribute and follows later property changes without writing the Attribute back.
|
||||
* Direction {@code model} shows the current value, writes the Attribute when the control
|
||||
* changes, and does not push later property changes to the control. A Path of one name is an
|
||||
* Attribute of this View's ViewModel. Further names step into a nested ViewModel, and the
|
||||
* direction applies to the last name. When a Complex attribute along the Path is null, the
|
||||
* control and the Model keep their current values. A collection of simple values is one shared
|
||||
* list: the Direct binding sets the control property to that
|
||||
* {@link javafx.collections.ObservableList}, so an add or remove is visible on both sides.
|
||||
* A path that is not a read-write Attribute fails the load. The context creates a Controller
|
||||
* the document names, and injects that Controller's services. When the document names no
|
||||
* Controller, the library supplies one. A Controller that implements {@link HasViewModel}
|
||||
* receives the ViewModel after construction and before bindings. {@link ViewModelClass} on that
|
||||
* Controller names the ViewModel class. The library constructs it with the Model instance
|
||||
* before load. With no annotation, the library builds the generic ViewModel. The first View of a
|
||||
* Model instance fixes that class. A later View that names a different class fails the load,
|
||||
* and the earlier View stays as it was. A later View that names none reuses the ViewModel.
|
||||
* The context creates a
|
||||
* Converter when a Direct binding names one. A converter that cannot produce a value
|
||||
* keeps the previous value and records one error Message on the ViewModel that owns the last
|
||||
* Attribute, with that Attribute's name as the source. A parent carries a copy with the source
|
||||
* extended. It does not fail the load.
|
||||
* A parent document names a Component class as an element. The context creates that Component
|
||||
* and injects its services. A second element of the same class is another injected instance,
|
||||
* and the cached singleton stays the first one. The Component document {@code ClassName.fxml},
|
||||
* in the same package, is loaded with the Component as {@code fx:root} and as the Controller.
|
||||
* That document's bindings are not applied during this load. They run when the Component's
|
||||
* model property becomes a non-null Model. {@link ViewModelClass} on the Component names the
|
||||
* ViewModel class for that Model. A Component that implements {@link HasViewModel} receives
|
||||
* that ViewModel after the property is set and before those bindings run.
|
||||
* A {@link Repeat} in the same list names a pane, a Component class, and a collection
|
||||
* Attribute. One new Component child is placed in that pane for each Model instance, in list
|
||||
* order. Adding or removing an element updates that pane. Those instances are created with
|
||||
* {@link de.neitzel.injection.ApplicationContext#createComponent} so the cached singleton of
|
||||
* that class stays in place. A collection of Model instances is an
|
||||
* {@link javafx.collections.ObservableList} of those instances, shared with the entity.
|
||||
* Selection is a separate Attribute of the parent Model. Null means nothing is selected.
|
||||
* A Direct binding can tie a control property to that Attribute. The repeated Component does
|
||||
* not store the selection.
|
||||
* Loading does not open a popup or fill a status bar. A Direct binding whose path is
|
||||
* {@code latestMessage} shows the latest Message text. {@link MessagePopup#showNewMessages}
|
||||
* shows each newly added Message in a dialog.
|
||||
*
|
||||
* @param fxml the FXML document
|
||||
* @param model the Model instance
|
||||
* @param context the application context
|
||||
* @return the loaded View
|
||||
* @throws IllegalArgumentException when a Direct binding names a path that is not a read-write Attribute
|
||||
*/
|
||||
public static LoadedView load(URL fxml, Object model, ApplicationContext context) {
|
||||
return load(fxml, model, context, new ViewLifetime(ViewModels.LEGACY));
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a View with a fresh Controller and Components in an explicitly owned Model context.
|
||||
* Close the result when its UI is removed, or close the Model context to close all its Views.
|
||||
*
|
||||
* Loads FXML with application-supplied dependencies and a managed Model context.
|
||||
* Load and close the View on the JavaFX application thread. The caller owns the dependencies.
|
||||
* @param fxml the FXML document
|
||||
* @param model the Model instance
|
||||
* @param context the container for Controllers and services
|
||||
* @param models the Model context shared by this View and related Views
|
||||
* @param dependencies the adapter for View objects and services
|
||||
* @param models the Model context that owns the View
|
||||
* @return the loaded View
|
||||
*/
|
||||
public static LoadedView load(URL fxml, Object model, ApplicationContext context, ViewModelContext models) {
|
||||
return load(fxml, model, context, models.openView());
|
||||
public static LoadedView loadWithDependencies(URL fxml, Object model,
|
||||
ViewDependencies dependencies, ViewModelContext models) {
|
||||
return load(fxml, model, new ViewInstances(dependencies), 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
|
||||
* Loads a managed Component from its sibling ClassName.fxml on the JavaFX application thread.
|
||||
* @param type the registered Component class
|
||||
* @param model the initial non-null Model
|
||||
* @param context the application container
|
||||
* @param models the owner of the View and its Model identity
|
||||
* @param dependencies the adapter for View objects and services
|
||||
* @param models the Model context that owns the View
|
||||
* @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");
|
||||
public static LoadedView loadComponentWithDependencies(Class<? extends Parent> type, Object model,
|
||||
ViewDependencies dependencies, ViewModelContext models) {
|
||||
return loadComponent(type, model, new ViewInstances(dependencies), models);
|
||||
}
|
||||
|
||||
private static LoadedView loadComponent(Class<? extends Parent> type, Object model,
|
||||
ViewInstances context, ViewModelContext models) {
|
||||
Objects.requireNonNull(model, "Component Model");
|
||||
ViewLifetime lifetime = models.openView();
|
||||
try {
|
||||
Parent root = type.cast(Components.fresh(type, context, lifetime));
|
||||
@@ -116,11 +58,15 @@ public final class ViewLoader {
|
||||
return new LoadedView(root, viewModel, lifetime);
|
||||
} catch (RuntimeException | Error failure) {
|
||||
lifetime.rollback(failure);
|
||||
if (failure instanceof RuntimeException) {
|
||||
throw new IllegalStateException("Could not load Component " + type.getName() + " from "
|
||||
+ type.getSimpleName() + ".fxml: " + failure.getMessage(), failure);
|
||||
}
|
||||
throw failure;
|
||||
}
|
||||
}
|
||||
|
||||
private static LoadedView load(URL fxml, Object model, ApplicationContext context, ViewLifetime lifetime) {
|
||||
private static LoadedView load(URL fxml, Object model, ViewInstances context, ViewLifetime lifetime) {
|
||||
LoadSession session = LoadSession.nest();
|
||||
try {
|
||||
Class<?> controllerType = FxmlControllers.controllerClass(fxml);
|
||||
@@ -128,7 +74,7 @@ public final class ViewLoader {
|
||||
Object viewModel = ViewModels.of(lifetime.models(), model, viewModelClass);
|
||||
FXMLLoader loader = new FXMLLoader(fxml, resources(context, fxml, controllerType));
|
||||
FxmlHandlers.observe(loader, lifetime);
|
||||
loader.setControllerFactory(type -> createController(context, type, lifetime));
|
||||
loader.setControllerFactory(type -> context.controller(type, lifetime));
|
||||
loader.setBuilderFactory(type -> Components.builder(type, context, lifetime));
|
||||
Object loaded = loader.load();
|
||||
if (!(loaded instanceof Parent root)) {
|
||||
@@ -153,27 +99,17 @@ public final class ViewLoader {
|
||||
throw new IllegalStateException("Could not load view " + fxml, failure);
|
||||
} catch (RuntimeException | Error failure) {
|
||||
lifetime.rollback(failure);
|
||||
if (failure instanceof RuntimeException) {
|
||||
throw new IllegalStateException("Could not load view " + fxml + ": " + failure.getMessage(), failure);
|
||||
}
|
||||
throw failure;
|
||||
} finally {
|
||||
LoadSession.unnest();
|
||||
}
|
||||
}
|
||||
|
||||
static Object createController(ApplicationContext context, Class<?> type, ViewLifetime lifetime) {
|
||||
if (!lifetime.managed()) return context.getComponent(type);
|
||||
Object controller = context.createComponent(type);
|
||||
lifetime.ownController(controller);
|
||||
return controller;
|
||||
static Messages resources(ViewInstances context, URL fxml, Class<?> type) {
|
||||
Localizer languages = context.localizer();
|
||||
return languages == null ? null : languages.forView(fxml, type);
|
||||
}
|
||||
|
||||
static Messages resources(ApplicationContext context, URL fxml, Class<?> type) {
|
||||
Localizer languages;
|
||||
try {
|
||||
languages = context.getComponent(Localizer.class);
|
||||
} catch (IllegalArgumentException unregistered) {
|
||||
return null;
|
||||
}
|
||||
return languages.forView(fxml, type);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package de.neitzel.fx.view;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* An instance and the action its View invokes once on release. Use an empty action when the
|
||||
* instance has no cleanup. The action is responsible for any container-specific destruction.
|
||||
*/
|
||||
public record ViewObject<T>(T instance, Runnable release) {
|
||||
|
||||
public ViewObject {
|
||||
Objects.requireNonNull(instance, "View object");
|
||||
Objects.requireNonNull(release, "View object release action");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user