Edit single content slots, tab and split layouts, and grid setup declarations

This commit is contained in:
2026-11-25 09:40:39 +01:00
parent 95d26eb88b
commit dc3dd6b6be
12 changed files with 970 additions and 20 deletions
@@ -14,9 +14,11 @@ final class FormDocument {
static {
register(
"javafx.scene.control",
"Label TextField TextArea PasswordField Button CheckBox ComboBox ToolBar");
"Label TextField TextArea PasswordField Button CheckBox ComboBox ToolBar Tab TabPane"
+ " ScrollPane SplitPane");
register(
"javafx.scene.layout", "GridPane VBox HBox StackPane AnchorPane FlowPane TilePane Pane");
"javafx.scene.layout",
"GridPane VBox HBox StackPane AnchorPane FlowPane TilePane Pane BorderPane");
register(
"de.neitzel.fx.control",
"StatusBar ToolBarPane AutoCompleteTextField TagChip NonNegativeIntegerSpinner HtmlViewer");
@@ -107,7 +109,10 @@ final class FormDocument {
return switch (type) {
case "GridPane", "HBox", "VBox", "StackPane", "Pane", "AnchorPane", "FlowPane", "TilePane" ->
List.of("children");
case "ToolBarPane", "ToolBar" -> List.of("items");
case "TabPane" -> List.of("tabs");
case "Tab", "ScrollPane" -> List.of("content");
case "BorderPane" -> List.of("center", "top", "bottom", "left", "right");
case "SplitPane", "ToolBarPane", "ToolBar" -> List.of("items");
case "StatusBar" -> List.of("leftItems", "rightItems");
default -> List.of();
};
@@ -174,13 +179,57 @@ final class FormDocument {
private List<Element> members(Slot slot) {
Element host = owner(slot), list = property(host, slot.property());
if (list == null && slot.owner().type().equals("StatusBar")) return List.of();
return children(list == null ? host : list).stream()
.filter(
e ->
!BindingDocument.FX.equals(e.getNamespaceURI())
&& Character.isUpperCase(simple(e.getTagName()).charAt(0)))
.toList();
if (list == null
&& (slot.owner().type().equals("StatusBar") || slot.owner().type().equals("BorderPane")))
return List.of();
return children(list == null ? host : list).stream().filter(FormDocument::instance).toList();
}
private static boolean instance(Element child) {
if (BindingDocument.FX.equals(child.getNamespaceURI()))
return Set.of("include", "reference", "copy").contains(child.getLocalName());
return Character.isUpperCase(simple(child.getTagName()).charAt(0));
}
private static boolean single(Slot slot) {
return slot.property().equals("content") || slot.owner().type().equals("BorderPane");
}
String destinationInfo(Slot slot) {
if (slot == null || !single(slot)) return "";
var host = owner(slot);
if (host.hasAttribute(slot.property())) return "Single place · supplied by an FXML attribute.";
var children = members(slot);
if (!children.isEmpty()) {
var child = children.getFirst();
boolean movable = items.stream().anyMatch(item -> element(item) == child);
return "Single place · occupied by "
+ child.getTagName()
+ (movable
? ". Move that child to a free destination first."
: ". Choose another destination or edit this place in Source.");
}
var wrapper = property(host, slot.property());
if (wrapper != null && (!children(wrapper).isEmpty() || !wrapper.getTextContent().isBlank()))
return "Single place · supplied by FXML content. Review this place in Source.";
return "Single place · empty";
}
private void accept(String type, Slot slot, Element moving) {
if (type.equals("Tab") != slot.property().equals("tabs"))
throw new IllegalArgumentException(
slot.property().equals("tabs")
? "TabPane.tabs accepts Tab only."
: "A Tab belongs in TabPane.tabs. This destination accepts Nodes.");
if (owner(slot).hasAttribute(slot.property()))
throw new IllegalArgumentException("This destination is supplied by an FXML attribute.");
var wrapper = property(owner(slot), slot.property());
if (single(slot)
&& (members(slot).stream().anyMatch(e -> e != moving)
|| wrapper != null
&& members(slot).isEmpty()
&& (!wrapper.getTextContent().isBlank() || !children(wrapper).isEmpty())))
throw new IllegalArgumentException("Occupied: " + slot + ". " + destinationInfo(slot));
}
int size(Slot slot) {
@@ -198,7 +247,8 @@ final class FormDocument {
}
var container = property(owner, slot.property());
if (container != null) return xml.append(container, snippet, "");
if (slot.owner().type().equals("StatusBar")
if ((single(slot) && members.isEmpty())
|| slot.owner().type().equals("StatusBar")
|| (slot.owner().type().equals("ToolBarPane") && members.isEmpty()))
snippet = "<" + slot.property() + ">" + snippet + "</" + slot.property() + ">";
return xml.append(owner, snippet, "");
@@ -234,6 +284,7 @@ final class FormDocument {
Edit add(String type, Slot slot, int at) {
var host = owner(slot);
if (!TYPES.containsKey(type)) throw new IllegalArgumentException("Choose a palette control.");
accept(type, slot, null);
String id = fresh(type), prefix = fxPrefix(host), qualified = TYPES.get(type);
var edits = new ArrayList<Replacement>();
String tagName = type;
@@ -245,7 +296,10 @@ final class FormDocument {
}
}
String extra =
type.equals("Label") || type.equals("Button") || type.equals("TagChip")
type.equals("Label")
|| type.equals("Button")
|| type.equals("TagChip")
|| type.equals("Tab")
? " text=\"" + type + "\""
: "";
if (slot.owner().type().equals("GridPane")) {
@@ -292,6 +346,7 @@ final class FormDocument {
throw new IllegalArgumentException("Only a child in a supported list can be moved.");
for (Node n = host; n != null; n = n.getParentNode())
if (n == source) throw new IllegalArgumentException("Cannot move a control into itself.");
accept(item.type(), slot, source);
var current = members(slot);
int old = current.indexOf(source);
if (old >= 0 && (at == old || at == old + 1)) return edit(List.of(), item.id());
@@ -316,6 +371,14 @@ final class FormDocument {
List<Field> fields(Item item) {
var e = element(item);
var fields = new LinkedHashMap<String, Field>();
if (item.type().equals("Tab")) {
field(fields, "text", "String", "");
field(fields, "closable", "Boolean", "true");
field(fields, "disable", "Boolean", "false");
field(fields, "style", "CSS", "");
field(fields, "styleClass", "CSS classes", "tab");
return withSourceFields(e, fields);
}
field(fields, "accessibleText", "String", "");
if (Set.of("TextField", "PasswordField", "AutoCompleteTextField").contains(item.type()))
field(fields, "prefColumnCount", "Integer", "12");
@@ -394,6 +457,10 @@ final class FormDocument {
}
for (String name : List.of("width", "height"))
fields.put(name, new Field(name, "Number", "layout", false));
return withSourceFields(e, fields);
}
private static List<Field> withSourceFields(Element e, Map<String, Field> fields) {
for (int i = 0; i < e.getAttributes().getLength(); i++) {
var a = e.getAttributes().item(i);
if (a.getNamespaceURI() == null)
@@ -10,6 +10,7 @@ import javafx.scene.layout.*;
final class FormEditor extends VBox {
private final ComboBox<String> palette = combo("form-palette");
private final ComboBox<FormDocument.Slot> destination = combo("form-destination");
private final Label destinationInfo = new Label();
private final Spinner<Integer> position = new Spinner<>(0, 0, 0);
private final ComboBox<FormDocument.Item> selected = combo("form-control");
private final ComboBox<FormDocument.Field> field = combo("form-property");
@@ -75,6 +76,7 @@ final class FormEditor extends VBox {
palette.getValue(),
destination.getValue(),
position.getValue())))),
destinationInfo,
row(
new Label("Selected control"),
selected,
@@ -171,6 +173,10 @@ final class FormEditor extends VBox {
private void setPosition() {
var slot = destination.getValue();
String hint = form == null ? "" : form.destinationInfo(slot);
destinationInfo.setText(hint);
destinationInfo.setVisible(!hint.isEmpty());
destinationInfo.setManaged(!hint.isEmpty());
int size = form == null || slot == null ? 0 : form.size(slot);
position.setValueFactory(new SpinnerValueFactory.IntegerSpinnerValueFactory(0, size, size));
}
@@ -13,6 +13,8 @@ final class GridDockDocument {
"DataGrid",
List.of(
"keyAttribute",
"setupId",
"setupVersion",
"pageSize",
"maximumPageSize",
"selectionMode",
@@ -33,7 +35,9 @@ final class GridDockDocument {
"maximumWidth",
"filterable",
"checklist",
"groupAttribute"),
"groupAttribute",
"fixedPosition",
"groupLabels"),
"DockGroupDefinition",
List.of(
"destinationId",
@@ -177,9 +181,54 @@ final class GridDockDocument {
return id;
}
Map<String, String> groupLabels(Element column) {
var names = new TreeMap<String, String>();
var map = property(column, "groupLabels");
if (map != null)
for (int i = 0; i < map.getAttributes().getLength(); i++) {
var attr = map.getAttributes().item(i);
if (attr.getNamespaceURI() == null
&& literalKey(attr.getNodeName())
&& !reference(attr.getNodeValue()))
names.put(attr.getNodeName(), attr.getNodeValue().replaceFirst("^\\\\", ""));
}
return names;
}
private static boolean literalKey(String key) {
// FXMLLoader reads an inline, read-only Map's ordinary property attributes as keys.
return key.matches("[\\p{L}_][\\p{L}\\p{N}_-]*")
&& !key.equals("xmlns")
&& !key.startsWith("on");
}
private static boolean reference(String text) {
return !text.isEmpty() && "%@$".indexOf(text.charAt(0)) >= 0;
}
List<Replacement> groupLabel(Element column, String key, String label, boolean remove) {
if (!type(column).equals("DataGridColumn") || !literalKey(key))
throw new IllegalArgumentException(
"Use a literal FXML map key (letters, digits, underscores or hyphens).");
if (column.hasAttribute("groupLabels"))
throw new IllegalArgumentException("This groupLabels map is supplied by an attribute.");
var map = property(column, "groupLabels");
if (map != null && (property(map, key) != null || reference(map.getAttribute(key))))
throw new IllegalArgumentException("This key is supplied by a nested value or reference.");
if (remove && (map == null || !map.hasAttribute(key))) return List.of();
if (!remove && label.isBlank()) throw new IllegalArgumentException("Enter a group label.");
String encoded = (reference(label) || label.startsWith("\\")) ? "\\" + label : label;
if (map == null)
return List.of(
xml.append(
column, "<groupLabels " + key + "=\"" + XmlSource.escape(encoded, '"') + "\"/>", ""));
return xml.source.attributes(
xml.elements.indexOf(map), Collections.singletonMap(key, remove ? null : encoded));
}
List<Replacement> set(Element e, String field, String value) {
if (!SETTINGS.getOrDefault(type(e), List.of()).contains(field)
|| Set.of("items", "source").contains(field))
|| Set.of("items", "source", "groupLabels").contains(field))
throw new IllegalArgumentException("Use a model binding for the data input.");
if (property(e, field) != null)
throw new IllegalArgumentException(
@@ -210,6 +259,15 @@ final class GridDockDocument {
&& Set.of("pageSize", "maximumPageSize").contains(field)
&& Integer.parseInt(value) < 1)
throw new IllegalArgumentException("Positive page size required");
if (field.equals("setupVersion") && !value.isEmpty() && Integer.parseInt(value) < 1)
throw new IllegalArgumentException("Setup version must be a positive integer.");
if (field.equals("fixedPosition") && !value.isEmpty()) {
int index = branches(parentOfType(e, "DataGrid")).indexOf(e);
int fixed = Integer.parseInt(value);
if (fixed != -1 && fixed != index)
throw new IllegalArgumentException(
"Use " + index + " for this column, or -1 to allow moving it.");
}
var edits = new ArrayList<Replacement>();
if (field.equals("destinationId")
&& destinations(e).stream().anyMatch(c -> c != e && c.getAttribute(field).equals(value)))
@@ -273,13 +331,24 @@ final class GridDockDocument {
if (index < 0) throw new IllegalArgumentException("Select a column.");
if (index + delta < 0 || index + delta >= columns.size()) return List.of();
var other = columns.get(index + delta);
if (fixed(col) || fixed(other))
throw new IllegalArgumentException("Clear the fixed position before moving these columns.");
return List.of(replace(other, fragment(col)), replace(col, fragment(other)));
}
private boolean fixed(Element col) {
return property(col, "fixedPosition") != null
|| col.hasAttribute("fixedPosition") && !col.getAttribute("fixedPosition").equals("-1");
}
List<Replacement> removeColumn(Element col) {
if (!type(col).equals("DataGridColumn")) throw new IllegalArgumentException("Select a column.");
if (grouped(parentOfType(col, "DataGrid")).contains(col.getAttribute("id")))
throw new IllegalArgumentException("Remove the column from groupBy first.");
var columns = branches(parentOfType(col, "DataGrid"));
if (columns.subList(columns.indexOf(col) + 1, columns.size()).stream().anyMatch(this::fixed))
throw new IllegalArgumentException(
"Removing this column shifts a fixed column. Clear its fixed position first.");
var span = tag(col);
int start = xml.text.lastIndexOf('\n', span.start() - 1) + 1;
int end = xml.text.indexOf('\n', span.end());
@@ -13,6 +13,13 @@ final class GridDockEditor extends VBox {
private final ComboBox<String> setting = new ComboBox<>(),
value = new ComboBox<>(),
area = new ComboBox<>();
private final ComboBox<String> groupKey = new ComboBox<>();
private final TextField groupText = new TextField();
private final FlowPane groupFields = new FlowPane(6, 4);
private final Label setupHint =
new Label(
"Saved setups use the setup ID and version. Restoring an older version requires a"
+ " migration.");
private final Label message = new Label(),
signals =
new Label(
@@ -55,6 +62,38 @@ final class GridDockEditor extends VBox {
() ->
doc.set(
selected(), setting.getValue(), value.getEditor().getText().trim())));
groupKey.setId("group-label-key");
groupKey.setEditable(true);
groupKey.setPromptText("Group key");
groupText.setId("group-label-text");
groupText.setPromptText("Displayed label");
groupKey.setOnAction(
event -> {
if (selected() != null && groupKey.getValue() != null)
groupText.setText(doc.groupLabels(selected()).getOrDefault(groupKey.getValue(), ""));
});
groupFields
.getChildren()
.addAll(
groupKey,
groupText,
button(
"Set group label",
() ->
edit(
() ->
doc.groupLabel(
selected(),
groupKey.getEditor().getText(),
groupText.getText(),
false))),
button(
"Remove group label",
() ->
edit(
() ->
doc.groupLabel(selected(), groupKey.getEditor().getText(), "", true))));
setupHint.setWrapText(true);
bind =
button(
"Edit data binding",
@@ -82,6 +121,8 @@ final class GridDockEditor extends VBox {
tree,
new FlowPane(6, 4, setting, value, apply, bind),
new FlowPane(6, 4, add, earlier, later, remove, area, addArea, moveArea, split),
groupFields,
setupHint,
signals,
message);
tree.getSelectionModel().selectedItemProperty().addListener((o, a, b) -> select());
@@ -185,7 +226,17 @@ final class GridDockEditor extends VBox {
String field = setting.getValue();
var e = selected();
boolean input = "items".equals(field) || "source".equals(field);
boolean editable = e != null && field != null && !input;
boolean labels = e != null && "groupLabels".equals(field);
visible(groupFields, labels);
visible(setupHint, "setupId".equals(field) || "setupVersion".equals(field));
if (labels) {
groupKey.getItems().setAll(doc.groupLabels(e).keySet());
groupKey.getSelectionModel().clearSelection();
groupKey.getEditor().clear();
groupText.clear();
groupKey.getSelectionModel().selectFirst();
}
boolean editable = e != null && field != null && !input && !labels;
visible(bind, input);
visible(apply, editable);
visible(value, editable);
@@ -45,6 +45,28 @@ class FormDocumentTest {
apply(doc.set(item, field, value == null ? "" : value, value == null));
}
@Test
void singleSlotsRespectReferencesAndDefaultTabContent() throws Exception {
source =
"""
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<BorderPane xmlns:fx="http://javafx.com/fxml/1" bottom="$footer">
<center><TabPane fx:id="tabs"><Tab fx:id="first"><ScrollPane fx:id="scroll">
<content><fx:include source="Other.fxml"/></content>
</ScrollPane></Tab></TabPane></center>
</BorderPane>
""";
add("Tab", "tabs", "tabs");
assertThrows(IllegalArgumentException.class, () -> add("Label", "first", "content"));
assertThrows(IllegalArgumentException.class, () -> add("Label", "scroll", "content"));
assertThrows(IllegalArgumentException.class, () -> add("Label", "", "bottom"));
assertThrows(IllegalArgumentException.class, () -> add("Label", "tabs", "tabs"));
apply(doc().move(item("scroll"), slot("tab", "content"), 0));
assertTrue(source.contains("<content><fx:include source=\"Other.fxml\"/></content>"));
add("Label", "first", "content");
}
@Test
void emptyGridAndNamedListsKeepReferencesAndRejectCycles() throws Exception {
assertEquals(
@@ -5,6 +5,25 @@ import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class GridDockDocumentTest {
@Test
void updatingOneGroupLabelRetainsReferencesCommentsAndNestedEntries() throws Exception {
String source =
"""
<?import de.neitzel.fx.view.*?>
<DataGrid><DataGridColumn id="folder" header="Folder"><groupLabels other="%localized" Post='old'>
<!-- keep --><custom><Unknown/></custom></groupLabels></DataGridColumn></DataGrid>
""";
var doc = new GridDockDocument(source);
var col = doc.branches(doc.roots().getFirst()).getFirst();
assertEquals(
source.replace("Post='old'", "Post='Post &amp; Briefe'"),
doc.xml.source.apply(doc.groupLabel(col, "Post", "Post & Briefe", false)));
assertThrows(
IllegalArgumentException.class, () -> doc.groupLabel(col, "other", "changed", false));
assertThrows(IllegalArgumentException.class, () -> doc.groupLabel(col, "custom", "", true));
assertThrows(IllegalArgumentException.class, () -> doc.set(col, "fixedPosition", "2"));
}
@Test
void implicitRowAttributeAndGroupingSurviveRename() throws Exception {
var source =