Let an application show a list of simple values in a control and on the entity as one shared list.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-10-30 17:28:18 +01:00
co-authored by Cursor
parent 99886efa8b
commit aab55cbde3
6 changed files with 228 additions and 4 deletions
@@ -0,0 +1,99 @@
package de.neitzel.fx.view;
import de.neitzel.fx.view.fixture.Person;
import de.neitzel.injection.ApplicationContext;
import javafx.application.Platform;
import javafx.scene.control.ListView;
import javafx.scene.layout.VBox;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
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.assertSame;
import static org.junit.jupiter.api.Assertions.fail;
class ShowAListOfSimpleValuesTest {
@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 listViewAndPersonShareTheTagList() throws Exception {
onFx(() -> {
Person person = new Person("Ada");
person.setTags(new ArrayList<>(List.of("red", "blue")));
URL fxml = ShowAListOfSimpleValuesTest.class.getResource("tags-both-ways.fxml");
assertNotNull(fxml);
ApplicationContext context = new ApplicationContext("de.neitzel.fx.view.fixture");
LoadedView loaded = ViewLoader.load(fxml, person, context);
VBox root = (VBox) loaded.getRoot();
assertEquals(1, root.getChildren().size());
@SuppressWarnings("unchecked")
ListView<String> tags = (ListView<String>) root.lookup("#tags");
assertEquals(List.of("red", "blue"), List.copyOf(tags.getItems()));
assertEquals("red", tags.getItems().get(0));
assertEquals("blue", tags.getItems().get(1));
assertSame(person.getTags(), tags.getItems());
tags.getItems().add("green");
assertEquals(List.of("red", "blue", "green"), List.copyOf(person.getTags()));
person.getTags().add("gold");
assertEquals(List.of("red", "blue", "green", "gold"), List.copyOf(tags.getItems()));
tags.getItems().remove("blue");
assertEquals(List.of("red", "green", "gold"), List.copyOf(person.getTags()));
});
}
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;
}
}
@@ -1,5 +1,7 @@
package de.neitzel.fx.view.fixture;
import java.util.List;
/**
* Ordinary model used by view-loading tests.
*/
@@ -7,6 +9,7 @@ public class Person {
private String name;
private Integer age;
private List<String> tags;
/**
* Creates a person with the given name.
@@ -61,4 +64,22 @@ public class Person {
public void setAge(Integer age) {
this.age = age;
}
/**
* Returns the tags. The value may be null.
*
* @return the tags
*/
public List<String> getTags() {
return tags;
}
/**
* Sets the tags.
*
* @param tags the tags
*/
public void setTags(List<String> tags) {
this.tags = tags;
}
}
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.VBox?>
<?import de.neitzel.fx.view.BindingList?>
<?import de.neitzel.fx.view.DirectBinding?>
<VBox xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml">
<fx:define>
<BindingList>
<DirectBinding control="tags" property="items" path="tags" direction="both"/>
</BindingList>
</fx:define>
<ListView fx:id="tags" id="tags"/>
</VBox>