Edit DataGrid columns and dock layout declarations in the designer
This commit is contained in:
@@ -106,11 +106,14 @@ final class BindingDocument {
|
||||
}
|
||||
|
||||
boolean named(Element e, String type) {
|
||||
if (e.getTagName().equals(PACKAGE + type)) return true;
|
||||
return named(e, PACKAGE, type);
|
||||
}
|
||||
|
||||
boolean named(Element e, String pkg, String type) {
|
||||
if (e.getTagName().equals(pkg + type)) return true;
|
||||
if (!e.getTagName().equals(type)) return false;
|
||||
boolean own = imports.contains(PACKAGE + type) || imports.contains(PACKAGE + "*");
|
||||
boolean other =
|
||||
imports.stream().anyMatch(i -> i.endsWith("." + type) && !i.equals(PACKAGE + type));
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -239,6 +239,18 @@ final class BindingEditor extends VBox {
|
||||
collection.setText(v.collection());
|
||||
}
|
||||
|
||||
void editProperty(BindingDocument.Values v) {
|
||||
var existing =
|
||||
bindings.getItems().stream()
|
||||
.filter(
|
||||
e ->
|
||||
e.values().control().equals(v.control())
|
||||
&& e.values().property().equals(v.property()))
|
||||
.findFirst();
|
||||
if (existing.isPresent()) bindings.getSelectionModel().select(existing.get());
|
||||
else newBinding(v);
|
||||
}
|
||||
|
||||
void newBinding(BindingDocument.Values v) {
|
||||
loading = true;
|
||||
bindings.getSelectionModel().clearSelection();
|
||||
|
||||
@@ -35,6 +35,7 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
|
||||
return content;
|
||||
}
|
||||
|
||||
private GridDockEditor gridDockEditor;
|
||||
private FormEditor formEditor;
|
||||
private final ImageView wholeImage = new ImageView();
|
||||
private final Spinner<Integer> previewWidth = new Spinner<>(120, 1600, 760, 40);
|
||||
@@ -423,6 +424,15 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
|
||||
repeatTab.setClosable(false);
|
||||
var tableTab = new Tab("Table / Columns", tableEditor);
|
||||
tableTab.setClosable(false);
|
||||
gridDockEditor =
|
||||
new GridDockEditor(
|
||||
this::editForm,
|
||||
values -> {
|
||||
directBindingTab.getTabPane().getSelectionModel().select(directBindingTab);
|
||||
bindingEditor.editProperty(values);
|
||||
});
|
||||
var gridDockTab = new Tab("DataGrid / Docking", gridDockEditor);
|
||||
gridDockTab.setClosable(false);
|
||||
var problemsTab = new Tab("Problems", diagnosticsPane);
|
||||
problemsTab.setClosable(false);
|
||||
var structureTab = new Tab("Structure", referenceEditor);
|
||||
@@ -439,6 +449,7 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
|
||||
commandTab,
|
||||
repeatTab,
|
||||
tableTab,
|
||||
gridDockTab,
|
||||
structureTab,
|
||||
problemsTab,
|
||||
discoveryTab,
|
||||
@@ -627,6 +638,7 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
|
||||
repeatEditor.load(value.text(), value.writable());
|
||||
commandEditor.load(value.text(), value.writable());
|
||||
tableEditor.load(value.text(), value.writable());
|
||||
gridDockEditor.load(value.text(), value.writable());
|
||||
referenceEditor.load(value.text(), value.writable());
|
||||
submittedSource = null;
|
||||
commitSource();
|
||||
@@ -663,6 +675,7 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
|
||||
repeatEditor.load(value.text(), value.writable());
|
||||
commandEditor.load(value.text(), value.writable());
|
||||
tableEditor.load(value.text(), value.writable());
|
||||
gridDockEditor.load(value.text(), value.writable());
|
||||
referenceEditor.load(value.text(), value.writable());
|
||||
if (!source.getText().equals(value.text())) {
|
||||
int anchor = source.getAnchor(), caret = source.getCaretPosition();
|
||||
@@ -725,6 +738,7 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
|
||||
} catch (Throwable error) {
|
||||
design.setDisable(true);
|
||||
componentInspector.setDisable(true);
|
||||
gridDockEditor.setDisable(true);
|
||||
projection = null;
|
||||
report(error);
|
||||
} finally {
|
||||
|
||||
@@ -171,16 +171,38 @@ public final class FxmlProjection {
|
||||
}
|
||||
var imports = new HashMap<String, String>();
|
||||
var remove = new ArrayList<Node>();
|
||||
var explicit = new HashMap<String, String>();
|
||||
var conflicts = new HashSet<String>();
|
||||
var unknownPackages = new ArrayList<String>();
|
||||
boolean docking = false;
|
||||
for (Node n = d.getFirstChild(); n != null; n = n.getNextSibling())
|
||||
if (n instanceof ProcessingInstruction pi && pi.getTarget().equals("import")) {
|
||||
String type = pi.getData().trim();
|
||||
if (!type.startsWith("javafx.")) {
|
||||
if (type.endsWith(".*"))
|
||||
throw new IOException("Application wildcard imports need an explicit Component import");
|
||||
imports.put(type.substring(type.lastIndexOf('.') + 1), type);
|
||||
remove.add(n);
|
||||
String simple = type.substring(type.lastIndexOf('.') + 1);
|
||||
if (!simple.equals("*")) {
|
||||
var previous = explicit.putIfAbsent(simple, type);
|
||||
if (previous != null && !previous.equals(type)) conflicts.add(simple);
|
||||
}
|
||||
if (type.startsWith("javafx.")) continue;
|
||||
if (type.equals("de.neitzel.fx.docking.*")) docking = true;
|
||||
else if (simple.equals("*")) unknownPackages.add(type);
|
||||
else imports.put(simple, type);
|
||||
remove.add(n);
|
||||
}
|
||||
for (int i = 0; i < originals.getLength(); i++) {
|
||||
var element = (Element) originals.item(i);
|
||||
String tag = element.getTagName();
|
||||
if (!isObject(element) || tag.contains(".")) continue;
|
||||
if (conflicts.contains(tag)) throw importError(i, tag, "Conflicting explicit imports");
|
||||
if (explicit.containsKey(tag)) continue;
|
||||
if (!unknownPackages.isEmpty()) {
|
||||
throw importError(
|
||||
i, tag, "Unresolved wildcard import (" + String.join(", ", unknownPackages) + ")");
|
||||
}
|
||||
if (docking
|
||||
&& Set.of("DockPane", "DockLayout", "DockGroupDefinition", "DockSplitDefinition")
|
||||
.contains(tag)) imports.put(tag, "de.neitzel.fx.docking." + tag);
|
||||
}
|
||||
remove.forEach(d::removeChild);
|
||||
d.insertBefore(
|
||||
d.createProcessingInstruction("import", SnapshotRegion.class.getName()),
|
||||
@@ -240,6 +262,21 @@ public final class FxmlProjection {
|
||||
return xml(d);
|
||||
}
|
||||
|
||||
private IOException importError(int index, String tag, String message) {
|
||||
int offset = source.tags.get(index).start();
|
||||
int line = 1 + (int) source.text.substring(0, offset).chars().filter(c -> c == '\n').count();
|
||||
int column = offset - source.text.lastIndexOf('\n', offset);
|
||||
return new IOException(
|
||||
message
|
||||
+ " for <"
|
||||
+ tag
|
||||
+ "> at line "
|
||||
+ line
|
||||
+ ", column "
|
||||
+ column
|
||||
+ ". Use one explicit import for this type.");
|
||||
}
|
||||
|
||||
private static Map<String, Element> indexed(Document d) throws IOException {
|
||||
var result = new LinkedHashMap<String, Element>();
|
||||
|
||||
|
||||
@@ -0,0 +1,373 @@
|
||||
package de.neitzel.fxdesigner.kit;
|
||||
|
||||
import de.neitzel.fxdesigner.contracts.EditBatch.Replacement;
|
||||
import java.util.*;
|
||||
import org.w3c.dom.*;
|
||||
|
||||
/** Address declarations by their source elements. Replacing a branch retains its complete XML. */
|
||||
final class GridDockDocument {
|
||||
static final String DOCK = "de.neitzel.fx.docking.";
|
||||
static final List<String> AREAS = List.of("center", "left", "right", "top", "bottom");
|
||||
static final Map<String, List<String>> SETTINGS =
|
||||
Map.of(
|
||||
"DataGrid",
|
||||
List.of(
|
||||
"keyAttribute",
|
||||
"pageSize",
|
||||
"maximumPageSize",
|
||||
"selectionMode",
|
||||
"groupingEnabled",
|
||||
"groupBy",
|
||||
"groupsCollapsed",
|
||||
"items",
|
||||
"source"),
|
||||
"DataGridColumn",
|
||||
List.of(
|
||||
"id",
|
||||
"attribute",
|
||||
"header",
|
||||
"type",
|
||||
"kind",
|
||||
"width",
|
||||
"minimumWidth",
|
||||
"maximumWidth",
|
||||
"filterable",
|
||||
"checklist",
|
||||
"groupAttribute"),
|
||||
"DockGroupDefinition",
|
||||
List.of(
|
||||
"destinationId",
|
||||
"tabSide",
|
||||
"mode",
|
||||
"visibleWhenEmpty",
|
||||
"releaseSpaceWhenEmpty",
|
||||
"collapsible",
|
||||
"preferredWidth",
|
||||
"preferredHeight"),
|
||||
"DockSplitDefinition", List.of("destinationId", "orientation", "position"));
|
||||
|
||||
static List<String> options(String field) {
|
||||
return switch (field) {
|
||||
case "filterable",
|
||||
"groupingEnabled",
|
||||
"groupsCollapsed",
|
||||
"visibleWhenEmpty",
|
||||
"releaseSpaceWhenEmpty",
|
||||
"collapsible" ->
|
||||
List.of("true", "false");
|
||||
case "checklist" -> List.of("DISABLED", "INHERIT", "CURRENT_PAGE", "SOURCE_WIDE");
|
||||
case "selectionMode" -> List.of("NONE", "SINGLE", "MULTIPLE");
|
||||
case "tabSide" -> List.of("TOP", "RIGHT", "BOTTOM", "LEFT");
|
||||
case "mode" -> List.of("DOCKED", "OVERLAY");
|
||||
case "orientation" -> List.of("HORIZONTAL", "VERTICAL");
|
||||
case "kind" -> List.of("TEXT", "INTEGER", "DECIMAL", "BOOLEAN");
|
||||
default -> List.of();
|
||||
};
|
||||
}
|
||||
|
||||
final BindingDocument xml;
|
||||
|
||||
GridDockDocument(String text) throws Exception {
|
||||
xml = new BindingDocument(text);
|
||||
}
|
||||
|
||||
String type(Element e) {
|
||||
String tag = e.getTagName(), name = tag.substring(tag.lastIndexOf('.') + 1);
|
||||
if ((name.equals("DataGrid") || name.equals("DataGridColumn")) && xml.named(e, name))
|
||||
return name;
|
||||
if (Set.of("DockPane", "DockLayout", "DockGroupDefinition", "DockSplitDefinition")
|
||||
.contains(name)
|
||||
&& xml.named(e, DOCK, name)) return name;
|
||||
return "";
|
||||
}
|
||||
|
||||
List<Element> roots() {
|
||||
return xml.elements.stream()
|
||||
.filter(e -> Set.of("DataGrid", "DockPane").contains(type(e)))
|
||||
.toList();
|
||||
}
|
||||
|
||||
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 property(Element e, String name) {
|
||||
var list = children(e).stream().filter(c -> c.getTagName().equals(name)).toList();
|
||||
if (list.size() > 1) throw new IllegalArgumentException("Multiple declarations of " + name);
|
||||
return list.isEmpty() ? null : list.getFirst();
|
||||
}
|
||||
|
||||
Element parentOfType(Element e, String type) {
|
||||
for (Node n = e; n instanceof Element p; n = n.getParentNode())
|
||||
if (type(p).equals(type)) return p;
|
||||
throw new IllegalArgumentException("Missing " + type);
|
||||
}
|
||||
|
||||
private Element columnsOwner(Element grid) {
|
||||
var wrapper = property(grid, "columns");
|
||||
if (wrapper != null && children(grid).stream().anyMatch(c -> type(c).equals("DataGridColumn")))
|
||||
throw new IllegalArgumentException(
|
||||
"Mixed column containers. Edit the declaration in Source.");
|
||||
return wrapper == null ? grid : wrapper;
|
||||
}
|
||||
|
||||
List<Element> branches(Element e) {
|
||||
if (type(e).equals("DataGrid"))
|
||||
return children(columnsOwner(e)).stream()
|
||||
.filter(c -> type(c).equals("DataGridColumn"))
|
||||
.toList();
|
||||
if (type(e).equals("DataGridColumn") || type(e).equals("DockGroupDefinition")) return List.of();
|
||||
return children(e).stream()
|
||||
.filter(
|
||||
c ->
|
||||
!type(c).isEmpty()
|
||||
|| AREAS.contains(c.getTagName())
|
||||
|| Set.of("initialLayout", "first", "second").contains(c.getTagName()))
|
||||
.toList();
|
||||
}
|
||||
|
||||
String label(Element e) {
|
||||
String label = type(e);
|
||||
if (label.isEmpty()) return e.getTagName();
|
||||
String id =
|
||||
e.hasAttribute("destinationId") ? e.getAttribute("destinationId") : e.getAttribute("id");
|
||||
if (id.isEmpty()) id = e.getAttributeNS(BindingDocument.FX, "id");
|
||||
return id.isEmpty() ? label : label + " · " + id;
|
||||
}
|
||||
|
||||
private XmlSource.Tag tag(Element e) {
|
||||
return xml.source.tags.get(xml.elements.indexOf(e));
|
||||
}
|
||||
|
||||
private String fragment(Element e) {
|
||||
var t = tag(e);
|
||||
return xml.text.substring(t.start(), t.end());
|
||||
}
|
||||
|
||||
private Replacement replace(Element e, String snippet) {
|
||||
var t = tag(e);
|
||||
return new Replacement(t.start(), t.end(), snippet);
|
||||
}
|
||||
|
||||
private List<String> grouped(Element grid) {
|
||||
return Arrays.stream(grid.getAttribute("groupBy").split(","))
|
||||
.map(String::trim)
|
||||
.filter(s -> !s.isEmpty())
|
||||
.toList();
|
||||
}
|
||||
|
||||
private List<Element> destinations(Element e) {
|
||||
var pane = parentOfType(e, "DockPane");
|
||||
return xml.elements.stream()
|
||||
.filter(
|
||||
c ->
|
||||
(pane.compareDocumentPosition(c) & Node.DOCUMENT_POSITION_CONTAINED_BY) != 0
|
||||
&& Set.of("DockGroupDefinition", "DockSplitDefinition").contains(type(c)))
|
||||
.toList();
|
||||
}
|
||||
|
||||
private String fresh(List<Element> elements, String attr, String base) {
|
||||
var used = new HashSet<String>();
|
||||
elements.forEach(e -> used.add(e.getAttribute(attr)));
|
||||
String id = base;
|
||||
for (int i = 2; used.contains(id); i++) id = base + i;
|
||||
return id;
|
||||
}
|
||||
|
||||
List<Replacement> set(Element e, String field, String value) {
|
||||
if (!SETTINGS.getOrDefault(type(e), List.of()).contains(field)
|
||||
|| Set.of("items", "source").contains(field))
|
||||
throw new IllegalArgumentException("Use a model binding for the data input.");
|
||||
if (property(e, field) != null)
|
||||
throw new IllegalArgumentException(
|
||||
"The property has a nested declaration. Edit it in Source.");
|
||||
if (e.getAttribute(field).matches("^[%$#].*"))
|
||||
throw new IllegalArgumentException("This property is a reference. Edit it in Source.");
|
||||
boolean required =
|
||||
field.equals("id") || field.equals("destinationId") || field.equals("header");
|
||||
if (required && value.isBlank())
|
||||
throw new IllegalArgumentException("A value is required for " + field);
|
||||
var opts = options(field);
|
||||
if (!value.isEmpty() && !opts.isEmpty() && !opts.contains(value))
|
||||
throw new IllegalArgumentException("Choose one of " + opts);
|
||||
if (!value.isEmpty()
|
||||
&& Set.of(
|
||||
"width",
|
||||
"minimumWidth",
|
||||
"maximumWidth",
|
||||
"preferredWidth",
|
||||
"preferredHeight",
|
||||
"position")
|
||||
.contains(field)) {
|
||||
double n = Double.parseDouble(value);
|
||||
if (!Double.isFinite(n) || n <= 0 || field.equals("position") && n >= 1)
|
||||
throw new IllegalArgumentException("Invalid " + field);
|
||||
}
|
||||
if (!value.isEmpty()
|
||||
&& Set.of("pageSize", "maximumPageSize").contains(field)
|
||||
&& Integer.parseInt(value) < 1)
|
||||
throw new IllegalArgumentException("Positive page size required");
|
||||
var edits = new ArrayList<Replacement>();
|
||||
if (field.equals("destinationId")
|
||||
&& destinations(e).stream().anyMatch(c -> c != e && c.getAttribute(field).equals(value)))
|
||||
throw new IllegalArgumentException("Destination already exists: " + value);
|
||||
if (field.equals("id")) {
|
||||
var grid = parentOfType(e, "DataGrid");
|
||||
if (branches(grid).stream().anyMatch(c -> c != e && c.getAttribute("id").equals(value)))
|
||||
throw new IllegalArgumentException("Column already exists: " + value);
|
||||
var groups = new ArrayList<>(grouped(grid));
|
||||
if (Collections.replaceAll(groups, e.getAttribute("id"), value))
|
||||
edits.addAll(
|
||||
xml.source.attributes(
|
||||
xml.elements.indexOf(grid), Map.of("groupBy", String.join(", ", groups))));
|
||||
// The library defaults the row attribute to the column id.
|
||||
if (!e.hasAttribute("attribute")
|
||||
&& !e.getAttribute("id").equals(value)
|
||||
&& !e.getAttribute("id").isEmpty()) {
|
||||
edits.addAll(
|
||||
xml.source.attributes(
|
||||
xml.elements.indexOf(e), Map.of("id", value, "attribute", e.getAttribute("id"))));
|
||||
return edits;
|
||||
}
|
||||
}
|
||||
if (field.equals("groupBy") && !value.isBlank()) {
|
||||
var ids = branches(e).stream().map(c -> c.getAttribute("id")).toList();
|
||||
var seen = new HashSet<String>();
|
||||
for (String token : value.split(",", -1))
|
||||
if (!ids.contains(token.trim()) || !seen.add(token.trim()))
|
||||
throw new IllegalArgumentException("Unknown or repeated grouping column: " + token);
|
||||
}
|
||||
edits.addAll(
|
||||
xml.source.attributes(
|
||||
xml.elements.indexOf(e),
|
||||
Collections.singletonMap(field, value.isEmpty() ? null : value)));
|
||||
return edits;
|
||||
}
|
||||
|
||||
List<Replacement> addColumn(Element e) {
|
||||
var grid = parentOfType(e, "DataGrid");
|
||||
var cols = branches(grid);
|
||||
String id = fresh(cols, "id", "column");
|
||||
// Start with an existing row attribute so the new column can render immediately.
|
||||
String attribute = cols.isEmpty() ? "value" : cols.getFirst().getAttribute("attribute");
|
||||
if (attribute.isEmpty() && !cols.isEmpty()) attribute = cols.getFirst().getAttribute("id");
|
||||
return List.of(
|
||||
xml.append(
|
||||
columnsOwner(grid),
|
||||
"<de.neitzel.fx.view.DataGridColumn id=\""
|
||||
+ id
|
||||
+ "\" attribute=\""
|
||||
+ XmlSource.escape(attribute, '"')
|
||||
+ "\" header=\""
|
||||
+ id
|
||||
+ "\"/>",
|
||||
""));
|
||||
}
|
||||
|
||||
List<Replacement> move(Element col, int delta) {
|
||||
var columns = branches(parentOfType(col, "DataGrid"));
|
||||
int index = columns.indexOf(col);
|
||||
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);
|
||||
return List.of(replace(other, fragment(col)), replace(col, fragment(other)));
|
||||
}
|
||||
|
||||
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 span = tag(col);
|
||||
int start = xml.text.lastIndexOf('\n', span.start() - 1) + 1;
|
||||
int end = xml.text.indexOf('\n', span.end());
|
||||
if (end >= 0
|
||||
&& xml.text.substring(start, span.start()).isBlank()
|
||||
&& xml.text.substring(span.end(), end).isBlank())
|
||||
return List.of(new Replacement(start, end + 1, ""));
|
||||
return List.of(replace(col, ""));
|
||||
}
|
||||
|
||||
private String group(String id) {
|
||||
return "<" + DOCK + "DockGroupDefinition destinationId=\"" + id + "\"/>";
|
||||
}
|
||||
|
||||
List<Replacement> addArea(Element e, String area) {
|
||||
if (!AREAS.contains(area)) throw new IllegalArgumentException("Choose a dock area.");
|
||||
var pane = parentOfType(e, "DockPane");
|
||||
var initial = property(pane, "initialLayout");
|
||||
var child =
|
||||
"<"
|
||||
+ area
|
||||
+ ">"
|
||||
+ group(fresh(destinations(pane), "destinationId", area))
|
||||
+ "</"
|
||||
+ area
|
||||
+ ">";
|
||||
if (initial == null) {
|
||||
if (pane.hasAttribute("initialLayout"))
|
||||
throw new IllegalArgumentException("The layout is supplied by reference.");
|
||||
if (!area.equals("center"))
|
||||
throw new IllegalArgumentException("Create the center area first.");
|
||||
return List.of(
|
||||
xml.append(
|
||||
pane,
|
||||
"<initialLayout><"
|
||||
+ DOCK
|
||||
+ "DockLayout>"
|
||||
+ child
|
||||
+ "</"
|
||||
+ DOCK
|
||||
+ "DockLayout></initialLayout>",
|
||||
""));
|
||||
}
|
||||
var layout =
|
||||
children(initial).stream()
|
||||
.filter(c -> type(c).equals("DockLayout"))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new IllegalArgumentException("An inline DockLayout is required."));
|
||||
if (property(layout, area) != null || layout.hasAttribute(area))
|
||||
throw new IllegalArgumentException("Area already exists: " + area);
|
||||
return List.of(xml.append(layout, child, ""));
|
||||
}
|
||||
|
||||
List<Replacement> moveArea(Element origin, String target) {
|
||||
if (!AREAS.contains(origin.getTagName())
|
||||
|| !AREAS.contains(target)
|
||||
|| origin.getTagName().equals("center"))
|
||||
throw new IllegalArgumentException("Select a side area to move.");
|
||||
var layout = parentOfType(origin, "DockLayout");
|
||||
if (property(layout, target) != null || layout.hasAttribute(target))
|
||||
throw new IllegalArgumentException("Area already exists: " + target);
|
||||
var t = tag(origin);
|
||||
String name = origin.getTagName();
|
||||
if (t.selfClosing())
|
||||
return List.of(new Replacement(t.start() + 1, t.start() + 1 + name.length(), target));
|
||||
return List.of(
|
||||
new Replacement(t.start() + 1, t.start() + 1 + name.length(), target),
|
||||
new Replacement(t.closeStart() + 2, t.closeStart() + 2 + name.length(), target));
|
||||
}
|
||||
|
||||
List<Replacement> split(Element e) {
|
||||
if (!Set.of("DockGroupDefinition", "DockSplitDefinition").contains(type(e)))
|
||||
throw new IllegalArgumentException("Select a dock group or split.");
|
||||
var destinations = destinations(e);
|
||||
String id = fresh(destinations, "destinationId", "split");
|
||||
return List.of(
|
||||
replace(
|
||||
e,
|
||||
"<"
|
||||
+ DOCK
|
||||
+ "DockSplitDefinition destinationId=\""
|
||||
+ id
|
||||
+ "\" orientation=\"HORIZONTAL\"><first>"
|
||||
+ fragment(e)
|
||||
+ "</first><second>"
|
||||
+ group(fresh(destinations, "destinationId", "group"))
|
||||
+ "</second></"
|
||||
+ DOCK
|
||||
+ "DockSplitDefinition>"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
package de.neitzel.fxdesigner.kit;
|
||||
|
||||
import de.neitzel.fxdesigner.contracts.EditBatch.Replacement;
|
||||
import java.util.*;
|
||||
import java.util.function.*;
|
||||
import javafx.scene.control.*;
|
||||
import javafx.scene.layout.*;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/** Source-backed inspector for the two declarative structures hidden inside Kit proxies. */
|
||||
final class GridDockEditor extends VBox {
|
||||
private final TreeView<Element> tree = new TreeView<>();
|
||||
private final ComboBox<String> setting = new ComboBox<>(),
|
||||
value = new ComboBox<>(),
|
||||
area = new ComboBox<>();
|
||||
private final Label message = new Label(),
|
||||
signals =
|
||||
new Label(
|
||||
"Selection outputs (read-only): selectedKey, selectedKeys, selectedElement,"
|
||||
+ " selectedElements");
|
||||
private GridDockDocument doc;
|
||||
private final Consumer<FormDocument.Edit> submit;
|
||||
private final Consumer<BindingDocument.Values> binding;
|
||||
private final Button apply, bind, add, remove, earlier, later, addArea, moveArea, split;
|
||||
|
||||
GridDockEditor(Consumer<FormDocument.Edit> submit, Consumer<BindingDocument.Values> binding) {
|
||||
super(4);
|
||||
this.submit = submit;
|
||||
this.binding = binding;
|
||||
tree.setId("declaration-tree");
|
||||
tree.setShowRoot(false);
|
||||
tree.setPrefHeight(155);
|
||||
tree.setMinHeight(88);
|
||||
tree.setCellFactory(
|
||||
v ->
|
||||
new TreeCell<>() {
|
||||
protected void updateItem(Element e, boolean empty) {
|
||||
super.updateItem(e, empty);
|
||||
setText(empty || e == null ? null : doc.label(e));
|
||||
}
|
||||
});
|
||||
setting.setId("declaration-property");
|
||||
value.setId("declaration-value");
|
||||
message.setId("declaration-message");
|
||||
value.setEditable(true);
|
||||
value.setPrefWidth(180);
|
||||
area.setId("dock-area");
|
||||
area.getItems().addAll(GridDockDocument.AREAS);
|
||||
area.setValue("center");
|
||||
apply =
|
||||
button(
|
||||
"Apply setting",
|
||||
() ->
|
||||
edit(
|
||||
() ->
|
||||
doc.set(
|
||||
selected(), setting.getValue(), value.getEditor().getText().trim())));
|
||||
bind =
|
||||
button(
|
||||
"Edit data binding",
|
||||
() -> {
|
||||
String id = selected().getAttributeNS(BindingDocument.FX, "id");
|
||||
if (id.isEmpty()) {
|
||||
message.setText("Give the grid an fx:id under Structure first.");
|
||||
return;
|
||||
}
|
||||
String field = setting.getValue();
|
||||
binding.accept(new BindingDocument.Values(id, field, "", "view", ""));
|
||||
});
|
||||
add = button("Add column", () -> edit(() -> doc.addColumn(selected())));
|
||||
remove = button("Delete column", () -> edit(() -> doc.removeColumn(selected())));
|
||||
earlier = button("Earlier", () -> edit(() -> doc.move(selected(), -1)));
|
||||
later = button("Later", () -> edit(() -> doc.move(selected(), 1)));
|
||||
addArea =
|
||||
button("Add area with group", () -> edit(() -> doc.addArea(selected(), area.getValue())));
|
||||
moveArea = button("Move area", () -> edit(() -> doc.moveArea(selected(), area.getValue())));
|
||||
split = button("Split branch", () -> edit(() -> doc.split(selected())));
|
||||
signals.setWrapText(true);
|
||||
message.setWrapText(true);
|
||||
getChildren()
|
||||
.addAll(
|
||||
tree,
|
||||
new FlowPane(6, 4, setting, value, apply, bind),
|
||||
new FlowPane(6, 4, add, earlier, later, remove, area, addArea, moveArea, split),
|
||||
signals,
|
||||
message);
|
||||
tree.getSelectionModel().selectedItemProperty().addListener((o, a, b) -> select());
|
||||
setting.valueProperty().addListener((o, a, b) -> showValue());
|
||||
}
|
||||
|
||||
private static Button button(String text, Runnable action) {
|
||||
var button = new Button(text);
|
||||
button.setOnAction(e -> action.run());
|
||||
return button;
|
||||
}
|
||||
|
||||
private Element selected() {
|
||||
var item = tree.getSelectionModel().getSelectedItem();
|
||||
return item == null ? null : item.getValue();
|
||||
}
|
||||
|
||||
private void edit(Supplier<List<Replacement>> action) {
|
||||
try {
|
||||
var edits = action.get();
|
||||
message.setText("");
|
||||
if (!edits.isEmpty()) submit.accept(new FormDocument.Edit(doc.xml.text, edits, ""));
|
||||
} catch (RuntimeException ex) {
|
||||
message.setText(ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private TreeItem<Element> item(Element e) {
|
||||
var node = new TreeItem<>(e);
|
||||
node.setExpanded(true);
|
||||
for (var c : doc.branches(e)) node.getChildren().add(item(c));
|
||||
return node;
|
||||
}
|
||||
|
||||
void load(String text, boolean writable) {
|
||||
var previous = selected();
|
||||
String label = previous == null ? "" : doc.label(previous);
|
||||
var oldItem = tree.getSelectionModel().getSelectedItem();
|
||||
var path = new LinkedList<Integer>();
|
||||
for (var n = oldItem; n != null && n.getParent() != null; n = n.getParent())
|
||||
path.addFirst(n.getParent().getChildren().indexOf(n));
|
||||
String field = setting.getValue();
|
||||
try {
|
||||
doc = new GridDockDocument(text);
|
||||
var root = new TreeItem<Element>();
|
||||
root.setExpanded(true);
|
||||
for (var e : doc.roots()) root.getChildren().add(item(e));
|
||||
tree.setRoot(root);
|
||||
setDisable(!writable);
|
||||
var match = root;
|
||||
for (int i : path) {
|
||||
if (i >= match.getChildren().size()) break;
|
||||
match = match.getChildren().get(i);
|
||||
}
|
||||
if (match == root && !root.getChildren().isEmpty()) match = root.getChildren().getFirst();
|
||||
if (match.getValue() != null
|
||||
&& !doc.label(match.getValue()).equals(label)
|
||||
&& match.getParent() != null)
|
||||
match =
|
||||
match.getParent().getChildren().stream()
|
||||
.filter(n -> doc.label(n.getValue()).equals(label))
|
||||
.findFirst()
|
||||
.orElse(match);
|
||||
tree.getSelectionModel().select(match);
|
||||
select();
|
||||
if (setting.getItems().contains(field)) setting.setValue(field);
|
||||
message.setText(root.getChildren().isEmpty() ? "No DataGrid or DockPane in this view." : "");
|
||||
} catch (Exception ex) {
|
||||
tree.setRoot(null);
|
||||
setDisable(true);
|
||||
message.setText(ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private static void visible(javafx.scene.Node node, boolean flag) {
|
||||
node.setVisible(flag);
|
||||
node.setManaged(flag);
|
||||
}
|
||||
|
||||
private void select() {
|
||||
var e = selected();
|
||||
String type = e == null ? "" : doc.type(e);
|
||||
boolean grid = type.equals("DataGrid"), column = type.equals("DataGridColumn");
|
||||
setting.getItems().setAll(GridDockDocument.SETTINGS.getOrDefault(type, List.of()));
|
||||
setting.getSelectionModel().selectFirst();
|
||||
for (var node : List.of(add, signals)) visible(node, grid || column);
|
||||
for (var node : List.of(earlier, later, remove)) visible(node, column);
|
||||
for (var node : List.of(area, addArea)) visible(node, e != null && !grid && !column);
|
||||
visible(
|
||||
moveArea,
|
||||
e != null
|
||||
&& GridDockDocument.AREAS.contains(e.getTagName())
|
||||
&& !e.getTagName().equals("center"));
|
||||
visible(split, type.equals("DockGroupDefinition") || type.equals("DockSplitDefinition"));
|
||||
visible(setting, !setting.getItems().isEmpty());
|
||||
message.setText("");
|
||||
showValue();
|
||||
}
|
||||
|
||||
private void showValue() {
|
||||
String field = setting.getValue();
|
||||
var e = selected();
|
||||
boolean input = "items".equals(field) || "source".equals(field);
|
||||
boolean editable = e != null && field != null && !input;
|
||||
visible(bind, input);
|
||||
visible(apply, editable);
|
||||
visible(value, editable);
|
||||
if (!editable) return;
|
||||
value.getItems().setAll(GridDockDocument.options(field));
|
||||
value.setValue(e.getAttribute(field));
|
||||
value.getEditor().setText(e.getAttribute(field));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package de.neitzel.fxdesigner.kit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class GridDockDocumentTest {
|
||||
@Test
|
||||
void implicitRowAttributeAndGroupingSurviveRename() throws Exception {
|
||||
var source =
|
||||
"""
|
||||
<?import de.neitzel.fx.view.DataGrid?>
|
||||
<?import de.neitzel.fx.view.DataGridColumn?>
|
||||
<DataGrid groupBy="name"><columns>
|
||||
<DataGridColumn id="name" header="Name"/>
|
||||
<DataGridColumn id="age"/>
|
||||
</columns></DataGrid>
|
||||
""";
|
||||
var doc = new GridDockDocument(source);
|
||||
var grid = doc.roots().getFirst();
|
||||
var col = doc.branches(grid).getFirst();
|
||||
assertThrows(IllegalArgumentException.class, () -> doc.removeColumn(col));
|
||||
assertThrows(IllegalArgumentException.class, () -> doc.set(col, "id", "age"));
|
||||
var renamed = doc.xml.source.apply(doc.set(col, "id", "title"));
|
||||
assertTrue(renamed.contains("groupBy=\"title\""));
|
||||
assertTrue(renamed.contains("attribute=\"name\""));
|
||||
var removed = doc.xml.source.apply(doc.removeColumn(doc.branches(grid).getLast()));
|
||||
assertEquals(source.replace(" <DataGridColumn id=\"age\"/>\n", ""), removed);
|
||||
}
|
||||
|
||||
@Test
|
||||
void rootDockProjectionAndAmbiguousImportPreserveOriginal() throws Exception {
|
||||
String source =
|
||||
"""
|
||||
<?import de.neitzel.fx.docking.*?>
|
||||
<DockPane><initialLayout><DockLayout><center><DockGroupDefinition destinationId='main'/></center></DockLayout></initialLayout></DockPane>
|
||||
""";
|
||||
var projection = new FxmlProjection(source);
|
||||
assertEquals(source, projection.restore(projection.project()));
|
||||
String conflict = source.replace("<DockPane>", "<?import my.docks.*?>\n<DockPane>");
|
||||
var ex = assertThrows(java.io.IOException.class, () -> new FxmlProjection(conflict).project());
|
||||
assertTrue(ex.getMessage().contains("<DockPane> at line 3, column 1"), ex::getMessage);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user