From f61a6309e763ce08d86448c45a6abc2b92151c60 Mon Sep 17 00:00:00 2001 From: ugur-vaadin Date: Tue, 27 Aug 2024 20:27:53 +0300 Subject: [PATCH 1/4] feat: implement max col count feature --- .../dashboard/tests/DashboardPage.java | 16 ++++++++++- .../dashboard/tests/DashboardIT.java | 27 +++++++++++++++++++ .../flow/component/dashboard/Dashboard.java | 12 +++++++++ .../dashboard/tests/DashboardTest.java | 24 +++++++++++++++++ 4 files changed, 78 insertions(+), 1 deletion(-) diff --git a/vaadin-dashboard-flow-parent/vaadin-dashboard-flow-integration-tests/src/main/java/com/vaadin/flow/component/dashboard/tests/DashboardPage.java b/vaadin-dashboard-flow-parent/vaadin-dashboard-flow-integration-tests/src/main/java/com/vaadin/flow/component/dashboard/tests/DashboardPage.java index 12a98e075e3..d9730dc64b7 100644 --- a/vaadin-dashboard-flow-parent/vaadin-dashboard-flow-integration-tests/src/main/java/com/vaadin/flow/component/dashboard/tests/DashboardPage.java +++ b/vaadin-dashboard-flow-parent/vaadin-dashboard-flow-integration-tests/src/main/java/com/vaadin/flow/component/dashboard/tests/DashboardPage.java @@ -38,6 +38,8 @@ public DashboardPage() { Dashboard dashboard = new Dashboard(); dashboard.add(widget1, widget2, widget3); + dashboard.setMaximumColumnCount(3); + NativeButton addWidgetAtIndex1 = new NativeButton( "Add widget at index 1"); addWidgetAtIndex1.addClickListener(click -> { @@ -69,7 +71,19 @@ public DashboardPage() { removeAllWidgets.addClickListener(click -> dashboard.removeAll()); removeAllWidgets.setId("remove-all-widgets"); + NativeButton setMaximumColumnCount1 = new NativeButton( + "Set maximum column count 1"); + setMaximumColumnCount1 + .addClickListener(click -> dashboard.setMaximumColumnCount(1)); + setMaximumColumnCount1.setId("set-maximum-column-count-1"); + + NativeButton setMaximumColumnCountNull = new NativeButton( + "Set maximum column count null"); + setMaximumColumnCountNull.addClickListener( + click -> dashboard.setMaximumColumnCount(null)); + setMaximumColumnCountNull.setId("set-maximum-column-count-null"); + add(addWidgetAtIndex1, removeFirstAndLastWidgets, removeAllWidgets, - dashboard); + setMaximumColumnCount1, setMaximumColumnCountNull, dashboard); } } diff --git a/vaadin-dashboard-flow-parent/vaadin-dashboard-flow-integration-tests/src/test/java/com/vaadin/flow/component/dashboard/tests/DashboardIT.java b/vaadin-dashboard-flow-parent/vaadin-dashboard-flow-integration-tests/src/test/java/com/vaadin/flow/component/dashboard/tests/DashboardIT.java index 68c20102e7b..b6b5fc863c1 100644 --- a/vaadin-dashboard-flow-parent/vaadin-dashboard-flow-integration-tests/src/test/java/com/vaadin/flow/component/dashboard/tests/DashboardIT.java +++ b/vaadin-dashboard-flow-parent/vaadin-dashboard-flow-integration-tests/src/test/java/com/vaadin/flow/component/dashboard/tests/DashboardIT.java @@ -58,6 +58,33 @@ public void removeAllWidgets_widgetsAreCorrectlyRemoved() { assertWidgetsByTitle(); } + @Test + public void changeMaximumColumnCountTo1_widgetsShouldBeOnTheSameColumn() { + List widgets = dashboardElement.getWidgets(); + // The first two widgets should initially be on the same horizontal line + int yOfWidget1 = widgets.get(0).getLocation().getY(); + Assert.assertEquals(yOfWidget1, widgets.get(1).getLocation().getY()); + + clickElementWithJs("set-maximum-column-count-1"); + // The first two widgets should be on the same vertical line + int xOfWidget1 = widgets.get(0).getLocation().getX(); + Assert.assertEquals(xOfWidget1, widgets.get(1).getLocation().getX()); + } + + @Test + public void changeMaximumColumnCountToNull_widgetsShouldBeOnTheSameRow() { + clickElementWithJs("set-maximum-column-count-1"); + List widgets = dashboardElement.getWidgets(); + // The first two widgets should be on the same vertical line + int xOfWidget1 = widgets.get(0).getLocation().getX(); + Assert.assertEquals(xOfWidget1, widgets.get(1).getLocation().getX()); + + clickElementWithJs("set-maximum-column-count-null"); + // The widgets should be on the same horizontal line + int yOfWidget1 = widgets.get(0).getLocation().getY(); + Assert.assertEquals(yOfWidget1, widgets.get(1).getLocation().getY()); + } + private void assertWidgetsByTitle(String... expectedWidgetTitles) { List widgets = dashboardElement.getWidgets(); List widgetTitles = widgets.stream() diff --git a/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/main/java/com/vaadin/flow/component/dashboard/Dashboard.java b/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/main/java/com/vaadin/flow/component/dashboard/Dashboard.java index b7526b27ba3..25dbd5b30fd 100644 --- a/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/main/java/com/vaadin/flow/component/dashboard/Dashboard.java +++ b/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/main/java/com/vaadin/flow/component/dashboard/Dashboard.java @@ -143,6 +143,18 @@ public void removeAll() { updateClient(); } + /** + * Sets the maximum column count of the dashboard. + * + * @param maxCount + * the new maximum column count. Pass in {@code null} to set the + * maximum column count back to the default value. + */ + public void setMaximumColumnCount(Integer maxCount) { + getStyle().set("--vaadin-dashboard-col-max-count", + maxCount == null ? null : String.valueOf(maxCount)); + } + @Override public Stream getChildren() { return getWidgets().stream().map(Component.class::cast); diff --git a/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/test/java/com/vaadin/flow/component/dashboard/tests/DashboardTest.java b/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/test/java/com/vaadin/flow/component/dashboard/tests/DashboardTest.java index 24c2af1d691..2260799586d 100644 --- a/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/test/java/com/vaadin/flow/component/dashboard/tests/DashboardTest.java +++ b/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/test/java/com/vaadin/flow/component/dashboard/tests/DashboardTest.java @@ -211,6 +211,30 @@ public void addWidgetToAnotherDashboard_widgetIsMoved() { assertWidgets(newDashboard, widget); } + @Test + public void setMaximumColumnCount_valueIsCorrectlySet() { + String propertyName = "--vaadin-dashboard-col-max-count"; + int valueToSet = 5; + + Assert.assertNull(dashboard.getStyle().get(propertyName)); + + dashboard.setMaximumColumnCount(valueToSet); + Assert.assertEquals(String.valueOf(valueToSet), + dashboard.getStyle().get(propertyName)); + + dashboard.setMaximumColumnCount(null); + Assert.assertNull(dashboard.getStyle().get(propertyName)); + } + + @Test + public void setMaximumColumnCountNull_propertyIsRemoved() { + dashboard.setMaximumColumnCount(5); + + dashboard.setMaximumColumnCount(null); + Assert.assertNull( + dashboard.getStyle().get("--vaadin-dashboard-col-max-count")); + } + private void fakeClientCommunication() { ui.getInternals().getStateTree().runExecutionsBeforeClientResponse(); ui.getInternals().getStateTree().collectChanges(ignore -> { From 1f1c35676a4be66f19deab7518353c1d6ff3ba83 Mon Sep 17 00:00:00 2001 From: ugur-vaadin Date: Wed, 28 Aug 2024 12:26:09 +0300 Subject: [PATCH 2/4] feat: add getter for maximum column count --- .../flow/component/dashboard/Dashboard.java | 10 ++++++++ .../dashboard/tests/DashboardTest.java | 23 +++++++++++++++---- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/main/java/com/vaadin/flow/component/dashboard/Dashboard.java b/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/main/java/com/vaadin/flow/component/dashboard/Dashboard.java index 25dbd5b30fd..f4b4d0166ee 100644 --- a/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/main/java/com/vaadin/flow/component/dashboard/Dashboard.java +++ b/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/main/java/com/vaadin/flow/component/dashboard/Dashboard.java @@ -143,6 +143,16 @@ public void removeAll() { updateClient(); } + /** + * Returns maximum column count of the dashboard. + * + * @return the maximum column count of the dashboard + */ + public Integer getMaximumColumnCount() { + String maxColCount = getStyle().get("--vaadin-dashboard-col-max-count"); + return maxColCount == null ? null : Integer.valueOf(maxColCount); + } + /** * Sets the maximum column count of the dashboard. * diff --git a/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/test/java/com/vaadin/flow/component/dashboard/tests/DashboardTest.java b/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/test/java/com/vaadin/flow/component/dashboard/tests/DashboardTest.java index 2260799586d..4851409abf5 100644 --- a/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/test/java/com/vaadin/flow/component/dashboard/tests/DashboardTest.java +++ b/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/test/java/com/vaadin/flow/component/dashboard/tests/DashboardTest.java @@ -215,13 +215,10 @@ public void addWidgetToAnotherDashboard_widgetIsMoved() { public void setMaximumColumnCount_valueIsCorrectlySet() { String propertyName = "--vaadin-dashboard-col-max-count"; int valueToSet = 5; - Assert.assertNull(dashboard.getStyle().get(propertyName)); - dashboard.setMaximumColumnCount(valueToSet); Assert.assertEquals(String.valueOf(valueToSet), dashboard.getStyle().get(propertyName)); - dashboard.setMaximumColumnCount(null); Assert.assertNull(dashboard.getStyle().get(propertyName)); } @@ -229,12 +226,30 @@ public void setMaximumColumnCount_valueIsCorrectlySet() { @Test public void setMaximumColumnCountNull_propertyIsRemoved() { dashboard.setMaximumColumnCount(5); - dashboard.setMaximumColumnCount(null); Assert.assertNull( dashboard.getStyle().get("--vaadin-dashboard-col-max-count")); } + @Test + public void defaultMaximumColumnCountValueIsCorrectlyRetrieved() { + Assert.assertNull(dashboard.getMaximumColumnCount()); + } + + @Test + public void setMaximumColumnCount_valueIsCorrectlyRetrieved() { + Integer valueToSet = 5; + dashboard.setMaximumColumnCount(valueToSet); + Assert.assertEquals(valueToSet, dashboard.getMaximumColumnCount()); + } + + @Test + public void setMaximumColumnCountNull_valueIsCorrectlyRetrieved() { + dashboard.setMaximumColumnCount(5); + dashboard.setMaximumColumnCount(null); + Assert.assertNull(dashboard.getMaximumColumnCount()); + } + private void fakeClientCommunication() { ui.getInternals().getStateTree().runExecutionsBeforeClientResponse(); ui.getInternals().getStateTree().collectChanges(ignore -> { From 5b62b61776edec852392b80258478ce2ba52b8fe Mon Sep 17 00:00:00 2001 From: Ugur Saglam <106508695+ugur-vaadin@users.noreply.github.com> Date: Wed, 28 Aug 2024 12:39:21 +0300 Subject: [PATCH 3/4] Update vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/main/java/com/vaadin/flow/component/dashboard/Dashboard.java Co-authored-by: Tomi Virkki --- .../java/com/vaadin/flow/component/dashboard/Dashboard.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/main/java/com/vaadin/flow/component/dashboard/Dashboard.java b/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/main/java/com/vaadin/flow/component/dashboard/Dashboard.java index f4b4d0166ee..42f3dddcb98 100644 --- a/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/main/java/com/vaadin/flow/component/dashboard/Dashboard.java +++ b/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/main/java/com/vaadin/flow/component/dashboard/Dashboard.java @@ -144,7 +144,7 @@ public void removeAll() { } /** - * Returns maximum column count of the dashboard. + * Returns the maximum column count of the dashboard. * * @return the maximum column count of the dashboard */ From 90e6d9d0e7b5195d34fc8def4727313161a3819a Mon Sep 17 00:00:00 2001 From: Ugur Saglam <106508695+ugur-vaadin@users.noreply.github.com> Date: Wed, 28 Aug 2024 12:39:34 +0300 Subject: [PATCH 4/4] Update vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/main/java/com/vaadin/flow/component/dashboard/Dashboard.java Co-authored-by: Tomi Virkki --- .../java/com/vaadin/flow/component/dashboard/Dashboard.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/main/java/com/vaadin/flow/component/dashboard/Dashboard.java b/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/main/java/com/vaadin/flow/component/dashboard/Dashboard.java index 42f3dddcb98..b6e4aed2736 100644 --- a/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/main/java/com/vaadin/flow/component/dashboard/Dashboard.java +++ b/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/main/java/com/vaadin/flow/component/dashboard/Dashboard.java @@ -160,9 +160,9 @@ public Integer getMaximumColumnCount() { * the new maximum column count. Pass in {@code null} to set the * maximum column count back to the default value. */ - public void setMaximumColumnCount(Integer maxCount) { + public void setMaximumColumnCount(Integer maxColCount) { getStyle().set("--vaadin-dashboard-col-max-count", - maxCount == null ? null : String.valueOf(maxCount)); + maxColCount == null ? null : String.valueOf(maxColCount)); } @Override