From f32b61addf3a9c15a830aadc9774b90831699ae6 Mon Sep 17 00:00:00 2001 From: Miki Date: Thu, 29 Aug 2024 18:01:43 +0300 Subject: [PATCH] #518 done (#520) --- .../miki/demo/builders/SuperTabsBuilder.java | 5 ++++- .../vaadin/miki/superfields/tabs/SuperTabs.java | 15 +++++++++++++++ .../miki/superfields/tabs/SuperTabsTest.java | 17 +++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/demo-v24/src/main/java/org/vaadin/miki/demo/builders/SuperTabsBuilder.java b/demo-v24/src/main/java/org/vaadin/miki/demo/builders/SuperTabsBuilder.java index 9658f942..7a21b64b 100644 --- a/demo-v24/src/main/java/org/vaadin/miki/demo/builders/SuperTabsBuilder.java +++ b/demo-v24/src/main/java/org/vaadin/miki/demo/builders/SuperTabsBuilder.java @@ -1,6 +1,7 @@ package org.vaadin.miki.demo.builders; import com.vaadin.flow.component.Component; +import com.vaadin.flow.component.button.Button; import com.vaadin.flow.component.checkbox.Checkbox; import com.vaadin.flow.component.combobox.ComboBox; import org.vaadin.miki.demo.ContentBuilder; @@ -30,6 +31,8 @@ public void buildContent(SuperTabs component, Consumer callback) component.setTabHandler(event.getValue()); }); - callback.accept(new Component[]{multilineTabs, tabHandlers}); + @SuppressWarnings("unchecked") + final Button tabSelect = new Button("Switch to \"Open source\" tab with .setSelected()", event -> ((SuperTabs)component).getTabHeader("Open source").ifPresent(tab -> tab.setSelected(true))); + callback.accept(new Component[]{multilineTabs, tabHandlers, tabSelect}); } } diff --git a/superfields/src/main/java/org/vaadin/miki/superfields/tabs/SuperTabs.java b/superfields/src/main/java/org/vaadin/miki/superfields/tabs/SuperTabs.java index 87672fc6..fa0346c5 100644 --- a/superfields/src/main/java/org/vaadin/miki/superfields/tabs/SuperTabs.java +++ b/superfields/src/main/java/org/vaadin/miki/superfields/tabs/SuperTabs.java @@ -12,6 +12,7 @@ import com.vaadin.flow.component.html.Span; import com.vaadin.flow.component.tabs.Tab; import com.vaadin.flow.component.tabs.Tabs; +import com.vaadin.flow.dom.PropertyChangeEvent; import org.vaadin.miki.markers.WithHelperMixin; import org.vaadin.miki.markers.WithHelperPositionableMixin; import org.vaadin.miki.markers.WithIdMixin; @@ -207,6 +208,20 @@ protected void addNewTab(T value, Tab tab, Component content, boolean select) { else this.updateValue(); } + tab.getElement().addPropertyChangeListener("selected", this::tabSelectedPropertyChanged); + } + + private void tabSelectedPropertyChanged(PropertyChangeEvent propertyChangeEvent) { + if(propertyChangeEvent.getPropertyName().equals("selected") && + (propertyChangeEvent.getOldValue() == null || !Boolean.parseBoolean(propertyChangeEvent.getOldValue().toString())) && + propertyChangeEvent.getValue() != null && + Boolean.parseBoolean(propertyChangeEvent.getValue().toString())) { + this.tabsToContents.entrySet().stream() + .filter(entry -> Objects.equals(entry.getKey().getElement(), propertyChangeEvent.getSource())) + .findFirst() + .map(Map.Entry::getKey) + .ifPresent(this.tabs::setSelectedTab); + } } /** diff --git a/superfields/src/test/java/org/vaadin/miki/superfields/tabs/SuperTabsTest.java b/superfields/src/test/java/org/vaadin/miki/superfields/tabs/SuperTabsTest.java index 4b19f07e..bb3cabdd 100644 --- a/superfields/src/test/java/org/vaadin/miki/superfields/tabs/SuperTabsTest.java +++ b/superfields/src/test/java/org/vaadin/miki/superfields/tabs/SuperTabsTest.java @@ -13,6 +13,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.Optional; public class SuperTabsTest { @@ -243,4 +244,20 @@ public void testChangingTabHandler() { } } + @Test + public void testTabSetSelected() { + final String tabTitle = "foo"; + this.tabs.addTab("something", "anything", tabTitle, "another thing"); + Assert.assertNotEquals(tabTitle, this.tabs.getValue()); + final Optional perhapsHeader = this.tabs.getTabHeader(tabTitle); + Assert.assertTrue(perhapsHeader.isPresent()); + final Tab tab = perhapsHeader.get(); + tab.setSelected(true); + Assert.assertEquals(tabTitle, this.tabs.getValue()); + + final Tab notThere = new Tab("oh wow"); + notThere.setSelected(true); + Assert.assertEquals(tabTitle, this.tabs.getValue()); + } + } \ No newline at end of file