Choose a compatible HtmlViewer for the Archiver media button

This commit is contained in:
2026-11-25 14:13:38 +01:00
parent d99e2e69cd
commit 512e2a7105
10 changed files with 598 additions and 22 deletions
@@ -68,6 +68,22 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
.componentAttributes(id)
.forEach(
(name, value) -> {
if (name.equals("viewer")
&& projection
.components()
.get(id)
.equals("de.neitzel.archiver.ui.HtmlMediaButton")) {
var edit = new Button("Choose HtmlViewer");
edit.setOnAction(
e -> {
var panel = (TitledPane) lookup("#form-controls-inspector");
panel.setExpanded(true);
formEditor.selectIndex(projection.sourceIndex(id));
formEditor.selectViewerField();
});
componentInspector.getChildren().add(new HBox(4, new Label(name), edit));
return;
}
var field = new TextField(value);
field.setId("component-property-" + name);
field.setOnAction(
@@ -481,6 +497,7 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
bindingEditor.editProperty(values);
});
var formPane = new TitledPane("Form controls", formEditor);
formPane.setId("form-controls-inspector");
formPane.setExpanded(false);
formPane
.expandedProperty()
@@ -9,6 +9,8 @@ import org.w3c.dom.*;
* Visual form operations use ranges from the current source so bindings keep their original text.
*/
final class FormDocument {
static final String VIEWER_REFERENCE = "HtmlViewer reference";
private static final String MEDIA_BUTTON = "HtmlMediaButton";
static final Map<String, String> TYPES = new TreeMap<>();
static {
@@ -22,6 +24,7 @@ final class FormDocument {
register(
"de.neitzel.fx.control",
"StatusBar ToolBarPane AutoCompleteTextField TagChip NonNegativeIntegerSpinner HtmlViewer");
register("de.neitzel.archiver.ui", MEDIA_BUTTON);
register("de.neitzel.fx.view", "DataGrid Table");
register("de.neitzel.fx.docking", "DockPane");
}
@@ -62,10 +65,14 @@ final class FormDocument {
final List<Slot> slots = new ArrayList<>();
private final Map<String, String> imports = new HashMap<>();
private final Set<String> packages = new HashSet<>();
private final Set<String> ids = new HashSet<>();
private final Set<String> ids = new HashSet<>(), duplicateIds = new HashSet<>();
FormDocument(String text) throws Exception {
xml = new BindingDocument(text);
this(new BindingDocument(text));
}
FormDocument(BindingDocument xml) {
this.xml = xml;
for (Node n = xml.document.getFirstChild(); n != null; n = n.getNextSibling()) {
if (n instanceof ProcessingInstruction pi && pi.getTarget().equals("import")) {
String type = pi.getData().trim();
@@ -76,7 +83,7 @@ final class FormDocument {
for (int i = 0; i < xml.elements.size(); i++) {
var e = xml.elements.get(i);
String id = e.getAttributeNS(BindingDocument.FX, "id");
if (!id.isEmpty()) ids.add(id);
if (!id.isEmpty() && !ids.add(id)) duplicateIds.add(id);
String tag = e.getTagName();
if (BindingDocument.FX.equals(e.getNamespaceURI()) && e.getLocalName().equals("root"))
tag = e.getAttribute("type");
@@ -88,6 +95,56 @@ final class FormDocument {
}
}
List<String> palette() {
boolean mediaButtonInDocument = items.stream().anyMatch(i -> i.type().equals(MEDIA_BUTTON));
return TYPES.keySet().stream()
.filter(type -> !type.equals("ContextMenu"))
.filter(
type ->
!type.equals(MEDIA_BUTTON)
|| mediaButtonInDocument
|| resolves(MEDIA_BUTTON, TYPES.get(MEDIA_BUTTON)))
.toList();
}
List<String> viewerTargets(Item item) {
var owner = element(item);
return xml.elements.subList(0, item.index()).stream()
.filter(
e ->
isViewer(e)
&& (e.compareDocumentPosition(owner) & Node.DOCUMENT_POSITION_CONTAINED_BY)
== 0)
.map(e -> e.getAttributeNS(BindingDocument.FX, "id"))
.filter(id -> !id.isEmpty() && !duplicateIds.contains(id))
.map(id -> "$" + id)
.toList();
}
private boolean isViewer(Element e) {
String tag =
BindingDocument.FX.equals(e.getNamespaceURI()) && "root".equals(e.getLocalName())
? e.getAttribute("type")
: e.getTagName();
return resolves(tag, TYPES.get("HtmlViewer"));
}
List<String> viewerReferenceErrors() {
var errors = new ArrayList<String>();
for (var item : items) {
if (!item.type().equals(MEDIA_BUTTON)) continue;
String value = element(item).getAttribute("viewer");
if (!value.isEmpty() && !value.startsWith("${") && !viewerTargets(item).contains(value))
errors.add(viewerReferenceError(value));
}
return errors;
}
private static String viewerReferenceError(String value) {
return "HtmlMediaButton.viewer requires a unique HtmlViewer fx:id declared before the button: "
+ value;
}
private boolean resolves(String tag, String type) {
if (tag.contains(".")) return tag.equals(type);
if (imports.containsKey(tag)) return type.equals(imports.get(tag));
@@ -289,7 +346,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.");
if (!palette().contains(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>();
@@ -445,6 +502,7 @@ final class FormDocument {
.contains(item.type())) field(fields, "editable", "Boolean", "true");
if (Set.of("Label", "Button", "CheckBox", "TextArea").contains(item.type()))
field(fields, "wrapText", "Boolean", "false");
if (item.type().equals(MEDIA_BUTTON)) field(fields, "viewer", VIEWER_REFERENCE, "none");
if (item.type().equals("HtmlViewer")) {
field(fields, "html", "String", "");
field(fields, "mediaButtonVisible", "Boolean", "true");
@@ -576,6 +634,10 @@ final class FormDocument {
}
if (!reset) {
switch (field.type()) {
case VIEWER_REFERENCE -> {
if (!viewerTargets(item).contains(input))
throw new IllegalArgumentException(viewerReferenceError(input));
}
case "Boolean" -> {
if (!Set.of("true", "false").contains(input))
throw new IllegalArgumentException("Choose true or false.");
@@ -14,6 +14,7 @@ final class FormEditor extends VBox {
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");
private final ComboBox<String> viewers = combo("form-viewer-reference");
private final ComboBox<String> handlers = combo("form-event-handlers");
private Optional<List<String>> verifiedHandlers = Optional.empty();
private final TextArea value = new TextArea();
@@ -29,13 +30,6 @@ final class FormEditor extends VBox {
Consumer<BindingDocument.Values> bind) {
super(5);
this.submit = submit;
palette
.getItems()
.setAll(
FormDocument.TYPES.keySet().stream()
.filter(type -> !type.equals("ContextMenu"))
.toList());
palette.setValue("Label");
position.setId("form-position");
position.setPrefWidth(74);
handlers.setPromptText("Choose controller method");
@@ -45,6 +39,9 @@ final class FormEditor extends VBox {
e -> {
if (handlers.getValue() != null) value.setText(handlers.getValue());
});
viewers.setPromptText("Choose HtmlViewer");
viewers.managedProperty().bind(viewers.visibleProperty());
value.managedProperty().bind(value.visibleProperty());
value.setId("form-value");
value.setPrefRowCount(2);
details.setId("form-field-type");
@@ -96,6 +93,7 @@ final class FormEditor extends VBox {
position.getValue())))),
row(new Label("Property"), field, details),
handlers,
viewers,
value,
row(
button(
@@ -112,7 +110,12 @@ final class FormEditor extends VBox {
+ " for ActionEvent. Choose a listed method with no"
+ " argument or a compatible event argument.");
return form.set(
selected.getValue(), field.getValue(), value.getText(), false);
selected.getValue(),
field.getValue(),
viewers.isVisible()
? Objects.toString(viewers.getValue(), "")
: value.getText(),
false);
})),
button(
"Reset property",
@@ -165,6 +168,13 @@ final class FormEditor extends VBox {
}
}
void selectViewerField() {
field.getItems().stream()
.filter(property -> property.type().equals(FormDocument.VIEWER_REFERENCE))
.findFirst()
.ifPresent(field::setValue);
}
void selectIndex(Integer index) {
if (form == null || index == null || loading) return;
form.items.stream()
@@ -218,6 +228,11 @@ final class FormEditor extends VBox {
handlers.getSelectionModel().clearSelection();
var f = field.getValue();
var item = selected.getValue();
boolean viewerReference = f != null && f.type().equals(FormDocument.VIEWER_REFERENCE);
viewers.setVisible(viewerReference);
value.setVisible(!viewerReference);
viewers.getItems().clear();
viewers.setValue(null);
if (form == null || item == null || f == null) {
details.setText("");
value.clear();
@@ -232,8 +247,17 @@ final class FormEditor extends VBox {
? "bind to a model list"
: f.editable() ? "editable" : "read only"));
if (eventSelected()) showHandlerStatus();
String current = form.value(item, f);
if (viewerReference) {
viewers.getItems().setAll(form.viewerTargets(item));
if (viewers.getItems().contains(current)) viewers.setValue(current);
else if (!current.isEmpty())
details.setText("Unresolved or incompatible HtmlViewer target " + current);
else if (viewers.getItems().isEmpty())
details.setText("No HtmlViewer with an earlier fx:id.");
}
value.setEditable(f.editable());
value.setText(form.value(item, f));
value.setText(current);
}
String load(String source, boolean writable) {
@@ -248,6 +272,9 @@ final class FormEditor extends VBox {
loading = true;
try {
form = new FormDocument(source);
String paletteType = palette.getValue();
palette.getItems().setAll(form.palette());
palette.setValue(palette.getItems().contains(paletteType) ? paletteType : "Label");
selected.getItems().setAll(form.items);
destination.getItems().setAll(form.slots);
selected.setValue(
@@ -401,6 +401,7 @@ final class ReferenceDocument {
&& a.getNodeName().equals("source")) id = a.getNodeValue();
if (id != null && !available.contains(id)) result.add("Unresolved FXML reference: " + id);
}
result.addAll(new FormDocument(xml).viewerReferenceErrors());
return List.copyOf(result);
}
@@ -70,7 +70,11 @@ class FormDocumentTest {
@Test
void emptyGridAndNamedListsKeepReferencesAndRejectCycles() throws Exception {
assertEquals(
9, FormDocument.TYPES.values().stream().filter(n -> n.startsWith("de.neitzel.")).count());
9,
doc().palette().stream()
.map(FormDocument.TYPES::get)
.filter(n -> n.startsWith("de.neitzel."))
.count());
for (String type :
List.of(
"Label",
@@ -168,4 +172,34 @@ class FormDocumentTest {
assertEquals(2, doc().size(slot("", "items")));
assertFalse(source.contains("<items>"));
}
@Test
void mediaReferenceChecksTypeAndDeclarationOrderAndPreservesHtmlBinding() throws Exception {
source =
"""
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.Label?>
<?import de.neitzel.archiver.ui.HtmlMediaButton?>
<?import de.neitzel.fx.control.HtmlViewer?>
<?import de.neitzel.fx.view.*?>
<HBox xmlns:fx="http://javafx.com/fxml/1">
<fx:define><BindingList><DirectBinding control="article" property="html" path="html" direction="view"/></BindingList></fx:define>
<HtmlViewer fx:id="article"/>
<Label fx:id="unrelated"/>
<HtmlMediaButton fx:id="media" viewer="$unrelated"/>
<HtmlViewer fx:id="later"/>
</HBox>
""";
assertTrue(doc().palette().contains("HtmlMediaButton"));
assertEquals(List.of("$article"), doc().viewerTargets(item("media")));
assertTrue(new ReferenceDocument(source).referenceErrors().getFirst().contains("$unrelated"));
assertThrows(IllegalArgumentException.class, () -> set("media", "viewer", "$later"));
assertThrows(IllegalArgumentException.class, () -> set("media", "viewer", "$missing"));
set("media", "viewer", "$article");
assertTrue(new ReferenceDocument(source).referenceErrors().isEmpty());
assertTrue(
source.contains(
"<DirectBinding control=\"article\" property=\"html\" path=\"html\""
+ " direction=\"view\"/>"));
}
}