feat(fx): open and activate dockables from Java and FXML

This commit is contained in:
2026-11-15 20:03:34 +01:00
parent deb363eeb6
commit 39261de098
23 changed files with 846 additions and 0 deletions
@@ -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 {
}
@@ -0,0 +1,95 @@
package de.neitzel.fx.view;
import de.neitzel.fx.docking.ActionResult;
import de.neitzel.fx.docking.DockGroupDefinition;
import de.neitzel.fx.docking.DockLayout;
import de.neitzel.fx.docking.DockPane;
import de.neitzel.fx.docking.DockTarget;
import de.neitzel.fx.docking.Dockable;
import de.neitzel.fx.docking.DockableId;
import de.neitzel.fx.docking.DockingManager;
import de.neitzel.fx.docking.DockingOptions;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
/** Activating a background window is an applied action even when its selected tab stays the same. */
class ActivateDockingWindowTest {
@BeforeAll
static void startJavaFx() throws Exception {
FxThread.start();
FxThread.onFx(() -> Platform.setImplicitExit(false));
}
@Test
void activatingTheSameContentInABackgroundWindowReportsApplied() throws Exception {
AtomicReference<Fixture> state = new AtomicReference<>();
FxThread.onFx(() -> {
DockLayout initial = new DockLayout();
initial.setCenter(new DockGroupDefinition("editors"));
DockPane pane = new DockPane();
pane.setInitialLayout(initial);
Stage main = new Stage();
main.setScene(new Scene(pane, 480, 280));
DockingManager manager = DockingManager.attach(main, pane, new DockingOptions());
TextArea editor = new TextArea("Unsaved text");
Dockable dockable = new Dockable(new DockableId("notes"), editor);
manager.open(dockable, DockTarget.area("editors"));
Stage other = new Stage();
other.setScene(new Scene(new TextArea("Other window"), 240, 160));
state.set(new Fixture(main, other, manager, dockable));
main.show();
manager.activate(dockable.getId());
});
Fixture fixture = state.get();
try {
focus(fixture.main());
FxThread.onFx(() -> fixture.other().show());
focus(fixture.other());
FxThread.onFx(() -> {
assertFalse(fixture.main().isFocused());
assertSame(fixture.dockable(), fixture.manager().activeDockable());
assertSame(fixture.dockable().getContent(), fixture.main().getScene().getFocusOwner());
assertInstanceOf(ActionResult.Applied.class, fixture.manager().activate(fixture.dockable().getId()));
});
} finally {
FxThread.onFx(() -> {
fixture.other().hide();
fixture.main().hide();
});
}
}
private static void focus(Stage stage) throws Exception {
CountDownLatch focused = new CountDownLatch(1);
ChangeListener<Boolean> listener = (observable, old, current) -> {
if (current) focused.countDown();
};
FxThread.onFx(() -> {
stage.focusedProperty().addListener(listener);
stage.toFront();
stage.requestFocus();
if (stage.isFocused()) focused.countDown();
});
try {
assertTrue(focused.await(5, TimeUnit.SECONDS), "Desktop did not focus the requested window");
} finally {
FxThread.onFx(() -> stage.focusedProperty().removeListener(listener));
}
}
private record Fixture(Stage main, Stage other, DockingManager manager, Dockable dockable) { }
}
@@ -0,0 +1,215 @@
package de.neitzel.fx.view;
import de.neitzel.fx.docking.*;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
/** Applications open and observe live content without MVVM. */
class OpenDockablesTest {
@BeforeAll
static void startJavaFx() throws InterruptedException {
FxThread.start();
}
@Test
void opensExistingContentInANamedAreaBeforeShowingTheWindow() throws Exception {
FxThread.onFx(() -> {
DockLayout initial = new DockLayout();
initial.setCenter(new DockGroupDefinition("editors"));
DockPane pane = new DockPane();
pane.setInitialLayout(initial);
Stage stage = new Stage();
stage.setScene(new Scene(pane));
DockingManager manager = DockingManager.attach(stage, pane, new DockingOptions());
TextArea editor = new TextArea("Unsaved text");
Dockable document = new Dockable(new DockableId("notes"), editor);
assertInstanceOf(ActionResult.Applied.class, manager.open(document, DockTarget.area("editors")));
assertSame(document, manager.findDockable(document.getId()).orElseThrow());
assertSame(document, manager.activeDockable());
assertEquals(List.of(document.getId()), manager.layout().windows().getFirst().areas().getFirst()
.groups().getFirst().dockableIds());
pane.applyCss();
pane.layout();
assertSame(pane.getScene(), editor.getScene());
assertFalse(stage.isShowing());
});
}
@Test
void selectingATabPublishesTheCompletedSelectionAndActiveContent() throws Exception {
FxThread.onFx(() -> {
DockLayout initial = new DockLayout();
initial.setCenter(new DockGroupDefinition("editors"));
DockPane pane = new DockPane();
pane.setInitialLayout(initial);
Stage stage = new Stage();
stage.setScene(new Scene(pane));
DockingManager manager = DockingManager.attach(stage, pane, new DockingOptions());
Dockable first = new Dockable(new DockableId("first"), new TextArea("one"));
Dockable second = new Dockable(new DockableId("second"), new TextArea("two"));
manager.open(first, DockTarget.area("editors"));
manager.open(second, DockTarget.area("editors"));
var changes = new java.util.ArrayList<DockLayoutView>();
manager.layoutProperty().addListener((observable, old, current) -> changes.add(current));
((javafx.scene.control.TabPane) pane.lookup(".tab-pane")).getSelectionModel().selectFirst();
assertSame(first, manager.activeDockable());
assertEquals(1, changes.size());
assertEquals(first.getId(), changes.getFirst().windows().getFirst().areas().getFirst()
.groups().getFirst().selectedDockableId());
});
}
@Test
void standardFxmlAndJavaHaveTheSameCapturedLayout() throws Exception {
FxThread.onFx(() -> {
DockPane fxmlPane = javafx.fxml.FXMLLoader.load(getClass().getResource("docking-initial.fxml"));
Stage fxmlStage = new Stage();
fxmlStage.setScene(new Scene(fxmlPane));
DockingManager fxml = DockingManager.attach(fxmlStage, fxmlPane, new DockingOptions());
DockLayout initial = new DockLayout();
initial.setCenter(new DockGroupDefinition("editors"));
DockGroupDefinition tools = new DockGroupDefinition("tools");
tools.setTabSide(javafx.geometry.Side.RIGHT);
tools.setPreferredWidth(200);
initial.setRight(tools);
DockPane pane = new DockPane();
pane.setInitialLayout(initial);
Stage stage = new Stage();
stage.setScene(new Scene(pane));
DockingManager manager = DockingManager.attach(stage, pane, new DockingOptions());
assertEquals(fxml.layout(), manager.layout());
initial.setCenter(new DockGroupDefinition("replacement"));
tools.setDestinationId("changed");
tools.setTabSide(javafx.geometry.Side.LEFT);
assertEquals(fxml.layout(), manager.layout());
assertThrows(UnsupportedOperationException.class, () -> manager.layout().windows().clear());
assertThrows(UnsupportedOperationException.class, () -> manager.layout().windows().getFirst().areas().clear());
Dockable project = new Dockable(new DockableId("project"), new TextArea());
assertInstanceOf(ActionResult.Applied.class, manager.open(project, DockTarget.area("tools")));
});
}
@Test
void invalidDefinitionsLeaveThePaneAvailableForACorrectedAttach() throws Exception {
FxThread.onFx(() -> {
DockLayout initial = new DockLayout();
initial.setCenter(new DockGroupDefinition("editors"));
initial.setLeft(new DockGroupDefinition("editors"));
DockPane pane = new DockPane();
pane.setInitialLayout(initial);
Stage stage = new Stage();
stage.setScene(new Scene(pane));
assertThrows(IllegalArgumentException.class, () -> DockingManager.attach(stage, pane, new DockingOptions()));
assertNull(pane.lookup(".tab-pane"));
initial.getLeft().setDestinationId("tools");
assertNotNull(DockingManager.attach(stage, pane, new DockingOptions()));
});
}
@Test
void activationUsesTheContentFocusTargetAndKeepsItAcrossGlobalActions() throws Exception {
FxThread.onFx(() -> {
DockLayout initial = new DockLayout();
initial.setCenter(new DockGroupDefinition("editors"));
DockPane pane = new DockPane();
pane.setInitialLayout(initial);
javafx.scene.control.TextField first = new javafx.scene.control.TextField();
javafx.scene.control.TextField second = new javafx.scene.control.TextField();
Dockable document = new Dockable(new DockableId("document"), new javafx.scene.layout.VBox(first, second));
document.setFocusTarget(() -> second);
javafx.scene.control.Button global = new javafx.scene.control.Button("Global action");
javafx.scene.layout.BorderPane shell = new javafx.scene.layout.BorderPane(pane);
shell.setTop(global);
Stage stage = new Stage();
stage.setScene(new Scene(shell, 500, 300));
DockingManager manager = DockingManager.attach(stage, pane, new DockingOptions());
manager.open(document, DockTarget.area("editors"));
pane.applyCss();
pane.layout();
manager.activate(document.getId());
assertSame(second, stage.getScene().getFocusOwner());
global.requestFocus();
assertSame(document, manager.activeDockable());
assertInstanceOf(ActionResult.Applied.class, manager.activate(document.getId()));
assertSame(second, stage.getScene().getFocusOwner());
});
}
@Test
void actualContentFocusActivatesAndIsRememberedForTheNextActivation() throws Exception {
FxThread.onFx(() -> {
DockLayout initial = new DockLayout();
initial.setCenter(new DockGroupDefinition("editors"));
initial.setRight(new DockGroupDefinition("tools"));
DockPane pane = new DockPane();
pane.setInitialLayout(initial);
javafx.scene.control.TextField first = new javafx.scene.control.TextField();
javafx.scene.control.TextField second = new javafx.scene.control.TextField();
Dockable document = new Dockable(new DockableId("document"), new javafx.scene.layout.VBox(first, second));
javafx.scene.control.Button global = new javafx.scene.control.Button("Global");
javafx.scene.layout.BorderPane shell = new javafx.scene.layout.BorderPane(pane);
shell.setTop(global);
Stage stage = new Stage();
stage.setScene(new Scene(shell, 500, 300));
DockingManager manager = DockingManager.attach(stage, pane, new DockingOptions());
manager.open(document, DockTarget.area("editors"));
manager.open(new Dockable(new DockableId("tool"), new TextArea()), DockTarget.area("tools"));
first.requestFocus();
assertSame(document, manager.activeDockable());
global.requestFocus();
manager.activate(document.getId());
assertSame(first, stage.getScene().getFocusOwner());
});
}
@Test
void duplicateOpenRetainsContentAndBindingWithoutMovingIt() throws Exception {
FxThread.onFx(() -> {
DockLayout initial = new DockLayout();
initial.setCenter(new DockGroupDefinition("editors"));
initial.setRight(new DockGroupDefinition("tools"));
DockPane pane = new DockPane();
pane.setInitialLayout(initial);
Stage stage = new Stage();
stage.setScene(new Scene(pane));
DockingManager manager = DockingManager.attach(stage, pane, new DockingOptions());
Dockable original = new Dockable(new DockableId("notes"), new TextArea("Unsaved"));
var title = new javafx.beans.property.SimpleStringProperty("Notes");
original.titleProperty().bind(title);
var icon = new javafx.scene.control.Label("N");
original.setIcon(icon);
manager.open(original, DockTarget.area("editors"));
Dockable replacement = new Dockable(original.getId(), new TextArea("Replacement"));
assertInstanceOf(ActionResult.NoChange.class, manager.open(replacement, DockTarget.area("tools")));
assertSame(original, manager.findDockable(original.getId()).orElseThrow());
assertNull(replacement.getContent().getParent());
assertTrue(manager.layout().windows().getFirst().areas().getLast().groups().getFirst().dockableIds().isEmpty());
title.set("Renamed");
javafx.scene.control.TabPane tabs = pane.lookupAll(".tab-pane").stream()
.map(javafx.scene.control.TabPane.class::cast).filter(tabPane -> !tabPane.getTabs().isEmpty()).findFirst().orElseThrow();
assertEquals("Renamed", tabs.getTabs().getFirst().getText());
assertSame(icon, tabs.getTabs().getFirst().getGraphic());
title.set("");
assertEquals("", tabs.getTabs().getFirst().getText());
assertSame(icon, tabs.getTabs().getFirst().getGraphic());
assertInstanceOf(ActionResult.Rejected.class, manager.activate(new DockableId("missing")));
});
}
@Test
void attachRequiresTheMatchingSceneAndExclusivePaneAndStage() throws Exception {
FxThread.onFx(() -> {
DockPane pane = new DockPane();
DockPane other = new DockPane();
Stage stage = new Stage();
stage.setScene(new Scene(new javafx.scene.layout.HBox(pane, other)));
Stage wrongStage = new Stage();
wrongStage.setScene(new Scene(new javafx.scene.layout.Pane()));
assertThrows(IllegalArgumentException.class, () -> DockingManager.attach(wrongStage, pane, new DockingOptions()));
DockingManager.attach(stage, pane, new DockingOptions());
assertThrows(IllegalStateException.class, () -> DockingManager.attach(stage, pane, new DockingOptions()));
assertThrows(IllegalStateException.class, () -> DockingManager.attach(stage, other, new DockingOptions()));
});
}
}
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import de.neitzel.fx.docking.DockPane?>
<?import de.neitzel.fx.docking.DockLayout?>
<?import de.neitzel.fx.docking.DockGroupDefinition?>
<DockPane xmlns:fx="http://javafx.com/fxml/1">
<initialLayout>
<DockLayout>
<center><DockGroupDefinition destinationId="editors"/></center>
<right><DockGroupDefinition destinationId="tools" tabSide="RIGHT" preferredWidth="200"/></right>
</DockLayout>
</initialLayout>
</DockPane>