Run Designer previews with application-owned DI adapters

This commit is contained in:
2026-11-26 16:01:19 +01:00
parent e49d586916
commit 6126967fdc
56 changed files with 2131 additions and 50 deletions
@@ -0,0 +1,45 @@
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>de.neitzel.fxdesigner.fixtures</groupId>
<artifactId>spring-di</artifactId>
<version>1</version>
</parent>
<artifactId>spring-di-app</artifactId>
<dependencies>
<dependency>
<groupId>de.neitzel.lib</groupId>
<artifactId>fx</artifactId>
<version>${neitzellib.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<version>${jakarta-annotation.version}</version>
</dependency>
<dependency>
<groupId>jakarta.inject</groupId>
<artifactId>jakarta.inject-api</artifactId>
<version>${jakarta-inject.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>${javafx-maven-plugin.version}</version>
<configuration>
<mainClass>fixture.spring.app/fixture.spring.app.SpringBook</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,40 @@
package fixture.spring.app;
import jakarta.annotation.PreDestroy;
import java.util.ArrayList;
import java.util.List;
/** An in-memory service shared by all View objects in one Spring context. */
public final class FixtureService {
private final String label;
private final List<Object> live = new ArrayList<>();
private int released;
private int atShutdown = -1;
private int shutdowns;
public FixtureService(String label) { this.label = label; }
public String label() { return label; }
public void created(Object object) { live.add(object); }
public void release(Object object) {
if (!live.remove(object)) throw new IllegalStateException("Unknown View object: " + object);
released++;
}
public int liveCount() { return live.size(); }
public int releasedCount() { return released; }
public int liveAtShutdown() { return atShutdown; }
public int shutdownCount() { return shutdowns; }
@PreDestroy
public void shutdown() {
atShutdown = live.size();
shutdowns++;
System.out.println("SPRING CLOSED " + label + " live=" + atShutdown + " released=" + released);
}
}
@@ -0,0 +1,35 @@
package fixture.spring.app;
import de.neitzel.fx.view.ViewLoader;
import de.neitzel.fx.view.ViewModelContext;
import fixture.spring.model.Book;
import fixture.spring.ui.BookController;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
public final class SpringBook extends Application {
private SpringBookContext context;
private final ViewModelContext models = new ViewModelContext();
public static void main(String[] args) { launch(args); }
@Override
public void start(Stage stage) {
context = SpringBookContext.open("Application service");
var view = ViewLoader.loadWithDependencies(BookController.class.getResource("Book.fxml"),
Book.sample("Spring contact book"), context.dependencies(), models);
stage.setTitle("Spring contact book");
stage.setScene(new Scene(view.getRoot(), 660, 500));
stage.show();
}
@Override
public void stop() {
try {
models.close();
} finally {
if (context != null) context.close();
}
}
}
@@ -0,0 +1,46 @@
package fixture.spring.app;
import de.neitzel.fx.view.Converter;
import fixture.spring.ui.BookController;
import fixture.spring.ui.EntryCard;
import fixture.spring.ui.EntryPanel;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public record SpringBookContext(AnnotationConfigApplicationContext spring, SpringViews dependencies)
implements AutoCloseable {
private static final Map<Class<?>, String> VIEW_BEANS = Map.of(
BookController.class, "bookController", EntryCard.class, "entryCard", EntryPanel.class, "entryPanel");
public static SpringBookContext open(String serviceLabel) {
SpringRuntime.requireNoNeitzelDi(serviceLabel);
var spring = new AnnotationConfigApplicationContext();
try {
spring.registerBean(FixtureService.class, () -> new FixtureService(serviceLabel));
spring.registerBean(NameConverter.class);
VIEW_BEANS.forEach((type, name) -> spring.registerBean(name, type,
bean -> bean.setScope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)));
spring.refresh();
return new SpringBookContext(spring,
new SpringViews(spring.getBeanFactory(), VIEW_BEANS, Set.of(EntryCard.class, EntryPanel.class)));
} catch (RuntimeException | Error failure) {
spring.close();
throw failure;
}
}
public FixtureService service() { return spring.getBean(FixtureService.class); }
@Override
public void close() { spring.close(); }
public static final class NameConverter implements Converter {
@Override
public Object toControlValue(Object value) { return "[" + value + "]"; }
@Override
public Object toAttributeValue(Object value) { return value; }
}
}
@@ -0,0 +1,35 @@
package fixture.spring.app;
import java.io.File;
import java.nio.file.Path;
import java.util.List;
import java.util.regex.Pattern;
/** Verifies the artifact isolation in both the fixture application and its preview process. */
final class SpringRuntime {
private static final Pattern NEITZEL_DI = Pattern.compile("(?:fx-injection|injection|reflections)-.*\\.jar");
private SpringRuntime() {}
static void requireNoNeitzelDi(String label) {
var loader = Thread.currentThread().getContextClassLoader();
for (String type : List.of("de.neitzel.injection.ApplicationContext", "org.reflections.Reflections")) {
try {
Class.forName(type, false, loader);
throw new IllegalStateException("Unexpected runtime type " + type);
} catch (ClassNotFoundException expected) {
// The fixture must be usable without either implementation.
}
}
for (String property : List.of("java.class.path", "jdk.module.path")) {
String path = System.getProperty(property, "");
if (path.isEmpty()) continue;
for (String entry : path.split(Pattern.quote(File.pathSeparator))) {
if (NEITZEL_DI.matcher(Path.of(entry).getFileName().toString()).matches())
throw new IllegalStateException("Unexpected DI artifact on " + property + ": " + entry);
}
}
var mode = SpringRuntime.class.getModule().isNamed() ? "module-path" : "classpath";
System.out.println("SPRING FX ISOLATION " + mode + " " + label);
}
}
@@ -0,0 +1,36 @@
package fixture.spring.app;
import de.neitzel.fx.view.ViewDependencies;
import de.neitzel.fx.view.ViewObject;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
public record SpringViews(ConfigurableBeanFactory beans, Map<Class<?>, String> viewBeans,
Set<Class<?>> components) implements ViewDependencies {
public SpringViews {
viewBeans = Map.copyOf(viewBeans);
components = Set.copyOf(components);
}
@Override
public boolean isComponent(Class<?> type) {
return components.contains(type);
}
@Override
public <T> Optional<T> findService(Class<T> type) {
return Optional.ofNullable(beans.getBeanProvider(type).getIfAvailable());
}
@Override
public <T> ViewObject<T> createViewObject(Class<T> type) {
String name = viewBeans.get(type);
if (name == null || !beans.isPrototype(name)) {
throw new IllegalArgumentException("Expected a prototype bean for " + type.getName());
}
T instance = beans.getBean(name, type);
return new ViewObject<>(instance, () -> beans.destroyBean(name, instance));
}
}
@@ -0,0 +1,33 @@
package fixture.spring.model;
import java.util.ArrayList;
import java.util.List;
public final class Book {
private String name;
private Entry featured;
private List<Entry> entries = new ArrayList<>();
public static Book sample(String name) {
var book = new Book();
book.setName(name);
var featured = new Entry("Featured");
featured.setDetail(new Entry("Nested entry"));
book.setFeatured(featured);
book.getEntries().add(new Entry("First entry"));
book.getEntries().add(new Entry("Second entry"));
return book;
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Entry getFeatured() { return featured; }
public void setFeatured(Entry featured) { this.featured = featured; }
public List<Entry> getEntries() { return entries; }
public void setEntries(List<Entry> entries) { this.entries = entries; }
}
@@ -0,0 +1,18 @@
package fixture.spring.model;
public final class Entry {
private String name;
private Entry detail;
public Entry(String name) {
this.name = name;
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Entry getDetail() { return detail; }
public void setDetail(Entry detail) { this.detail = detail; }
}
@@ -0,0 +1,31 @@
package fixture.spring.ui;
import fixture.spring.app.FixtureService;
import jakarta.annotation.PreDestroy;
import jakarta.inject.Inject;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
public final class BookController {
private final FixtureService service;
@FXML
private TextField bookName;
@Inject
public BookController(FixtureService service) {
this.service = service;
service.created(this);
}
@FXML
private void initialize() {
bookName.setPromptText(service.label());
bookName.setUserData(this);
}
public FixtureService service() { return service; }
@PreDestroy
public void dispose() { service.release(this); }
}
@@ -0,0 +1,29 @@
package fixture.spring.ui;
import fixture.spring.app.FixtureService;
import fixture.spring.model.Entry;
import jakarta.annotation.PreDestroy;
import jakarta.inject.Inject;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.layout.VBox;
public final class EntryCard extends VBox {
private final FixtureService service;
private final ObjectProperty<Entry> model = new SimpleObjectProperty<>(this, "model");
@Inject
public EntryCard(FixtureService service) {
this.service = service;
service.created(this);
}
public ObjectProperty<Entry> modelProperty() { return model; }
public Entry getModel() { return model.get(); }
public void setModel(Entry entry) { model.set(entry); }
@PreDestroy
public void dispose() { service.release(this); }
}
@@ -0,0 +1,29 @@
package fixture.spring.ui;
import fixture.spring.app.FixtureService;
import fixture.spring.model.Entry;
import jakarta.annotation.PreDestroy;
import jakarta.inject.Inject;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.layout.VBox;
public final class EntryPanel extends VBox {
private final FixtureService service;
private final ObjectProperty<Entry> model = new SimpleObjectProperty<>(this, "model");
@Inject
public EntryPanel(FixtureService service) {
this.service = service;
service.created(this);
}
public ObjectProperty<Entry> modelProperty() { return model; }
public Entry getModel() { return model.get(); }
public void setModel(Entry entry) { model.set(entry); }
@PreDestroy
public void dispose() { service.release(this); }
}
@@ -0,0 +1,16 @@
module fixture.spring.app {
requires fx;
requires jakarta.annotation;
requires jakarta.inject;
requires javafx.controls;
requires javafx.fxml;
requires spring.beans;
requires spring.context;
exports fixture.spring.app;
exports fixture.spring.model;
exports fixture.spring.ui;
opens fixture.spring.model to fx;
opens fixture.spring.ui;
}
@@ -0,0 +1,2 @@
<?import javafx.scene.layout.VBox?>
<VBox/>
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<?neitzel-model fixture.spring.model.Book?>
<?import de.neitzel.fx.view.BindingList?>
<?import de.neitzel.fx.view.Column?>
<?import de.neitzel.fx.view.DirectBinding?>
<?import de.neitzel.fx.view.Repeat?>
<?import de.neitzel.fx.view.Table?>
<?import fixture.spring.ui.EntryPanel?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.VBox?>
<VBox xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="fixture.spring.ui.BookController" spacing="8">
<fx:define>
<BindingList>
<DirectBinding control="bookName" property="text" path="name" direction="both"/>
<DirectBinding control="convertedName" property="text" path="name" direction="view"
converter="fixture.spring.app.SpringBookContext$NameConverter"/>
<DirectBinding control="featured" property="model" path="featured" direction="both"/>
<Repeat pane="entries" component="fixture.spring.ui.EntryCard" collection="entries"/>
</BindingList>
</fx:define>
<TextField fx:id="bookName"/>
<Label fx:id="convertedName"/>
<EntryPanel fx:id="featured"/>
<VBox fx:id="entries"/>
<Table fx:id="entriesTable" collection="entries" row="fixture.spring.ui.EntryCard" prefHeight="120">
<Column header="Entry" attribute="name"/>
</Table>
</VBox>
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<?neitzel-model fixture.spring.model.Entry?>
<?import de.neitzel.fx.view.BindingList?>
<?import de.neitzel.fx.view.DirectBinding?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.VBox?>
<fx:root type="javafx.scene.layout.VBox" xmlns:fx="http://javafx.com/fxml/1">
<fx:define>
<BindingList>
<DirectBinding control="entryName" property="text" path="name" direction="both"/>
</BindingList>
</fx:define>
<TextField fx:id="entryName"/>
</fx:root>
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<?neitzel-model fixture.spring.model.Entry?>
<?import de.neitzel.fx.view.BindingList?>
<?import de.neitzel.fx.view.DirectBinding?>
<?import fixture.spring.ui.EntryCard?>
<?import javafx.scene.layout.VBox?>
<fx:root type="javafx.scene.layout.VBox" xmlns:fx="http://javafx.com/fxml/1">
<fx:define>
<BindingList>
<DirectBinding control="nested" property="model" path="detail" direction="both"/>
</BindingList>
</fx:define>
<EntryCard fx:id="nested"/>
</fx:root>
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import fixture.spring.ui.EntryCard?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.VBox?>
<VBox xmlns:fx="http://javafx.com/fxml/1" fx:controller="fixture.spring.ui.BookController">
<TextField fx:id="bookName"/>
<EntryCard unknownProperty="fails after acquisition"/>
</VBox>