Add reference-safe FXML rename, subtree copy and delete

This commit is contained in:
2026-11-13 16:13:03 +01:00
parent 5462f480a4
commit 910a8b0e85
13 changed files with 1191 additions and 7 deletions
@@ -61,6 +61,21 @@ class FxmlProjectionTest {
assertTrue(anonymous.previewText().contains("fx:id=\"__designer_"));
}
@Test
void kitIdentifierRenameUpdatesKnownBindingsInOneSourceTransaction() throws Exception {
String text =
XML.replace(
"<app.Binding control=\"card\" path=\"home\"/>",
"<de.neitzel.fx.view.BindingList><de.neitzel.fx.view.DirectBinding control=\"card\""
+ " path=\"home\" property=\"model\"/></de.neitzel.fx.view.BindingList>");
var projection = new FxmlProjection(text);
String projected = projection.project();
assertEquals(
text.replace("fx:id=\"card\"", "fx:id=\"renamed\"")
.replace("control=\"card\"", "control=\"renamed\""),
projection.restore(projected.replace("fx:id=\"card\"", "fx:id=\"renamed\"")));
}
@Test
void preservesEveryUnchangedCharacterAndUsesAttributeRanges() throws Exception {
String text = XML.replace("caption=\"before\"", "caption = 'a &amp; b'").replace("\n", "\r\n");
@@ -675,6 +675,186 @@ public final class ProjectionHostChecks {
await(() -> fx(() -> source(pane).getText().equals(after)));
}
private static void structureSelect(DesignerPane pane, String id) throws Exception {
fx(
() -> {
var list = (ListView<Object>) pane.lookup("#structure-nodes");
if (id.isEmpty()) list.getSelectionModel().select(0);
else
list.getSelectionModel()
.select(
list.getItems().stream()
.filter(e -> e.toString().startsWith(id + " :"))
.findFirst()
.orElseThrow());
return null;
});
}
private static void structureEdits(DesignerPane pane, DocumentSession document, String full)
throws Exception {
String base =
full.replace(
"<VBox fx:id=\"visits\"/>",
"<VBox fx:id=\"group\"><VBox fx:id=\"visits\"/><javafx.scene.control.Label"
+ " fx:id=\"summary\" text=\"untouched\"/></VBox>")
.replace(
"</BindingList>",
"<de.neitzel.fx.view.ComputedBinding control=\"summary\" property=\"text\""
+ " method=\"#greeting\"/></BindingList>");
set(document, base);
await(() -> fx(() -> source(pane).getText().equals(base)));
bindingTab(pane, "Structure");
structureSelect(pane, "summary");
fx(
() -> {
((TextField) pane.lookup("#structure-name")).setText("renamedSummary");
return null;
});
click(pane, "Rename identifier");
String renamed =
base.replace("fx:id=\"summary\"", "fx:id=\"renamedSummary\"")
.replace("control=\"summary\"", "control=\"renamedSummary\"");
await(() -> documentText(document).equals(renamed));
repeatHistory(pane, document, base, renamed);
structureSelect(pane, "visits");
fx(
() -> {
((TextField) pane.lookup("#structure-name")).setText("visitList");
return null;
});
click(pane, "Rename identifier");
String repeatRenamed =
renamed
.replace("fx:id=\"visits\"", "fx:id=\"visitList\"")
.replace("pane=\"visits\"", "pane=\"visitList\"");
await(() -> documentText(document).equals(repeatRenamed));
repeatHistory(pane, document, renamed, repeatRenamed);
structureSelect(pane, "group");
click(pane, "Copy subtree");
check(documentText(document).equals(repeatRenamed), "Copy mutated source");
structureSelect(pane, "");
click(pane, "Paste subtree");
await(() -> !documentText(document).equals(repeatRenamed));
String pasted = documentText(document);
check(
pasted.contains("fx:id=\"groupCopy\"")
&& pasted.contains("pane=\"visitListCopy\"")
&& pasted.contains("control=\"renamedSummaryCopy\""),
"Subtree/binding IDs not copied together");
repeatHistory(pane, document, repeatRenamed, pasted);
structureSelect(pane, "group");
click(pane, "Delete subtree");
await(() -> !documentText(document).equals(pasted));
String deleted = documentText(document);
check(
!deleted.contains("pane=\"visitList\"")
&& !deleted.contains("control=\"renamedSummary\"")
&& deleted.contains("pane=\"visitListCopy\""),
"Delete did not isolate descendant target bindings");
repeatHistory(pane, document, pasted, deleted);
document
.save(document.snapshot().toCompletableFuture().get().revision())
.toCompletableFuture()
.get();
check(
java.nio.file.Files.readString(
java.nio.file.Path.of(document.snapshot().toCompletableFuture().get().document()))
.equals(deleted),
"Structural save changed source");
click(pane, "Interactive preview");
await(
() ->
fx(
() ->
pane.lookupAll(".label").stream()
.anyMatch(
n ->
n instanceof Label l
&& "Interactive preview running from captured drafts"
.equals(l.getText()))));
click(pane, "Stop preview");
await(() -> pane.interactivePid() < 0);
structureSelect(pane, "rankInput");
click(pane, "Copy subtree");
var file = java.nio.file.Path.of(document.snapshot().toCompletableFuture().get().document());
var root = file.getParent();
while (root != null
&& !java.nio.file.Files.isRegularFile(root.resolve(".neitzel-fx-designer/host.properties")))
root = root.getParent();
check(root != null, "Fixture host configuration absent");
var launch =
de.neitzel.fxdesigner.workflow.HostLaunch.load(
root.resolve(".neitzel-fx-designer/host.properties"));
var destination = file.resolveSibling("AddressForm.fxml");
try (var otherDocument =
pane.kit().host().documents().open(destination.toUri()).toCompletableFuture().get()) {
String original = documentText(otherDocument);
var other = fx(() -> new DesignerPane(pane.kit().host(), launch, destination));
var stage =
fx(
() -> {
var window = new javafx.stage.Stage();
window.setScene(new javafx.scene.Scene(other, 1200, 850));
window.show();
return window;
});
try {
await(() -> fx(() -> other.kit().editor().getFxomDocument() != null));
bindingTab(other, "Structure");
structureSelect(other, "");
click(other, "Paste subtree");
await(() -> !documentText(otherDocument).equals(original));
String cross = documentText(otherDocument);
check(
cross.contains("rankInputCopy") && cross.contains("path=\"rank\""),
"Cross-View paste lost binding payload");
check(
fx(
() ->
((Label) other.lookup("#structure-status"))
.getText()
.contains("Model declarations differ")),
"Cross-View context mismatch not exposed");
bindingTab(other, "Direct bindings");
await(
() ->
fx(
() ->
((Label) other.lookup("#direct-status"))
.getText()
.contains("Unknown or unresolved Model path: rank")));
repeatHistory(other, otherDocument, original, cross);
click(other, "Render drafts");
await(
() ->
fx(
() ->
other.lookupAll(".label").stream()
.anyMatch(
n ->
n instanceof Label l
&& l.getText() != null
&& l.getText().startsWith("Preview blocked:")
&& l.getText().contains("rank"))));
set(otherDocument, original);
await(() -> fx(() -> source(other).getText().equals(original)));
} finally {
fx(
() -> {
other.close();
stage.close();
return null;
});
}
}
set(document, full);
await(() -> fx(() -> source(pane).getText().equals(full)));
System.out.println(
"PASS reference-safe structure: rename, subtree copy/delete, single-step exact history,"
+ " save/load, cross-View paste and destination validation");
}
private static void tableField(DesignerPane pane, String field, String value) throws Exception {
fx(
() -> {
@@ -1527,6 +1707,7 @@ public final class ProjectionHostChecks {
repeatBindingEdits(pane, document, full);
tableDiscovery(pane, document, full);
tableEdits(pane, document, full);
structureEdits(pane, document, full);
document
.save(document.snapshot().toCompletableFuture().get().revision())
.toCompletableFuture()
@@ -0,0 +1,183 @@
package de.neitzel.fxdesigner.kit;
import static org.junit.jupiter.api.Assertions.*;
import de.neitzel.fxdesigner.contracts.*;
import java.net.URI;
import java.util.*;
import org.junit.jupiter.api.Test;
class ReferenceDocumentTest {
private static final String XML =
"<?neitzel-model app.Person?>\r\n"
+ "<?import javafx.scene.layout.VBox?><?import javafx.scene.control.Label?><?import"
+ " de.neitzel.fx.view.*?><VBox xmlns:f='http://javafx.com/fxml/1'"
+ " f:id='root'><f:define><BindingList><elements><!-- before --><DirectBinding control ="
+ " 'child' property='text' path='child' direction='view' extra='keep'/><ComputedBinding"
+ " control='box' property='visible' method='#visible'/><Repeat pane='box'"
+ " component='app.Row' collection='items'/><DirectBinding property='text' path='name'"
+ " direction='view'><control>child</control></DirectBinding><!-- after"
+ " --></elements></BindingList></f:define><!-- outside --><VBox f:id='box'><Label"
+ " f:id='child' text='literal child'/></VBox><Label f:id='other'"
+ " labelFor='$child'/></VBox>";
private int index(ReferenceDocument d, String id) {
return d.entries.stream().filter(e -> e.id().equals(id)).findFirst().orElseThrow().index();
}
private String apply(String before, ReferenceDocument.Change c) throws Exception {
String result =
new EditBatch(4, "structure", c.edits())
.applyTo(new DocumentSnapshot(URI.create("file:/v.fxml"), 4, before, true, false));
new ReferenceDocument(result);
return result;
}
@Test
void renamesTargetReferencesWithoutTouchingPathsMethodsOrLiteralText() throws Exception {
var d = new ReferenceDocument(XML);
String result = apply(XML, d.rename(index(d, "child"), "renamed"));
assertEquals(
XML.replace("f:id='child'", "f:id='renamed'")
.replace("control = 'child'", "control = 'renamed'")
.replace("<control>child</control>", "<control>renamed</control>")
.replace("labelFor='$child'", "labelFor='$renamed'"),
result);
String box = apply(XML, d.rename(index(d, "box"), "group"));
assertTrue(box.contains("control='group'"));
assertTrue(box.contains("pane='group'"));
assertTrue(box.contains("method='#visible'"));
}
@Test
void nestedTargetRenameRetainsWhitespaceAndComments() throws Exception {
String text =
XML.replace(
"<control>child</control>",
"<control>\r\n<!-- target --> child <!-- retained -->\r\n</control>");
var d = new ReferenceDocument(text);
String result = apply(text, d.rename(index(d, "child"), "newName"));
assertTrue(
result.contains("<control>\r\n<!-- target --> newName <!-- retained -->\r\n</control>"));
}
@Test
void deletesSubtreeAndOnlyItsTargetBindingsLeavingOtherDependenciesVisible() throws Exception {
var d = new ReferenceDocument(XML);
String result = apply(XML, d.delete(index(d, "box")));
assertFalse(result.contains("<DirectBinding"));
assertFalse(result.contains("<ComputedBinding"));
assertFalse(result.contains("<Repeat"));
assertTrue(result.contains("<!-- before --><!-- after -->"));
assertTrue(result.contains("<!-- outside -->"));
assertTrue(result.contains("labelFor='$child'"));
assertEquals(
List.of("Unresolved FXML reference: child"),
new ReferenceDocument(result).referenceErrors());
}
@Test
void copiesWholeSubtreeWithFreshIdsAndExactBindingPayloads() throws Exception {
var d = new ReferenceDocument(XML);
var clip = d.copy(index(d, "box"), "one");
String result = apply(XML, d.paste(clip, index(d, "root"), "one"));
assertTrue(
result.contains(
"<VBox f:id='boxCopy'><Label f:id='childCopy' text='literal child'/></VBox>"));
assertTrue(
result.contains(
"control = 'childCopy' property='text' path='child' direction='view' extra='keep'"));
assertTrue(result.contains("pane='boxCopy'"));
assertTrue(result.contains("<control>childCopy</control>"));
assertTrue(result.contains("control = 'child'"));
assertTrue(result.contains("<!-- outside -->"));
var n = new ReferenceDocument(result);
String twice = apply(result, n.paste(clip, index(n, "root"), "one"));
assertTrue(twice.contains("f:id='childCopy2'"));
}
@Test
void carriesNamespacesImportsAndReportsDestinationContextDifferences() throws Exception {
String source =
XML.replace(
"<?import de.neitzel.fx.view.*?>",
"<?import de.neitzel.fx.view.BindingList?><?import"
+ " de.neitzel.fx.view.DirectBinding?><?import"
+ " de.neitzel.fx.view.ComputedBinding?><?import de.neitzel.fx.view.Repeat?>");
var from = new ReferenceDocument(source);
String target =
"<?neitzel-model app.Other?><?import javafx.scene.layout.VBox?><VBox"
+ " xmlns:fx='http://javafx.com/fxml/1' fx:controller='app.OtherController'/>";
var d = new ReferenceDocument(target);
var change = d.paste(from.copy(index(from, "box"), "source"), 0, "destination");
String result = apply(target, change);
assertTrue(result.contains("<?neitzel-model app.Other?>"));
assertTrue(result.contains("xmlns:f=\"http://javafx.com/fxml/1\""));
assertTrue(result.contains("<?import javafx.scene.control.Label?>"));
assertTrue(change.notes().stream().anyMatch(n -> n.contains("Model declarations differ")));
assertTrue(change.notes().stream().anyMatch(n -> n.contains("Controllers differ")));
assertEquals(
4,
new BindingDocument(result)
.elements.stream()
.filter(e -> e.getTagName().endsWith("Binding") || e.getTagName().equals("Repeat"))
.count());
}
@Test
void updatesSimpleFxmlExpressionsAndReferenceNodesAndRejectsComplexOnes() throws Exception {
String text =
XML.replace("labelFor='$child'", "labelFor='${child.text}'")
.replace(
"</VBox><Label f:id='other'",
"<f:reference source='child'/></VBox><Label f:id='other'");
var d = new ReferenceDocument(text);
String result = apply(text, d.rename(index(d, "child"), "x"));
assertTrue(result.contains("${x.text}"));
assertTrue(result.contains("source='x'"));
var complex =
new ReferenceDocument(
XML.replace("labelFor='$child'", "text='${child.text + other.text}'"));
assertThrows(
IllegalArgumentException.class, () -> complex.rename(index(complex, "child"), "x"));
}
@Test
void rejectsDuplicateIdsCollisionsRootsUnknownBindingsAndUnsafePaste() throws Exception {
var d = new ReferenceDocument(XML);
assertThrows(IllegalArgumentException.class, () -> d.rename(index(d, "child"), "box"));
assertThrows(IllegalArgumentException.class, () -> d.rename(index(d, "child"), "bad id"));
assertThrows(IllegalArgumentException.class, () -> d.delete(0));
assertThrows(IllegalArgumentException.class, () -> d.copy(0, "one"));
assertThrows(
IllegalArgumentException.class,
() -> new ReferenceDocument(XML.replace("f:id='other'", "f:id='child'")));
var unknown =
new ReferenceDocument(
XML.replace("<!-- before -->", "<app.Unknown control='child'/><!-- before -->"));
assertThrows(
IllegalArgumentException.class, () -> unknown.rename(index(unknown, "child"), "x"));
assertThrows(
IllegalArgumentException.class,
() -> d.paste(d.copy(index(d, "box"), "one"), index(d, "other"), "one"));
}
@Test
void copyRemapsInternalReferencesAndDoesNotRetargetExternalOnes() throws Exception {
String text = XML.replace("text='literal child'", "text='literal child' labelFor='$box'");
var d = new ReferenceDocument(text);
String result = apply(text, d.paste(d.copy(index(d, "box"), "one"), 0, "one"));
assertTrue(result.contains("labelFor='$boxCopy'"));
assertTrue(result.contains("labelFor='$child'"));
}
@Test
void assignsIdToAnonymousObjectWithoutWritingProjectionIdentity() throws Exception {
var d =
new ReferenceDocument(
"<javafx.scene.layout.VBox><javafx.scene.control.Label/></javafx.scene.layout.VBox>");
String result = apply(d.xml.text, d.rename(1, "label"));
assertTrue(result.contains("fx:id=\"label\""));
assertFalse(result.contains("__designer_"));
}
}
@@ -75,6 +75,22 @@ class TableDocumentTest {
}
}
@Test
void removingTableAlsoRemovesItsTargetBindings() throws Exception {
String declaration = "<DirectBinding control='t' property='visible' path='enabled'/>";
String text =
START.replace(
"<!-- keep -->",
"<f:define><BindingList><!-- binding -->"
+ declaration
+ "</BindingList></f:define><!-- keep -->");
var d = new TableDocument(text);
String result = apply(text, d.removeTable(d.tables.getFirst().index()));
assertFalse(result.contains(declaration));
assertTrue(result.contains("<!-- binding -->"));
assertTrue(result.contains("<Label text='untouched'/>"));
}
@Test
void preservesNestedDeclarationsAndRejectsAmbiguousContainers() throws Exception {
String text =