From 0aa9f7fae3982c99363e34661949c6286ea414c3 Mon Sep 17 00:00:00 2001 From: Konrad Neitzel Date: Sun, 15 Nov 2026 22:42:56 +0100 Subject: [PATCH] feat(fx-example): demonstrate MVVM docking with local and global bars --- fx-example/README.md | 4 +- fx-example/docs/docking-mvvm.md | 55 ++++++ .../example/docking/mvvm/MvvmDockingApp.java | 19 ++ .../example/docking/mvvm/MvvmDockingMain.java | 10 + .../docking/mvvm/MvvmDockingWorkspace.java | 91 +++++++++ .../fx/example/docking/mvvm/MvvmEditor.java | 80 ++++++++ .../fx/example/docking/mvvm/MvvmEditor.fxml | 21 ++ .../example/docking/mvvm/MvvmDockingTest.java | 181 ++++++++++++++++++ fx/README.md | 1 + 9 files changed, 461 insertions(+), 1 deletion(-) create mode 100644 fx-example/docs/docking-mvvm.md create mode 100644 fx-example/src/main/java/de/neitzel/fx/example/docking/mvvm/MvvmDockingApp.java create mode 100644 fx-example/src/main/java/de/neitzel/fx/example/docking/mvvm/MvvmDockingMain.java create mode 100644 fx-example/src/main/java/de/neitzel/fx/example/docking/mvvm/MvvmDockingWorkspace.java create mode 100644 fx-example/src/main/java/de/neitzel/fx/example/docking/mvvm/MvvmEditor.java create mode 100644 fx-example/src/main/resources/de/neitzel/fx/example/docking/mvvm/MvvmEditor.fxml create mode 100644 fx-example/src/test/java/de/neitzel/fx/example/docking/mvvm/MvvmDockingTest.java diff --git a/fx-example/README.md b/fx-example/README.md index 3df43e5..6fa6cb1 100644 --- a/fx-example/README.md +++ b/fx-example/README.md @@ -4,7 +4,9 @@ This runnable application demonstrates [NeitzelLib FX](../fx/README.md) and [inj 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. +Their guides describe the implemented behavior and launch options. The +[MVVM docking editor](docs/docking-mvvm.md) shows how an already loaded View and +its local bars move between windows while global commands follow the active editor. ## Build and run diff --git a/fx-example/docs/docking-mvvm.md b/fx-example/docs/docking-mvvm.md new file mode 100644 index 0000000..dcf1c4c --- /dev/null +++ b/fx-example/docs/docking-mvvm.md @@ -0,0 +1,55 @@ +# MVVM content and local/global bars + +The docking API accepts an ordinary Node. This example passes the root of one +`LoadedView` to a `Dockable`; the manager never loads that View again. Its Note +JavaBean, ViewModel, TextArea, local toolbar and local status bar remain the same +objects when the tab moves, floats or returns. A second editor uses a plain +TextArea without a ViewModel. + +Build from the repository root with Java 25, without replacing installed runtime +artifacts used by the Designer: + +```sh +./mvnw -pl fx-example -am package +java --enable-native-access=ALL-UNNAMED \ + -cp 'fx-example/target/classes:fx/target/classes:fx-example/target/libs/*' \ + de.neitzel.fx.example.docking.mvvm.MvvmDockingMain +``` + +In the example: + +1. Edit Notes. Its tab title gains an asterisk and the local character count changes. +2. Choose **Move Notes to tools**, **Float Notes**, then **Return Notes**. The + local Clear note button and status bar travel with the editor; the global + command and status bars stay in the main window. +3. While Notes is floating, click **Checkpoint active** in the main window. + Notes keeps its command target when focus enters that toolbar. The command + records a checkpoint in memory and clears the asterisk; it does not save a file. +4. Select Plain editor. The checkpoint command is disabled for this content, and + the global status identifies the newly active Dockable. +5. Close Notes with its tab close button to run the application's close hook. + +`MvvmEditor.fxml` declares a bidirectional DirectBinding between the TextArea and +the Note's `text` attribute. Application updates use `LoadedView.property("text")`; +calling a JavaBean setter directly does not notify an already loaded ViewModel. +`MvvmEditor` binds the Dockable title and local status to presentation properties. +`MvvmDockingWorkspace` binds the global command availability and status to +`activeDockableProperty()`. These are application bindings, not a docking-specific +MVVM service. + +The application adapter owns its change listener and derived bindings. Its +`onClosed` and `onDiscarded` hooks release those resources explicitly. Moves, +collapse and veto do not run those hooks. A rejected factory-created editor is +discarded; rejection of a directly supplied Dockable leaves cleanup to its caller. +This does not dispose the LoadedView or evict the runtime's strong Model-to-ViewModel +identity cache. See the [MVVM lifetime contract](../../fx/docs/architecture.md). + +`MvvmDockingTest` loads the actual example FXML and checks model/property/control +edits before and after moves, object identity, bar ownership, global toolbar +focus and resource hooks. Its Scene-focus assertions complement the separately +qualified [native keyboard transfer tests](../../fx/docs/docking-floating.md); +this integration test does not claim to inject native keystrokes. + +For entirely independent docking and bars without initializing MVVM or injection, +use the [plain docking example](../../fx/docs/docking.md) and +[standalone bars example](../../fx/docs/bars.md). diff --git a/fx-example/src/main/java/de/neitzel/fx/example/docking/mvvm/MvvmDockingApp.java b/fx-example/src/main/java/de/neitzel/fx/example/docking/mvvm/MvvmDockingApp.java new file mode 100644 index 0000000..4668abb --- /dev/null +++ b/fx-example/src/main/java/de/neitzel/fx/example/docking/mvvm/MvvmDockingApp.java @@ -0,0 +1,19 @@ +package de.neitzel.fx.example.docking.mvvm; + +import javafx.application.Application; +import javafx.stage.Stage; + +/** Visible example of optional MVVM content hosted by the plain docking API. */ +public final class MvvmDockingApp extends Application { + private MvvmDockingWorkspace workspace; + + @Override public void start(Stage stage) { + workspace = new MvvmDockingWorkspace(stage); + stage.setTitle("NeitzelLib — MVVM editor and local/global bars"); + stage.show(); + } + + @Override public void stop() { + if (workspace != null) workspace.close(); + } +} diff --git a/fx-example/src/main/java/de/neitzel/fx/example/docking/mvvm/MvvmDockingMain.java b/fx-example/src/main/java/de/neitzel/fx/example/docking/mvvm/MvvmDockingMain.java new file mode 100644 index 0000000..20c70d3 --- /dev/null +++ b/fx-example/src/main/java/de/neitzel/fx/example/docking/mvvm/MvvmDockingMain.java @@ -0,0 +1,10 @@ +package de.neitzel.fx.example.docking.mvvm; + +import javafx.application.Application; + +/** Classpath launcher that does not extend JavaFX Application. */ +public final class MvvmDockingMain { + private MvvmDockingMain() { } + + public static void main(String[] args) { Application.launch(MvvmDockingApp.class, args); } +} diff --git a/fx-example/src/main/java/de/neitzel/fx/example/docking/mvvm/MvvmDockingWorkspace.java b/fx-example/src/main/java/de/neitzel/fx/example/docking/mvvm/MvvmDockingWorkspace.java new file mode 100644 index 0000000..10b2192 --- /dev/null +++ b/fx-example/src/main/java/de/neitzel/fx/example/docking/mvvm/MvvmDockingWorkspace.java @@ -0,0 +1,91 @@ +package de.neitzel.fx.example.docking.mvvm; + +import de.neitzel.fx.control.StatusBar; +import de.neitzel.fx.docking.DockGroupDefinition; +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.scene.Scene; +import javafx.beans.binding.Bindings; +import javafx.beans.binding.BooleanBinding; +import javafx.beans.binding.StringBinding; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.control.TextArea; +import javafx.scene.control.ToolBar; +import javafx.scene.layout.BorderPane; +import javafx.stage.Stage; +import lombok.Getter; + +/** Composes global bars with an optional MVVM editor and ordinary JavaFX content. */ +public final class MvvmDockingWorkspace implements AutoCloseable { + @Getter private final DockingManager manager; + @Getter private final MvvmEditor editor; + @Getter private final Dockable plain; + private final ToolBar globalTools; + private final Button save; + private final Label active; + private final BooleanBinding saveDisabled; + private final StringBinding activeText; + + public MvvmDockingWorkspace(Stage stage) { + DockPane pane = new DockPane(); + pane.getInitialLayout().setCenter(new DockGroupDefinition("editors")); + DockGroupDefinition tools = new DockGroupDefinition("tools"); + tools.setPreferredWidth(360); + pane.getInitialLayout().setRight(tools); + Button move = new Button("Move Notes to tools"); + Button floating = new Button("Float Notes"); + Button returning = new Button("Return Notes"); + save = new Button("Checkpoint active"); + save.setId("saveActive"); + globalTools = new ToolBar(save, move, floating, returning); + globalTools.setId("globalTools"); + StatusBar globalStatus = new StatusBar(); + globalStatus.setId("globalStatus"); + active = new Label(); + active.setId("activeStatus"); + globalStatus.getLeftItems().add(active); + globalStatus.getRightItems().add(new Label("Checkpoints stay in memory")); + BorderPane shell = new BorderPane(pane, globalTools, null, globalStatus, null); + stage.setScene(new Scene(shell, 1040, 620)); + manager = DockingManager.attach(stage, pane, new DockingOptions()); + editor = new MvvmEditor("notes"); + plain = new Dockable(new DockableId("plain"), new TextArea("A plain JavaFX editor needs no ViewLoader.")); + plain.setTitle("Plain editor"); + saveDisabled = Bindings.createBooleanBinding( + () -> manager.activeDockable() != editor.getDockable() || !editor.dirtyProperty().get(), + manager.activeDockableProperty(), editor.dirtyProperty()); + save.disableProperty().bind(saveDisabled); + activeText = Bindings.createStringBinding( + () -> manager.activeDockable() == null ? "No active content" : "Active: " + manager.activeDockable().getTitle(), + manager.activeDockableProperty(), editor.getDockable().titleProperty(), plain.titleProperty()); + active.textProperty().bind(activeText); + save.setOnAction(event -> { + if (manager.activeDockable() == editor.getDockable()) editor.checkpoint(); + }); + manager.open(plain, DockTarget.area("editors")); + manager.open(editor.getDockable(), DockTarget.area("editors")); + move.setOnAction(event -> manager.move(editor.getDockable().getId(), DockTarget.area("tools"))); + floating.setOnAction(event -> manager.move(editor.getDockable().getId(), DockTarget.floating())); + returning.setOnAction(event -> manager.move(editor.getDockable().getId(), DockTarget.returnToMain())); + } + + /** Releases application-owned global bindings when this demonstration ends. */ + @Override public void close() { + save.disableProperty().unbind(); + saveDisabled.dispose(); + active.textProperty().unbind(); + activeText.dispose(); + for (var item : globalTools.getItems()) { + if (item instanceof Button button) { + button.setOnAction(null); + button.setDisable(true); + } + } + editor.close(); + } +} diff --git a/fx-example/src/main/java/de/neitzel/fx/example/docking/mvvm/MvvmEditor.java b/fx-example/src/main/java/de/neitzel/fx/example/docking/mvvm/MvvmEditor.java new file mode 100644 index 0000000..18211b2 --- /dev/null +++ b/fx-example/src/main/java/de/neitzel/fx/example/docking/mvvm/MvvmEditor.java @@ -0,0 +1,80 @@ +package de.neitzel.fx.example.docking.mvvm; + +import de.neitzel.fx.docking.Dockable; +import de.neitzel.fx.docking.DockableId; +import de.neitzel.fx.example.model.Note; +import de.neitzel.fx.view.LoadedView; +import de.neitzel.fx.view.ViewLoader; +import de.neitzel.injection.ApplicationContext; +import javafx.beans.binding.Bindings; +import javafx.beans.binding.StringBinding; +import javafx.beans.property.Property; +import javafx.beans.property.ReadOnlyBooleanProperty; +import javafx.beans.property.ReadOnlyBooleanWrapper; +import javafx.beans.value.ChangeListener; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.control.TextArea; +import javafx.scene.control.ToolBar; +import lombok.Getter; + +/** Application adapter: one loaded View becomes ordinary Dockable content. */ +public final class MvvmEditor implements AutoCloseable { + @Getter private final Note model = new Note("Edit this note, then move or float its tab."); + @Getter private final LoadedView view; + @Getter private final Dockable dockable; + private final Property text; + private final Label status; + private final Button clear; + private final StringBinding titleBinding; + private final StringBinding statusBinding; + private String checkpoint; + private boolean released; + private final ReadOnlyBooleanWrapper dirty = new ReadOnlyBooleanWrapper(this, "dirty"); + private final ChangeListener changed = (observable, old, current) -> dirty.set(!java.util.Objects.equals(checkpoint, current)); + + @SuppressWarnings("unchecked") + public MvvmEditor(String id) { + view = ViewLoader.load(MvvmEditor.class.getResource("MvvmEditor.fxml"), model, + new ApplicationContext(MvvmEditor.class)); + text = (Property) view.property("text"); + TextArea input = (TextArea) view.getRoot().lookup("#text"); + dockable = new Dockable(new DockableId(id), view.getRoot()); + dockable.setFocusTarget(() -> input); + checkpoint = model.getText(); + text.addListener(changed); + titleBinding = Bindings.createStringBinding(() -> "Notes" + (dirty.get() ? " *" : ""), dirty); + dockable.titleProperty().bind(titleBinding); + status = (Label) view.getRoot().lookup("#localMessage"); + statusBinding = Bindings.createStringBinding(() -> java.util.Objects.toString(text.getValue(), "").length() + " characters", text); + status.textProperty().bind(statusBinding); + ToolBar tools = (ToolBar) view.getRoot().lookup("#localTools"); + clear = (Button) tools.getItems().getFirst(); + clear.setOnAction(event -> input.clear()); + dockable.setOnClosed(this::close); + dockable.setOnDiscarded(this::close); + } + + public ReadOnlyBooleanProperty dirtyProperty() { return dirty.getReadOnlyProperty(); } + + /** Records an in-memory checkpoint; this example does not write a file. */ + public void checkpoint() { + if (released) return; + checkpoint = text.getValue(); + dirty.set(false); + } + + /** Releases this adapter's resources, not the LoadedView or global ViewModel identity cache. */ + @Override public void close() { + if (released) return; + released = true; + text.removeListener(changed); + dockable.titleProperty().unbind(); + titleBinding.dispose(); + status.textProperty().unbind(); + statusBinding.dispose(); + status.setText("Released application listeners"); + clear.setOnAction(null); + clear.setDisable(true); + } +} diff --git a/fx-example/src/main/resources/de/neitzel/fx/example/docking/mvvm/MvvmEditor.fxml b/fx-example/src/main/resources/de/neitzel/fx/example/docking/mvvm/MvvmEditor.fxml new file mode 100644 index 0000000..ed6aa6b --- /dev/null +++ b/fx-example/src/main/resources/de/neitzel/fx/example/docking/mvvm/MvvmEditor.fxml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + +