Validate grid width bounds and qualify Archiver window and document states in IntelliJ
This commit is contained in:
@@ -104,7 +104,7 @@ final class GridDockDocument {
|
||||
return result;
|
||||
}
|
||||
|
||||
private Element property(Element e, String name) {
|
||||
static 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();
|
||||
@@ -232,6 +232,59 @@ final class GridDockDocument {
|
||||
xml.elements.indexOf(map), Collections.singletonMap(key, remove ? null : encoded));
|
||||
}
|
||||
|
||||
// DataGridColumn defaults in the target library. The Kit does not load target classes.
|
||||
static final Map<String, String> COLUMN_WIDTHS =
|
||||
Map.of("minimumWidth", "80", "width", "150", "maximumWidth", "1200");
|
||||
|
||||
private record ColumnWidth(String description, Double literal) {
|
||||
static ColumnWidth read(Element column, String name, String edited, String proposed) {
|
||||
String text = column.getAttribute(name);
|
||||
if (name.equals(edited)) text = proposed;
|
||||
else {
|
||||
var nested = property(column, name);
|
||||
if (nested != null) {
|
||||
if (!children(nested).isEmpty()) return new ColumnWidth(name + "=<nested FXML>", null);
|
||||
text = nested.getTextContent().strip();
|
||||
}
|
||||
}
|
||||
boolean useDefault = text.isEmpty();
|
||||
if (useDefault) text = COLUMN_WIDTHS.get(name);
|
||||
String label = name + "=" + text;
|
||||
if (reference(text) || text.startsWith("#")) {
|
||||
if (name.equals(edited))
|
||||
throw new IllegalArgumentException("Enter a literal pixel width for " + name);
|
||||
return new ColumnWidth(label + " (resolved at runtime)", null);
|
||||
}
|
||||
double pixels;
|
||||
try {
|
||||
pixels = Double.parseDouble(text);
|
||||
} catch (NumberFormatException ex) {
|
||||
throw new IllegalArgumentException("Expected a pixel width for " + label);
|
||||
}
|
||||
if (!Double.isFinite(pixels) || pixels <= 0)
|
||||
throw new IllegalArgumentException("Expected a finite, positive pixel width for " + label);
|
||||
return new ColumnWidth(label + (useDefault ? " (default)" : ""), pixels);
|
||||
}
|
||||
|
||||
boolean exceeds(ColumnWidth other) {
|
||||
return literal != null && other.literal != null && literal > other.literal;
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateColumnWidths(Element column, String edited, String proposed) {
|
||||
var minimum = ColumnWidth.read(column, "minimumWidth", edited, proposed);
|
||||
var width = ColumnWidth.read(column, "width", edited, proposed);
|
||||
var maximum = ColumnWidth.read(column, "maximumWidth", edited, proposed);
|
||||
if (minimum.exceeds(width) || width.exceeds(maximum) || minimum.exceeds(maximum))
|
||||
throw new IllegalArgumentException(
|
||||
"Expected minimumWidth ≤ width ≤ maximumWidth: "
|
||||
+ minimum.description()
|
||||
+ ", "
|
||||
+ width.description()
|
||||
+ ", "
|
||||
+ maximum.description());
|
||||
}
|
||||
|
||||
List<Replacement> set(Element e, String field, String value) {
|
||||
if (!SETTINGS.getOrDefault(type(e), List.of()).contains(field)
|
||||
|| Set.of("items", "source", "groupLabels").contains(field))
|
||||
@@ -239,7 +292,7 @@ final class GridDockDocument {
|
||||
if (property(e, field) != null)
|
||||
throw new IllegalArgumentException(
|
||||
"The property has a nested declaration. Edit it in Source.");
|
||||
if (e.getAttribute(field).matches("^[%$#].*"))
|
||||
if (reference(e.getAttribute(field)) || e.getAttribute(field).startsWith("#"))
|
||||
throw new IllegalArgumentException("This property is a reference. Edit it in Source.");
|
||||
boolean required =
|
||||
field.equals("id") || field.equals("destinationId") || field.equals("header");
|
||||
@@ -248,7 +301,9 @@ final class GridDockDocument {
|
||||
var opts = options(field);
|
||||
if (!value.isEmpty() && !opts.isEmpty() && !opts.contains(value))
|
||||
throw new IllegalArgumentException("Choose one of " + opts);
|
||||
if (!value.isEmpty()
|
||||
if (type(e).equals("DataGridColumn") && COLUMN_WIDTHS.containsKey(field)) {
|
||||
validateColumnWidths(e, field, value);
|
||||
} else if (!value.isEmpty()
|
||||
&& Set.of(
|
||||
"width",
|
||||
"minimumWidth",
|
||||
|
||||
@@ -250,6 +250,15 @@ final class GridDockEditor extends VBox {
|
||||
default -> "";
|
||||
}
|
||||
: "";
|
||||
if (doc.type(e).equals("DataGridColumn") && GridDockDocument.COLUMN_WIDTHS.containsKey(field)) {
|
||||
hint =
|
||||
GridDockDocument.property(e, field) == null
|
||||
? "Default: " + GridDockDocument.COLUMN_WIDTHS.get(field) + " px"
|
||||
: "Nested FXML value";
|
||||
value.setTooltip(
|
||||
new Tooltip(
|
||||
"minimumWidth ≤ width ≤ maximumWidth. FXML references are resolved at runtime."));
|
||||
} else value.setTooltip(null);
|
||||
value.setPromptText(hint);
|
||||
value.getItems().setAll(GridDockDocument.options(field));
|
||||
value.setValue(e.getAttribute(field));
|
||||
|
||||
@@ -5,6 +5,33 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class GridDockDocumentTest {
|
||||
@Test
|
||||
void columnBoundsIncludeDefaultsResetsAndKnownPartsOfDynamicWidths() throws Exception {
|
||||
var doc =
|
||||
new GridDockDocument(
|
||||
"<?import de.neitzel.fx.view.*?><DataGridColumn width='230' minimumWidth='200'/>");
|
||||
var col = doc.xml.elements.getFirst();
|
||||
assertThrows(IllegalArgumentException.class, () -> doc.set(col, "width", ""));
|
||||
assertThrows(IllegalArgumentException.class, () -> doc.set(col, "minimumWidth", "300"));
|
||||
assertThrows(IllegalArgumentException.class, () -> doc.set(col, "maximumWidth", "100"));
|
||||
assertThrows(IllegalArgumentException.class, () -> doc.set(col, "width", "%size"));
|
||||
assertDoesNotThrow(() -> doc.set(col, "maximumWidth", "230"));
|
||||
assertDoesNotThrow(() -> doc.set(col, "minimumWidth", ""));
|
||||
var dynamic =
|
||||
new GridDockDocument(
|
||||
"<?import de.neitzel.fx.view.*?><DataGridColumn width='%start' maximumWidth='200'/>");
|
||||
var bound = dynamic.xml.elements.getFirst();
|
||||
assertTrue(
|
||||
assertThrows(
|
||||
IllegalArgumentException.class, () -> dynamic.set(bound, "minimumWidth", "201"))
|
||||
.getMessage()
|
||||
.contains("%start (resolved at runtime)"));
|
||||
assertEquals(
|
||||
dynamic.xml.text.replace("200", "210"),
|
||||
dynamic.xml.source.apply(dynamic.set(bound, "maximumWidth", "210")));
|
||||
assertThrows(IllegalArgumentException.class, () -> dynamic.set(bound, "width", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void dockMinimaAllowZeroWithoutChangingGridColumnRules() throws Exception {
|
||||
var source =
|
||||
|
||||
Reference in New Issue
Block a user