From d5ba93d46076d45b7fb4ae06f54705d9a79b5fc9 Mon Sep 17 00:00:00 2001 From: Ugur Saglam <106508695+ugur-vaadin@users.noreply.github.com> Date: Tue, 6 Aug 2024 11:03:42 +0300 Subject: [PATCH] feat: introduce gettabcount and deprecate getcomponentcount (#6495) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: introduce gettabcount and deprecate getcomponentcount * Update vaadin-tabs-flow-parent/vaadin-tabs-flow/src/main/java/com/vaadin/flow/component/tabs/Tabs.java Co-authored-by: Sascha Ißbrücker * Update vaadin-tabs-flow-parent/vaadin-tabs-flow/src/main/java/com/vaadin/flow/component/tabs/TabSheet.java Co-authored-by: Sascha Ißbrücker --------- Co-authored-by: Sascha Ißbrücker --- .../vaadin/flow/component/tabs/TabSheet.java | 9 +++++++++ .../com/vaadin/flow/component/tabs/Tabs.java | 19 +++++++++++++++---- .../component/tabs/tests/TabSheetTest.java | 11 +++++++++++ .../flow/component/tabs/tests/TabsTest.java | 6 ++---- 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/vaadin-tabs-flow-parent/vaadin-tabs-flow/src/main/java/com/vaadin/flow/component/tabs/TabSheet.java b/vaadin-tabs-flow-parent/vaadin-tabs-flow/src/main/java/com/vaadin/flow/component/tabs/TabSheet.java index 37e1d365d71..83a201033b9 100644 --- a/vaadin-tabs-flow-parent/vaadin-tabs-flow/src/main/java/com/vaadin/flow/component/tabs/TabSheet.java +++ b/vaadin-tabs-flow-parent/vaadin-tabs-flow/src/main/java/com/vaadin/flow/component/tabs/TabSheet.java @@ -250,6 +250,15 @@ public void setSelectedTab(Tab selectedTab) { tabs.setSelectedTab(selectedTab); } + /** + * Gets the number of tabs. + * + * @return the number of tabs + */ + public int getTabCount() { + return tabs.getTabCount(); + } + /** * Returns the tab at the given position. * diff --git a/vaadin-tabs-flow-parent/vaadin-tabs-flow/src/main/java/com/vaadin/flow/component/tabs/Tabs.java b/vaadin-tabs-flow-parent/vaadin-tabs-flow/src/main/java/com/vaadin/flow/component/tabs/Tabs.java index 798f786aea5..820a95a62ed 100644 --- a/vaadin-tabs-flow-parent/vaadin-tabs-flow/src/main/java/com/vaadin/flow/component/tabs/Tabs.java +++ b/vaadin-tabs-flow-parent/vaadin-tabs-flow/src/main/java/com/vaadin/flow/component/tabs/Tabs.java @@ -173,7 +173,7 @@ public void add(Component... components) { */ public void add(Tab... tabs) { Objects.requireNonNull(tabs, "Tabs should not be null"); - boolean wasEmpty = getComponentCount() == 0; + boolean wasEmpty = getTabCount() == 0; Arrays.stream(tabs).map( tab -> Objects.requireNonNull(tab, "Tab to add cannot be null")) .map(Tab::getElement).forEach(getElement()::appendChild); @@ -248,11 +248,11 @@ public void remove(Tab... tabs) { int newSelectedIndex = getSelectedIndex() - lowerIndices; // In case the last tab was removed - if (newSelectedIndex > 0 && newSelectedIndex >= getComponentCount()) { - newSelectedIndex = getComponentCount() - 1; + if (newSelectedIndex > 0 && newSelectedIndex >= getTabCount()) { + newSelectedIndex = getTabCount() - 1; } - if (getComponentCount() == 0 || (isSelectedTab && !isAutoselect())) { + if (getTabCount() == 0 || (isSelectedTab && !isAutoselect())) { newSelectedIndex = -1; } @@ -749,8 +749,19 @@ public int indexOf(Tab tab) { * Gets the number of children tabs. * * @return the number of tabs + * @deprecated since 24.5, use {@link #getTabCount} instead. */ + @Deprecated public int getComponentCount() { + return getTabCount(); + } + + /** + * Gets the number of tabs. + * + * @return the number of tabs + */ + public int getTabCount() { return (int) getChildren().count(); } diff --git a/vaadin-tabs-flow-parent/vaadin-tabs-flow/src/test/java/com/vaadin/flow/component/tabs/tests/TabSheetTest.java b/vaadin-tabs-flow-parent/vaadin-tabs-flow/src/test/java/com/vaadin/flow/component/tabs/tests/TabSheetTest.java index 7d8f82f9a4b..5a695bfd611 100644 --- a/vaadin-tabs-flow-parent/vaadin-tabs-flow/src/test/java/com/vaadin/flow/component/tabs/tests/TabSheetTest.java +++ b/vaadin-tabs-flow-parent/vaadin-tabs-flow/src/test/java/com/vaadin/flow/component/tabs/tests/TabSheetTest.java @@ -155,6 +155,16 @@ public void addSecondTab_contentDisabled() { Assert.assertFalse(content1.isEnabled()); } + @Test + public void addTabs_tabCountCorrect() { + Assert.assertEquals(0, tabSheet.getTabCount()); + + tabSheet.add("Tab 0", new Span("Content 0")); + tabSheet.add("Tab 1", new Span("Content 1")); + + Assert.assertEquals(2, tabSheet.getTabCount()); + } + @Test public void changeTab_contentEnabled() { tabSheet.add("Tab 0", new Span("Content 0")); @@ -261,6 +271,7 @@ public void removeTab_removesTab() { var tab = tabSheet.add("Tab 0", new Span("Content 0")); tabSheet.remove(tab); Assert.assertFalse(tab.getParent().isPresent()); + Assert.assertEquals(0, tabSheet.getTabCount()); } @Test diff --git a/vaadin-tabs-flow-parent/vaadin-tabs-flow/src/test/java/com/vaadin/flow/component/tabs/tests/TabsTest.java b/vaadin-tabs-flow-parent/vaadin-tabs-flow/src/test/java/com/vaadin/flow/component/tabs/tests/TabsTest.java index fddeb225d15..f5f6c288cb2 100644 --- a/vaadin-tabs-flow-parent/vaadin-tabs-flow/src/test/java/com/vaadin/flow/component/tabs/tests/TabsTest.java +++ b/vaadin-tabs-flow-parent/vaadin-tabs-flow/src/test/java/com/vaadin/flow/component/tabs/tests/TabsTest.java @@ -42,8 +42,7 @@ public class TabsTest { public void createTabsInDefaultState() { Tabs tabs = new Tabs(); - assertThat("Initial child count is invalid", tabs.getComponentCount(), - is(0)); + assertThat("Initial tab count is invalid", tabs.getTabCount(), is(0)); assertThat("Initial orientation is invalid", tabs.getOrientation(), is(Tabs.Orientation.HORIZONTAL)); assertThat("Initial selected index is invalid", tabs.getSelectedIndex(), @@ -59,8 +58,7 @@ public void createTabsWithChildren() { Tab tab3 = new Tab("Tab three"); Tabs tabs = new Tabs(tab1, tab2, tab3); - assertThat("Initial child count is invalid", tabs.getComponentCount(), - is(3)); + assertThat("Initial tab count is invalid", tabs.getTabCount(), is(3)); assertThat("Initial orientation is invalid", tabs.getOrientation(), is(Tabs.Orientation.HORIZONTAL)); assertThat("Initial selected tab is invalid", tabs.getSelectedTab(),