From fb53db71f5f3f5edb94f5766589993290da53e6f Mon Sep 17 00:00:00 2001 From: Danil Sokolov Date: Sat, 21 Nov 2026 17:01:19 +0000 Subject: [PATCH] Save and clone floating group without bounds A floating group's bounds stay null until its frame is shown, so FloatingGroupSerializer.serialize and FloatingGroup.clone threw NullPointerException for such a group, as did Layout.clone, which clones every group. Fixes #32 --- .../org/flexdock/docking/state/FloatingGroup.java | 2 +- .../persist/xml/FloatingGroupSerializer.java | 11 ++++++++--- .../persist/xml/FloatingGroupSerializerTest.java | 13 +++++++------ .../org/flexdock/test/docking/LayoutStateTest.java | 10 ++++++++++ release-notes.txt | 1 + 5 files changed, 27 insertions(+), 10 deletions(-) diff --git a/flexdock-core/src/main/java/org/flexdock/docking/state/FloatingGroup.java b/flexdock-core/src/main/java/org/flexdock/docking/state/FloatingGroup.java index 61982d9b..5e867dd0 100644 --- a/flexdock-core/src/main/java/org/flexdock/docking/state/FloatingGroup.java +++ b/flexdock-core/src/main/java/org/flexdock/docking/state/FloatingGroup.java @@ -189,7 +189,7 @@ public Object clone() { HashSet set = (HashSet)dockables.clone(); FloatingGroup clone = new FloatingGroup(name, set); clone.frame = frame; - clone.windowBounds = (Rectangle)windowBounds.clone(); + clone.windowBounds = getBounds(); return clone; } diff --git a/flexdock-perspective/src/main/java/org/flexdock/perspective/persist/xml/FloatingGroupSerializer.java b/flexdock-perspective/src/main/java/org/flexdock/perspective/persist/xml/FloatingGroupSerializer.java index ac149435..c527e572 100644 --- a/flexdock-perspective/src/main/java/org/flexdock/perspective/persist/xml/FloatingGroupSerializer.java +++ b/flexdock-perspective/src/main/java/org/flexdock/perspective/persist/xml/FloatingGroupSerializer.java @@ -45,9 +45,14 @@ public Element serialize(Document document, Object object) { Element floatingGroupElement = document.createElement(PersistenceConstants.FLOATING_GROUP_ELEMENT_NAME); floatingGroupElement.setAttribute(PersistenceConstants.FLOATING_GROUP_ATTRIBUTE_NAME, floatingGroup.getName()); - ISerializer rectangleSerializer = SerializerRegistry.getSerializer(Rectangle.class); - Element rectangleElement = rectangleSerializer.serialize(document, floatingGroup.getBounds()); - floatingGroupElement.appendChild(rectangleElement); + // Bounds are null until the group's frame is first shown; deserialize + // already accepts a missing Rectangle element + Rectangle bounds = floatingGroup.getBounds(); + if (bounds != null) { + ISerializer rectangleSerializer = SerializerRegistry.getSerializer(Rectangle.class); + Element rectangleElement = rectangleSerializer.serialize(document, bounds); + floatingGroupElement.appendChild(rectangleElement); + } for (Iterator it = floatingGroup.getDockableIterator(); it.hasNext();) { String dockableId = (String) it.next(); diff --git a/flexdock-perspective/src/test/java/org/flexdock/perspective/persist/xml/FloatingGroupSerializerTest.java b/flexdock-perspective/src/test/java/org/flexdock/perspective/persist/xml/FloatingGroupSerializerTest.java index ec8bb791..3e852658 100644 --- a/flexdock-perspective/src/test/java/org/flexdock/perspective/persist/xml/FloatingGroupSerializerTest.java +++ b/flexdock-perspective/src/test/java/org/flexdock/perspective/persist/xml/FloatingGroupSerializerTest.java @@ -56,15 +56,16 @@ void testGroupWithoutDockablesSurvives() throws Exception { assertEquals(0, result.getDockableCount()); } - // Known bug (https://github.com/opencollab/flexdock/issues/32): - // a group whose bounds were never set cannot be serialized (NPE in - // RectangleSerializer). Once fixed, assertThrows fails: round-trip the group instead and - // check that its bounds stay null. @Test - void testGroupWithoutBoundsKnownBug() { + void testGroupWithoutBoundsSurvives() throws Exception { FloatingGroup group = new FloatingGroup("unbounded"); + group.addDockable("first"); + + FloatingGroup result = (FloatingGroup) XmlRoundTrip.roundTrip(serializer, group); - assertThrows(NullPointerException.class, () -> XmlRoundTrip.roundTrip(serializer, group)); + assertEquals("unbounded", result.getName()); + assertNull(result.getBounds()); + assertEquals(getDockableIds(group), getDockableIds(result)); } } diff --git a/flexdock-view/src/test/java/org/flexdock/test/docking/LayoutStateTest.java b/flexdock-view/src/test/java/org/flexdock/test/docking/LayoutStateTest.java index c16891f1..3997d283 100644 --- a/flexdock-view/src/test/java/org/flexdock/test/docking/LayoutStateTest.java +++ b/flexdock-view/src/test/java/org/flexdock/test/docking/LayoutStateTest.java @@ -125,6 +125,16 @@ void testCloneCopiesFloatingGroups() { assertEquals(new Rectangle(5, 6, 7, 8), clone.getGroup("state.group").getBounds()); } + @Test + void testCloneCopiesFloatingGroupWithoutBounds() { + layout.addFloatingGroup(new FloatingGroup("state.unbounded")); + + Layout clone = (Layout) layout.clone(); + + assertNotNull(clone.getGroup("state.unbounded")); + assertNull(clone.getGroup("state.unbounded").getBounds()); + } + @Test void testDockableJoinsOneFloatingGroupAtATime() { layout.add(known); diff --git a/release-notes.txt b/release-notes.txt index fbed0b2a..a3d2bafa 100644 --- a/release-notes.txt +++ b/release-notes.txt @@ -20,6 +20,7 @@ Fixes: * DockingSplitPane: apply the percentage consistently in setDividerLocation() * DockingSplitPane: keep the percentage set before the size is known * UIFactory: create a custom theme under a look and feel without a theme mapping +* FloatingGroup: save and clone a floating group whose bounds were never set == flexdock 1.3 RELEASE NOTES ==