Bind named read-only observables toward FXML views

This commit is contained in:
2026-11-23 18:22:59 +01:00
parent e915ed392f
commit 981cd68f49
7 changed files with 275 additions and 27 deletions
@@ -0,0 +1,92 @@
package de.neitzel.fx.view;
import de.neitzel.fx.view.fixture.Person;
import de.neitzel.fx.view.readable.ReadableSourcesController.Sources;
import de.neitzel.injection.ApplicationContext;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
/** DirectBinding reads declared observables toward the view. */
class BindReadOnlySourcesTest {
private static final URL DOCUMENT = BindReadOnlySourcesTest.class.getResource("readable-sources.fxml");
@BeforeAll
static void startJavaFx() throws InterruptedException {
FxThread.start();
}
@Test
void followsOnlyDependenciesOfTheViewModelSource() throws Exception {
FxThread.onFx(() -> {
LoadedView view = ViewLoader.load(DOCUMENT, new Person("Ada"), context());
Sources sources = (Sources) view.getViewModel();
Button commit = (Button) view.getRoot().lookup("#commit");
Label title = (Label) view.getRoot().lookup("#title");
Label caption = (Label) view.getRoot().lookup("#caption");
TextField unrelated = (TextField) view.getRoot().lookup("#unrelated");
assertTrue(commit.isDisable());
assertEquals("First", title.getText());
assertEquals("Caption: First", caption.getText());
assertNull(view.property("title"));
int evaluations = sources.getEvaluations();
unrelated.setText("A change outside the binding's dependencies");
assertEquals(evaluations, sources.getEvaluations());
sources.getChoices().add("A");
assertFalse(commit.isDisable());
sources.getChoices().clear();
assertTrue(commit.isDisable());
assertEquals(evaluations + 2, sources.getEvaluations());
title.setText("A local edit");
assertEquals("First", sources.titleProperty().get());
sources.setTitle("");
assertEquals("", title.getText());
assertEquals("Caption: First", caption.getText());
assertEquals("observableTitle", sources.getLatestMessage().getSource());
sources.setTitle("Second");
assertEquals("Second", title.getText());
assertEquals("Caption: Second", caption.getText());
assertNull(sources.getLatestMessage());
});
}
@ParameterizedTest
@ValueSource(strings = {"both", "model"})
void identifiesAnInvalidWriteBackWithItsDocumentAndEndpoints(String direction, @TempDir Path temporary)
throws Exception {
Path invalid = temporary.resolve("invalid-" + direction + ".fxml");
String fxml = Files.readString(Path.of(DOCUMENT.toURI()));
Files.writeString(invalid, fxml.replace("path=\"observableTitle\" direction=\"view\"",
"path=\"observableTitle\" direction=\"" + direction + "\""));
FxThread.onFx(() -> {
IllegalArgumentException failure = assertThrows(IllegalArgumentException.class,
() -> ViewLoader.load(invalid.toUri().toURL(), new Person("Ada"), context()));
assertTrue(failure.getMessage().contains("Read-only source"), failure.getMessage());
assertTrue(failure.getMessage().contains("observableTitle"), failure.getMessage());
assertTrue(failure.getMessage().contains("caption.text"), failure.getMessage());
assertTrue(failure.getMessage().contains(invalid.getFileName().toString()), failure.getMessage());
});
}
private static ApplicationContext context() {
return new ApplicationContext("de.neitzel.fx.view.readable");
}
}
@@ -0,0 +1,65 @@
package de.neitzel.fx.view.readable;
import de.neitzel.fx.view.Converter;
import de.neitzel.fx.view.ViewModel;
import de.neitzel.fx.view.ViewModelClass;
import jakarta.inject.Singleton;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.BooleanBinding;
import javafx.beans.property.ReadOnlyStringProperty;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import lombok.Getter;
/** Controller and presentation state for the read-only DirectBinding fixture. */
@Singleton
@ViewModelClass(ReadableSourcesController.Sources.class)
public class ReadableSourcesController {
/** Sources include a JavaFX binding, a read-only property, and an observable-typed accessor. */
public static class Sources extends ViewModel {
@Getter
private final ObservableList<String> choices = FXCollections.observableArrayList();
@Getter
private int evaluations;
private final BooleanBinding disabled = Bindings.createBooleanBinding(() -> {
evaluations++;
return choices.isEmpty();
}, choices);
private final ReadOnlyStringWrapper title = new ReadOnlyStringWrapper("First");
public Sources(Object model) {
super(model);
}
public BooleanBinding disabledProperty() {
return disabled;
}
public ReadOnlyStringProperty titleProperty() {
return title.getReadOnlyProperty();
}
public ObservableValue<String> observableTitleProperty() {
return title;
}
public void setTitle(String value) {
title.set(value);
}
}
/** A display-only converter, with an invalid title for testing Message reporting. */
@Singleton
public static class Caption implements Converter {
@Override
public Object toControlValue(Object value) {
if ("".equals(value)) {
throw new IllegalArgumentException("Caption is required");
}
return "Caption: " + value;
}
}
}
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import de.neitzel.fx.view.BindingList?>
<?import de.neitzel.fx.view.DirectBinding?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.VBox?>
<VBox xmlns:fx="http://javafx.com/fxml/1"
fx:controller="de.neitzel.fx.view.readable.ReadableSourcesController">
<fx:define>
<BindingList>
<DirectBinding control="commit" property="disable" path="disabled" direction="view"/>
<DirectBinding control="title" property="text" path="title" direction="view"/>
<DirectBinding control="caption" property="text" path="observableTitle" direction="view"
converter="de.neitzel.fx.view.readable.ReadableSourcesController$Caption"/>
</BindingList>
</fx:define>
<Button fx:id="commit" text="Commit"/>
<Label fx:id="title"/>
<Label fx:id="caption"/>
<TextField fx:id="unrelated"/>
</VBox>