Skip to content

Commit b608d3e

Browse files
committed
feat: add support for legacy demos
1 parent e60d99b commit b608d3e

File tree

4 files changed

+66
-19
lines changed

4 files changed

+66
-19
lines changed

src/main/java/com/flowingcode/vaadin/addons/demo/RouteTabs.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
package com.flowingcode.vaadin.addons.demo;
22

3-
import java.util.LinkedHashMap;
4-
import java.util.Map;
5-
import java.util.Optional;
3+
import com.vaadin.flow.component.Component;
4+
import com.vaadin.flow.component.UI;
65
import com.vaadin.flow.component.tabs.Tab;
76
import com.vaadin.flow.component.tabs.Tabs;
87
import com.vaadin.flow.router.BeforeEnterEvent;
98
import com.vaadin.flow.router.BeforeEnterObserver;
109
import com.vaadin.flow.router.HighlightConditions;
10+
import com.vaadin.flow.router.Location;
1111
import com.vaadin.flow.router.RouterLink;
12+
import java.util.LinkedHashMap;
13+
import java.util.Map;
14+
import java.util.Optional;
1215

1316
/**
1417
* Extension of Tabs in order to allow to bind tabs with Routes.
15-
*
18+
*
1619
* @see https://cookbook.vaadin.com/tabs-with-routes/a
1720
*/
1821
public class RouteTabs extends Tabs implements BeforeEnterObserver {
@@ -22,13 +25,17 @@ public class RouteTabs extends Tabs implements BeforeEnterObserver {
2225
public void add(RouterLink routerLink) {
2326
routerLink.setHighlightCondition(HighlightConditions.sameLocation());
2427
routerLink.setHighlightAction((link, shouldHighlight) -> {
25-
if (shouldHighlight)
28+
if (shouldHighlight) {
2629
setSelectedTab(routerLinkTabMap.get(routerLink));
30+
}
2731
});
2832
routerLinkTabMap.put(routerLink, new Tab(routerLink));
2933
add(routerLinkTabMap.get(routerLink));
3034
}
3135

36+
public void addLegacyTab(RouterLink routerLink) {
37+
38+
}
3239
@Override
3340
public void beforeEnter(BeforeEnterEvent event) {
3441
// In case no tabs will match
@@ -44,4 +51,18 @@ public RouterLink getFirstRoute() {
4451
routerLinkTabMap.entrySet().stream().map(Map.Entry::getKey).findFirst();
4552
return first.isPresent() ? first.get() : null;
4653
}
54+
55+
@Deprecated
56+
public void addLegacyTab(String label, Component content) {
57+
Tab tab = new Tab(label);
58+
add(tab);
59+
addSelectedChangeListener(ev -> {
60+
if (ev.getSelectedTab() == tab) {
61+
UI.getCurrent().getPage().getHistory().pushState(null, new Location(""));
62+
((TabbedDemo) getParent().get()).removeRouterLayoutContent(null);
63+
((TabbedDemo) getParent().get()).showRouterLayoutContent(content);
64+
}
65+
});
66+
}
67+
4768
}

src/main/java/com/flowingcode/vaadin/addons/demo/TabbedDemo.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,36 +83,26 @@ public TabbedDemo() {
8383
*
8484
* @param demo the demo instance
8585
*/
86+
@Deprecated
8687
public void addDemo(Component demo) {
8788
DemoSource demoSource = demo.getClass().getAnnotation(DemoSource.class);
8889

89-
String sourceCodeUrl = null;
90-
if (demoSource != null) {
91-
sourceCodeUrl = demoSource.value();
92-
if (sourceCodeUrl.equals(DemoSource.GITHUB_SOURCE)) {
93-
sourceCodeUrl = Optional.ofNullable(this.getClass().getAnnotation(GithubLink.class))
94-
.map(githubLink -> githubLink.value() + "/blob/master/src/test/java/"
95-
+ demo.getClass().getName().replace('.', '/') + ".java")
96-
.orElse(null);
97-
}
98-
}
99-
10090
String label =
10191
Optional.ofNullable(demo.getClass().getAnnotation(PageTitle.class))
10292
.map(PageTitle::value)
10393
.orElse(demo.getClass().getSimpleName());
10494

105-
addDemo(demo, label, sourceCodeUrl);
95+
addDemo(demo, label, null);
10696
}
10797

10898
/**
10999
* @param demo the demo instance
110100
* @param label the demo name (tab label)
111-
* @param sourceCodeUrl the url of the demo, <b>null</b> to not show source code section.
101+
* @param sourceCodeUrl ignored.
112102
*/
113103
@Deprecated
114104
public void addDemo(Component demo, String label, String sourceCodeUrl) {
115-
this.addDemo(demo.getClass(), label);
105+
tabs.addLegacyTab(label, demo);
116106
}
117107

118108
/** Add a tab with a demo component.
@@ -143,6 +133,7 @@ public void addDemo(Class<? extends Component> clazz) {
143133
addDemo(clazz, label);
144134
}
145135

136+
@Deprecated
146137
public void addDemo(Component demo, String label) {
147138
addDemo(demo, label, null);
148139
}

src/test/java/com/flowingcode/vaadin/addons/demo/Demo.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
public class Demo extends TabbedDemo {
3131

3232
public Demo() {
33+
addDemo(new LegacyDemo());
3334
addDemo(SampleDemo.class, "Demo");
3435
addDemo(SampleDemoDefault.class);
3536
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*-
2+
* #%L
3+
* Commons Demo
4+
* %%
5+
* Copyright (C) 2020 - 2021 Flowing Code
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.flowingcode.vaadin.addons.demo;
21+
22+
import com.vaadin.flow.component.html.Div;
23+
import com.vaadin.flow.component.html.Span;
24+
import com.vaadin.flow.router.PageTitle;
25+
26+
@PageTitle("Legacy")
27+
@DemoSource
28+
public class LegacyDemo extends Div {
29+
30+
public LegacyDemo() {
31+
add(new Span("Legacy demo"));
32+
}
33+
34+
}

0 commit comments

Comments
 (0)