Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Tabs with EnhancedTabs in TabbedDemo #88

Merged
merged 5 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.flowingcode.vaadin.addons.demo</groupId>
<artifactId>commons-demo</artifactId>
<version>4.0.1-SNAPSHOT</version>
<version>4.1.0-SNAPSHOT</version>

<name>Commons Demo</name>
<description>Common classes for add-ons demo</description>
Expand All @@ -23,7 +23,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<vaadin.version>24.3.2</vaadin.version>
<vaadin.version>24.3.13</vaadin.version>
<jetty.version>11.0.12</jetty.version>
</properties>

Expand Down Expand Up @@ -90,6 +90,12 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.flowingcode.vaadin.addons</groupId>
<artifactId>enhanced-tabs-addon</artifactId>
<version>1.0.0</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<RouterLink, Tab> 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<RouterLink, Tab> getRouterLinkTabMap() {
return routerLinkTabMap;
}

public RouterLink getFirstRoute() {
Optional<RouterLink> 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);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<RouterLink, Tab> routerLinkTabMap = new LinkedHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down
Loading