Edit popup menu commands and literal DataGrid group keys

This commit is contained in:
2026-11-25 13:52:30 +01:00
parent 9c63b2dd0f
commit d99e2e69cd
14 changed files with 581 additions and 30 deletions
@@ -110,8 +110,12 @@ final class BindingDocument {
}
boolean named(Element e, String pkg, String type) {
if (e.getTagName().equals(pkg + type)) return true;
if (!e.getTagName().equals(type)) return false;
return resolves(e.getTagName(), pkg, type);
}
boolean resolves(String tag, String pkg, String type) {
if (tag.equals(pkg + type)) return true;
if (!tag.equals(type)) return false;
boolean own = imports.contains(pkg + type) || imports.contains(pkg + "*");
boolean other = imports.stream().anyMatch(i -> i.endsWith("." + type) && !i.equals(pkg + type));
return own && !other;
@@ -1,6 +1,7 @@
package de.neitzel.fxdesigner.kit;
import com.oracle.javafx.scenebuilder.kit.editor.panel.content.ContentPanelController;
import com.oracle.javafx.scenebuilder.kit.editor.panel.content.driver.AbstractDriver;
import com.oracle.javafx.scenebuilder.kit.editor.panel.hierarchy.HierarchyPanelController;
import com.oracle.javafx.scenebuilder.kit.editor.panel.inspector.InspectorPanelController;
import com.oracle.javafx.scenebuilder.kit.fxom.*;
@@ -172,7 +173,15 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
this.file = file;
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
kit = new KitView(host);
content = new ContentPanelController(kit.editor());
content =
new ContentPanelController(kit.editor()) {
@Override
public AbstractDriver lookupDriver(FXOMObject object) {
// Popup menus have no editable Scene accessory or bounds on the form canvas.
if (object.getSceneGraphObject() instanceof ContextMenu) return null;
return super.lookupDriver(object);
}
};
design =
new SplitPane(
new HierarchyPanelController(kit.editor()).getPanelRoot(),
@@ -15,7 +15,7 @@ final class FormDocument {
register(
"javafx.scene.control",
"Label TextField TextArea PasswordField Button CheckBox ComboBox ToolBar Tab TabPane"
+ " ScrollPane SplitPane");
+ " ScrollPane SplitPane MenuItem ContextMenu");
register(
"javafx.scene.layout",
"GridPane VBox HBox StackPane AnchorPane FlowPane TilePane Pane BorderPane");
@@ -112,7 +112,7 @@ final class FormDocument {
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 "ContextMenu", "SplitPane", "ToolBarPane", "ToolBar" -> List.of("items");
case "StatusBar" -> List.of("leftItems", "rightItems");
default -> List.of();
};
@@ -216,6 +216,11 @@ final class FormDocument {
}
private void accept(String type, Slot slot, Element moving) {
if (type.equals("ContextMenu"))
throw new IllegalArgumentException("A ContextMenu belongs to its owning control.");
if (type.equals("MenuItem") != slot.owner().type().equals("ContextMenu"))
throw new IllegalArgumentException(
"A ContextMenu accepts MenuItems. Other destinations accept controls.");
if (type.equals("Tab") != slot.property().equals("tabs"))
throw new IllegalArgumentException(
slot.property().equals("tabs")
@@ -249,6 +254,7 @@ final class FormDocument {
if (container != null) return xml.append(container, snippet, "");
if ((single(slot) && members.isEmpty())
|| slot.owner().type().equals("StatusBar")
|| slot.owner().type().equals("ContextMenu")
|| (slot.owner().type().equals("ToolBarPane") && members.isEmpty()))
snippet = "<" + slot.property() + ">" + snippet + "</" + slot.property() + ">";
return xml.append(owner, snippet, "");
@@ -298,6 +304,7 @@ final class FormDocument {
String extra =
type.equals("Label")
|| type.equals("Button")
|| type.equals("MenuItem")
|| type.equals("TagChip")
|| type.equals("Tab")
? " text=\"" + type + "\""
@@ -371,6 +378,14 @@ final class FormDocument {
List<Field> fields(Item item) {
var e = element(item);
var fields = new LinkedHashMap<String, Field>();
if (item.type().equals("ContextMenu")) return withSourceFields(e, fields);
if (item.type().equals("MenuItem")) {
field(fields, "text", "String", "");
field(fields, "disable", "Boolean", "false");
field(fields, "visible", "Boolean", "true");
field(fields, "onAction", "Event handler", "");
return withSourceFields(e, fields);
}
if (item.type().equals("Tab")) {
field(fields, "text", "String", "");
field(fields, "closable", "Boolean", "true");
@@ -29,7 +29,12 @@ final class FormEditor extends VBox {
Consumer<BindingDocument.Values> bind) {
super(5);
this.submit = submit;
palette.getItems().setAll(FormDocument.TYPES.keySet());
palette
.getItems()
.setAll(
FormDocument.TYPES.keySet().stream()
.filter(type -> !type.equals("ContextMenu"))
.toList());
palette.setValue("Label");
position.setId("form-position");
position.setPrefWidth(74);
@@ -196,11 +196,44 @@ final class GridDockDocument {
if (attr.getNamespaceURI() == null
&& literalKey(attr.getNodeName())
&& !reference(attr.getNodeValue()))
names.put(attr.getNodeName(), attr.getNodeValue().replaceFirst("^\\\\", ""));
names.put(attr.getNodeName(), literal(attr.getNodeValue()));
}
for (var pair : labelPairs(column)) {
if (pair.hasAttribute("key")
&& pair.hasAttribute("value")
&& !reference(pair.getAttribute("key"))
&& !reference(pair.getAttribute("value"))) {
names.put(literal(pair.getAttribute("key")), literal(pair.getAttribute("value")));
}
}
return names;
}
private List<Element> labelPairs(Element column) {
return children(column).stream()
.filter(
child -> {
String tag = child.getTagName();
String suffix = ".groupLabel";
return tag.endsWith(suffix)
&& xml.resolves(
tag.substring(0, tag.length() - suffix.length()),
"de.neitzel.fx.view.",
"DataGridColumn");
})
.flatMap(child -> children(child).stream())
.filter(child -> xml.named(child, "javafx.util.", "Pair"))
.toList();
}
private static String literal(String value) {
return value.startsWith("\\") ? value.substring(1) : value;
}
private static String fxmlLiteral(String value) {
return reference(value) || value.startsWith("\\") ? "\\" + value : value;
}
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}_-]*")
@@ -213,17 +246,62 @@ final class GridDockDocument {
}
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 (!type(column).equals("DataGridColumn") || key.isEmpty())
throw new IllegalArgumentException("Enter a group key.");
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))))
if (map != null
&& literalKey(key)
&& (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;
String encoded = fxmlLiteral(label);
var pairs =
labelPairs(column).stream()
.filter(
pair ->
pair.hasAttribute("key")
&& !reference(pair.getAttribute("key"))
&& literal(pair.getAttribute("key")).equals(key))
.toList();
if (pairs.size() > 1
|| !pairs.isEmpty() && literalKey(key) && map != null && map.hasAttribute(key))
throw new IllegalArgumentException(
"Duplicate group key. Resolve the declarations in Source.");
if (!pairs.isEmpty()) {
var pair = pairs.getFirst();
if (!pair.hasAttribute("value")
|| reference(pair.getAttribute("value"))
|| !children(pair).isEmpty())
throw new IllegalArgumentException("This key is supplied by a nested value or reference.");
if (remove) {
var property = (Element) pair.getParentNode();
if (children(property).size() > 1) return List.of(replace(pair, ""));
var wrapper = tag(property);
var entry = tag(pair);
String remaining =
xml.text.substring(wrapper.openEnd(), entry.start())
+ xml.text.substring(entry.end(), wrapper.closeStart());
return List.of(replace(property, remaining.isBlank() ? "" : remaining));
}
return xml.source.attributes(xml.elements.indexOf(pair), Map.of("value", encoded));
}
if (remove && (map == null || !map.hasAttribute(key))) return List.of();
if (!literalKey(key)) {
String property = column.getTagName() + ".groupLabel";
String snippet =
"<"
+ property
+ "><javafx.util.Pair key=\""
+ XmlSource.escape(fxmlLiteral(key), '"')
+ "\" value=\""
+ XmlSource.escape(encoded, '"')
+ "\"/></"
+ property
+ ">";
return List.of(xml.append(column, snippet, ""));
}
if (map == null)
return List.of(
xml.append(
@@ -76,6 +76,47 @@ class GridDockDocumentTest {
assertThrows(IllegalArgumentException.class, () -> doc.set(col, "fixedPosition", "2"));
}
@Test
void pathLabelsRoundTripWithoutChangingNeighbouringMapEntries() throws Exception {
String source =
"""
<?import de.neitzel.fx.view.*?>
<DataGridColumn><groupLabels Post='Alt' another="%reference"/><!-- keep --></DataGridColumn>
""";
for (String key : java.util.List.of("/", "Post/Eingang West", "Ohne Endung", "$path & more")) {
var doc = new GridDockDocument(source);
String edited =
doc.xml.source.apply(doc.groupLabel(doc.xml.elements.getFirst(), key, "%T & T", false));
var changed = new GridDockDocument(edited);
var col = changed.xml.elements.getFirst();
assertEquals("%T & T", changed.groupLabels(col).get(key));
assertTrue(edited.contains("<groupLabels Post='Alt' another=\"%reference\"/><!-- keep -->"));
String removed = changed.xml.source.apply(changed.groupLabel(col, key, "", true));
var restored = new GridDockDocument(removed);
assertEquals(
java.util.Map.of("Post", "Alt"), restored.groupLabels(restored.xml.elements.getFirst()));
assertTrue(removed.contains("<groupLabels Post='Alt' another=\"%reference\"/><!-- keep -->"));
}
}
@Test
void deletingAPairPreservesSiblingsAndReferences() throws Exception {
String source =
"""
<?import de.neitzel.fx.view.*?>
<DataGridColumn><DataGridColumn.groupLabel><!-- keep -->
<javafx.util.Pair key="/" value="Archiv"/><javafx.util.Pair key="Post/Eingang" value="%title"/>
</DataGridColumn.groupLabel></DataGridColumn>
""";
var doc = new GridDockDocument(source);
var col = doc.xml.elements.getFirst();
assertEquals(
source.replace("<javafx.util.Pair key=\"/\" value=\"Archiv\"/>", ""),
doc.xml.source.apply(doc.groupLabel(col, "/", "", true)));
assertThrows(
IllegalArgumentException.class, () -> doc.groupLabel(col, "Post/Eingang", "", true));
}
@Test
void implicitRowAttributeAndGroupingSurviveRename() throws Exception {
var source =