diff --git a/fx-example/README.md b/fx-example/README.md index 6aac1d4..3ab5c13 100644 --- a/fx-example/README.md +++ b/fx-example/README.md @@ -2,6 +2,10 @@ This runnable application demonstrates [NeitzelLib FX](../fx/README.md) and [injection](../injection/README.md) together. It edits one Person with a home Address, nested Note, visit list, selected visit and tags. Use it as a reference for wiring a real screen; the [minimal quick start](../fx/docs/quick-start.md) is the smaller starting point for a new project. +Separate Java/FXML launchers demonstrate [standalone bars](../fx/docs/bars.md) +and [docking](../fx/docs/docking.md) without initializing MVVM or injection. +Their guides describe the implemented behavior and launch options. + ## Build and run From the NeitzelLib root with JDK 25: diff --git a/fx-example/src/main/java/de/neitzel/fx/example/docking/DockingApp.java b/fx-example/src/main/java/de/neitzel/fx/example/docking/DockingApp.java new file mode 100644 index 0000000..1e96f6c --- /dev/null +++ b/fx-example/src/main/java/de/neitzel/fx/example/docking/DockingApp.java @@ -0,0 +1,61 @@ +package de.neitzel.fx.example.docking; + +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.Application; +import javafx.fxml.FXMLLoader; +import javafx.geometry.Side; +import javafx.scene.Scene; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.control.TextArea; +import javafx.scene.layout.BorderPane; +import javafx.stage.Stage; + +/** A small ordinary JavaFX application with editor and tool tabs, without MVVM. */ +public final class DockingApp extends Application { + @Override + public void start(Stage stage) throws Exception { + DockPane pane = getParameters().getRaw().contains("--fxml") + ? FXMLLoader.load(DockingApp.class.getResource("Docking.fxml")) : initialPane(); + Button activate = new Button("Activate Notes"); + BorderPane shell = new BorderPane(pane); + shell.setTop(activate); + stage.setScene(new Scene(shell, 900, 600)); + DockingManager manager = DockingManager.attach(stage, pane, new DockingOptions()); + DockableId notesId = new DockableId("notes"); + TextArea notes = new TextArea("Unsaved notes stay in this live editor."); + Dockable editor = new Dockable(notesId, notes); + editor.setTitle("Notes"); + manager.open(editor, DockTarget.area("editors")); + Dockable other = new Dockable(new DockableId("readme"), new TextArea("Readme")); + other.setTitle("Readme"); + manager.open(other, DockTarget.area("editors")); + Dockable project = new Dockable(new DockableId("project"), new Label("Project files")); + project.setTitle("Project"); + project.setIcon(new Label("▦")); + manager.open(project, DockTarget.area("tools")); + activate.setOnAction(event -> manager.activate(notesId)); + stage.setTitle("NeitzelLib docking — Java or ordinary FXML"); + stage.show(); + manager.activate(notesId); + } + + private static DockPane initialPane() { + DockLayout layout = new DockLayout(); + layout.setCenter(new DockGroupDefinition("editors")); + DockGroupDefinition tools = new DockGroupDefinition("tools"); + tools.setTabSide(Side.RIGHT); + tools.setPreferredWidth(200); + layout.setRight(tools); + DockPane pane = new DockPane(); + pane.setInitialLayout(layout); + return pane; + } +} diff --git a/fx-example/src/main/java/de/neitzel/fx/example/docking/DockingMain.java b/fx-example/src/main/java/de/neitzel/fx/example/docking/DockingMain.java new file mode 100644 index 0000000..c41d0db --- /dev/null +++ b/fx-example/src/main/java/de/neitzel/fx/example/docking/DockingMain.java @@ -0,0 +1,12 @@ +package de.neitzel.fx.example.docking; + +import javafx.application.Application; + +/** Plain launcher for the docking example; pass --fxml to load the equivalent FXML layout. */ +public final class DockingMain { + private DockingMain() { } + + public static void main(String[] args) { + Application.launch(DockingApp.class, args); + } +} diff --git a/fx-example/src/main/resources/de/neitzel/fx/example/docking/Docking.fxml b/fx-example/src/main/resources/de/neitzel/fx/example/docking/Docking.fxml new file mode 100644 index 0000000..1a6f315 --- /dev/null +++ b/fx-example/src/main/resources/de/neitzel/fx/example/docking/Docking.fxml @@ -0,0 +1,12 @@ + + + + + + + +
+ +
+
+
diff --git a/fx/README.md b/fx/README.md index e8b7e93..cc61dbb 100644 --- a/fx/README.md +++ b/fx/README.md @@ -8,6 +8,8 @@ - This page is the detailed FXML and runtime reference. - [Generated ViewModel classes and registries](docs/generated-viewmodels.md). - [Bounded integer spinners](docs/integer-spinners.md) and [system tray support](docs/tray-support.md). +- [Standalone toolbar and status bar](docs/bars.md), usable without MVVM. +- [Docking: opening and activating live contents](docs/docking.md), with ordinary Java/FXML startup examples. - [Dependency injection](../injection/README.md). ## Introduction diff --git a/fx/docs/docking.md b/fx/docs/docking.md new file mode 100644 index 0000000..ef4128d --- /dev/null +++ b/fx/docs/docking.md @@ -0,0 +1,49 @@ +# Docking + +Docking lives in the `fx` artifact and works with ordinary JavaFX Nodes and a +standard FXMLLoader. Neither a ViewLoader nor an injection context is needed. +A loaded NeitzelLib View root can also be used as content. + +## Open content + +Create a `DockLayout` with named `DockGroupDefinition` destinations, assign it +to a `DockPane`, and embed that pane in your main Stage's Scene. Then call +`DockingManager.attach(stage, pane, new DockingOptions())`. Attachment is allowed +before showing the Stage. A pane and main Stage can each have one active manager. +The initial definition is captured; later changes to the definition do not alter +the attached layout. `DockingOptions` currently has no policy fields in this +first implementation slice. + +A Dockable consists of a stable `DockableId` and the existing content Node. +Use `open(dockable, DockTarget.area("editors"))` to open it. Opening an ID that +is already open activates the existing Dockable and keeps its placement and +content. The title and icon expose ordinary JavaFX properties for bindings. +Groups can place their tab strip at any JavaFX `Side`; a blank title with an +icon provides a compact icon tab. + +Call operations on the JavaFX Application Thread. `ActionResult` distinguishes +`Applied`, `NoChange` and `Rejected`; the latter carries a machine-readable +`DockFailure`. Attachment errors throw with a diagnostic because no manager +exists yet to return an action result. + +## Observe and activate + +`layout()` and the read-only `layoutProperty()` expose immutable windows, +areas and groups, including the selected Dockable and tab order. Empty named +destinations are included. A selected-tab change publishes its completed state. +`findDockable(id)` reads an open instance without activating or creating it. +`activeDockable()` and its read-only property supply the target for global +application actions. Moving keyboard focus to a global button preserves it. + +`activate(id)` selects the Dockable and requests focus within its content. +The content instance remains owned by the application. Future docking slices +extend this API with movement, splits, close consent and restoration. + +## Java and FXML example + +The `fx-example` module includes `de.neitzel.fx.example.docking.DockingMain`. +Its Java-built layout and `--fxml` variant open the same editor and right-side +tool groups. It demonstrates unsaved editor text, tab selection and a global +Activate Notes action without MVVM. Build the reactor with Java 25, then launch +that main class using the example's runtime dependencies; no Designer build is +required. diff --git a/fx/src/main/java/de/neitzel/fx/docking/ActionResult.java b/fx/src/main/java/de/neitzel/fx/docking/ActionResult.java new file mode 100644 index 0000000..2190463 --- /dev/null +++ b/fx/src/main/java/de/neitzel/fx/docking/ActionResult.java @@ -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 { } +} diff --git a/fx/src/main/java/de/neitzel/fx/docking/DockAreaId.java b/fx/src/main/java/de/neitzel/fx/docking/DockAreaId.java new file mode 100644 index 0000000..63adea7 --- /dev/null +++ b/fx/src/main/java/de/neitzel/fx/docking/DockAreaId.java @@ -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"); + } +} diff --git a/fx/src/main/java/de/neitzel/fx/docking/DockFailure.java b/fx/src/main/java/de/neitzel/fx/docking/DockFailure.java new file mode 100644 index 0000000..0955f88 --- /dev/null +++ b/fx/src/main/java/de/neitzel/fx/docking/DockFailure.java @@ -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 } +} diff --git a/fx/src/main/java/de/neitzel/fx/docking/DockGroupDefinition.java b/fx/src/main/java/de/neitzel/fx/docking/DockGroupDefinition.java new file mode 100644 index 0000000..fe4ce0e --- /dev/null +++ b/fx/src/main/java/de/neitzel/fx/docking/DockGroupDefinition.java @@ -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; + } +} diff --git a/fx/src/main/java/de/neitzel/fx/docking/DockGroupId.java b/fx/src/main/java/de/neitzel/fx/docking/DockGroupId.java new file mode 100644 index 0000000..b991f25 --- /dev/null +++ b/fx/src/main/java/de/neitzel/fx/docking/DockGroupId.java @@ -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"); + } +} diff --git a/fx/src/main/java/de/neitzel/fx/docking/DockLayout.java b/fx/src/main/java/de/neitzel/fx/docking/DockLayout.java new file mode 100644 index 0000000..14df4bd --- /dev/null +++ b/fx/src/main/java/de/neitzel/fx/docking/DockLayout.java @@ -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; +} diff --git a/fx/src/main/java/de/neitzel/fx/docking/DockLayoutView.java b/fx/src/main/java/de/neitzel/fx/docking/DockLayoutView.java new file mode 100644 index 0000000..2a46c5b --- /dev/null +++ b/fx/src/main/java/de/neitzel/fx/docking/DockLayoutView.java @@ -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 windows) { + public DockLayoutView { windows = List.copyOf(windows); } + public record Window(DockWindowId id, boolean main, List areas) { + public Window { areas = List.copyOf(areas); } + } + public record Area(DockAreaId id, List groups) { + public Area { groups = List.copyOf(groups); } + } + public record Group(DockGroupId id, Side tabSide, List dockableIds, + DockableId selectedDockableId) { + public Group { dockableIds = List.copyOf(dockableIds); } + } +} diff --git a/fx/src/main/java/de/neitzel/fx/docking/DockPane.java b/fx/src/main/java/de/neitzel/fx/docking/DockPane.java new file mode 100644 index 0000000..459c1c9 --- /dev/null +++ b/fx/src/main/java/de/neitzel/fx/docking/DockPane.java @@ -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); } +} diff --git a/fx/src/main/java/de/neitzel/fx/docking/DockTarget.java b/fx/src/main/java/de/neitzel/fx/docking/DockTarget.java new file mode 100644 index 0000000..b02a85e --- /dev/null +++ b/fx/src/main/java/de/neitzel/fx/docking/DockTarget.java @@ -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"); } + } +} diff --git a/fx/src/main/java/de/neitzel/fx/docking/DockWindowId.java b/fx/src/main/java/de/neitzel/fx/docking/DockWindowId.java new file mode 100644 index 0000000..70dac55 --- /dev/null +++ b/fx/src/main/java/de/neitzel/fx/docking/DockWindowId.java @@ -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"); + } +} diff --git a/fx/src/main/java/de/neitzel/fx/docking/Dockable.java b/fx/src/main/java/de/neitzel/fx/docking/Dockable.java new file mode 100644 index 0000000..d4c2af5 --- /dev/null +++ b/fx/src/main/java/de/neitzel/fx/docking/Dockable.java @@ -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 focusTarget; + private final StringProperty title = new SimpleStringProperty(this, "title", ""); + private final ObjectProperty 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 iconProperty() { return icon; } + public Node getIcon() { return icon.get(); } + public void setIcon(Node value) { icon.set(value); } +} diff --git a/fx/src/main/java/de/neitzel/fx/docking/DockableId.java b/fx/src/main/java/de/neitzel/fx/docking/DockableId.java new file mode 100644 index 0000000..936e72d --- /dev/null +++ b/fx/src/main/java/de/neitzel/fx/docking/DockableId.java @@ -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"); + } +} diff --git a/fx/src/main/java/de/neitzel/fx/docking/DockingManager.java b/fx/src/main/java/de/neitzel/fx/docking/DockingManager.java new file mode 100644 index 0000000..fd8106d --- /dev/null +++ b/fx/src/main/java/de/neitzel/fx/docking/DockingManager.java @@ -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 groups = new LinkedHashMap<>(); + private final Map dockables = new LinkedHashMap<>(); + private final Map previousFocus = new LinkedHashMap<>(); + private final javafx.beans.value.ChangeListener focusListener = (observable, old, current) -> rememberFocus(current); + private final ReadOnlyObjectWrapper active = new ReadOnlyObjectWrapper<>(this, "activeDockable"); + private final ReadOnlyObjectWrapper 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 findDockable(DockableId id) { return Optional.ofNullable(dockables.get(id)); } + public Dockable activeDockable() { return active.get(); } + public ReadOnlyObjectProperty activeDockableProperty() { return active.getReadOnlyProperty(); } + public DockLayoutView layout() { return layout.get(); } + public ReadOnlyObjectProperty layoutProperty() { return layout.getReadOnlyProperty(); } + + private void addGroup(DockGroupDefinition definition, java.util.function.Consumer 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(); + 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 areas = groups.entrySet().stream().map(entry -> { + Group group = entry.getValue(); + List 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) { } +} diff --git a/fx/src/main/java/de/neitzel/fx/docking/DockingOptions.java b/fx/src/main/java/de/neitzel/fx/docking/DockingOptions.java new file mode 100644 index 0000000..c13f5a8 --- /dev/null +++ b/fx/src/main/java/de/neitzel/fx/docking/DockingOptions.java @@ -0,0 +1,5 @@ +package de.neitzel.fx.docking; + +/** Attachment options. Additional policies are introduced with their operations. */ +public final class DockingOptions { +} diff --git a/fx/src/test/java/de/neitzel/fx/view/ActivateDockingWindowTest.java b/fx/src/test/java/de/neitzel/fx/view/ActivateDockingWindowTest.java new file mode 100644 index 0000000..2831c62 --- /dev/null +++ b/fx/src/test/java/de/neitzel/fx/view/ActivateDockingWindowTest.java @@ -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 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 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) { } +} diff --git a/fx/src/test/java/de/neitzel/fx/view/OpenDockablesTest.java b/fx/src/test/java/de/neitzel/fx/view/OpenDockablesTest.java new file mode 100644 index 0000000..02d9836 --- /dev/null +++ b/fx/src/test/java/de/neitzel/fx/view/OpenDockablesTest.java @@ -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(); + 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())); + }); + } +} diff --git a/fx/src/test/resources/de/neitzel/fx/view/docking-initial.fxml b/fx/src/test/resources/de/neitzel/fx/view/docking-initial.fxml new file mode 100644 index 0000000..1a6f315 --- /dev/null +++ b/fx/src/test/resources/de/neitzel/fx/view/docking-initial.fxml @@ -0,0 +1,12 @@ + + + + + + + +
+ +
+
+