Skip to content

Commit b215502

Browse files
javier-godoypaodb
authored andcommitted
feat: allow DemoSource URL to default to the location of the class
1 parent 63b7ae7 commit b215502

File tree

6 files changed

+61
-8
lines changed

6 files changed

+61
-8
lines changed

src/main/java/com/flowingcode/vaadin/addons/GithubLink.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import java.lang.annotation.Target;
2727

2828
/**
29-
* This annotation is used for configuring the URL in the {@code GitHubCorner}
29+
* This annotation is used for configuring the URL in the {@code GitHubCorner} and {@code DemoClass}
3030
*
3131
* @author Javier Godoy / Flowing Code
3232
*/

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,20 @@
2525
import java.lang.annotation.Target;
2626

2727
/**
28-
* This annotation is used for configuring the source code URL in a {@link TabbedDemo}
28+
* This annotation is used for configuring the source code URL in a {@link TabbedDemo}. If no
29+
* {@code value} is specified, and the demo view is annotated with {@link @GithubLink}, the source
30+
* URL defaults to the location of the annotated class under {@code src/java/test} in the master
31+
* branch of the repository.
2932
*
3033
* @author Javier Godoy / Flowing Code
3134
*/
3235
@Retention(RetentionPolicy.RUNTIME)
3336
@Target(ElementType.TYPE)
3437
public @interface DemoSource {
3538

36-
String value();
39+
static final String GITHUB_SOURCE = "__GITHUB__";
40+
41+
/** A link to the source code, if different from the annotated class. */
42+
String value() default GITHUB_SOURCE;
43+
3744
}

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020
package com.flowingcode.vaadin.addons.demo;
2121

22+
import com.flowingcode.vaadin.addons.GithubLink;
2223
import com.vaadin.flow.component.Component;
2324
import com.vaadin.flow.component.checkbox.Checkbox;
2425
import com.vaadin.flow.component.dependency.StyleSheet;
@@ -94,10 +95,19 @@ public TabbedDemo() {
9495
* @param demo the demo instance
9596
*/
9697
public void addDemo(Component demo) {
97-
String sourceCodeUrl =
98-
Optional.ofNullable(demo.getClass().getAnnotation(DemoSource.class))
99-
.map(DemoSource::value)
98+
DemoSource demoSource = demo.getClass().getAnnotation(DemoSource.class);
99+
100+
String sourceCodeUrl = null;
101+
if (demoSource != null) {
102+
sourceCodeUrl = demoSource.value();
103+
if (sourceCodeUrl.equals(DemoSource.GITHUB_SOURCE)) {
104+
sourceCodeUrl = Optional.ofNullable(this.getClass().getAnnotation(GithubLink.class))
105+
.map(githubLink -> githubLink.value() + "/blob/master/src/test/java/"
106+
+ demo.getClass().getName().replace('.', '/'))
100107
.orElse(null);
108+
}
109+
}
110+
101111
String label =
102112
Optional.ofNullable(demo.getClass().getAnnotation(PageTitle.class))
103113
.map(PageTitle::value)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
*/
2020
package com.flowingcode.vaadin.addons.demo;
2121

22+
import com.flowingcode.vaadin.addons.GithubLink;
2223
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
2324
import com.vaadin.flow.component.textfield.TextField;
2425
import com.vaadin.flow.router.Route;
2526

2627
/** Hello world! */
2728
@Route("")
29+
@GithubLink("https://github.com/FlowingCode/CommonsDemo")
2830
public class Demo extends TabbedDemo {
2931

3032
public Demo() {
@@ -47,5 +49,6 @@ public Demo() {
4749
addDemo(vl3, "Demo Without Source Code");
4850

4951
addDemo(new SampleDemo());
52+
addDemo(new SampleDemoDefault());
5053
}
5154
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
import com.vaadin.flow.router.PageTitle;
2525

2626
@PageTitle("Demo 4")
27-
@DemoSource(
28-
"https://github.com/FlowingCode/CommonsDemo/blob/master/src/test/java/com/flowingcode/vaadin/addons/demo/SampleDemo.java")
27+
@DemoSource("https://github.com/FlowingCode/CommonsDemo/blob/master/src/test/java/com/flowingcode/vaadin/addons/demo/SampleDemo.java")
2928
public class SampleDemo extends Div {
3029

3130
public SampleDemo() {
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("Demo 5")
27+
@DemoSource
28+
public class SampleDemoDefault extends Div {
29+
30+
public SampleDemoDefault() {
31+
add(new Span("Demo component with defaulted @DemoSource annotation"));
32+
}
33+
34+
}

0 commit comments

Comments
 (0)