Edit and exercise Archiver CommandBindings in the IntelliJ designer
This commit is contained in:
@@ -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<Replacement> 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
|
||||
|
||||
@@ -31,6 +31,7 @@ final class BindingEditor extends VBox {
|
||||
private final BindingDocument.Kind kind;
|
||||
private final ComboBox<BindingMetadata.Converter> converters = new ComboBox<>();
|
||||
private final Button inspect = new Button("Find Converters / Validate");
|
||||
private final ComboBox<String> commandPaths = new ComboBox<>(), commandTargets = new ComboBox<>();
|
||||
private final ComboBox<String> 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();
|
||||
|
||||
@@ -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<DocumentSnapshot> drafts, String example, long ticket) {
|
||||
if (ticket != renderSequence.get() || closed) return;
|
||||
var nodes = new LinkedHashMap<String, DesignerWorkflow.Size>();
|
||||
for (String id : projection.components().keySet()) {
|
||||
for (String id : projection == null ? Set.<String>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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -17,7 +17,11 @@ final class ReferenceDocument {
|
||||
|
||||
record Clip(String source, int index, String origin) {}
|
||||
|
||||
record Change(List<Replacement> edits, List<String> notes) {}
|
||||
record Change(List<Replacement> edits, List<String> notes, Set<String> importedCommandTargets) {
|
||||
Change(List<Replacement> edits, List<String> 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<String>();
|
||||
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<Replacement> 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 {
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -34,6 +34,26 @@ class FxmlProjectionTest {
|
||||
assertTrue(restored.contains("neitzel-model"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void editsTheStatusBarBesideAnAttachedMarginWithoutChangingItsXml() throws Exception {
|
||||
String source = """
|
||||
<?import javafx.scene.layout.*?>
|
||||
<?import javafx.geometry.Insets?>
|
||||
<?import de.neitzel.fx.view.BindingList?>
|
||||
<BorderPane xmlns:fx="http://javafx.com/fxml/1">
|
||||
<fx:define><BindingList/></fx:define>
|
||||
<bottom><HBox spacing='8'>
|
||||
<BorderPane.margin><Insets top='5' right='12' bottom='9' left='12'/></BorderPane.margin>
|
||||
</HBox></bottom>
|
||||
</BorderPane>
|
||||
""";
|
||||
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(
|
||||
|
||||
@@ -96,6 +96,26 @@ class ReferenceDocumentTest {
|
||||
assertTrue(twice.contains("f:id='childCopy2'"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void copiesMenuCommandsWithFreshIdsAndTracksTargetsForDestinationValidation() throws Exception {
|
||||
String text =
|
||||
"<?import de.neitzel.fx.view.*?><?import javafx.scene.control.*?>"
|
||||
+ "<Menu xmlns:fx='http://javafx.com/fxml/1' fx:id='file'><fx:define><BindingList>"
|
||||
+ "<CommandBinding control='open' path='open'/></BindingList></fx:define>"
|
||||
+ "<items><MenuItem fx:id='open' accelerator='Shortcut+O'/></items></Menu>";
|
||||
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 =
|
||||
|
||||
Reference in New Issue
Block a user