From 98d37c6c3d82091360b3c0b2b2237e2cf065473f Mon Sep 17 00:00:00 2001 From: Konrad Neitzel Date: Tue, 24 Nov 2026 14:42:35 +0100 Subject: [PATCH] Edit and exercise Archiver CommandBindings in the IntelliJ designer --- README.md | 13 + .../fxdesigner/protocol/BindingMetadata.java | 64 ++- .../fxdesigner/kit/BindingDocument.java | 67 ++- .../neitzel/fxdesigner/kit/BindingEditor.java | 39 +- .../neitzel/fxdesigner/kit/DesignerPane.java | 135 ++++-- .../fxdesigner/kit/FxmlDiagnostics.java | 3 + .../fxdesigner/kit/FxmlProjection.java | 3 +- .../fxdesigner/kit/ReferenceDocument.java | 41 +- .../fxdesigner/kit/ReferenceEditor.java | 4 +- .../fxdesigner/kit/FxmlProjectionTest.java | 20 + .../fxdesigner/kit/ReferenceDocumentTest.java | 20 + .../fxdesigner/target/BindingDiscovery.java | 99 ++++- .../fxdesigner/target/TargetPreview.java | 37 +- .../target/BindingDiscoveryTest.java | 61 +++ docs/runtime/neitzellib-baseline.json | 83 ++-- docs/runtime/neitzellib-baseline.md | 36 +- .../java/fixture/components/CommandCard.java | 32 ++ .../fixture/components/PersonController.java | 2 + .../fixture/components/CommandCard.fxml | 8 + .../java/fixture/checks/PreviewChecks.java | 5 +- .../test/java/fixture/checks/StageChecks.java | 24 +- .../preview/classpath/pom.xml | 2 +- .../java/fixture/components/CommandCard.java | 32 ++ .../fixture/components/PersonController.java | 2 + .../fixture/components/CommandCard.fxml | 8 + .../java/fixture/checks/PreviewChecks.java | 5 +- .../main/java/fixture/checks/StageChecks.java | 24 +- .../preview/modulepath/pom.xml | 2 +- .../fx-example/PersonDeskRender.java | 37 ++ .../idea/ArchiveTargetProbe.java | 115 +++++ .../idea/ArchiveWorkflowProbe.java | 408 ++++++++++++++++++ pom.xml | 2 +- scripts/check-connected-workflow.py | 2 +- scripts/check-ide-host.py | 9 +- scripts/check-neitzellib-artifact.py | 2 +- scripts/check-preview-fixtures.py | 2 +- scripts/configure-library-example.py | 12 +- scripts/run-fixture-host.py | 34 +- 38 files changed, 1304 insertions(+), 190 deletions(-) create mode 100644 integration-fixtures/preview/classpath/app/src/main/java/fixture/components/CommandCard.java create mode 100644 integration-fixtures/preview/classpath/app/src/main/resources/fixture/components/CommandCard.fxml create mode 100644 integration-fixtures/preview/modulepath/app/src/main/java/fixture/components/CommandCard.java create mode 100644 integration-fixtures/preview/modulepath/app/src/main/resources/fixture/components/CommandCard.fxml create mode 100644 integration-tests/fx-example/PersonDeskRender.java create mode 100644 integration-tests/idea/ArchiveTargetProbe.java create mode 100644 integration-tests/idea/ArchiveWorkflowProbe.java diff --git a/README.md b/README.md index 5386524..447c4fb 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,19 @@ Qualify the registered editor and loaded JavaFX natives in real disposable IDE w python3 scripts/check-ide-host.py --java-home "$JAVA_HOME" --target-java-home "$TARGET_JAVA_HOME" --idea-home "$IDEA_HOME" ``` +### Archiver in IntelliJ + +Build the [pinned NeitzelLib snapshot](docs/runtime/neitzellib-baseline.md), then build the Designer modules and plugin with the bootstrap command above. Archiver includes its example in the `designer-preview` Maven profile. To open an isolated IntelliJ instance with a project copy and the built plugin, run from this checkout with the same Maven environment: + +```sh +python3 scripts/run-fixture-host.py intellij --archiver ../Archiver \ + --idea-home "$IDEA_HOME" --java-home "$JAVA_HOME" --target-java-home "$JAVA_HOME" +``` + +Open `ArchiveWindow.fxml` in **Neitzel Designer**, choose **Build and Refresh** and start **Interactive preview** with **Beispielarchiv**. The open menu command enables the trash button. Under **Bindings → Command bindings** select `open`, change its path to `openReadOnly`, apply and restart the preview. The status bar identifies the read-only example. **Discover / Validate Commands** lists public getters returning a NeitzelLib `Command`, including the Command exposed by an `AsyncCommand`. The Problems view navigates to an invalid declaration. To repeat the automated acceptance, use the same arguments with `python3 scripts/check-ide-host.py --archiver ../Archiver` (omit the `intellij` positional argument). + +For an existing Archiver checkout, install the built plugin ZIP and create the ignored local JDK and worker mappings once with `python3 scripts/configure-library-example.py --project ../Archiver --target-java-home "$JAVA_HOME"`. Launch the IDE with access to the Maven repository containing the qualified snapshots. + The [save/startup reliability follow-up](docs/ide-host-reliability.md) covers queued IntelliJ VFS writes and host-revision/validation races, including repeated registered module-path qualification. Add `--mode modulepath --repeat 3` to the IDE runner to repeat that gate. Worker startup cancellation and ready-preview cleanup are covered by the [native lifecycle regression](docs/native-worker-lifecycle.md); host checks also reject new native JVM crash reports. diff --git a/designer-contracts/src/main/java/de/neitzel/fxdesigner/protocol/BindingMetadata.java b/designer-contracts/src/main/java/de/neitzel/fxdesigner/protocol/BindingMetadata.java index 8b8513a..76d95dc 100644 --- a/designer-contracts/src/main/java/de/neitzel/fxdesigner/protocol/BindingMetadata.java +++ b/designer-contracts/src/main/java/de/neitzel/fxdesigner/protocol/BindingMetadata.java @@ -20,7 +20,49 @@ public record BindingMetadata( List panes, List tableCollections, List tableRows, - List tableColumns) { + List tableColumns, + List commandPaths, + List commandTargets) { + public BindingMetadata( + String model, + String path, + String direction, + List attributes, + List targets, + List diagnostics, + List converters, + List problems, + String controller, + String viewModel, + List computed, + List collections, + List components, + List panes, + List tableCollections, + List tableRows, + List tableColumns) { + this( + model, + path, + direction, + attributes, + targets, + diagnostics, + converters, + problems, + controller, + viewModel, + computed, + collections, + components, + panes, + tableCollections, + tableRows, + tableColumns, + List.of(), + List.of()); + } + public BindingMetadata( String model, String path, @@ -160,6 +202,8 @@ public record BindingMetadata( tableCollections = List.copyOf(tableCollections); tableRows = List.copyOf(tableRows); tableColumns = List.copyOf(tableColumns); + commandPaths = List.copyOf(commandPaths); + commandTargets = List.copyOf(commandTargets); if (!Set.of("view", "model", "both").contains(direction)) throw new IllegalArgumentException("Invalid direction"); } @@ -167,6 +211,8 @@ public record BindingMetadata( public Map encode() { var m = new TreeMap(); m.put("format", "5"); + encodeStrings(m, "commands", commandPaths); + encodeStrings(m, "commandTargets", commandTargets); encodeOptions(m, "rc", collections); encodeOptions(m, "rt", components); encodeOptions(m, "rp", panes); @@ -228,6 +274,17 @@ public record BindingMetadata( return m; } + private static void encodeStrings(Map fields, String key, List values) { + fields.put(key, Integer.toString(values.size())); + for (int i = 0; i < values.size(); i++) fields.put(key + "." + i, values.get(i)); + } + + private static List decodeStrings(Map fields, String key) { + return java.util.stream.IntStream.range(0, count(fields, key)) + .mapToObj(i -> field(fields, key + "." + i)) + .toList(); + } + private static void encodeOptions( Map m, String prefix, List values) { m.put(prefix, "" + values.size()); @@ -266,6 +323,7 @@ public record BindingMetadata( } public static BindingMetadata decode(Map m) { + boolean commands = m.containsKey("commands"); if (!"5".equals(field(m, "format"))) throw new IllegalArgumentException("Unsupported binding metadata"); var a = new ArrayList(); @@ -333,6 +391,8 @@ public record BindingMetadata( decodeOptions(m, "rp"), decodeOptions(m, "tc"), decodeOptions(m, "tr"), - decodeOptions(m, "ta")); + decodeOptions(m, "ta"), + commands ? decodeStrings(m, "commands") : List.of(), + commands ? decodeStrings(m, "commandTargets") : List.of()); } } diff --git a/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/BindingDocument.java b/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/BindingDocument.java index b973728..fb44a11 100644 --- a/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/BindingDocument.java +++ b/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/BindingDocument.java @@ -11,11 +11,13 @@ final class BindingDocument { enum Kind { DIRECT, COMPUTED, + COMMAND, REPEAT; String element() { return switch (this) { case DIRECT -> "DirectBinding"; + case COMMAND -> "CommandBinding"; case COMPUTED -> "ComputedBinding"; case REPEAT -> "Repeat"; }; @@ -54,8 +56,7 @@ final class BindingDocument { if (!values.pane().isEmpty()) return values.pane() + " ← " + values.collection() + " (" + values.component() + ")"; return values.control() - + "." - + values.property() + + (values.property().isEmpty() ? "" : "." + values.property()) + " ← " + (values.method().isEmpty() ? values.path() : values.method()); } @@ -214,6 +215,14 @@ final class BindingDocument { private boolean conflicts(Element e, Values value) { if (kind == Kind.REPEAT) return binding(e, "Repeat") && e.getAttribute("pane").equals(value.pane()); + if (e.getAttribute("control").equals(value.control())) { + if (kind == Kind.COMMAND) + return binding(e, "CommandBinding") + || ((binding(e, "DirectBinding") || binding(e, "ComputedBinding")) + && Set.of("disable", "onAction").contains(e.getAttribute("property"))); + if (binding(e, "CommandBinding")) + return Set.of("disable", "onAction").contains(value.property()); + } return (binding(e, "DirectBinding") || binding(e, "ComputedBinding")) && e.getAttribute("control").equals(value.control()) && e.getAttribute("property").equals(value.property()); @@ -228,8 +237,11 @@ final class BindingDocument { List write(Integer index, Values value, boolean replaceConflicts) { if (kind == Kind.REPEAT && value.pane().isBlank()) throw new IllegalArgumentException("Choose a pane"); - if (kind != Kind.REPEAT && (value.control().isBlank() || value.property().isBlank())) + if (kind != Kind.REPEAT + && (value.control().isBlank() || (kind != Kind.COMMAND && value.property().isBlank()))) throw new IllegalArgumentException("Choose a control and property"); + if (kind == Kind.COMMAND && (!qualifiedName(value.path()) || value.path().contains("."))) + throw new IllegalArgumentException("Choose a simple Command path"); Element original = index == null ? null : entry(index); if (original != null && hasElementChildren(original)) throw new IllegalArgumentException( @@ -256,25 +268,27 @@ final class BindingDocument { value.component(), "collection", value.collection()) - : kind == Kind.COMPUTED - ? Map.of( - "control", - value.control(), - "property", - value.property(), - "method", - value.method()) - : Map.of( - "control", - value.control(), - "property", - value.property(), - "path", - value.path(), - "direction", - value.direction(), - "converter", - value.converter()); + : kind == Kind.COMMAND + ? Map.of("control", value.control(), "path", value.path()) + : kind == Kind.COMPUTED + ? Map.of( + "control", + value.control(), + "property", + value.property(), + "method", + value.method()) + : Map.of( + "control", + value.control(), + "property", + value.property(), + "path", + value.path(), + "direction", + value.direction(), + "converter", + value.converter()); for (var f : fields.entrySet()) if (!original.getAttribute(f.getKey()).equals(f.getValue())) changed.put( @@ -303,6 +317,15 @@ final class BindingDocument { + "\" collection=\"" + XmlSource.escape(value.collection(), '"') + "\""; + else if (kind == Kind.COMMAND) + node = + "<" + + PACKAGE + + "CommandBinding control=\"" + + XmlSource.escape(value.control(), '"') + + "\" path=\"" + + XmlSource.escape(value.path(), '"') + + "\""; else if (kind == Kind.COMPUTED) node += " method=\"" + XmlSource.escape(value.method(), '"') + "\""; else diff --git a/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/BindingEditor.java b/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/BindingEditor.java index fd235b0..6cbff7d 100644 --- a/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/BindingEditor.java +++ b/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/BindingEditor.java @@ -31,6 +31,7 @@ final class BindingEditor extends VBox { private final BindingDocument.Kind kind; private final ComboBox converters = new ComboBox<>(); private final Button inspect = new Button("Find Converters / Validate"); + private final ComboBox commandPaths = new ComboBox<>(), commandTargets = new ComboBox<>(); private final ComboBox direction = new ComboBox<>(); private final CheckBox replace = new CheckBox("Replace conflicting bindings"); private final Label status = new Label("Open a valid FXML document to edit bindings."); @@ -50,7 +51,8 @@ final class BindingEditor extends VBox { this.kind = kind; boolean computed = kind == BindingDocument.Kind.COMPUTED; boolean repeat = kind == BindingDocument.Kind.REPEAT; - String prefix = repeat ? "repeat-" : computed ? "computed-" : "direct-"; + boolean command = kind == BindingDocument.Kind.COMMAND; + String prefix = command ? "command-" : repeat ? "repeat-" : computed ? "computed-" : "direct-"; this.navigate = navigate; for (var field : java.util.List.of(control, property, path, converter, method, pane, component, collection)) @@ -68,6 +70,20 @@ final class BindingEditor extends VBox { apply.setText("Apply Computed binding"); remove.setText("Remove Computed binding"); } + if (command) { + inspect.setText("Discover / Validate Commands"); + fresh.setText("New Command binding"); + apply.setText("Apply Command binding"); + remove.setText("Remove Command binding"); + commandPaths.setOnAction( + e -> { + if (commandPaths.getValue() != null) path.setText(commandPaths.getValue()); + }); + commandTargets.setOnAction( + e -> { + if (commandTargets.getValue() != null) control.setText(commandTargets.getValue()); + }); + } if (repeat) { inspect.setText("Discover / Validate Repeat"); fresh.setText("New Repeat binding"); @@ -126,7 +142,7 @@ final class BindingEditor extends VBox { var grid = new GridPane(); grid.setHgap(8); grid.setVgap(4); - if (!computed && !repeat) + if (kind == BindingDocument.Kind.DIRECT) grid.addRow( 0, new Label("Control (fx:id)"), @@ -135,7 +151,7 @@ final class BindingEditor extends VBox { property, new Label("Direction"), direction); - if (!computed && !repeat) + if (kind == BindingDocument.Kind.DIRECT) grid.addRow(1, new Label("Model path"), path, new Label("Converter (optional)"), converter); else if (computed) { grid.addRow(0, new Label("Control (fx:id)"), control, new Label("Property"), property); @@ -143,6 +159,10 @@ final class BindingEditor extends VBox { for (var field : java.util.List.of(control, property)) field.textProperty().addListener((o, a, b) -> methods.getItems().clear()); } + if (command) { + grid.addRow(0, new Label("Control (fx:id)"), control, commandTargets); + grid.addRow(1, new Label("Command path"), path, commandPaths); + } if (repeat) { repeatCollections.setId("repeat-collections"); repeatComponents.setId("repeat-components"); @@ -177,7 +197,9 @@ final class BindingEditor extends VBox { .addAll( bindings, grid, - repeat ? new HBox(8, inspect) : new HBox(8, inspect, computed ? methods : converters), + repeat || command + ? new HBox(8, inspect) + : new HBox(8, inspect, computed ? methods : converters), new HBox(8, fresh, apply, remove, replace), status); buttons(); @@ -209,7 +231,7 @@ final class BindingEditor extends VBox { control.setText(v.control()); property.setText(v.property()); path.setText(v.path()); - direction.setValue(v.direction()); + direction.setValue(kind == BindingDocument.Kind.COMMAND ? "view" : v.direction()); converter.setText(v.converter()); method.setText(v.method()); pane.setText(v.pane()); @@ -329,6 +351,11 @@ final class BindingEditor extends VBox { } return; } + if (kind == BindingDocument.Kind.COMMAND) { + commandPaths.getItems().setAll(metadata.commandPaths()); + commandTargets.getItems().setAll(metadata.commandTargets()); + return; + } if (requested == null || !values().equals(requested)) return; if (kind == BindingDocument.Kind.COMPUTED) { methods.getItems().setAll(metadata.computed()); @@ -345,6 +372,8 @@ final class BindingEditor extends VBox { } void invalidateDiscovery() { + commandPaths.getItems().clear(); + commandTargets.getItems().clear(); updatingChoices = true; try { repeatCollections.getItems().clear(); diff --git a/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/DesignerPane.java b/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/DesignerPane.java index 5091c32..6daa4e4 100644 --- a/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/DesignerPane.java +++ b/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/DesignerPane.java @@ -46,6 +46,7 @@ public final class DesignerPane extends BorderPane implements AutoCloseable { if (bindingEditor != null) bindingEditor.selectControl(id); if (computedEditor != null) computedEditor.selectControl(id); if (repeatEditor != null) repeatEditor.selectControl(id); + if (commandEditor != null) commandEditor.selectControl(id); if (tableEditor != null) tableEditor.selectControl(id); if (referenceEditor != null) referenceEditor.selectIndex(projection.sourceIndex(id)); if (id == null || !projection.components().containsKey(id)) return; @@ -120,7 +121,7 @@ public final class DesignerPane extends BorderPane implements AutoCloseable { private DesignerWorkflow bindingWorkflow; private ViewGenerationPane viewGeneration; private BindingBrowser bindingBrowser; - private BindingEditor bindingEditor, computedEditor, repeatEditor; + private BindingEditor bindingEditor, computedEditor, repeatEditor, commandEditor; private TableEditor tableEditor; private ReferenceEditor referenceEditor; private DiagnosticsPane diagnosticsPane; @@ -194,36 +195,10 @@ public final class DesignerPane extends BorderPane implements AutoCloseable { launch.maven(), launch.tools(), this::log); - bindingEditor = - new BindingEditor( - BindingDocument.Kind.DIRECT, - this::editBinding, - id -> { - if (kit.editor().getFxomDocument() == null) return; - var target = new FXOMFxIdIndex(kit.editor().getFxomDocument()).lookup(id); - if (target != null) kit.editor().getSelection().select(target); - }, - values -> validateBindings(values, false)); - computedEditor = - new BindingEditor( - BindingDocument.Kind.COMPUTED, - this::editBinding, - id -> { - if (kit.editor().getFxomDocument() == null) return; - var target = new FXOMFxIdIndex(kit.editor().getFxomDocument()).lookup(id); - if (target != null) kit.editor().getSelection().select(target); - }, - values -> validateBindings(values, false)); - repeatEditor = - new BindingEditor( - BindingDocument.Kind.REPEAT, - this::editBinding, - id -> { - if (kit.editor().getFxomDocument() == null) return; - var target = new FXOMFxIdIndex(kit.editor().getFxomDocument()).lookup(id); - if (target != null) kit.editor().getSelection().select(target); - }, - values -> validateBindings(values, false)); + bindingEditor = editor(BindingDocument.Kind.DIRECT); + computedEditor = editor(BindingDocument.Kind.COMPUTED); + repeatEditor = editor(BindingDocument.Kind.REPEAT); + commandEditor = editor(BindingDocument.Kind.COMMAND); tableEditor = new TableEditor( this::editTable, this::navigateComponent, values -> validateBindings(values, false)); @@ -379,6 +354,8 @@ public final class DesignerPane extends BorderPane implements AutoCloseable { directBindingTab.setClosable(false); var computedTab = new Tab("Computed bindings", computedEditor); computedTab.setClosable(false); + var commandTab = new Tab("Command bindings", commandEditor); + commandTab.setClosable(false); var repeatTab = new Tab("Repeat bindings", repeatEditor); repeatTab.setClosable(false); var tableTab = new Tab("Table / Columns", tableEditor); @@ -396,6 +373,7 @@ public final class DesignerPane extends BorderPane implements AutoCloseable { new TabPane( directBindingTab, computedTab, + commandTab, repeatTab, tableTab, structureTab, @@ -546,6 +524,7 @@ public final class DesignerPane extends BorderPane implements AutoCloseable { bindingEditor.load(value.text(), value.writable()); computedEditor.load(value.text(), value.writable()); repeatEditor.load(value.text(), value.writable()); + commandEditor.load(value.text(), value.writable()); tableEditor.load(value.text(), value.writable()); referenceEditor.load(value.text(), value.writable()); submittedSource = null; @@ -578,6 +557,7 @@ public final class DesignerPane extends BorderPane implements AutoCloseable { bindingEditor.load(value.text(), value.writable()); computedEditor.load(value.text(), value.writable()); repeatEditor.load(value.text(), value.writable()); + commandEditor.load(value.text(), value.writable()); tableEditor.load(value.text(), value.writable()); referenceEditor.load(value.text(), value.writable()); if (!source.getText().equals(value.text())) { @@ -750,6 +730,7 @@ public final class DesignerPane extends BorderPane implements AutoCloseable { bindingWorkflow.cancel(); bindingBrowser.invalidate(message); repeatEditor.invalidateDiscovery(); + commandEditor.invalidateDiscovery(); tableEditor.invalidateDiscovery(); } @@ -846,6 +827,10 @@ public final class DesignerPane extends BorderPane implements AutoCloseable { if (!draft.writable()) throw new IllegalStateException("Document is read-only"); if (!draft.text().equals(intent.source())) throw new IllegalStateException("Document changed; select the source object again"); + if (!intent.change().importedCommandTargets().isEmpty()) { + validateCommandPaste(intent); + return; + } if (intent.validate()) pendingReferenceValidation = apply(draft.text(), intent.change().edits()); submit(intent.change().edits(), intent.label()); @@ -855,6 +840,58 @@ public final class DesignerPane extends BorderPane implements AutoCloseable { }); } + /** Validate imported Command paths against the destination snapshot before the first write. */ + private void validateCommandPaste(ReferenceEditor.Intent intent) { + var original = draft; + String proposed = apply(original.text(), intent.change().edits()); + var candidate = + new DocumentSnapshot( + original.document(), original.revision(), proposed, original.writable(), true); + bindingWorkflow + .bindingDetails(last, candidate, "", "view", "", "") + .whenComplete( + (metadata, error) -> + fx( + () -> { + if (closed) return; + if (error != null) { + referenceEditor.error( + "Cannot validate the destination Commands: " + error.getMessage()); + return; + } + if (draft.revision() != original.revision() + || !source.getText().equals(original.text())) { + referenceEditor.error( + "Destination changed. Select the destination and paste again."); + return; + } + if (!metadata + .commandTargets() + .containsAll(intent.change().importedCommandTargets())) { + referenceEditor.error( + "The destination build could not resolve the copied Command targets."); + return; + } + var conflicts = + metadata.problems().stream() + .filter( + p -> + p.severity().equals("ERROR") + && intent + .change() + .importedCommandTargets() + .contains(p.control())) + .map(p -> p.message()) + .toList(); + if (!conflicts.isEmpty()) { + referenceEditor.error(String.join("; ", conflicts)); + return; + } + pendingReferenceValidation = proposed; + submit(intent.change().edits(), intent.label()); + })); + } + private void editTable(TableEditor.Intent intent) { withDraft( () -> { @@ -869,6 +906,19 @@ public final class DesignerPane extends BorderPane implements AutoCloseable { }); } + private BindingEditor editor(BindingDocument.Kind kind) { + return new BindingEditor( + kind, + this::editBinding, + id -> { + var document = kit.editor().getFxomDocument(); + if (document == null) return; + var selection = new FXOMFxIdIndex(document).lookup(id); + if (selection != null) kit.editor().getSelection().select(selection); + }, + values -> validateBindings(kind == BindingDocument.Kind.COMMAND ? null : values, false)); + } + private void editBinding(BindingEditor.Intent intent) { withDraft( () -> { @@ -891,7 +941,9 @@ public final class DesignerPane extends BorderPane implements AutoCloseable { ? repeatEditor : intent.kind() == BindingDocument.Kind.COMPUTED ? computedEditor - : bindingEditor) + : intent.kind() == BindingDocument.Kind.COMMAND + ? commandEditor + : bindingEditor) .error(error.getMessage()); } }); @@ -979,6 +1031,10 @@ public final class DesignerPane extends BorderPane implements AutoCloseable { : "Current successful build") + " · draft revision " + captured.revision()); + commandEditor.metadata( + metadata, + values, + last ? "Retained build" : "Current successful build"); repeatEditor.metadata( metadata, values, @@ -1159,7 +1215,11 @@ public final class DesignerPane extends BorderPane implements AutoCloseable { return; } var selected = examples.getValue(); - if (selected == null || projection == null) return; + if (selected == null) { + status.setText("Select a preview example after Build and Refresh."); + return; + } + if (projection == null && !interactiveRequested) return; long ticket = renderSequence.incrementAndGet(); host.documents() .snapshots() @@ -1191,7 +1251,7 @@ public final class DesignerPane extends BorderPane implements AutoCloseable { new DocumentSnapshot( draft.document(), draft.revision(), - projection.previewText(), + projection == null ? draft.text() : projection.previewText(), draft.writable(), draft.dirty())); renderComponents(List.copyOf(drafts.values()), selected.id(), ticket); @@ -1202,7 +1262,7 @@ public final class DesignerPane extends BorderPane implements AutoCloseable { private void renderComponents(List drafts, String example, long ticket) { if (ticket != renderSequence.get() || closed) return; var nodes = new LinkedHashMap(); - for (String id : projection.components().keySet()) { + for (String id : projection == null ? Set.of() : projection.components().keySet()) { var region = proxy(id); if (region == null) continue; int @@ -1246,7 +1306,10 @@ public final class DesignerPane extends BorderPane implements AutoCloseable { })); } if (nodes.isEmpty()) { - status.setText("No embedded Components; use Interactive preview for this View"); + status.setText( + projection == null + ? "Canvas projection unavailable; interactive preview uses the FXML draft" + : "No embedded Components; use Interactive preview for this View"); return; } workflow diff --git a/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/FxmlDiagnostics.java b/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/FxmlDiagnostics.java index a52249f..5bd4dd7 100644 --- a/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/FxmlDiagnostics.java +++ b/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/FxmlDiagnostics.java @@ -44,6 +44,9 @@ final class FxmlDiagnostics { if ((xml.named(e, "DirectBinding") || xml.named(e, "ComputedBinding")) && property(e, "control").equals(p.control()) && property(e, "property").equals(p.property())) matches.add(i); + else if (xml.named(e, "CommandBinding") + && p.property().equals("command") + && property(e, "control").equals(p.control())) matches.add(i); else if (xml.named(e, "Repeat") && p.property().equals("children") && property(e, "pane").equals(p.control())) matches.add(i); diff --git a/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/FxmlProjection.java b/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/FxmlProjection.java index 46b33e5..de4bfff 100644 --- a/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/FxmlProjection.java +++ b/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/FxmlProjection.java @@ -212,7 +212,8 @@ public final class FxmlProjection { || attr.getName().equals("vgap")) && !attr.getName().equals("fx:id")) e.removeAttributeNode(attr); } - } else if (Character.isUpperCase(tag.charAt(0)) + } else if (isObject(e) + && Character.isUpperCase(tag.charAt(0)) && tag.contains(".") && !tag.startsWith("javafx.")) throw new IOException("Unsupported application element: " + tag); diff --git a/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/ReferenceDocument.java b/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/ReferenceDocument.java index ed35cf6..9879054 100644 --- a/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/ReferenceDocument.java +++ b/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/ReferenceDocument.java @@ -17,7 +17,11 @@ final class ReferenceDocument { record Clip(String source, int index, String origin) {} - record Change(List edits, List notes) {} + record Change(List edits, List notes, Set importedCommandTargets) { + Change(List edits, List notes) { + this(edits, notes, Set.of()); + } + } private record Target(Element binding, Element element, String attribute, String value) {} @@ -54,6 +58,7 @@ final class ReferenceDocument { private boolean binding(Element e) { if (!(xml.named(e, "DirectBinding") + || xml.named(e, "CommandBinding") || xml.named(e, "ComputedBinding") || xml.named(e, "Repeat"))) return false; Node parent = e.getParentNode(); @@ -211,9 +216,9 @@ final class ReferenceDocument { Change paste(Clip clip, int destination, String origin) throws Exception { if (clip == null) throw new IllegalArgumentException("Copy a subtree first"); Element parent = entry(destination); - requireContainer(parent); var from = new ReferenceDocument(clip.source()); Element selected = from.entry(clip.index()); + String childProperty = childProperty(parent, selected); if (!clip.origin().equals(origin)) for (var e : from.xml.elements) if (within(e, selected)) @@ -273,7 +278,7 @@ final class ReferenceDocument { .collect(java.util.stream.Collectors.joining("\n")); Element container = children(parent).stream() - .filter(c -> c.getTagName().equals("children")) + .filter(c -> c.getTagName().equals(childProperty)) .findFirst() .orElse(parent); String result = xml.source.apply(List.of(xml.append(container, fragment, ""))); @@ -299,7 +304,16 @@ final class ReferenceDocument { + " validation."); } notes.add("Assigned identifiers: " + mapping); - return new Change(difference(xml.text, result), List.copyOf(notes)); + var importedCommands = new HashSet(); + if (!clip.origin().equals(origin)) { + for (var target : from.targets) { + if (from.xml.named(target.binding(), "CommandBinding") + && mapping.containsKey(target.value())) + importedCommands.add(mapping.get(target.value())); + } + } + return new Change( + difference(xml.text, result), List.copyOf(notes), Set.copyOf(importedCommands)); } private String fragment(Element e, List edits) { @@ -407,7 +421,23 @@ final class ReferenceDocument { return p; } - private void requireContainer(Element e) { + private String childProperty(Element e, Element child) { + String childName = child.getTagName(); + String type = childName.substring(childName.lastIndexOf('.') + 1); + boolean menuItem = + Set.of( + "MenuItem", + "Menu", + "RadioMenuItem", + "CheckMenuItem", + "CustomMenuItem", + "SeparatorMenuItem") + .contains(type); + if (e.getTagName().equals("Menu") || e.getTagName().equals("javafx.scene.control.Menu")) { + if (!menuItem) throw new IllegalArgumentException("A Menu accepts MenuItem children"); + return "items"; + } + if (menuItem) throw new IllegalArgumentException("Paste a MenuItem into a Menu"); String name = e.getTagName(); if (FX.equals(e.getNamespaceURI()) && "root".equals(e.getLocalName())) name = e.getAttribute("type"); @@ -427,6 +457,7 @@ final class ReferenceDocument { "Paste into a standard Pane; other child properties require Source editing"); if (children(e).stream().filter(c -> c.getTagName().equals("children")).count() > 1) throw new IllegalArgumentException("Ambiguous children containers"); + return "children"; } private String appendBindings(String body) throws Exception { diff --git a/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/ReferenceEditor.java b/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/ReferenceEditor.java index 8054e80..534079f 100644 --- a/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/ReferenceEditor.java +++ b/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/ReferenceEditor.java @@ -39,7 +39,7 @@ final class ReferenceEditor extends VBox { .addAll( new Label( "Select a source object or its canvas item. Paste adds children to the selected" - + " Pane."), + + " Pane or Menu."), nodes, new HBox( 6, @@ -59,7 +59,7 @@ final class ReferenceEditor extends VBox { clipboard = document.copy(selected(), origin); actionStatus = "Copied subtree and its associated bindings. Select a destination Pane" - + " in this or another View."; + + " or Menu in this or another View."; status.setText(actionStatus); } catch (Exception e) { error(e.getMessage()); diff --git a/designer-kit-ui/src/test/java/de/neitzel/fxdesigner/kit/FxmlProjectionTest.java b/designer-kit-ui/src/test/java/de/neitzel/fxdesigner/kit/FxmlProjectionTest.java index 62164ab..ad71ee9 100644 --- a/designer-kit-ui/src/test/java/de/neitzel/fxdesigner/kit/FxmlProjectionTest.java +++ b/designer-kit-ui/src/test/java/de/neitzel/fxdesigner/kit/FxmlProjectionTest.java @@ -34,6 +34,26 @@ class FxmlProjectionTest { assertTrue(restored.contains("neitzel-model")); } + @Test + void editsTheStatusBarBesideAnAttachedMarginWithoutChangingItsXml() throws Exception { + String source = """ + + + + + + + + + + """; + var projection = new FxmlProjection(source); + String projected = projection.project(); + String edited = projection.restore(projected.replace("spacing=\"8\"", "spacing=\"10\"")); + assertEquals(source.replace("spacing='8'", "spacing='10'"), edited); + assertEquals(source, projection.restore(projected)); + } + @Test void rejectsExternalEntitiesAndExecutableFXML() throws Exception { assertThrows( diff --git a/designer-kit-ui/src/test/java/de/neitzel/fxdesigner/kit/ReferenceDocumentTest.java b/designer-kit-ui/src/test/java/de/neitzel/fxdesigner/kit/ReferenceDocumentTest.java index f9e1d15..beb90ba 100644 --- a/designer-kit-ui/src/test/java/de/neitzel/fxdesigner/kit/ReferenceDocumentTest.java +++ b/designer-kit-ui/src/test/java/de/neitzel/fxdesigner/kit/ReferenceDocumentTest.java @@ -96,6 +96,26 @@ class ReferenceDocumentTest { assertTrue(twice.contains("f:id='childCopy2'")); } + @Test + void copiesMenuCommandsWithFreshIdsAndTracksTargetsForDestinationValidation() throws Exception { + String text = + "" + + "" + + "" + + ""; + var menu = new ReferenceDocument(text); + var clip = menu.copy(index(menu, "open"), "source"); + var copied = menu.paste(clip, index(menu, "file"), "destination"); + assertEquals(Set.of("openCopy"), copied.importedCommandTargets()); + String result = apply(text, copied); + assertTrue(result.contains("fx:id='openCopy' accelerator='Shortcut+O'")); + assertTrue(result.contains("control='openCopy' path='open'")); + var layout = new ReferenceDocument(XML); + assertThrows( + IllegalArgumentException.class, + () -> layout.paste(clip, index(layout, "box"), "destination")); + } + @Test void carriesNamespacesImportsAndReportsDestinationContextDifferences() throws Exception { String source = diff --git a/designer-target-tools/src/main/java/de/neitzel/fxdesigner/target/BindingDiscovery.java b/designer-target-tools/src/main/java/de/neitzel/fxdesigner/target/BindingDiscovery.java index daf60f5..9fe1825 100644 --- a/designer-target-tools/src/main/java/de/neitzel/fxdesigner/target/BindingDiscovery.java +++ b/designer-target-tools/src/main/java/de/neitzel/fxdesigner/target/BindingDiscovery.java @@ -1,7 +1,7 @@ package de.neitzel.fxdesigner.target; -import de.neitzel.fxdesigner.protocol.BindingMetadata.*; import de.neitzel.fxdesigner.protocol.BindingMetadata; +import de.neitzel.fxdesigner.protocol.BindingMetadata.*; import java.beans.*; import java.io.*; import java.lang.reflect.*; @@ -28,6 +28,7 @@ public final class BindingDiscovery { private record TargetType(Class type, boolean writable) {} + private final Set actionHandlers = new HashSet<>(); private final Map> controlTypes = new HashMap<>(); private RepeatDiscovery repeats; private final Map targetTypes = new HashMap<>(); @@ -313,6 +314,10 @@ public final class BindingDiscovery { ? owner.controller : resolve(e, imports); controlTypes.put(id, control); + if (e.hasAttribute("onAction")) actionHandlers.add(id); + for (Node child = e.getFirstChild(); child != null; child = child.getNextSibling()) + if (child instanceof Element property && property.getTagName().equals("onAction")) + actionHandlers.add(id); String componentModel = ""; var componentResource = control.getResource(control.getSimpleName() + ".fxml"); if (componentResource != null) @@ -398,7 +403,8 @@ public final class BindingDiscovery { : converters.candidates(source, chosen == null ? null : chosen.type(), direction); repeats = new RepeatDiscovery(loader, attributes, owner, incompleteModel, controlTypes, ids); var tables = new TableDiscovery(loader, repeats, modelType, owner); - var problems = new ArrayList<>(validate(doc, imports, ids, converters)); + var commands = commandPaths(); + var problems = new ArrayList<>(validate(doc, imports, ids, converters, commands)); problems.addAll(tables.validate(doc)); var componentOptions = repeats.candidates(selected); for (var a : attributes) @@ -421,7 +427,35 @@ public final class BindingDiscovery { repeats.panes(), tables.collections(), tables.rows(selected), - tables.columns(selected)); + tables.columns(selected), + commands, + controlTypes.entrySet().stream() + .filter(e -> commandTarget(e.getValue())) + .map(Map.Entry::getKey) + .sorted() + .toList()); + } + + private List commandPaths() { + if (owner.viewModel == null) return List.of(); + return Arrays.stream(owner.viewModel.getMethods()) + .filter( + m -> + !Modifier.isStatic(m.getModifiers()) + && m.getParameterCount() == 0 + && m.getName().startsWith("get") + && m.getName().length() > 3 + && de.neitzel.fx.view.Command.class.isAssignableFrom(m.getReturnType())) + .map(m -> java.beans.Introspector.decapitalize(m.getName().substring(3))) + .distinct() + .sorted() + .toList(); + } + + private static boolean commandTarget(Class type) { + return type != null + && (javafx.scene.control.ButtonBase.class.isAssignableFrom(type) + || javafx.scene.control.MenuItem.class.isAssignableFrom(type)); } private void selectOwner(Document doc, List imports, String resource, Class model) @@ -492,17 +526,31 @@ public final class BindingDiscovery { .anyMatch(a -> a.path().equals(path) && Set.of("COMPLEX", "COLLECTION").contains(a.kind())); } + private static String propertyValue(Element declaration, String name) { + if (declaration.hasAttribute(name)) return declaration.getAttribute(name); + for (Node n = declaration.getFirstChild(); n != null; n = n.getNextSibling()) + if (n instanceof Element child && child.getTagName().equals(name)) + return child.getTextContent().trim(); + return ""; + } + private List validate( - Document doc, List imports, Set ids, ConverterDiscovery converters) + Document doc, + List imports, + Set ids, + ConverterDiscovery converters, + List commands) throws Exception { var problems = new ArrayList(selectionProblems); problems.addAll(owner.problems); var occupied = new HashSet(); + var commandControls = new TreeSet(); var nodes = doc.getElementsByTagName("*"); for (int i = 0; i < nodes.getLength(); i++) { var e = (Element) nodes.item(i); if (!library(e, "DirectBinding", imports) && !library(e, "ComputedBinding", imports) + && !library(e, "CommandBinding", imports) && !library(e, "Repeat", imports)) continue; Node parent = e.getParentNode(); if (parent instanceof Element p && p.getTagName().equals("elements")) @@ -529,9 +577,39 @@ public final class BindingDiscovery { } continue; } - String control = e.getAttribute("control"), - property = e.getAttribute("property"), + String control = propertyValue(e, "control"), + property = propertyValue(e, "property"), key = control + "." + property; + if (library(e, "CommandBinding", imports)) { + String path = propertyValue(e, "path"); + if (!ids.contains(control)) + problems.add( + new Problem(control, "command", "ERROR", "Unknown Command target fx:id: " + control)); + else if (!commandTarget(controlTypes.get(control))) + problems.add( + new Problem( + control, + "command", + "ERROR", + "Command target must be a ButtonBase or MenuItem: " + control)); + if (path.isBlank() || path.contains(".")) + problems.add( + new Problem(control, "command", "ERROR", "Expected a simple Command path: " + path)); + else if (!commands.contains(path)) + problems.add(new Problem(control, "command", "ERROR", "Unknown Command path: " + path)); + if (!commandControls.add(control)) + problems.add( + new Problem( + control, + "command", + "ERROR", + "Duplicate Command binding for disable and onAction")); + if (actionHandlers.contains(control)) + problems.add( + new Problem( + control, "command", "ERROR", "Command conflicts with existing onAction handler")); + continue; + } if (!occupied.add(key)) problems.add(new Problem(control, property, "ERROR", "Duplicate binding target")); @@ -624,6 +702,15 @@ public final class BindingDiscovery { } if (!severity.isEmpty()) problems.add(new Problem(control, property, severity, message)); } + for (String control : commandControls) + for (String endpoint : List.of("disable", "onAction")) + if (occupied.contains(control + "." + endpoint)) + problems.add( + new Problem( + control, + "command", + "ERROR", + "Command conflicts with another binding for " + endpoint)); return problems; } diff --git a/designer-target-tools/src/main/java/de/neitzel/fxdesigner/target/TargetPreview.java b/designer-target-tools/src/main/java/de/neitzel/fxdesigner/target/TargetPreview.java index 09aeae5..43621a4 100644 --- a/designer-target-tools/src/main/java/de/neitzel/fxdesigner/target/TargetPreview.java +++ b/designer-target-tools/src/main/java/de/neitzel/fxdesigner/target/TargetPreview.java @@ -12,13 +12,12 @@ import javafx.scene.*; import javafx.scene.image.WritableImage; import javafx.scene.paint.Color; -/** - * One preview session per target JVM: the library retains process-wide Model/ViewModel identities. - */ +/** A worker owns one preview session and its managed Views. */ public final class TargetPreview implements AutoCloseable { private static final AtomicBoolean CONSUMED = new AtomicBoolean(); private PreviewSession session; private LoadedView view; + private final ViewModelContext models = new ViewModelContext(); private Scene scene; private final Occurrences occurrences = new Occurrences(); private javafx.stage.Stage interactive; @@ -128,31 +127,9 @@ public final class TargetPreview implements AutoCloseable { if (component) { String name = d.viewResource().substring(0, d.viewResource().length() - 5).replace('/', '.'); - Class componentType = Class.forName(name, false, loader); - if (!Parent.class.isAssignableFrom(componentType)) - throw new IllegalArgumentException("Component root is not a Parent"); - byte[] wrapper = ("<" + name + "/>").getBytes(java.nio.charset.StandardCharsets.UTF_8); - var wrapped = - new java.net.URL( - null, - "memory:component", - new java.net.URLStreamHandler() { - protected java.net.URLConnection openConnection(java.net.URL url) { - return new java.net.URLConnection(url) { - public void connect() {} - - public java.io.InputStream getInputStream() { - return new java.io.ByteArrayInputStream(wrapper); - } - }; - } - }); - view = ViewLoader.load(wrapped, session.model(), context); - var model = componentType.getMethod("modelProperty").invoke(view.getRoot()); - if (!(model instanceof javafx.beans.property.Property property)) - throw new IllegalArgumentException("Component has no writable Model"); - property.setValue(session.model()); - } else view = ViewLoader.load(resource, session.model(), context); + var componentType = Class.forName(name, false, loader).asSubclass(Parent.class); + view = ViewLoader.loadComponent(componentType, session.model(), context, models); + } else view = ViewLoader.load(resource, session.model(), context, models); scene = new Scene(view.getRoot(), 420, 240); layout(420, 240); var wait = session.ready(); @@ -316,7 +293,9 @@ public final class TargetPreview implements AutoCloseable { closed = true; timer.shutdownNow(); try { - if (session != null) session.close(); + try (var ownedSession = session) { + models.close(); + } cleanup.complete(null); } catch (Throwable e) { if (failure != null) failure.addSuppressed(e); diff --git a/designer-target-tools/src/test/java/de/neitzel/fxdesigner/target/BindingDiscoveryTest.java b/designer-target-tools/src/test/java/de/neitzel/fxdesigner/target/BindingDiscoveryTest.java index 5bd2ba7..4b886fe 100644 --- a/designer-target-tools/src/test/java/de/neitzel/fxdesigner/target/BindingDiscoveryTest.java +++ b/designer-target-tools/src/test/java/de/neitzel/fxdesigner/target/BindingDiscoveryTest.java @@ -112,6 +112,67 @@ class BindingDiscoveryTest { () -> BindingDiscovery.inspect(fxml(), "", "wrong", getClass().getClassLoader())); } + @de.neitzel.fx.view.ViewModelClass(Commands.class) + public static class Controller {} + + public static class Commands { + public Commands(Model model) { + throw new AssertionError("Discovery constructed the ViewModel"); + } + + public de.neitzel.fx.view.Command getOpen() { + throw new AssertionError("Discovery executed the Command getter"); + } + } + + @Test + void commandDiscoveryResolvesTargetsAndLocatesConflictsInEitherOrder() throws Exception { + String command = ""; + String xml = + "" + + "" + + "" + + "" + + command + + "