Show one Address component per address so the pane stays in step with the person's list.

A second component of the same class is a new injected instance, leaving the cached singleton in place, and selection stays an attribute of the person.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-10-30 18:17:47 +01:00
co-authored by Cursor
parent a89fe47133
commit 0321e0de8d
13 changed files with 642 additions and 31 deletions
@@ -0,0 +1,231 @@
package de.neitzel.fx.view;
import de.neitzel.fx.view.component.AddressForm;
import de.neitzel.fx.view.fixture.Address;
import de.neitzel.fx.view.fixture.Person;
import de.neitzel.injection.ApplicationContext;
import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
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.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.fail;
class RepeatAddressComponentsTest {
@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 addressesAreTheAddressInstancesOnTheSharedList() throws Exception {
onFx(() -> {
Address main = new Address("Main");
Address oak = new Address("Oak");
Person person = new Person("Ada");
person.setAddresses(new ArrayList<>(List.of(main, oak)));
URL fxml = RepeatAddressComponentsTest.class.getResource("plain-view.fxml");
assertNotNull(fxml);
ApplicationContext context = new ApplicationContext("de.neitzel.fx.view.fixture");
LoadedView loaded = ViewLoader.load(fxml, person, context);
assertInstanceOf(ObservableList.class, loaded.property("addresses").getValue());
@SuppressWarnings("unchecked")
ObservableList<Address> addresses = (ObservableList<Address>) loaded.property("addresses").getValue();
assertSame(main, addresses.get(0));
assertSame(oak, addresses.get(1));
assertEquals("Main", addresses.get(0).getStreet());
assertEquals("Oak", addresses.get(1).getStreet());
assertSame(person.getAddresses(), addresses);
addresses.add(new Address("Elm"));
assertEquals(List.of("Main", "Oak", "Elm"), streets(person.getAddresses()));
});
}
@Test
void eachAddressAppearsAsItsOwnFormInListOrder() throws Exception {
onFx(() -> {
Address main = new Address("Main");
Address oak = new Address("Oak");
Person person = new Person("Ada");
person.setAddresses(new ArrayList<>(List.of(main, oak)));
URL fxml = RepeatAddressComponentsTest.class.getResource("repeat-addresses.fxml");
assertNotNull(fxml);
ApplicationContext context = new ApplicationContext("de.neitzel.fx.view.component");
AddressForm cached = context.getComponent(AddressForm.class);
LoadedView loaded = ViewLoader.load(fxml, person, context);
VBox pane = (VBox) loaded.getRoot().lookup("#addressPane");
assertEquals(2, pane.getChildren().size());
AddressForm first = (AddressForm) pane.getChildren().get(0);
AddressForm second = (AddressForm) pane.getChildren().get(1);
assertNotSame(first, second);
assertNotSame(cached, first);
assertNotSame(cached, second);
assertSame(cached, context.getComponent(AddressForm.class));
assertSame(main, first.getModel());
assertSame(oak, second.getModel());
assertEquals("Main", ((TextField) first.lookup("#streetField")).getText());
assertEquals("Oak", ((TextField) second.lookup("#streetField")).getText());
assertEquals("Lane", ((Label) first.lookup("#streetLabel")).getText());
assertEquals("Lane", ((Label) second.lookup("#streetLabel")).getText());
});
}
@Test
void addingAndRemovingAnAddressUpdatesThePaneAndTheEntity() throws Exception {
onFx(() -> {
Address main = new Address("Main");
Address oak = new Address("Oak");
Person person = new Person("Ada");
person.setAddresses(new ArrayList<>(List.of(main, oak)));
URL fxml = RepeatAddressComponentsTest.class.getResource("repeat-addresses.fxml");
assertNotNull(fxml);
ApplicationContext context = new ApplicationContext("de.neitzel.fx.view.component");
LoadedView loaded = ViewLoader.load(fxml, person, context);
VBox pane = (VBox) loaded.getRoot().lookup("#addressPane");
@SuppressWarnings("unchecked")
ObservableList<Address> addresses = (ObservableList<Address>) loaded.property("addresses").getValue();
Address elm = new Address("Elm");
addresses.add(elm);
assertEquals(3, pane.getChildren().size());
AddressForm added = (AddressForm) pane.getChildren().get(2);
assertSame(elm, added.getModel());
assertEquals("Elm", ((TextField) added.lookup("#streetField")).getText());
assertEquals(List.of("Main", "Oak", "Elm"), streets(person.getAddresses()));
addresses.remove(oak);
assertEquals(2, pane.getChildren().size());
assertSame(main, ((AddressForm) pane.getChildren().get(0)).getModel());
assertSame(elm, ((AddressForm) pane.getChildren().get(1)).getModel());
assertEquals(List.of("Main", "Elm"), streets(person.getAddresses()));
Address pine = new Address("Pine");
person.getAddresses().add(pine);
assertEquals(3, pane.getChildren().size());
AddressForm pineForm = (AddressForm) pane.getChildren().get(2);
assertSame(pine, pineForm.getModel());
assertEquals("Pine", ((TextField) pineForm.lookup("#streetField")).getText());
});
}
@Test
void selectionIsASeparateAttributeAndTheFormsDoNotStoreIt() throws Exception {
onFx(() -> {
Address main = new Address("Main");
Address oak = new Address("Oak");
Person person = new Person("Ada");
person.setAddresses(new ArrayList<>(List.of(main, oak)));
person.setSelectedAddress(oak);
URL fxml = RepeatAddressComponentsTest.class.getResource("repeat-addresses.fxml");
assertNotNull(fxml);
ApplicationContext context = new ApplicationContext("de.neitzel.fx.view.component");
LoadedView loaded = ViewLoader.load(fxml, person, context);
@SuppressWarnings("unchecked")
ComboBox<Address> selection = (ComboBox<Address>) loaded.getRoot().lookup("#selection");
assertSame(oak, selection.getValue());
selection.setValue(null);
assertNull(person.getSelectedAddress());
selection.setValue(main);
assertSame(main, person.getSelectedAddress());
VBox pane = (VBox) loaded.getRoot().lookup("#addressPane");
assertEquals(2, pane.getChildren().size());
AddressForm first = (AddressForm) pane.getChildren().get(0);
AddressForm second = (AddressForm) pane.getChildren().get(1);
assertSame(main, first.getModel());
assertSame(oak, second.getModel());
assertEquals("Main", ((TextField) first.lookup("#streetField")).getText());
assertEquals("Oak", ((TextField) second.lookup("#streetField")).getText());
});
}
@Test
void aSecondAddressFormInTheSameDocumentIsAnotherComponent() throws Exception {
onFx(() -> {
Address home = new Address("Main");
Address work = new Address("Oak");
Person person = new Person("Ada");
person.setHome(home);
person.setWork(work);
URL fxml = RepeatAddressComponentsTest.class.getResource("two-address-forms.fxml");
assertNotNull(fxml);
ApplicationContext context = new ApplicationContext("de.neitzel.fx.view.component");
AddressForm cached = context.getComponent(AddressForm.class);
LoadedView loaded = ViewLoader.load(fxml, person, context);
AddressForm homeForm = (AddressForm) loaded.getRoot().lookup("#homeForm");
AddressForm workForm = (AddressForm) loaded.getRoot().lookup("#workForm");
assertNotNull(homeForm);
assertNotNull(workForm);
assertNotSame(homeForm, workForm);
assertSame(cached, context.getComponent(AddressForm.class));
assertSame(home, homeForm.getModel());
assertSame(work, workForm.getModel());
assertEquals("Main", ((TextField) homeForm.lookup("#streetField")).getText());
assertEquals("Oak", ((TextField) workForm.lookup("#streetField")).getText());
assertEquals("Lane", ((Label) homeForm.lookup("#streetLabel")).getText());
assertEquals("Lane", ((Label) workForm.lookup("#streetLabel")).getText());
});
}
private static List<String> streets(List<Address> addresses) {
return addresses.stream().map(Address::getStreet).toList();
}
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;
}
}
@@ -22,6 +22,8 @@ public class Person {
private Date updated;
private Calendar hired;
private List<String> tags;
private List<Address> addresses;
private Address selectedAddress;
/**
* Creates a person with the given name.
@@ -256,4 +258,40 @@ public class Person {
public void setTags(List<String> tags) {
this.tags = tags;
}
/**
* Returns the addresses. The value may be null.
*
* @return the addresses
*/
public List<Address> getAddresses() {
return addresses;
}
/**
* Sets the addresses.
*
* @param addresses the addresses
*/
public void setAddresses(List<Address> addresses) {
this.addresses = addresses;
}
/**
* Returns the selected address. Null means nothing is selected.
*
* @return the selected address, or {@code null}
*/
public Address getSelectedAddress() {
return selectedAddress;
}
/**
* Sets the selected address.
*
* @param selectedAddress the selected address, or {@code null} when nothing is selected
*/
public void setSelectedAddress(Address selectedAddress) {
this.selectedAddress = selectedAddress;
}
}
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.layout.VBox?>
<?import de.neitzel.fx.view.BindingList?>
<?import de.neitzel.fx.view.DirectBinding?>
<?import de.neitzel.fx.view.Repeat?>
<VBox xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml">
<fx:define>
<BindingList>
<Repeat pane="addressPane" component="de.neitzel.fx.view.component.AddressForm" collection="addresses"/>
<DirectBinding control="selection" property="value" path="selectedAddress" direction="both"/>
</BindingList>
</fx:define>
<VBox fx:id="addressPane" id="addressPane"/>
<ComboBox fx:id="selection" id="selection"/>
</VBox>
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.VBox?>
<?import de.neitzel.fx.view.BindingList?>
<?import de.neitzel.fx.view.DirectBinding?>
<?import de.neitzel.fx.view.component.AddressForm?>
<VBox xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml">
<fx:define>
<BindingList>
<DirectBinding control="homeForm" property="model" path="home" direction="both"/>
<DirectBinding control="workForm" property="model" path="work" direction="both"/>
</BindingList>
</fx:define>
<AddressForm fx:id="homeForm"/>
<AddressForm fx:id="workForm"/>
</VBox>