Skip to content

Commit 49dc25c

Browse files
committed
feat: add clazz attribute to DemoSource annotation
1 parent 4c873e5 commit 49dc25c

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

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

Lines changed: 19 additions & 8 deletions
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 - 2024 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.
@@ -29,12 +29,12 @@
2929

3030
/**
3131
* This annotation is used for configuring the source code URL in a {@link TabbedDemo}. If no {@code
32-
* value} is specified, and the demo view is annotated with {@link GithubLink}, then the source URL
33-
* defaults to the location of the annotated class under {@code src/test/java} and the branch is
34-
* determined from the value of {@link GithubBranch} in the demo view class (if the annotation is
35-
* present) or the containing package of the demo view class. If the source URL is defaulted and no
36-
* {@code GithubBranch} annotation is present either in the demo view class or its containing
37-
* package, then the branch defaults to {@code master}.
32+
* value} or {@code clazz} is specified, and the demo view is annotated with {@link GithubLink},
33+
* then the source URL defaults to the location of the annotated class under {@code src/test/java}
34+
* and the branch is determined from the value of {@link GithubBranch} in the demo view class (if
35+
* the annotation is present) or the containing package of the demo view class. If the source URL is
36+
* defaulted and no {@code GithubBranch} annotation is present either in the demo view class or its
37+
* containing package, then the branch defaults to {@code master}.
3838
*
3939
* @author Javier Godoy / Flowing Code
4040
*/
@@ -47,9 +47,20 @@
4747

4848
static final String DEFAULT_VALUE = "__DEFAULT__";
4949

50-
/** A link to the source code, if different from the annotated class. */
50+
/**
51+
* A link to the source code, if different from the annotated class.
52+
* <p>
53+
* It is an error if both {@code value} and {@link #clazz()} are specified.
54+
*/
5155
String value() default GITHUB_SOURCE;
5256

57+
/**
58+
* The class to display, if different from the annotated class.
59+
* <p>
60+
* It is an error if both {@link #value()} and {@code clazz} are specified.
61+
*/
62+
Class<?> clazz() default DemoSource.class;
63+
5364
/**
5465
* The caption of the source tab (displayed if several sources are provided). Default to the file
5566
* name.

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,12 @@ private Optional<SourceCodeTab> createSourceCodeTab(Class<?> annotatedClass, Dem
214214
String demoFile;
215215
String url = annotation.value();
216216
if (url.equals(DemoSource.GITHUB_SOURCE)) {
217-
String className = annotatedClass.getName().replace('.', '/');
217+
String className;
218+
if (annotation.clazz() == DemoSource.class) {
219+
className = annotatedClass.getName().replace('.', '/');
220+
} else {
221+
className = annotation.clazz().getName().replace('.', '/');
222+
}
218223
demoFile = "src/test/java/" + className + ".java";
219224
} else if (url.startsWith("/src/test/")) {
220225
demoFile = url.substring(1);

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@
2626

2727
@Route(value = "demo/multisource", layout = Demo.class)
2828
@PageTitle("Demo with multiple sources")
29+
// show-source @DemoSource
30+
// show-source @DemoSource("/src/test/resources/META-INF/resources/frontend/multi-source-demo.css")
31+
// show-source @DemoSource(clazz = Demo.class)
2932
@DemoSource
30-
@DemoSource(value = "/src/test/resources/META-INF/resources/frontend/multi-source-demo.css")
33+
@DemoSource("/src/test/resources/META-INF/resources/frontend/multi-source-demo.css")
34+
@DemoSource(clazz = Demo.class)
3135
@StyleSheet("./multi-source-demo.css")
3236
public class MultiSourceDemo extends Div {
3337
public MultiSourceDemo() {

0 commit comments

Comments
 (0)