Skip to content

Commit 2ff359e

Browse files
javier-godoypaodb
authored andcommitted
feat: add MultiSourceCodeViewer component
1 parent 2b6eb9f commit 2ff359e

4 files changed

Lines changed: 119 additions & 2 deletions

File tree

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@
110110
</exclusions>
111111
</dependency>
112112

113+
<dependency>
114+
<groupId>org.projectlombok</groupId>
115+
<artifactId>lombok</artifactId>
116+
<version>1.18.30</version>
117+
<scope>provided</scope>
118+
</dependency>
119+
113120
<dependency>
114121
<groupId>junit</groupId>
115122
<artifactId>junit</artifactId>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.flowingcode.vaadin.addons.demo;
2+
3+
import com.vaadin.flow.component.ComponentUtil;
4+
import com.vaadin.flow.component.html.Div;
5+
import com.vaadin.flow.component.tabs.Tab;
6+
import com.vaadin.flow.component.tabs.Tabs;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
public class MultiSourceCodeViewer extends Div {
11+
12+
private static final String DATA_URL = "source-url";
13+
private static final String DATA_LANGUAGE = "source-language";
14+
15+
private SourceCodeViewer codeViewer;
16+
17+
public MultiSourceCodeViewer(List<SourceCodeTab> sourceCodeTabs, Map<String, String> properties) {
18+
19+
Tab tab;
20+
if (sourceCodeTabs.size() > 1) {
21+
Tabs tabs = new Tabs(createTabs(sourceCodeTabs));
22+
tabs.addSelectedChangeListener(ev -> onTabSelected(ev.getSelectedTab()));
23+
add(tabs);
24+
tab = tabs.getSelectedTab();
25+
} else {
26+
tab = createTab(sourceCodeTabs.get(0));
27+
}
28+
29+
String url = (String) ComponentUtil.getData(tab, DATA_URL);
30+
String language = (String) ComponentUtil.getData(tab, DATA_LANGUAGE);
31+
codeViewer = new SourceCodeViewer(url, language, properties);
32+
33+
add(codeViewer);
34+
codeViewer.getStyle().set("flex-grow", "1");
35+
getStyle().set("display", "flex");
36+
getStyle().set("flex-direction", "column");
37+
}
38+
39+
private Tab[] createTabs(List<SourceCodeTab> sourceCodeTabs) {
40+
return sourceCodeTabs.stream().map(this::createTab).toArray(Tab[]::new);
41+
}
42+
43+
private Tab createTab(SourceCodeTab sourceCodeTab) {
44+
String url = sourceCodeTab.getUrl();
45+
String language = sourceCodeTab.getLanguage();
46+
47+
String caption = sourceCodeTab.getCaption();
48+
if (caption == null) {
49+
caption = url.replaceAll(".*/", "");
50+
}
51+
52+
if (language == null) {
53+
String ext = url.replaceAll(".*\\.", "");
54+
switch (ext) {
55+
case "java":
56+
language = "java";
57+
break;
58+
case "css":
59+
language = "css";
60+
break;
61+
case "js":
62+
language = "js";
63+
break;
64+
case "ts":
65+
language = "ts";
66+
break;
67+
default:
68+
language = "unknown";
69+
break;
70+
}
71+
}
72+
73+
Tab tab = new Tab(caption);
74+
ComponentUtil.setData(tab, DATA_URL, url);
75+
ComponentUtil.setData(tab, DATA_LANGUAGE, language);
76+
return tab;
77+
}
78+
79+
private void onTabSelected(Tab tab) {
80+
String url = (String) ComponentUtil.getData(tab, DATA_URL);
81+
String language = (String) ComponentUtil.getData(tab, DATA_LANGUAGE);
82+
fetchContents(url, language);
83+
}
84+
85+
private void fetchContents(String url, String language) {
86+
codeViewer.fetchContents(url, language);
87+
}
88+
89+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.flowingcode.vaadin.addons.demo;
2+
3+
import lombok.AccessLevel;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Getter;
6+
import lombok.RequiredArgsConstructor;
7+
import lombok.With;
8+
9+
@Getter
10+
@With
11+
@RequiredArgsConstructor
12+
@AllArgsConstructor(access = AccessLevel.PRIVATE)
13+
public class SourceCodeTab {
14+
private final String url;
15+
private String caption;
16+
private String language;
17+
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@
2424
import com.vaadin.flow.component.splitlayout.SplitLayout;
2525
import com.vaadin.flow.component.splitlayout.SplitLayout.Orientation;
2626
import com.vaadin.flow.server.Version;
27+
import java.util.ArrayList;
2728
import java.util.HashMap;
29+
import java.util.List;
2830
import java.util.Map;
2931

3032
@SuppressWarnings("serial")
3133
class SplitLayoutDemo extends Composite<SplitLayout> {
3234

33-
private SourceCodeViewer code;
35+
private MultiSourceCodeViewer code;
3436

3537
public SplitLayoutDemo(Component demo, String sourceUrl) {
3638
getContent().setOrientation(Orientation.HORIZONTAL);
@@ -39,7 +41,9 @@ public SplitLayoutDemo(Component demo, String sourceUrl) {
3941
properties.put("vaadin", VaadinVersion.getVaadinVersion());
4042
properties.put("flow", Version.getFullVersion());
4143

42-
code = new SourceCodeViewer(sourceUrl, properties);
44+
List<SourceCodeTab> tabs = new ArrayList<>();
45+
tabs.add(new SourceCodeTab(sourceUrl));
46+
code = new MultiSourceCodeViewer(tabs, properties);
4347
getContent().addToPrimary(demo);
4448
getContent().addToSecondary(code);
4549
getContent().setSizeFull();

0 commit comments

Comments
 (0)