From 2b58b2acf98466c79326ff900b1193e0857bd4a4 Mon Sep 17 00:00:00 2001 From: Danil Sokolov Date: Sat, 21 Nov 2026 15:38:52 +0000 Subject: [PATCH 1/3] Return null region for undocked dockable DefaultDockingStrategy.findRegion dereferenced the docking port, which is null for a dockable that is not docked. As a result both getSibling overloads threw NullPointerException instead of returning null, as their javadoc promises. --- .../docking/defaults/DefaultDockingStrategy.java | 4 ++++ .../org/flexdock/test/docking/SiblingDockingTest.java | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/flexdock-core/src/main/java/org/flexdock/docking/defaults/DefaultDockingStrategy.java b/flexdock-core/src/main/java/org/flexdock/docking/defaults/DefaultDockingStrategy.java index d2c0870f..04bb2d32 100644 --- a/flexdock-core/src/main/java/org/flexdock/docking/defaults/DefaultDockingStrategy.java +++ b/flexdock-core/src/main/java/org/flexdock/docking/defaults/DefaultDockingStrategy.java @@ -239,6 +239,10 @@ public static String findRegion(Component comp) { } DockingPort port = DockingManager.getDockingPort(comp); + if (port == null) { + return null; + } + Component docked = port.getDockedComponent(); if (!(docked instanceof JSplitPane)) { diff --git a/flexdock-view/src/test/java/org/flexdock/test/docking/SiblingDockingTest.java b/flexdock-view/src/test/java/org/flexdock/test/docking/SiblingDockingTest.java index 370ae922..dd262af0 100644 --- a/flexdock-view/src/test/java/org/flexdock/test/docking/SiblingDockingTest.java +++ b/flexdock-view/src/test/java/org/flexdock/test/docking/SiblingDockingTest.java @@ -3,6 +3,7 @@ import org.flexdock.docking.Dockable; import org.flexdock.docking.DockingConstants; import org.flexdock.docking.defaults.DefaultDockingStrategy; +import org.flexdock.view.View; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; @@ -41,6 +42,15 @@ void testNoSiblingOutsideLayout() { assertSibling(null, bottom, DockingConstants.SOUTH_REGION); } + @Test + void testNoSiblingForUndockedDockable() { + View undocked = new View(getClass().getSimpleName() + ".undocked", "Undocked"); + + assertNull(DefaultDockingStrategy.findRegion(undocked)); + assertNull(DefaultDockingStrategy.getSibling(undocked)); + assertNull(DefaultDockingStrategy.getSibling(undocked, DockingConstants.WEST_REGION)); + } + @Test void testNoSiblingForCenterInvalidRegionOrNullDockable() { assertSibling(null, centre, DockingConstants.CENTER_REGION); From 0564b9069ab13bfb4cf17257de74064f83f85816 Mon Sep 17 00:00:00 2001 From: Danil Sokolov Date: Sat, 21 Nov 2026 15:49:13 +0000 Subject: [PATCH 2/3] Skip restore handlers without docking state DockPathHandler and PointHandler dereferenced the docking state, which is null for a dockable that is not registered under its persistent id. They threw NullPointerException instead of declining, so UnknownStateHandler was never tried, unlike after the other handlers. PointHandler also threw when there was no window to restore into, as before any docking window exists. --- .../restore/handlers/DockPathHandler.java | 4 ++++ .../restore/handlers/PointHandler.java | 6 +++++- .../test/docking/RestorationHandlerTest.java | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/flexdock-perspective/src/main/java/org/flexdock/perspective/restore/handlers/DockPathHandler.java b/flexdock-perspective/src/main/java/org/flexdock/perspective/restore/handlers/DockPathHandler.java index 86ccdf93..2fafb080 100644 --- a/flexdock-perspective/src/main/java/org/flexdock/perspective/restore/handlers/DockPathHandler.java +++ b/flexdock-perspective/src/main/java/org/flexdock/perspective/restore/handlers/DockPathHandler.java @@ -35,6 +35,10 @@ public class DockPathHandler implements RestorationHandler { @Override public boolean restore(Dockable dockable, DockingState dockingState, Map context) { + if (dockingState == null) { + return false; + } + DockingPath dockingPath = dockingState.getPath(); if (dockingPath == null) { return false; diff --git a/flexdock-perspective/src/main/java/org/flexdock/perspective/restore/handlers/PointHandler.java b/flexdock-perspective/src/main/java/org/flexdock/perspective/restore/handlers/PointHandler.java index 8e666e59..dcea2d0c 100644 --- a/flexdock-perspective/src/main/java/org/flexdock/perspective/restore/handlers/PointHandler.java +++ b/flexdock-perspective/src/main/java/org/flexdock/perspective/restore/handlers/PointHandler.java @@ -44,7 +44,7 @@ public class PointHandler implements RestorationHandler, DockingConstants { @Override public boolean restore(Dockable dockable, DockingState dockingState, Map context) { - if(DockingManager.isDocked(dockable)) { + if(dockingState == null || DockingManager.isDocked(dockable)) { return false; } @@ -54,6 +54,10 @@ public boolean restore(Dockable dockable, DockingState dockingState, Map context private boolean restoreDockable(Dockable dockable, Component win, DockingState dockingState) { RootWindow window = RootWindow.getRootContainer(win); + if(window == null) { + return false; + } + Container contentPane = window.getContentPane(); Point dropPoint = getDropPoint(dockable, contentPane, dockingState); diff --git a/flexdock-view/src/test/java/org/flexdock/test/docking/RestorationHandlerTest.java b/flexdock-view/src/test/java/org/flexdock/test/docking/RestorationHandlerTest.java index 26efcab9..50ce8839 100644 --- a/flexdock-view/src/test/java/org/flexdock/test/docking/RestorationHandlerTest.java +++ b/flexdock-view/src/test/java/org/flexdock/test/docking/RestorationHandlerTest.java @@ -6,6 +6,8 @@ import org.flexdock.docking.state.DockingState; import org.flexdock.perspective.PerspectiveManager; import org.flexdock.perspective.restore.handlers.AlreadyRestoredHandler; +import org.flexdock.perspective.restore.handlers.DockPathHandler; +import org.flexdock.perspective.restore.handlers.PointHandler; import org.flexdock.perspective.restore.handlers.RelativeHandler; import org.flexdock.view.View; import org.flexdock.view.Viewport; @@ -103,4 +105,21 @@ void testAlreadyRestoredHandlerOnlyAcceptsDockedDockables() { new HashMap())); } + @Test + void testDockPathHandlerDeclinesWithoutStateOrPath() { + View view = new View("restore.nopath", "No path"); + + assertFalse(new DockPathHandler().restore(view, null, new HashMap())); + assertFalse(new DockPathHandler().restore(view, new DockingState("restore.nopath"), new HashMap())); + } + + @Test + void testPointHandlerDeclinesWithoutStateOrWindow() { + View view = new View("restore.nopoint", "No point"); + + assertFalse(new PointHandler().restore(view, null, new HashMap())); + // Headless, so there is no docking window to restore into + assertFalse(new PointHandler().restore(view, new DockingState("restore.nopoint"), new HashMap())); + } + } From cb3141dd121eaa193118b333f56e893c097c87ad Mon Sep 17 00:00:00 2001 From: Danil Sokolov Date: Sat, 21 Nov 2026 15:59:05 +0000 Subject: [PATCH 3/3] Create titlebar action collections for any actions Titlebar.setActions created actionList and actionButtons only for a null argument. With one or more actions addAction threw NullPointerException, and after an empty array any later addAction did. --- .../main/java/org/flexdock/view/Titlebar.java | 3 ++ .../org/flexdock/test/view/TitlebarTest.java | 41 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 flexdock-view/src/test/java/org/flexdock/test/view/TitlebarTest.java diff --git a/flexdock-view/src/main/java/org/flexdock/view/Titlebar.java b/flexdock-view/src/main/java/org/flexdock/view/Titlebar.java index 2a258b9f..176e962a 100644 --- a/flexdock-view/src/main/java/org/flexdock/view/Titlebar.java +++ b/flexdock-view/src/main/java/org/flexdock/view/Titlebar.java @@ -88,6 +88,9 @@ public void setText(String text) { protected void setActions(Action[] actions) { if (actions == null) { actions = new Action[0]; + } + + if (actionList == null) { actionList = new ArrayList(3); actionButtons = new HashMap(3); } diff --git a/flexdock-view/src/test/java/org/flexdock/test/view/TitlebarTest.java b/flexdock-view/src/test/java/org/flexdock/test/view/TitlebarTest.java new file mode 100644 index 00000000..387a9953 --- /dev/null +++ b/flexdock-view/src/test/java/org/flexdock/test/view/TitlebarTest.java @@ -0,0 +1,41 @@ +package org.flexdock.test.view; + +import org.flexdock.view.Titlebar; +import org.junit.jupiter.api.Test; + +import javax.swing.AbstractAction; +import javax.swing.Action; +import java.awt.event.ActionEvent; + +import static org.junit.jupiter.api.Assertions.*; + +class TitlebarTest { + + private static Action createAction(String name) { + return new AbstractAction(name) { + @Override + public void actionPerformed(ActionEvent e) { + } + }; + } + + @Test + void testTitlebarWithActions() { + Titlebar titlebar = new Titlebar(new Action[] {createAction("first"), createAction("second")}); + + assertEquals(2, titlebar.getActions().length); + assertNotNull(titlebar.getActionButton("first")); + assertNotNull(titlebar.getActionButton("second")); + } + + @Test + void testActionAddedAfterEmptyActions() { + Titlebar titlebar = new Titlebar(new Action[0]); + + titlebar.addAction(createAction("added")); + + assertEquals(1, titlebar.getActions().length); + assertNotNull(titlebar.getActionButton("added")); + } + +}