Skip to content

Commit 0dbe57f

Browse files
committed
feat: show DemoSource based on Vaadin version
Close #132
1 parent cc992bd commit 0dbe57f

File tree

9 files changed

+143
-12
lines changed

9 files changed

+143
-12
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* #%L
33
* Commons Demo
44
* %%
5-
* Copyright (C) 2020 - 2025 Flowing Code
5+
* Copyright (C) 2020 - 2026 Flowing Code
66
* %%
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
@@ -77,4 +77,6 @@
7777
/** Source code position in the layout */
7878
SourcePosition sourcePosition() default SourcePosition.SECONDARY;
7979

80+
String condition() default "";
81+
8082
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*-
2+
* #%L
3+
* Commons Demo
4+
* %%
5+
* Copyright (C) 2020 - 2026 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 java.util.Map;
23+
import lombok.NonNull;
24+
import lombok.experimental.UtilityClass;
25+
26+
@UtilityClass
27+
class DemoSourceConditionHelper {
28+
29+
public boolean eval(String condition, Map<String, String> env) {
30+
if (condition == null) {
31+
return true;
32+
}
33+
34+
String[] expr = condition.split(" ");
35+
36+
if (expr.length == 3) {
37+
String lhs = env.get(expr[0]);
38+
39+
if (lhs == null) {
40+
return false;
41+
}
42+
43+
String operator = expr[1];
44+
String rhs = expr[2];
45+
46+
switch (operator) {
47+
case "lt":
48+
return compare(lhs, rhs) < 0;
49+
case "le":
50+
return compare(lhs, rhs) <= 0;
51+
case "eq":
52+
return compare(lhs, rhs) == 0;
53+
case "ge":
54+
return compare(lhs, rhs) >= 0;
55+
case "gt":
56+
return compare(lhs, rhs) > 0;
57+
case "ne":
58+
return compare(lhs, rhs) != 0;
59+
default:
60+
throw new IllegalArgumentException("Unknown operator: " + operator);
61+
}
62+
} else {
63+
throw new IllegalArgumentException("Invalid condition: '" + condition
64+
+ "'. Must be exactly 3 components: [VARIABLE] [OPERATOR] [VERSION]");
65+
}
66+
}
67+
68+
private int compare(String a, String b) {
69+
String[] aa = split(a);
70+
String[] bb = split(b);
71+
72+
int minLength = Math.min(aa.length, bb.length);
73+
74+
for (int i = 0; i < minLength; i++) {
75+
int ai = Integer.parseInt(aa[i]);
76+
int bi = Integer.parseInt(bb[i]);
77+
int c = Integer.compare(ai, bi);
78+
if (c != 0) {
79+
return c;
80+
}
81+
}
82+
83+
return 0;
84+
}
85+
86+
private String[] split(@NonNull String version) {
87+
if (!version.matches("^\\d+(\\.\\d+){0,2}$")) {
88+
throw new IllegalArgumentException("Invalid version: '" + version
89+
+ "'. Must be 'major', 'major.minor', or major.minor.patch'");
90+
}
91+
return version.split("\\.");
92+
}
93+
}

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* #%L
33
* Commons Demo
44
* %%
5-
* Copyright (C) 2020 - 2025 Flowing Code
5+
* Copyright (C) 2020 - 2026 Flowing Code
66
* %%
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
2525
import com.vaadin.flow.component.html.Div;
2626
import com.vaadin.flow.component.tabs.Tab;
2727
import elemental.json.JsonValue;
28+
import java.util.ArrayList;
2829
import java.util.List;
2930
import java.util.Map;
3031
import java.util.Optional;
@@ -42,6 +43,12 @@ public class MultiSourceCodeViewer extends Div {
4243
private EnhancedTabs tabs;
4344

4445
public MultiSourceCodeViewer(List<SourceCodeTab> sourceCodeTabs, Map<String, String> properties) {
46+
sourceCodeTabs = new ArrayList<>(sourceCodeTabs);
47+
sourceCodeTabs.removeIf(tab -> !DemoSourceConditionHelper.eval(tab.getCondition(), properties));
48+
if (sourceCodeTabs.isEmpty()) {
49+
return;
50+
}
51+
4552
if (sourceCodeTabs.size() > 1) {
4653
tabs = new EnhancedTabs(createTabs(sourceCodeTabs));
4754
tabs.addSelectedChangeListener(ev -> onTabSelected(ev.getSelectedTab()));
@@ -69,6 +76,10 @@ public MultiSourceCodeViewer(List<SourceCodeTab> sourceCodeTabs, Map<String, Str
6976
getStyle().set("flex-direction", "column");
7077
}
7178

79+
public boolean isEmpty() {
80+
return selectedTab == null;
81+
}
82+
7283
private Tab[] createTabs(List<SourceCodeTab> sourceCodeTabs) {
7384
return sourceCodeTabs.stream().map(this::createTab).toArray(Tab[]::new);
7485
}
@@ -134,7 +145,12 @@ private void fetchContents(String url, String language) {
134145
}
135146

136147
public SourcePosition getSourcePosition() {
137-
return (SourcePosition) ComponentUtil.getData(selectedTab, DATA_POSITION);
148+
if (selectedTab != null) {
149+
return (SourcePosition) ComponentUtil.getData(selectedTab, DATA_POSITION);
150+
} else {
151+
return SourcePosition.SECONDARY;
152+
}
153+
138154
}
139155

140156
private Optional<Tab> findTabWithFilename(String filename) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* #%L
33
* Commons Demo
44
* %%
5-
* Copyright (C) 2020 - 2024 Flowing Code
5+
* Copyright (C) 2020 - 2026 Flowing Code
66
* %%
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
@@ -36,6 +36,7 @@ public class SourceCodeTab {
3636
private final String url;
3737
private String caption;
3838
private String language;
39+
private String condition;
3940
@NonNull
4041
private final SourcePosition sourcePosition;
4142

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* #%L
33
* Commons Demo
44
* %%
5-
* Copyright (C) 2020 - 2023 Flowing Code
5+
* Copyright (C) 2020 - 2026 Flowing Code
66
* %%
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
@@ -54,6 +54,10 @@ public SplitLayoutDemo(Component demo, List<SourceCodeTab> tabs) {
5454
getContent().setSizeFull();
5555
}
5656

57+
public boolean isEmpty() {
58+
return code.isEmpty();
59+
}
60+
5761
private void setSourcePosition(SourcePosition position) {
5862
if (!position.equals(sourcePosition)) {
5963
getContent().removeAll();

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* #%L
33
* Commons Demo
44
* %%
5-
* Copyright (C) 2020 - 2025 Flowing Code
5+
* Copyright (C) 2020 - 2026 Flowing Code
66
* %%
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
@@ -181,16 +181,21 @@ public void showRouterLayoutContent(HasElement content) {
181181
}
182182

183183
if (!sourceTabs.isEmpty()) {
184-
content = new SplitLayoutDemo(demo, sourceTabs);
185-
currentLayout = (SplitLayoutDemo) content;
184+
currentLayout = new SplitLayoutDemo(demo, sourceTabs);
185+
if (currentLayout.isEmpty()) {
186+
demo.getElement().removeAttribute("slot");
187+
currentLayout = null;
188+
}
189+
}
190+
191+
if (currentLayout != null) {
192+
content = currentLayout;
186193
if (splitOrientation != null) {
187194
setOrientation(splitOrientation);
188195
updateSplitterPosition();
189196
}
190197

191-
if (currentLayout != null) {
192-
setupDemoHelperButton(currentLayout.getContent().getPrimaryComponent().getClass());
193-
}
198+
setupDemoHelperButton(currentLayout.getContent().getPrimaryComponent().getClass());
194199
} else {
195200
currentLayout = null;
196201
demo.getElement().getStyle().set("height", "100%");
@@ -245,6 +250,10 @@ private Optional<SourceCodeTab> createSourceCodeTab(Class<?> annotatedClass, Dem
245250
builder.language(annotation.caption());
246251
}
247252

253+
if (!annotation.condition().isEmpty()) {
254+
builder.condition(annotation.condition());
255+
}
256+
248257
builder.sourcePosition(annotation.sourcePosition());
249258

250259
return Optional.of(builder.build());

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*-
22
* #%L
33
* %%
4-
* Copyright (C) 2020 - 2024 Flowing Code
4+
* Copyright (C) 2020 - 2026 Flowing Code
55
* %%
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -29,9 +29,13 @@
2929
// show-source @DemoSource
3030
// show-source @DemoSource(clazz = AdditionalSources.class)
3131
// show-source @DemoSource("/src/test/resources/META-INF/resources/frontend/multi-source-demo.css")
32+
// show-source @DemoSource(value="/src/test/resources/META-INF/resources/frontend/multi-source-demo.css", condition = "vaadin ge 14")
33+
// show-source @DemoSource(value="/src/test/resources/META-INF/resources/frontend/multi-source-demo.css", condition = "vaadin eq 0")
3234
@DemoSource
3335
@DemoSource(clazz = AdditionalSources.class)
3436
@DemoSource("/src/test/resources/META-INF/resources/frontend/multi-source-demo.css")
37+
@DemoSource(value="/src/test/resources/META-INF/resources/frontend/condition-true.css", condition = "vaadin ge 14")
38+
@DemoSource(value="/src/test/resources/META-INF/resources/frontend/condition-false.css", condition = "vaadin eq 0")
3539
@StyleSheet("./multi-source-demo.css")
3640
public class MultiSourceDemo extends Div {
3741
public MultiSourceDemo() {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/** This source file is conditionally hidden. */
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/** This source is shown when Vaadin version is 14+ */

0 commit comments

Comments
 (0)