feat(fx): open and activate dockables from Java and FXML
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
package de.neitzel.fx.docking;
|
||||
|
||||
/** Outcome of a synchronous docking operation. */
|
||||
public sealed interface ActionResult {
|
||||
record Applied() implements ActionResult { }
|
||||
record NoChange() implements ActionResult { }
|
||||
record Rejected(DockFailure failure) implements ActionResult { }
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package de.neitzel.fx.docking;
|
||||
|
||||
/** Stable identity of an application's predefined destination. */
|
||||
public record DockAreaId(String value) {
|
||||
public DockAreaId {
|
||||
if (value == null || value.isBlank()) throw new IllegalArgumentException("Area ID must not be blank");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package de.neitzel.fx.docking;
|
||||
|
||||
/** Machine-readable rejection with optional affected identity and technical cause. */
|
||||
public record DockFailure(Code code, DockableId dockableId, String message, Throwable cause) {
|
||||
public enum Code { TARGET_NOT_FOUND, DOCKABLE_NOT_FOUND, CONTENT_ALREADY_ATTACHED }
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package de.neitzel.fx.docking;
|
||||
|
||||
import javafx.geometry.Side;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/** JavaBean definition of a named initial tab group, usable in ordinary FXML. */
|
||||
@Getter @Setter
|
||||
public final class DockGroupDefinition {
|
||||
private String destinationId;
|
||||
private Side tabSide = Side.TOP;
|
||||
private double preferredWidth = 200;
|
||||
private double preferredHeight = 150;
|
||||
|
||||
/** Creates a definition for FXML configuration. */
|
||||
public DockGroupDefinition() { }
|
||||
|
||||
/** Creates a named destination. */
|
||||
public DockGroupDefinition(String destinationId) {
|
||||
this.destinationId = destinationId;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package de.neitzel.fx.docking;
|
||||
|
||||
/** Identity of a live tab group. */
|
||||
public record DockGroupId(String value) {
|
||||
public DockGroupId {
|
||||
if (value == null || value.isBlank()) throw new IllegalArgumentException("Group ID must not be blank");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package de.neitzel.fx.docking;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/** Initial named destinations surrounding a central group. */
|
||||
@Getter @Setter
|
||||
public final class DockLayout {
|
||||
private DockGroupDefinition center;
|
||||
private DockGroupDefinition left;
|
||||
private DockGroupDefinition right;
|
||||
private DockGroupDefinition top;
|
||||
private DockGroupDefinition bottom;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package de.neitzel.fx.docking;
|
||||
|
||||
import javafx.geometry.Side;
|
||||
import java.util.List;
|
||||
|
||||
/** Immutable overview published after a completed layout change. */
|
||||
public record DockLayoutView(List<Window> windows) {
|
||||
public DockLayoutView { windows = List.copyOf(windows); }
|
||||
public record Window(DockWindowId id, boolean main, List<Area> areas) {
|
||||
public Window { areas = List.copyOf(areas); }
|
||||
}
|
||||
public record Area(DockAreaId id, List<Group> groups) {
|
||||
public Area { groups = List.copyOf(groups); }
|
||||
}
|
||||
public record Group(DockGroupId id, Side tabSide, List<DockableId> dockableIds,
|
||||
DockableId selectedDockableId) {
|
||||
public Group { dockableIds = List.copyOf(dockableIds); }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package de.neitzel.fx.docking;
|
||||
|
||||
import javafx.scene.layout.BorderPane;
|
||||
import javafx.scene.layout.Region;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/** Embeddable docking area whose definition is captured when a manager attaches. */
|
||||
public final class DockPane extends Region {
|
||||
@Getter @Setter private DockLayout initialLayout = new DockLayout();
|
||||
final BorderPane surface = new BorderPane();
|
||||
DockingManager manager;
|
||||
|
||||
public DockPane() {
|
||||
getStyleClass().add("dock-pane");
|
||||
getChildren().add(surface);
|
||||
}
|
||||
|
||||
@Override protected void layoutChildren() { surface.resizeRelocate(0, 0, getWidth(), getHeight()); }
|
||||
@Override protected double computePrefWidth(double height) { return surface.prefWidth(height); }
|
||||
@Override protected double computePrefHeight(double width) { return surface.prefHeight(width); }
|
||||
@Override protected double computeMinWidth(double height) { return surface.minWidth(height); }
|
||||
@Override protected double computeMinHeight(double width) { return surface.minHeight(width); }
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package de.neitzel.fx.docking;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/** A destination for a Dockable. */
|
||||
public sealed interface DockTarget {
|
||||
static DockTarget area(String id) { return new Area(new DockAreaId(id)); }
|
||||
static DockTarget area(DockAreaId id) { return new Area(id); }
|
||||
record Area(DockAreaId id) implements DockTarget {
|
||||
public Area { Objects.requireNonNull(id, "id"); }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package de.neitzel.fx.docking;
|
||||
|
||||
/** Identity of a window managed by a docking manager. */
|
||||
public record DockWindowId(String value) {
|
||||
public DockWindowId {
|
||||
if (value == null || value.isBlank()) throw new IllegalArgumentException("Window ID must not be blank");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package de.neitzel.fx.docking;
|
||||
|
||||
import javafx.beans.property.ObjectProperty;
|
||||
import javafx.beans.property.SimpleObjectProperty;
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
import javafx.beans.property.StringProperty;
|
||||
import javafx.scene.Node;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/** Existing live content with stable identity and bindable tab presentation. */
|
||||
public final class Dockable {
|
||||
@Getter private final DockableId id;
|
||||
@Getter private final Node content;
|
||||
@Getter @Setter private Supplier<? extends Node> focusTarget;
|
||||
private final StringProperty title = new SimpleStringProperty(this, "title", "");
|
||||
private final ObjectProperty<Node> icon = new SimpleObjectProperty<>(this, "icon");
|
||||
|
||||
public Dockable(DockableId id, Node content) {
|
||||
this.id = Objects.requireNonNull(id, "id");
|
||||
this.content = Objects.requireNonNull(content, "content");
|
||||
}
|
||||
|
||||
public StringProperty titleProperty() { return title; }
|
||||
public String getTitle() { return title.get(); }
|
||||
public void setTitle(String value) { title.set(value); }
|
||||
public ObjectProperty<Node> iconProperty() { return icon; }
|
||||
public Node getIcon() { return icon.get(); }
|
||||
public void setIcon(Node value) { icon.set(value); }
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package de.neitzel.fx.docking;
|
||||
|
||||
/** Stable application identity of one Dockable. */
|
||||
public record DockableId(String value) {
|
||||
public DockableId {
|
||||
if (value == null || value.isBlank()) throw new IllegalArgumentException("Dockable ID must not be blank");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,209 @@
|
||||
package de.neitzel.fx.docking;
|
||||
|
||||
import javafx.application.Platform;
|
||||
import javafx.beans.property.ReadOnlyObjectProperty;
|
||||
import javafx.beans.property.ReadOnlyObjectWrapper;
|
||||
import javafx.scene.Node;
|
||||
import javafx.scene.control.Tab;
|
||||
import javafx.scene.control.TabPane;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
/** Owns the live tab groups of one embedded docking area. */
|
||||
public final class DockingManager {
|
||||
private static final Object ATTACHMENT_KEY = new Object();
|
||||
private final Stage stage;
|
||||
private final DockPane pane;
|
||||
private boolean updating;
|
||||
private final Map<DockAreaId, Group> groups = new LinkedHashMap<>();
|
||||
private final Map<DockableId, Dockable> dockables = new LinkedHashMap<>();
|
||||
private final Map<DockableId, Node> previousFocus = new LinkedHashMap<>();
|
||||
private final javafx.beans.value.ChangeListener<Node> focusListener = (observable, old, current) -> rememberFocus(current);
|
||||
private final ReadOnlyObjectWrapper<Dockable> active = new ReadOnlyObjectWrapper<>(this, "activeDockable");
|
||||
private final ReadOnlyObjectWrapper<DockLayoutView> layout = new ReadOnlyObjectWrapper<>(this, "layout");
|
||||
|
||||
private DockingManager(Stage stage, DockPane pane) {
|
||||
this.stage = stage;
|
||||
this.pane = pane;
|
||||
DockLayout initial = Objects.requireNonNull(pane.getInitialLayout(), "initialLayout");
|
||||
validate(initial);
|
||||
addGroup(initial.getCenter(), pane.surface::setCenter);
|
||||
addGroup(initial.getLeft(), pane.surface::setLeft);
|
||||
addGroup(initial.getRight(), pane.surface::setRight);
|
||||
addGroup(initial.getTop(), pane.surface::setTop);
|
||||
addGroup(initial.getBottom(), pane.surface::setBottom);
|
||||
publish();
|
||||
stage.getScene().focusOwnerProperty().addListener(focusListener);
|
||||
}
|
||||
|
||||
public static DockingManager attach(Stage stage, DockPane pane, DockingOptions options) {
|
||||
requireFxThread();
|
||||
Objects.requireNonNull(stage, "stage");
|
||||
Objects.requireNonNull(pane, "pane");
|
||||
Objects.requireNonNull(options, "options");
|
||||
if (stage.getScene() == null || stage.getScene() != pane.getScene()) {
|
||||
throw new IllegalArgumentException("DockPane must belong to the main Stage's Scene");
|
||||
}
|
||||
if (pane.manager != null || stage.getProperties().containsKey(ATTACHMENT_KEY)) {
|
||||
throw new IllegalStateException("DockPane or main Stage already has a docking manager");
|
||||
}
|
||||
DockingManager manager = new DockingManager(stage, pane);
|
||||
pane.manager = manager;
|
||||
stage.getProperties().put(ATTACHMENT_KEY, manager);
|
||||
return manager;
|
||||
}
|
||||
|
||||
public ActionResult open(Dockable dockable, DockTarget target) {
|
||||
requireFxThread();
|
||||
Objects.requireNonNull(dockable, "dockable");
|
||||
if (dockables.containsKey(dockable.getId())) return activate(dockable.getId());
|
||||
Objects.requireNonNull(target, "target");
|
||||
Group group = groups.get(((DockTarget.Area) target).id());
|
||||
if (group == null) return rejected(DockFailure.Code.TARGET_NOT_FOUND, dockable.getId(), "Destination does not exist");
|
||||
if (dockable.getContent().getParent() != null || dockable.getContent().getScene() != null) {
|
||||
return rejected(DockFailure.Code.CONTENT_ALREADY_ATTACHED, dockable.getId(), "Content already belongs to a scene graph");
|
||||
}
|
||||
Tab tab = new Tab();
|
||||
tab.setClosable(false);
|
||||
tab.textProperty().bind(dockable.titleProperty());
|
||||
tab.graphicProperty().bind(dockable.iconProperty());
|
||||
tab.setContent(dockable.getContent());
|
||||
tab.setUserData(dockable.getId());
|
||||
dockables.put(dockable.getId(), dockable);
|
||||
updating = true;
|
||||
try {
|
||||
group.tabs.getTabs().add(tab);
|
||||
group.tabs.getSelectionModel().select(tab);
|
||||
} finally {
|
||||
updating = false;
|
||||
}
|
||||
active.set(dockable);
|
||||
focus(dockable);
|
||||
publish();
|
||||
return new ActionResult.Applied();
|
||||
}
|
||||
|
||||
public ActionResult activate(DockableId id) {
|
||||
requireFxThread();
|
||||
Dockable dockable = dockables.get(id);
|
||||
if (dockable == null) return rejected(DockFailure.Code.DOCKABLE_NOT_FOUND, id, "Dockable is not open");
|
||||
Node target = focusTarget(dockable);
|
||||
boolean changed = active.get() != dockable || stage.getScene().getFocusOwner() != target
|
||||
|| (stage.isShowing() && !stage.isFocused());
|
||||
for (Group group : groups.values()) {
|
||||
for (Tab tab : group.tabs.getTabs()) {
|
||||
if (id.equals(tab.getUserData())) {
|
||||
changed |= group.tabs.getSelectionModel().getSelectedItem() != tab;
|
||||
updating = true;
|
||||
try {
|
||||
group.tabs.getSelectionModel().select(tab);
|
||||
} finally {
|
||||
updating = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
active.set(dockable);
|
||||
if (stage.isShowing()) stage.toFront();
|
||||
focus(dockable);
|
||||
if (changed) publish();
|
||||
return changed ? new ActionResult.Applied() : new ActionResult.NoChange();
|
||||
}
|
||||
|
||||
public Optional<Dockable> findDockable(DockableId id) { return Optional.ofNullable(dockables.get(id)); }
|
||||
public Dockable activeDockable() { return active.get(); }
|
||||
public ReadOnlyObjectProperty<Dockable> activeDockableProperty() { return active.getReadOnlyProperty(); }
|
||||
public DockLayoutView layout() { return layout.get(); }
|
||||
public ReadOnlyObjectProperty<DockLayoutView> layoutProperty() { return layout.getReadOnlyProperty(); }
|
||||
|
||||
private void addGroup(DockGroupDefinition definition, java.util.function.Consumer<javafx.scene.Node> place) {
|
||||
if (definition == null) return;
|
||||
DockAreaId area = new DockAreaId(definition.getDestinationId());
|
||||
if (groups.containsKey(area)) throw new IllegalArgumentException("Duplicate destination: " + area.value());
|
||||
TabPane tabs = new TabPane();
|
||||
tabs.setSide(Objects.requireNonNull(definition.getTabSide(), "tabSide"));
|
||||
tabs.setPrefSize(definition.getPreferredWidth(), definition.getPreferredHeight());
|
||||
Group group = new Group(new DockGroupId(area.value()), tabs);
|
||||
groups.put(area, group);
|
||||
tabs.getSelectionModel().selectedItemProperty().addListener((observable, old, selected) -> {
|
||||
if (!updating && selected != null) {
|
||||
active.set(dockables.get((DockableId) selected.getUserData()));
|
||||
focus(active.get());
|
||||
publish();
|
||||
}
|
||||
});
|
||||
place.accept(tabs);
|
||||
}
|
||||
|
||||
private void rememberFocus(Node current) {
|
||||
if (current == null) return;
|
||||
for (Dockable dockable : dockables.values()) {
|
||||
if (contains(dockable.getContent(), current)) {
|
||||
previousFocus.put(dockable.getId(), current);
|
||||
active.set(dockable);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Node focusTarget(Dockable dockable) {
|
||||
Node target = dockable.getFocusTarget() == null ? previousFocus.get(dockable.getId()) : dockable.getFocusTarget().get();
|
||||
if (target == null || !contains(dockable.getContent(), target)) return dockable.getContent();
|
||||
return target;
|
||||
}
|
||||
|
||||
private void focus(Dockable dockable) {
|
||||
if (dockable == null) return;
|
||||
pane.applyCss();
|
||||
Node target = focusTarget(dockable);
|
||||
target.requestFocus();
|
||||
}
|
||||
|
||||
private static boolean contains(Node root, Node node) {
|
||||
for (Node current = node; current != null; current = current.getParent()) {
|
||||
if (current == root) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void validate(DockLayout initial) {
|
||||
var destinations = new java.util.HashSet<DockAreaId>();
|
||||
for (DockGroupDefinition definition : new DockGroupDefinition[]{initial.getCenter(), initial.getLeft(),
|
||||
initial.getRight(), initial.getTop(), initial.getBottom()}) {
|
||||
if (definition == null) continue;
|
||||
DockAreaId id = new DockAreaId(definition.getDestinationId());
|
||||
if (!destinations.add(id)) throw new IllegalArgumentException("Duplicate destination: " + id.value());
|
||||
if (definition.getTabSide() == null) throw new IllegalArgumentException("Tab side is required");
|
||||
if (!Double.isFinite(definition.getPreferredWidth()) || definition.getPreferredWidth() <= 0
|
||||
|| !Double.isFinite(definition.getPreferredHeight()) || definition.getPreferredHeight() <= 0) {
|
||||
throw new IllegalArgumentException("Group preferred dimensions must be positive and finite");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void publish() {
|
||||
List<DockLayoutView.Area> areas = groups.entrySet().stream().map(entry -> {
|
||||
Group group = entry.getValue();
|
||||
List<DockableId> ids = group.tabs.getTabs().stream().map(tab -> (DockableId) tab.getUserData()).toList();
|
||||
Tab selected = group.tabs.getSelectionModel().getSelectedItem();
|
||||
return new DockLayoutView.Area(entry.getKey(), List.of(new DockLayoutView.Group(group.id,
|
||||
group.tabs.getSide(), ids, selected == null ? null : (DockableId) selected.getUserData())));
|
||||
}).toList();
|
||||
layout.set(new DockLayoutView(List.of(new DockLayoutView.Window(new DockWindowId("main"), true, areas))));
|
||||
}
|
||||
|
||||
private static ActionResult rejected(DockFailure.Code code, DockableId id, String message) {
|
||||
return new ActionResult.Rejected(new DockFailure(code, id, message, null));
|
||||
}
|
||||
|
||||
private static void requireFxThread() {
|
||||
if (!Platform.isFxApplicationThread()) throw new IllegalStateException("Docking operations require the JavaFX Application Thread");
|
||||
}
|
||||
|
||||
private record Group(DockGroupId id, TabPane tabs) { }
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package de.neitzel.fx.docking;
|
||||
|
||||
/** Attachment options. Additional policies are introduced with their operations. */
|
||||
public final class DockingOptions {
|
||||
}
|
||||
Reference in New Issue
Block a user