Let a label show a Controller method and follow other values in the View.

The method does not write the Model, and a change to the label itself does not run the method again.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-10-30 17:37:06 +01:00
co-authored by Cursor
parent 99886efa8b
commit 6e3036669a
8 changed files with 442 additions and 16 deletions
@@ -0,0 +1,135 @@
package de.neitzel.fx.view;
import de.neitzel.fx.view.fixture.Person;
import de.neitzel.injection.ApplicationContext;
import javafx.application.Platform;
import javafx.beans.property.Property;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.net.URL;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;
class ComputeAValueTest {
@BeforeAll
static void startJavaFx() throws InterruptedException {
CountDownLatch started = new CountDownLatch(1);
try {
Platform.startup(started::countDown);
} catch (IllegalStateException alreadyStarted) {
started.countDown();
}
if (!started.await(10, TimeUnit.SECONDS)) {
fail("JavaFX toolkit did not start");
}
}
@Test
void computedBindingShowsTheMethodResultAndLeavesTheModelUnchanged() throws Exception {
onFx(() -> {
Person person = new Person("Ada");
person.setAge(36);
URL fxml = ComputeAValueTest.class.getResource("working-age.fxml");
assertNotNull(fxml);
ApplicationContext context = new ApplicationContext("de.neitzel.fx.view.computed");
LoadedView loaded = ViewLoader.load(fxml, person, context);
Label workingAgeLabel = (Label) loaded.getRoot().lookup("#workingAgeLabel");
assertEquals("initial-age", workingAgeLabel.getText());
assertEquals("Ada", person.getName());
assertEquals(36, person.getAge());
});
}
@Test
void typingInAnotherTextFieldUpdatesTheComputedLabel() throws Exception {
onFx(() -> {
Person person = new Person("Ada");
URL fxml = ComputeAValueTest.class.getResource("working-age.fxml");
assertNotNull(fxml);
ApplicationContext context = new ApplicationContext("de.neitzel.fx.view.computed");
LoadedView loaded = ViewLoader.load(fxml, person, context);
TextField nameField = (TextField) loaded.getRoot().lookup("#nameField");
Label workingAgeLabel = (Label) loaded.getRoot().lookup("#workingAgeLabel");
nameField.setText("Grace");
assertEquals("typed-age", workingAgeLabel.getText());
assertEquals("Grace", person.getName());
});
}
@Test
void settingAViewModelPropertyUpdatesTheComputedLabel() throws Exception {
onFx(() -> {
Person person = new Person("Ada");
URL fxml = ComputeAValueTest.class.getResource("working-age.fxml");
assertNotNull(fxml);
ApplicationContext context = new ApplicationContext("de.neitzel.fx.view.computed");
LoadedView loaded = ViewLoader.load(fxml, person, context);
Label workingAgeLabel = (Label) loaded.getRoot().lookup("#workingAgeLabel");
@SuppressWarnings("unchecked")
Property<String> name = (Property<String>) loaded.property("name");
name.setValue("Edsger");
assertEquals("model-age", workingAgeLabel.getText());
assertEquals("Edsger", person.getName());
});
}
@Test
void changingTheComputedLabelDoesNotRunTheMethodAgain() throws Exception {
onFx(() -> {
Person person = new Person("Ada");
URL fxml = ComputeAValueTest.class.getResource("working-age.fxml");
assertNotNull(fxml);
ApplicationContext context = new ApplicationContext("de.neitzel.fx.view.computed");
LoadedView loaded = ViewLoader.load(fxml, person, context);
Label workingAgeLabel = (Label) loaded.getRoot().lookup("#workingAgeLabel");
assertEquals("initial-age", workingAgeLabel.getText());
workingAgeLabel.setText("sentinel");
assertEquals("sentinel", workingAgeLabel.getText());
});
}
private static void onFx(FxAction action) throws Exception {
if (Platform.isFxApplicationThread()) {
action.run();
return;
}
AtomicReference<Throwable> thrown = new AtomicReference<>();
CountDownLatch done = new CountDownLatch(1);
Platform.runLater(() -> {
try {
action.run();
} catch (Throwable failure) {
thrown.set(failure);
} finally {
done.countDown();
}
});
if (!done.await(10, TimeUnit.SECONDS)) {
fail("JavaFX thread did not finish");
}
Throwable failure = thrown.get();
if (failure instanceof Exception exception) {
throw exception;
}
if (failure instanceof Error error) {
throw error;
}
if (failure != null) {
throw new AssertionError(failure);
}
}
@FunctionalInterface
private interface FxAction {
void run() throws Exception;
}
}
@@ -0,0 +1,30 @@
package de.neitzel.fx.view.computed;
import jakarta.inject.Singleton;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
/**
* Controller that derives a label from the name field and does not write the Model.
*/
@Singleton
public class WorkingAgeController {
@FXML
private TextField nameField;
/**
* Returns a literal chosen from the current name field.
*
* @return the value written to the label
*/
public String workingAge() {
if ("Ada".equals(nameField.getText())) {
return "initial-age";
}
if ("Grace".equals(nameField.getText())) {
return "typed-age";
}
return "model-age";
}
}
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.VBox?>
<?import de.neitzel.fx.view.BindingList?>
<?import de.neitzel.fx.view.ComputedBinding?>
<?import de.neitzel.fx.view.DirectBinding?>
<VBox xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml"
fx:controller="de.neitzel.fx.view.computed.WorkingAgeController">
<fx:define>
<BindingList>
<DirectBinding control="nameField" property="text" path="name" direction="both"/>
<ComputedBinding control="workingAgeLabel" property="text" method="#workingAge"/>
</BindingList>
</fx:define>
<TextField fx:id="nameField" id="nameField"/>
<Label fx:id="workingAgeLabel" id="workingAgeLabel"/>
</VBox>