Skip to content

Commit d5ec76b

Browse files
javier-godoymlopezFC
authored andcommitted
feat: add annotation-based configuration
1 parent 86c1930 commit d5ec76b

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.flowingcode.vaadin.addons.demo;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* This annotation is used for configuring the source code URL in a {@link TabbedDemo}
10+
*
11+
* @author Javier Godoy / Flowing Code
12+
*/
13+
@Retention(RetentionPolicy.RUNTIME)
14+
@Target(ElementType.TYPE)
15+
public @interface DemoSource {
16+
17+
String value();
18+
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
import com.vaadin.flow.component.splitlayout.SplitLayout.Orientation;
2828
import com.vaadin.flow.component.tabs.Tab;
2929
import com.vaadin.flow.component.tabs.Tabs;
30+
import com.vaadin.flow.router.PageTitle;
3031
import java.util.HashMap;
3132
import java.util.Map;
33+
import java.util.Optional;
3234

3335
@StyleSheet("context://frontend/styles/commons-demo/shared-styles.css")
3436
@SuppressWarnings("serial")
@@ -84,6 +86,26 @@ public TabbedDemo() {
8486
setSizeFull();
8587
}
8688

89+
/**
90+
* Add a tab with a {@code demo} component. The tab label and source code URL are retrieved from
91+
* the {@link PageTitle} (required) and {@link DemoSource} (optional) annotations in the demo
92+
* class, respectively.
93+
*
94+
* @param demo the demo instance
95+
*/
96+
public void addDemo(Component demo) {
97+
String sourceCodeUrl =
98+
Optional.ofNullable(demo.getClass().getAnnotation(DemoSource.class))
99+
.map(DemoSource::value)
100+
.orElse(null);
101+
String label =
102+
Optional.ofNullable(demo.getClass().getAnnotation(PageTitle.class))
103+
.map(PageTitle::value)
104+
.orElse(demo.getClass().getName());
105+
106+
addDemo(demo, label, sourceCodeUrl);
107+
}
108+
87109
/**
88110
* @param demo the demo instance
89111
* @param name the demo name (tab label)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public Demo() {
4747
vl3.add(tf);
4848
tabbedDemo.addDemo(vl3, "Demo Without Source Code");
4949

50+
tabbedDemo.addDemo(new SampleDemo());
51+
5052
add(tabbedDemo);
5153
setSizeFull();
5254
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.flowingcode.vaadin.addons.demo;
2+
3+
import com.vaadin.flow.component.html.Div;
4+
import com.vaadin.flow.component.html.Span;
5+
import com.vaadin.flow.router.PageTitle;
6+
7+
@PageTitle("Demo 4")
8+
@DemoSource(
9+
"https://github.com/FlowingCode/CommonsDemo/blob/master/src/test/java/com/flowingcode/vaadin/addons/demo/impl/SampleDemo.java")
10+
public class SampleDemo extends Div {
11+
12+
public SampleDemo() {
13+
add(new Span("Demo component with annotations"));
14+
}
15+
}

0 commit comments

Comments
 (0)