diff --git a/pom.xml b/pom.xml index 41701ea..fc1192f 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.flowingcode.vaadin.addons.demo commons-demo - 4.0.1-SNAPSHOT + 4.1.0-SNAPSHOT Commons Demo Common classes for add-ons demo @@ -23,7 +23,7 @@ UTF-8 17 17 - 24.3.2 + 24.3.13 11.0.12 @@ -90,6 +90,12 @@ provided + + com.flowingcode.vaadin.addons + enhanced-tabs-addon + 1.0.0 + + junit junit diff --git a/src/main/java/com/flowingcode/vaadin/addons/demo/EnhancedRouteTabs.java b/src/main/java/com/flowingcode/vaadin/addons/demo/EnhancedRouteTabs.java new file mode 100644 index 0000000..4df0b45 --- /dev/null +++ b/src/main/java/com/flowingcode/vaadin/addons/demo/EnhancedRouteTabs.java @@ -0,0 +1,95 @@ +/*- + * #%L + * Commons Demo + * %% + * Copyright (C) 2020 - 2024 Flowing Code + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ +package com.flowingcode.vaadin.addons.demo; + +import com.flowingcode.vaadin.addons.enhancedtabs.EnhancedTabs; +import com.vaadin.flow.component.Component; +import com.vaadin.flow.component.UI; +import com.vaadin.flow.component.tabs.Tab; +import com.vaadin.flow.router.BeforeEnterEvent; +import com.vaadin.flow.router.BeforeEnterObserver; +import com.vaadin.flow.router.HighlightConditions; +import com.vaadin.flow.router.Location; +import com.vaadin.flow.router.Route; +import com.vaadin.flow.router.RouterLink; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Optional; + +/** + * Extension of EnhancedTabs in order to allow to bind tabs with Routes. + * + * @see https://cookbook.vaadin.com/tabs-with-routes/a + */ +public class EnhancedRouteTabs extends EnhancedTabs implements BeforeEnterObserver { + + private final Map routerLinkTabMap = new LinkedHashMap<>(); + + public void add(RouterLink routerLink) { + routerLink.setHighlightCondition(HighlightConditions.sameLocation()); + routerLink.setHighlightAction( + (link, shouldHighlight) -> { + if (shouldHighlight) { + setSelectedTab(routerLinkTabMap.get(routerLink)); + } + }); + routerLinkTabMap.put(routerLink, new Tab(routerLink)); + add(routerLinkTabMap.get(routerLink)); + } + + @Override + public void beforeEnter(BeforeEnterEvent event) { + setSelectedTab(null); + if (TabbedDemo.class.isAssignableFrom(event.getNavigationTarget())) { + RouterLink first = getFirstRoute(); + if (first != null) { + event.forwardTo(first.getHref()); + } else { + getChildren().findFirst().ifPresent(tab -> setSelectedTab((Tab) tab)); + } + } + } + + public Map getRouterLinkTabMap() { + return routerLinkTabMap; + } + + public RouterLink getFirstRoute() { + Optional first = + routerLinkTabMap.entrySet().stream().map(Map.Entry::getKey).findFirst(); + return first.isPresent() ? first.get() : null; + } + + @Deprecated + public void addLegacyTab(String label, Component content) { + Tab tab = new Tab(label); + add(tab); + addSelectedChangeListener( + ev -> { + if (ev.getSelectedTab() == tab) { + TabbedDemo tabbedDemo = (TabbedDemo) getParent().get(); + String route = tabbedDemo.getClass().getAnnotation(Route.class).value(); + UI.getCurrent().getPage().getHistory().pushState(null, new Location(route)); + tabbedDemo.removeRouterLayoutContent(null); + tabbedDemo.showRouterLayoutContent(content); + } + }); + } +} diff --git a/src/main/java/com/flowingcode/vaadin/addons/demo/RouteTabs.java b/src/main/java/com/flowingcode/vaadin/addons/demo/RouteTabs.java index 480280f..1631b36 100644 --- a/src/main/java/com/flowingcode/vaadin/addons/demo/RouteTabs.java +++ b/src/main/java/com/flowingcode/vaadin/addons/demo/RouteTabs.java @@ -2,7 +2,7 @@ * #%L * Commons Demo * %% - * Copyright (C) 2020 - 2023 Flowing Code + * Copyright (C) 2020 - 2024 Flowing Code * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -37,7 +37,10 @@ * Extension of Tabs in order to allow to bind tabs with Routes. * * @see https://cookbook.vaadin.com/tabs-with-routes/a + * @deprecated Deprecated for removal. Use {@link EnhancedRouteTabs} instead. */ +@SuppressWarnings("serial") +@Deprecated(forRemoval = true) public class RouteTabs extends Tabs implements BeforeEnterObserver { private final Map routerLinkTabMap = new LinkedHashMap<>(); diff --git a/src/main/java/com/flowingcode/vaadin/addons/demo/TabbedDemo.java b/src/main/java/com/flowingcode/vaadin/addons/demo/TabbedDemo.java index bd8de0a..d1d6ae2 100644 --- a/src/main/java/com/flowingcode/vaadin/addons/demo/TabbedDemo.java +++ b/src/main/java/com/flowingcode/vaadin/addons/demo/TabbedDemo.java @@ -2,7 +2,7 @@ * #%L * Commons Demo * %% - * Copyright (C) 2020 - 2023 Flowing Code + * Copyright (C) 2020 - 2024 Flowing Code * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -54,7 +54,7 @@ public class TabbedDemo extends VerticalLayout implements RouterLayout { private static final Logger logger = LoggerFactory.getLogger(TabbedDemo.class); private static final int MOBILE_DEVICE_BREAKPOINT_WIDTH = 768; - private RouteTabs tabs; + private EnhancedRouteTabs tabs; private HorizontalLayout footer; private SplitLayoutDemo currentLayout; private Checkbox orientationCB; @@ -68,7 +68,7 @@ public class TabbedDemo extends VerticalLayout implements RouterLayout { public TabbedDemo() { demoHelperViewer = new DialogDemoHelperViewer(); - tabs = new RouteTabs(); + tabs = new EnhancedRouteTabs(); tabs.setWidthFull(); // Footer