Skip to content

Commit 102479f

Browse files
Felipe Langjavier-godoy
authored andcommitted
feat: add support to change source code position
Close #72
1 parent 92d53b6 commit 102479f

6 files changed

Lines changed: 108 additions & 12 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,7 @@
6262
*/
6363
String language() default DEFAULT_VALUE;
6464

65+
/** Source code position in the layout */
66+
SourcePosition sourcePosition() default SourcePosition.SECONDARY;
67+
6568
}

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@ public class MultiSourceCodeViewer extends Div {
1111

1212
private static final String DATA_URL = "source-url";
1313
private static final String DATA_LANGUAGE = "source-language";
14+
private static final String DATA_POSITION = "source-position";
1415

1516
private SourceCodeViewer codeViewer;
17+
private Tab selectedTab;
1618

1719
public MultiSourceCodeViewer(List<SourceCodeTab> sourceCodeTabs, Map<String, String> properties) {
18-
19-
Tab tab;
2020
if (sourceCodeTabs.size() > 1) {
2121
Tabs tabs = new Tabs(createTabs(sourceCodeTabs));
2222
tabs.addSelectedChangeListener(ev -> onTabSelected(ev.getSelectedTab()));
2323
add(tabs);
24-
tab = tabs.getSelectedTab();
24+
selectedTab = tabs.getSelectedTab();
2525
} else {
26-
tab = createTab(sourceCodeTabs.get(0));
26+
selectedTab = createTab(sourceCodeTabs.get(0));
2727
}
2828

29-
String url = (String) ComponentUtil.getData(tab, DATA_URL);
30-
String language = (String) ComponentUtil.getData(tab, DATA_LANGUAGE);
29+
String url = (String) ComponentUtil.getData(selectedTab, DATA_URL);
30+
String language = (String) ComponentUtil.getData(selectedTab, DATA_LANGUAGE);
3131
codeViewer = new SourceCodeViewer(url, language, properties);
3232

3333
add(codeViewer);
@@ -74,6 +74,7 @@ private Tab createTab(SourceCodeTab sourceCodeTab) {
7474
Tab tab = new Tab(caption);
7575
ComponentUtil.setData(tab, DATA_URL, url);
7676
ComponentUtil.setData(tab, DATA_LANGUAGE, language);
77+
ComponentUtil.setData(tab, DATA_POSITION, sourceCodeTab.getSourcePosition());
7778
return tab;
7879
}
7980

@@ -88,6 +89,8 @@ private String getExtension(String filename) {
8889
}
8990

9091
private void onTabSelected(Tab tab) {
92+
this.selectedTab = tab;
93+
9194
String url = (String) ComponentUtil.getData(tab, DATA_URL);
9295
String language = (String) ComponentUtil.getData(tab, DATA_LANGUAGE);
9396
fetchContents(url, language);
@@ -97,4 +100,8 @@ private void fetchContents(String url, String language) {
97100
codeViewer.fetchContents(url, language);
98101
}
99102

103+
public SourcePosition getSourcePosition() {
104+
return (SourcePosition) ComponentUtil.getData(selectedTab, DATA_POSITION);
105+
}
106+
100107
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,7 @@ public class SourceCodeTab {
1717
private final String url;
1818
private String caption;
1919
private String language;
20+
@NonNull
21+
private final SourcePosition sourcePosition;
2022

2123
}
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+
public enum SourcePosition {
4+
5+
PRIMARY, SECONDARY;
6+
7+
public SourcePosition toggle() {
8+
switch (this) {
9+
case SECONDARY:
10+
return PRIMARY;
11+
case PRIMARY:
12+
default:
13+
return SECONDARY;
14+
}
15+
}
16+
17+
}

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

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,18 @@
2828
import java.util.HashMap;
2929
import java.util.List;
3030
import java.util.Map;
31+
import java.util.Objects;
32+
import java.util.Optional;
33+
import org.apache.commons.lang3.StringUtils;
3134

3235
@SuppressWarnings("serial")
3336
class SplitLayoutDemo extends Composite<SplitLayout> {
3437

3538
private MultiSourceCodeViewer code;
39+
private SourcePosition sourcePosition;
3640

37-
public SplitLayoutDemo(Component demo, String sourceUrl) {
38-
this(demo, Arrays.asList(new SourceCodeTab(sourceUrl)));
41+
public SplitLayoutDemo(Component demo, String sourceUrl, SourcePosition sourcePosition) {
42+
this(demo, Arrays.asList(new SourceCodeTab(sourceUrl, sourcePosition)));
3943
}
4044

4145
public SplitLayoutDemo(Component demo, List<SourceCodeTab> tabs) {
@@ -46,11 +50,37 @@ public SplitLayoutDemo(Component demo, List<SourceCodeTab> tabs) {
4650
properties.put("flow", Version.getFullVersion());
4751

4852
code = new MultiSourceCodeViewer(tabs, properties);
49-
getContent().addToPrimary(demo);
50-
getContent().addToSecondary(code);
53+
54+
this.sourcePosition = code.getSourcePosition();
55+
switch (this.sourcePosition) {
56+
case PRIMARY:
57+
getContent().addToPrimary(code);
58+
getContent().addToSecondary(demo);
59+
break;
60+
case SECONDARY:
61+
default:
62+
getContent().addToPrimary(demo);
63+
getContent().addToSecondary(code);
64+
}
65+
5166
getContent().setSizeFull();
5267
}
5368

69+
public void switchSourcePosition(SourcePosition position) {
70+
if (!this.sourcePosition.equals(position)) {
71+
toggleSourcePosition();
72+
}
73+
}
74+
75+
public void toggleSourcePosition() {
76+
Component primary = getContent().getPrimaryComponent();
77+
Component secondary = getContent().getSecondaryComponent();
78+
getContent().removeAll();
79+
getContent().addToPrimary(secondary);
80+
getContent().addToSecondary(primary);
81+
this.sourcePosition = this.sourcePosition.toggle();
82+
}
83+
5484
public void setOrientation(Orientation o) {
5585
getContent().setOrientation(o);
5686
getContent()
@@ -71,4 +101,20 @@ public void setSplitterPosition(int pos) {
71101
public void setSizeFull() {
72102
getContent().setSizeFull();
73103
}
104+
105+
public void showSourceCode() {
106+
getContent().setSplitterPosition(50);
107+
}
108+
109+
public void hideSourceCode() {
110+
switch (sourcePosition) {
111+
case PRIMARY:
112+
getContent().setSplitterPosition(0);
113+
break;
114+
case SECONDARY:
115+
getContent().setSplitterPosition(100);
116+
break;
117+
}
118+
}
119+
74120
}

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public class TabbedDemo extends VerticalLayout implements RouterLayout {
5959
private SplitLayoutDemo currentLayout;
6060
private Checkbox orientationCB;
6161
private Checkbox codeCB;
62+
private Checkbox codePositionCB;
6263
private Checkbox themeCB;
6364
private Orientation splitOrientation;
6465
private Button helperButton;
@@ -83,6 +84,10 @@ public TabbedDemo() {
8384
codeCB.setValue(true);
8485
codeCB.addClassName("smallcheckbox");
8586
codeCB.addValueChangeListener(ev -> updateSplitterPosition());
87+
codePositionCB = new Checkbox("Toggle Code Position");
88+
codePositionCB.setValue(true);
89+
codePositionCB.addClassName("smallcheckbox");
90+
codePositionCB.addValueChangeListener(ev -> toggleSourcePosition());
8691
themeCB = new Checkbox("Dark Theme");
8792
themeCB.setValue(false);
8893
themeCB.addClassName("smallcheckbox");
@@ -92,7 +97,7 @@ public TabbedDemo() {
9297
footer = new HorizontalLayout();
9398
footer.setWidthFull();
9499
footer.setJustifyContentMode(JustifyContentMode.END);
95-
footer.add(codeCB, orientationCB, themeCB);
100+
footer.add(codeCB, codePositionCB, orientationCB, themeCB);
96101
footer.setClassName("demo-footer");
97102

98103
Package pkg = this.getClass().getPackage();
@@ -242,6 +247,8 @@ private Optional<SourceCodeTab> createSourceCodeTab(Class<?> annotatedClass, Dem
242247
if (!annotation.language().equals(DemoSource.DEFAULT_VALUE)) {
243248
builder.language(annotation.caption());
244249
}
250+
251+
builder.sourcePosition(annotation.sourcePosition());
245252

246253
return Optional.of(builder.build());
247254
}
@@ -264,13 +271,25 @@ public void removeRouterLayoutContent(HasElement oldContent) {
264271

265272
private void updateSplitterPosition() {
266273
if (currentLayout != null) {
267-
currentLayout.setSplitterPosition(codeCB.getValue() ? 50 : 100);
274+
if (codeCB.getValue()) {
275+
currentLayout.showSourceCode();
276+
} else {
277+
currentLayout.hideSourceCode();
278+
}
268279
orientationCB.setEnabled(codeCB.getValue());
280+
codePositionCB.setEnabled(codeCB.getValue());
269281
}
270282
}
271283

272284
public void setSourceVisible(boolean visible) {
273285
codeCB.setValue(visible);
286+
codePositionCB.setVisible(visible);
287+
}
288+
289+
public void toggleSourcePosition() {
290+
if (currentLayout != null) {
291+
currentLayout.toggleSourcePosition();
292+
}
274293
}
275294

276295
private void toggleSplitterOrientation() {
@@ -307,6 +326,7 @@ private void updateFooterButtonsVisibility() {
307326
ComponentUtil.fireEvent(this, new TabbedDemoSourceEvent(this, hasSourceCode));
308327
orientationCB.setVisible(hasSourceCode);
309328
codeCB.setVisible(hasSourceCode);
329+
codePositionCB.setVisible(hasSourceCode);
310330
}
311331

312332
public void addTabbedDemoSourceListener(ComponentEventListener<TabbedDemoSourceEvent> listener) {
@@ -321,6 +341,7 @@ protected void onAttach(AttachEvent attachEvent) {
321341
getUI().ifPresent(ui -> ui.getPage().retrieveExtendedClientDetails(receiver -> {
322342
boolean mobile = receiver.getBodyClientWidth() <= MOBILE_DEVICE_BREAKPOINT_WIDTH;
323343
codeCB.setValue(codeCB.getValue() && !mobile);
344+
codePositionCB.setValue(codeCB.getValue() && !mobile);
324345

325346
boolean portraitOrientation = receiver.getBodyClientHeight() > receiver.getBodyClientWidth();
326347
adjustSplitOrientation(portraitOrientation);

0 commit comments

Comments
 (0)