Add shared source-preserving Table and Column authoring

This commit is contained in:
2026-11-13 15:07:38 +01:00
parent 0d14ac14e0
commit 3417788194
9 changed files with 796 additions and 5 deletions
@@ -104,7 +104,7 @@ final class BindingDocument {
entries.add(new Entry(indexes.get(e), values(e), !hasElementChildren(e)));
}
private boolean named(Element e, String type) {
boolean named(Element e, String type) {
if (e.getTagName().equals(PACKAGE + type)) return true;
if (!e.getTagName().equals(type)) return false;
boolean own = imports.contains(PACKAGE + type) || imports.contains(PACKAGE + "*");
@@ -372,7 +372,7 @@ final class BindingDocument {
return text.contains("\r\n") ? "\r\n" : "\n";
}
private Replacement append(Element container, String child, String attributes) {
Replacement append(Element container, String child, String attributes) {
var t = source.tags.get(indexes.get(container));
String eol = newline();
if (t.selfClosing())
@@ -45,6 +45,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 (tableEditor != null) tableEditor.selectControl(id);
if (id == null || !projection.components().containsKey(id)) return;
componentInspector.getChildren().add(new Label(projection.components().get(id) + " · " + id));
componentInspector
@@ -117,6 +118,7 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
private DesignerWorkflow bindingWorkflow;
private BindingBrowser bindingBrowser;
private BindingEditor bindingEditor, computedEditor, repeatEditor;
private TableEditor tableEditor;
private String validatedSource, validatingSource;
private boolean validationPreview;
private java.util.List<de.neitzel.fxdesigner.protocol.BindingMetadata.Problem> bindingProblems =
@@ -211,6 +213,7 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
if (target != null) kit.editor().getSelection().select(target);
},
values -> validateBindings(values, false));
tableEditor = new TableEditor(this::editTable, this::navigateComponent);
bindingBrowser =
new BindingBrowser(
this::discoverBindings,
@@ -361,9 +364,12 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
computedTab.setClosable(false);
var repeatTab = new Tab("Repeat bindings", repeatEditor);
repeatTab.setClosable(false);
var tableTab = new Tab("Table / Columns", tableEditor);
tableTab.setClosable(false);
var bindings =
new TitledPane(
"Bindings", new TabPane(directBindingTab, computedTab, repeatTab, discoveryTab));
"Bindings",
new TabPane(directBindingTab, computedTab, repeatTab, tableTab, discoveryTab));
bindings.setExpanded(false);
center.setBottom(new VBox(properties, bindings));
output.setEditable(false);
@@ -506,6 +512,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());
tableEditor.load(value.text(), value.writable());
submittedSource = null;
commitSource();
return;
@@ -530,6 +537,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());
tableEditor.load(value.text(), value.writable());
if (!source.getText().equals(value.text())) {
int anchor = source.getAnchor(), caret = source.getCaretPosition();
var edits =
@@ -685,6 +693,20 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
repeatEditor.invalidateDiscovery();
}
private void editTable(TableEditor.Intent intent) {
withDraft(
() -> {
try {
if (!draft.writable()) throw new IllegalStateException("Document is read-only");
if (!draft.text().equals(intent.source()))
throw new IllegalStateException("Document changed; select the Table again");
submit(intent.edits(), intent.label());
} catch (Exception error) {
tableEditor.error(error.getMessage());
}
});
}
private void editBinding(BindingEditor.Intent intent) {
withDraft(
() -> {
@@ -715,7 +737,8 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
private boolean bindingPreviewAllowed() {
try {
var errors = new BindingDocument(draft.text()).errors();
var errors = new ArrayList<>(new BindingDocument(draft.text()).errors());
errors.addAll(new TableDocument(draft.text()).errors());
if (errors.isEmpty()) return true;
status.setText("Preview blocked: " + String.join("; ", errors));
if (projection != null)
@@ -0,0 +1,228 @@
package de.neitzel.fxdesigner.kit;
import de.neitzel.fxdesigner.contracts.EditBatch.Replacement;
import java.util.*;
import org.w3c.dom.*;
/** Table/Column intents over the authoritative XML, never over Kit's projection. */
final class TableDocument {
record Table(int index, String id, String collection, String row, boolean editable) {
public String toString() {
return (id.isEmpty() ? "Table #" + index : id) + " ← " + collection;
}
}
record Column(int index, String attribute, String header, String width, boolean editable) {
public String toString() {
return header + " (" + attribute + ")";
}
}
final BindingDocument xml;
final List<Table> tables = new ArrayList<>();
TableDocument(String text) throws Exception {
xml = new BindingDocument(text);
for (int i = 0; i < xml.elements.size(); i++) {
var e = xml.elements.get(i);
if (xml.named(e, "Table"))
tables.add(
new Table(
i,
e.getAttributeNS(BindingDocument.FX, "id"),
e.getAttribute("collection"),
e.getAttribute("row"),
!property(e, "collection") && !property(e, "row")));
}
}
private boolean property(Element e, String name) {
return children(e).stream().anyMatch(c -> c.getTagName().equals(name));
}
private static List<Element> children(Element e) {
var result = new ArrayList<Element>();
for (Node n = e.getFirstChild(); n != null; n = n.getNextSibling())
if (n instanceof Element c) result.add(c);
return result;
}
private Element table(int index) {
if (tables.stream().noneMatch(t -> t.index() == index))
throw new IllegalArgumentException("Select a Table again");
return xml.elements.get(index);
}
private Element columnsContainer(int index) {
var e = table(index);
var wrappers = children(e).stream().filter(c -> c.getTagName().equals("columns")).toList();
if (wrappers.size() > 1
|| !wrappers.isEmpty() && children(e).stream().anyMatch(c -> xml.named(c, "Column")))
throw new IllegalArgumentException("Mixed Column containers; edit in Source");
return wrappers.isEmpty() ? e : wrappers.getFirst();
}
List<Column> columns(int index) {
return children(columnsContainer(index)).stream()
.filter(e -> xml.named(e, "Column"))
.map(
e ->
new Column(
xml.elements.indexOf(e),
e.getAttribute("attribute"),
e.getAttribute("header"),
e.getAttribute("width"),
children(e).isEmpty()))
.toList();
}
List<Replacement> configure(int index, String collection, String row) {
if (!tables.stream().filter(t -> t.index() == index).findFirst().orElseThrow().editable())
throw new IllegalArgumentException("Nested Table properties must be edited in Source");
return attributes(index, Map.of("collection", collection, "row", row));
}
List<Replacement> create(String collection, String row) {
var root = xml.document.getDocumentElement();
String name = root.getTagName();
if (BindingDocument.FX.equals(root.getNamespaceURI()) && root.getLocalName().equals("root"))
name = root.getAttribute("type");
if (!Set.of(
"VBox",
"HBox",
"Pane",
"AnchorPane",
"StackPane",
"FlowPane",
"TilePane",
"GridPane",
"javafx.scene.layout.VBox",
"javafx.scene.layout.HBox",
"javafx.scene.layout.Pane",
"javafx.scene.layout.AnchorPane",
"javafx.scene.layout.StackPane",
"javafx.scene.layout.FlowPane",
"javafx.scene.layout.TilePane",
"javafx.scene.layout.GridPane")
.contains(name))
throw new IllegalArgumentException(
"New Table requires a supported root Pane; insert it in Source for another parent");
Element parent =
children(root).stream()
.filter(c -> c.getTagName().equals("children"))
.findFirst()
.orElse(root);
String node =
"<de.neitzel.fx.view.Table collection=\""
+ escape(collection)
+ "\" row=\""
+ escape(row)
+ "\"/>";
return List.of(xml.append(parent, node, ""));
}
List<Replacement> removeTable(int index) {
if (table(index) == xml.document.getDocumentElement())
throw new IllegalArgumentException("A root Table cannot be removed; edit in Source");
return remove(index);
}
List<Replacement> writeColumn(
int table, Integer index, String attribute, String header, String width) {
if (index != null) {
var c = columns(table).stream().filter(v -> v.index() == index).findFirst().orElseThrow();
if (!c.editable())
throw new IllegalArgumentException("Nested Column properties must be edited in Source");
var values = new LinkedHashMap<String, String>();
values.put("attribute", attribute);
values.put("header", header);
values.put("width", width.isEmpty() ? null : width);
return attributes(index, values);
}
return List.of(
xml.append(
columnsContainer(table),
"<de.neitzel.fx.view.Column attribute=\""
+ escape(attribute)
+ "\" header=\""
+ escape(header)
+ "\""
+ (width.isEmpty() ? "" : " width=\"" + escape(width) + "\"")
+ "/>",
""));
}
List<Replacement> removeColumn(int table, int index) {
if (columns(table).stream().noneMatch(c -> c.index() == index))
throw new IllegalArgumentException("Select a Column again");
return remove(index);
}
List<Replacement> moveColumn(int table, int index, int delta) {
var columns = columns(table);
int from = -1;
for (int i = 0; i < columns.size(); i++) if (columns.get(i).index() == index) from = i;
if (from < 0) throw new IllegalArgumentException("Select a Column again");
int to = from + delta;
if (to < 0 || to >= columns.size()) return List.of();
var a = xml.source.tags.get(index);
var b = xml.source.tags.get(columns.get(to).index());
return List.of(
new Replacement(a.start(), a.end(), xml.text.substring(b.start(), b.end())),
new Replacement(b.start(), b.end(), xml.text.substring(a.start(), a.end())));
}
private List<Replacement> remove(int index) {
var t = xml.source.tags.get(index);
return List.of(new Replacement(t.start(), t.end(), ""));
}
private List<Replacement> attributes(int index, Map<String, String> values) {
var changed = new LinkedHashMap<String, String>();
var e = xml.elements.get(index);
values.forEach(
(k, v) -> {
if (v == null ? e.hasAttribute(k) : !v.equals(e.getAttribute(k))) changed.put(k, v);
});
return xml.source.attributes(index, changed);
}
private static String escape(String v) {
return XmlSource.escape(v, '"');
}
private static boolean path(String v) {
return v.matches(
"[\\p{javaJavaIdentifierStart}][\\p{javaJavaIdentifierPart}]*(\\.[\\p{javaJavaIdentifierStart}][\\p{javaJavaIdentifierPart}]*)*");
}
List<String> errors() {
var errors = new ArrayList<String>();
for (var t : tables) {
if (!t.editable()) continue;
if (!path(t.collection())) errors.add("Invalid Table collection: " + t);
if (!path(t.row())) errors.add("Invalid Table Row class: " + t);
try {
var columns = columns(t.index());
if (columns.isEmpty()) errors.add("Table needs at least one Column: " + t);
var seen = new HashSet<String>();
for (var c : columns) {
if (!c.editable()) continue;
if (!path(c.attribute())) errors.add("Invalid Column Attribute: " + c.attribute());
if (!seen.add(c.attribute())) errors.add("Duplicate Column Attribute: " + c.attribute());
if (c.header().isBlank()) errors.add("Column needs a header: " + c.attribute());
if (!c.width().isEmpty())
try {
double w = Double.parseDouble(c.width());
if (!Double.isFinite(w) || w <= 0) throw new NumberFormatException();
} catch (NumberFormatException e) {
errors.add("Column width must be positive and finite: " + c.attribute());
}
}
} catch (IllegalArgumentException e) {
errors.add(e.getMessage());
}
}
return List.copyOf(errors);
}
}
@@ -0,0 +1,249 @@
package de.neitzel.fxdesigner.kit;
import de.neitzel.fxdesigner.contracts.EditBatch.Replacement;
import java.util.List;
import java.util.function.Consumer;
import javafx.scene.control.*;
import javafx.scene.layout.*;
/** Shared Table and ordered Column form. Applying an intent is always a host document command. */
final class TableEditor extends VBox {
record Intent(String source, String label, List<Replacement> edits) {}
private final ListView<TableDocument.Table> tables = new ListView<>();
private final ListView<TableDocument.Column> columns = new ListView<>();
private final TextField collection = field("collection"),
row = field("row"),
attribute = field("attribute"),
header = field("header"),
width = field("width");
private final Label status = new Label();
private final Consumer<Intent> edit;
private String source = "";
private TableDocument document;
private boolean loading;
private Integer table, column;
TableEditor(Consumer<Intent> edit, Consumer<String> navigate) {
super(5);
this.edit = edit;
setId("table-editor");
tables.setId("table-bindings");
columns.setId("table-columns");
status.setId("table-status");
status.setWrapText(true);
tables.setPrefHeight(65);
columns.setPrefHeight(65);
tables
.getSelectionModel()
.selectedItemProperty()
.addListener(
(o, a, b) -> {
if (!loading && b != null) select(b);
});
columns
.getSelectionModel()
.selectedItemProperty()
.addListener(
(o, a, b) -> {
if (!loading && b != null) {
column = b.index();
attribute.setText(b.attribute());
header.setText(b.header());
width.setText(b.width());
}
});
var tableFields =
new HBox(6, new Label("Collection"), collection, new Label("Row Component"), row);
var columnFields =
new HBox(
6,
new Label("Attribute"),
attribute,
new Label("Header"),
header,
new Label("Width (optional)"),
width);
getChildren()
.addAll(
tables,
tableFields,
new HBox(
6,
button(
"New Table",
() -> {
tables.getSelectionModel().clearSelection();
table = null;
column = null;
columns.getItems().clear();
collection.clear();
row.clear();
}),
button(
"Apply Table",
() ->
change(
"Edit Table",
() ->
table == null
? document.create(collection.getText(), row.getText())
: document.configure(
table, collection.getText(), row.getText()))),
button(
"Remove Table",
() -> change("Remove Table", () -> document.removeTable(requireTable()))),
button(
"Open selected Row Component",
() -> {
if (!row.getText().isBlank()) navigate.accept(row.getText());
})),
columns,
columnFields,
new HBox(
6,
button(
"New Column",
() -> {
column = null;
columns.getSelectionModel().clearSelection();
attribute.clear();
header.clear();
width.clear();
}),
button(
"Apply Column",
() ->
change(
"Edit Column",
() ->
document.writeColumn(
requireTable(),
column,
attribute.getText(),
header.getText(),
width.getText()))),
button(
"Remove Column",
() ->
change(
"Remove Column",
() -> document.removeColumn(requireTable(), requireColumn()))),
button(
"Column up",
() ->
change(
"Move Column",
() -> document.moveColumn(requireTable(), requireColumn(), -1))),
button(
"Column down",
() ->
change(
"Move Column",
() -> document.moveColumn(requireTable(), requireColumn(), 1)))),
status);
}
private TextField field(String name) {
var f = new TextField();
f.setId("table-" + name);
return f;
}
private Button button(String text, Runnable action) {
var b = new Button(text);
b.setOnAction(e -> action.run());
return b;
}
private int requireTable() {
if (table == null) throw new IllegalArgumentException("Select a Table");
return table;
}
private int requireColumn() {
if (column == null) throw new IllegalArgumentException("Select a Column");
return column;
}
private void change(String label, java.util.concurrent.Callable<List<Replacement>> operation) {
try {
var edits = operation.call();
if (!edits.isEmpty()) edit.accept(new Intent(source, label, edits));
} catch (Exception e) {
error(e.getMessage());
}
}
private void select(TableDocument.Table value) {
table = value.index();
collection.setText(value.collection());
row.setText(value.row());
column = null;
attribute.clear();
header.clear();
width.clear();
try {
columns.getItems().setAll(document.columns(table));
} catch (Exception e) {
columns.getItems().clear();
error(e.getMessage());
}
}
void selectControl(String id) {
if (id == null || id.isEmpty()) return;
tables.getItems().stream()
.filter(t -> id.equals(t.id()))
.findFirst()
.ifPresent(t -> tables.getSelectionModel().select(t));
}
void load(String text, boolean writable) {
setDisable(!writable);
if (source.equals(text)) return;
source = text;
loading = true;
try {
document = new TableDocument(text);
tables.getItems().setAll(document.tables);
var selected =
document.tables.stream()
.filter(t -> table != null && t.index() == table)
.findFirst()
.orElse(null);
tables.getSelectionModel().select(selected);
columns.getItems().clear();
column = null;
if (selected != null) select(selected);
else {
table = null;
collection.clear();
row.clear();
attribute.clear();
header.clear();
width.clear();
}
var errors = document.errors();
status.setText(
(errors.isEmpty() ? "No structural Table/Column errors." : String.join("\n", errors))
+ "\n"
+ "Column/Row alignment and type compatibility are unverified. Open Row Component to"
+ " review its positional fields; Column edits never rearrange the shared Row"
+ " layout.");
} catch (Exception e) {
document = null;
tables.getItems().clear();
columns.getItems().clear();
table = null;
column = null;
error("Table editor unavailable: " + e.getMessage());
} finally {
loading = false;
}
}
void error(String message) {
status.setText(message);
}
}