Edit form controls and render localized application previews in the IntelliJ designer

This commit is contained in:
2026-11-24 15:13:31 +01:00
parent 73da1f985e
commit 93a9f04b11
18 changed files with 1623 additions and 63 deletions
@@ -35,6 +35,13 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
return content;
}
private FormEditor formEditor;
private final ImageView wholeImage = new ImageView();
private final Spinner<Integer> previewWidth = new Spinner<>(120, 1600, 760, 40);
private final Spinner<Integer> previewHeight = new Spinner<>(100, 1200, 520, 40);
private final Label previewState = new Label("Build and Refresh, then select an example.");
private boolean showWhole;
private final VBox componentInspector = new VBox(4);
private void componentSelection() {
@@ -43,6 +50,7 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
var hit = kit.editor().getSelection().getHitItem();
if (hit == null) return;
String id = hit.getFxId();
if (formEditor != null) formEditor.selectIndex(projection.sourceIndex(id));
if (bindingEditor != null) bindingEditor.selectControl(id);
if (computedEditor != null) computedEditor.selectControl(id);
if (repeatEditor != null) repeatEditor.selectControl(id);
@@ -233,7 +241,7 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
fx(
() -> {
invalidateBindings("Document changed; discover bindings again.");
if (interactiveRequested)
if (interactiveRequested && affectsInteractive(uri))
interactiveStatus.setText(
"Interactive preview uses earlier drafts; rerun or save to"
+ " restart");
@@ -252,7 +260,7 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
if (Path.of(uri).startsWith(reactorRoot))
fx(
() -> {
if (interactiveRequested) {
if (interactiveRequested && affectsInteractive(uri)) {
interactiveRestart = true;
interactiveRequest++;
interactiveWorkflow.cancel();
@@ -330,13 +338,68 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
return null;
}
});
examples.setId("preview-example");
examples
.valueProperty()
.addListener(
(o, before, after) -> {
renderSequence.incrementAndGet();
clearFormPreview(
after == null
? "Build and Refresh, then select an example."
: "Rendering " + after.label() + "…");
if (interactiveRequested) {
interactiveRequest++;
interactiveWorkflow.cancel();
interactiveRestart = true;
}
resize.playFromStart();
});
previewWidth.setId("preview-width");
previewHeight.setId("preview-height");
previewState.setId("preview-state");
wholeImage.setId("full-preview-image");
wholeImage.setPreserveRatio(true);
var picture = new ScrollPane(wholeImage);
picture.setMinSize(0, 0);
picture
.viewportBoundsProperty()
.addListener(
(o, a, bounds) -> {
wholeImage.setFitWidth(Math.max(1, bounds.getWidth()));
wholeImage.setFitHeight(Math.max(1, bounds.getHeight()));
});
VBox.setVgrow(picture, Priority.ALWAYS);
var fullPreview =
new VBox(
4,
new FlowPane(
5, 4, new Label("Width"), previewWidth, new Label("Height"), previewHeight),
previewState,
picture);
previewWidth.valueProperty().addListener((o, a, b) -> previewSizeChanged());
previewHeight.valueProperty().addListener((o, a, b) -> previewSizeChanged());
var split = new SplitPane(source, design);
split
.getItems()
.addListener(
(javafx.collections.ListChangeListener<javafx.scene.Node>)
change -> {
showWhole = split.getItems().contains(fullPreview);
});
sourceSplit = split;
split.setDividerPositions(.3);
var modes =
new ToolBar(
button("Source", () -> split.getItems().setAll(source)),
button("Design", () -> split.getItems().setAll(design)),
button(
"Form preview",
() -> {
split.getItems().setAll(design, fullPreview);
split.setDividerPositions(.48);
render();
}),
button(
"Split",
() -> {
@@ -381,7 +444,48 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
discoveryTab,
generationTab));
bindings.setExpanded(false);
center.setBottom(new VBox(properties, bindings));
formEditor =
new FormEditor(
edit ->
flush()
.whenComplete(
(ok, error) ->
fx(
() -> {
if (error != null) report(error);
else if (draft.writable() && draft.text().equals(edit.source()))
submit(edit.changes(), "Edit form");
else
status.setText("Document changed; select the control again.");
})),
index -> {
if (loading || projection == null || kit.editor().getFxomDocument() == null) return;
String id = projection.canvasId(index);
if (id == null) return;
var object = new FXOMFxIdIndex(kit.editor().getFxomDocument()).lookup(id);
if (object != null && kit.editor().getSelection().getHitItem() != object)
kit.editor().getSelection().select(object);
},
values -> {
bindings.setExpanded(true);
directBindingTab.getTabPane().getSelectionModel().select(directBindingTab);
bindingEditor.newBinding(values);
});
var formPane = new TitledPane("Form controls", formEditor);
formPane.setExpanded(false);
formPane
.expandedProperty()
.addListener(
(o, a, b) -> {
if (b) bindings.setExpanded(false);
});
bindings
.expandedProperty()
.addListener(
(o, a, b) -> {
if (b) formPane.setExpanded(false);
});
center.setBottom(new VBox(properties, formPane, bindings));
output.setEditable(false);
output.setPrefRowCount(5);
Path origin = file.resolveSibling(file.getFileName() + ".origin");
@@ -421,7 +525,8 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
fx(
() -> {
if (error != null) report(error);
else if (draft == null || d.revision() != draft.revision())
else if (!pending
&& (draft == null || d.revision() > draft.revision()))
load(d);
}));
}));
@@ -480,6 +585,11 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
new HostServices.Diagnostic(
"Designer", HostServices.Severity.ERROR, String.valueOf(error), Optional.empty()));
if (draft != null && diagnosticsPane != null) validationUnavailable(error);
if (showWhole)
clearFormPreview(
"Preview unavailable · "
+ error.getMessage()
+ " · use Build and Refresh after correcting the problem.");
status.setText("Designer: " + error.getMessage());
log(error.toString());
}
@@ -520,6 +630,8 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
}
}
draft = value;
formEditor.load(value.text(), value.writable());
clearFormPreview("Draft changed · select an example to render the current revision.");
publishDiagnostics("Source checked; target validation outdated", java.util.List.of());
bindingEditor.load(value.text(), value.writable());
computedEditor.load(value.text(), value.writable());
@@ -553,6 +665,9 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
var hit = kit.editor().getSelection().getHitItem();
String selectedId = hit == null ? null : hit.getFxId();
draft = value;
String requestedSelection = formEditor.load(value.text(), value.writable());
if (!requestedSelection.isEmpty()) selectedId = requestedSelection;
clearFormPreview("Draft changed · select an example to render the current revision.");
publishDiagnostics("Source checked; target validation outdated", java.util.List.of());
bindingEditor.load(value.text(), value.writable());
computedEditor.load(value.text(), value.writable());
@@ -627,7 +742,7 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
loading = false;
// The change debounce may already have fired against the previous draft.
// Render only after the authoritative revision and its projection agree.
if (projection != null && examples.getValue() != null) resize.playFromStart();
if ((projection != null || showWhole) && examples.getValue() != null) resize.playFromStart();
if (pendingSourceOffset != null) {
int offset = pendingSourceOffset;
pendingSourceOffset = null;
@@ -956,6 +1071,7 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
errors.addAll(new ReferenceDocument(draft.text()).referenceErrors());
if (errors.isEmpty()) return true;
status.setText("Preview blocked: " + String.join("; ", errors));
clearFormPreview(status.getText());
if (projection != null)
for (String id : projection.components().keySet()) {
var region = proxy(id);
@@ -1006,6 +1122,10 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
validationUnavailable(error);
status.setText(
"Binding validation unavailable: " + error.getMessage());
clearFormPreview(
status.getText()
+ " · use Build and Refresh, or choose Preview last"
+ " successful.");
return;
}
validatedSource = captured.text();
@@ -1116,6 +1236,7 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
withDraft(
() -> {
renderSequence.incrementAndGet();
clearFormPreview("Building selected context…");
status.setText("Building selected context…");
buildRun = workflow.build(approvals);
buildRun
@@ -1151,6 +1272,11 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
}
} else if (result.status() == BuildRefresh.Status.READY)
discover(false);
else
clearFormPreview(
result.message()
+ " · correct the build and use Build and Refresh, or choose"
+ " Preview last successful.");
}));
});
}
@@ -1197,6 +1323,27 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
});
}
private void clearFormPreview(String message) {
wholeImage.setImage(null);
previewState.setText(message);
}
private void previewSizeChanged() {
renderSequence.incrementAndGet();
wholeImage.setImage(null);
previewState.setText("Rendering selected size…");
resize.playFromStart();
}
private boolean affectsInteractive(java.net.URI uri) {
if (file.toUri().equals(uri) || Path.of(uri).equals(file)) return true;
if (projection == null) return false;
String path = Path.of(uri).toString().replace(java.io.File.separatorChar, '/');
return java.util.stream.Stream.concat(
projection.navigationTypes().values().stream(), projection.rowComponents().stream())
.anyMatch(type -> path.endsWith("/" + type.replace('.', '/') + ".fxml"));
}
public void render() {
withDraft(
() -> {
@@ -1212,14 +1359,18 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
.filter(p -> p.severity().equals("ERROR"))
.map(p -> p.message())
.collect(java.util.stream.Collectors.joining("; ")));
clearFormPreview(status.getText());
return;
}
var selected = examples.getValue();
if (selected == null) {
status.setText("Select a preview example after Build and Refresh.");
previewState.setText(
"Select a preview example after Build and Refresh. If none is listed, register an"
+ " example for this FXML.");
status.setText(previewState.getText());
return;
}
if (projection == null && !interactiveRequested) return;
if (projection == null && !interactiveRequested && !showWhole) return;
long ticket = renderSequence.incrementAndGet();
host.documents()
.snapshots()
@@ -1305,6 +1456,8 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
+ " restart");
}));
}
if (showWhole)
nodes.put("", new DesignerWorkflow.Size(previewWidth.getValue(), previewHeight.getValue()));
if (nodes.isEmpty()) {
status.setText(
projection == null
@@ -1320,14 +1473,19 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
() -> {
if (ticket != renderSequence.get()) return;
if (error != null) {
projection
.components()
.keySet()
.forEach(
id -> {
var region = proxy(id);
if (region != null) region.failed();
});
if (showWhole) {
wholeImage.setImage(null);
previewState.setText("Preview failed: " + error.getMessage());
}
if (projection != null)
projection
.components()
.keySet()
.forEach(
id -> {
var region = proxy(id);
if (region != null) region.failed();
});
report(error);
return;
}
@@ -1354,6 +1512,21 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
argb,
0,
width);
if (frame.node().isEmpty()) {
wholeImage.setImage(image);
previewState.setText(
(last
? "Retained build · Build and Refresh for current sources · "
: "")
+ examples.getValue().label()
+ " · "
+ width
+ " × "
+ height
+ " · revision "
+ draft.revision());
continue;
}
images.put(frame.node(), image);
var region = proxy(frame.node());
if (region != null) {
@@ -1365,14 +1538,15 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
layoutPasses = measurementsChanged ? layoutPasses + 1 : 0;
if (layoutPasses >= 8) {
resize.stop();
projection
.components()
.keySet()
.forEach(
id -> {
var region = proxy(id);
if (region != null) region.failed();
});
if (projection != null)
projection
.components()
.keySet()
.forEach(
id -> {
var region = proxy(id);
if (region != null) region.failed();
});
status.setText("Target layout did not stabilize; retaining last layout");
return;
}