Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support to change source code position #73

Merged
merged 2 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.flowingcode.vaadin.addons.demo</groupId>
<artifactId>commons-demo</artifactId>
<version>3.9.1-SNAPSHOT</version>
<version>3.10.0-SNAPSHOT</version>

<name>Commons Demo</name>
<description>Common classes for add-ons demo</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,7 @@
*/
String language() default DEFAULT_VALUE;

/** Source code position in the layout */
SourcePosition sourcePosition() default SourcePosition.SECONDARY;

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ public class MultiSourceCodeViewer extends Div {

private static final String DATA_URL = "source-url";
private static final String DATA_LANGUAGE = "source-language";
private static final String DATA_POSITION = "source-position";

private SourceCodeViewer codeViewer;
private Tab selectedTab;

public MultiSourceCodeViewer(List<SourceCodeTab> sourceCodeTabs, Map<String, String> properties) {

Tab tab;
if (sourceCodeTabs.size() > 1) {
Tabs tabs = new Tabs(createTabs(sourceCodeTabs));
tabs.addSelectedChangeListener(ev -> onTabSelected(ev.getSelectedTab()));
add(tabs);
tab = tabs.getSelectedTab();
selectedTab = tabs.getSelectedTab();
} else {
tab = createTab(sourceCodeTabs.get(0));
selectedTab = createTab(sourceCodeTabs.get(0));
}

String url = (String) ComponentUtil.getData(tab, DATA_URL);
String language = (String) ComponentUtil.getData(tab, DATA_LANGUAGE);
String url = (String) ComponentUtil.getData(selectedTab, DATA_URL);
String language = (String) ComponentUtil.getData(selectedTab, DATA_LANGUAGE);
codeViewer = new SourceCodeViewer(url, language, properties);

add(codeViewer);
Expand Down Expand Up @@ -74,6 +74,7 @@ private Tab createTab(SourceCodeTab sourceCodeTab) {
Tab tab = new Tab(caption);
ComponentUtil.setData(tab, DATA_URL, url);
ComponentUtil.setData(tab, DATA_LANGUAGE, language);
ComponentUtil.setData(tab, DATA_POSITION, sourceCodeTab.getSourcePosition());
return tab;
}

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

private void onTabSelected(Tab tab) {
this.selectedTab = tab;

String url = (String) ComponentUtil.getData(tab, DATA_URL);
String language = (String) ComponentUtil.getData(tab, DATA_LANGUAGE);
fetchContents(url, language);
Expand All @@ -97,4 +100,8 @@ private void fetchContents(String url, String language) {
codeViewer.fetchContents(url, language);
}

public SourcePosition getSourcePosition() {
return (SourcePosition) ComponentUtil.getData(selectedTab, DATA_POSITION);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ public class SourceCodeTab {
private final String url;
private String caption;
private String language;
@NonNull
private final SourcePosition sourcePosition;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.flowingcode.vaadin.addons.demo;

public enum SourcePosition {

PRIMARY, SECONDARY;

public SourcePosition toggle() {
switch (this) {
case SECONDARY:
return PRIMARY;
case PRIMARY:
default:
return SECONDARY;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import org.apache.commons.lang3.StringUtils;

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

private MultiSourceCodeViewer code;
private SourcePosition sourcePosition;

public SplitLayoutDemo(Component demo, String sourceUrl) {
this(demo, Arrays.asList(new SourceCodeTab(sourceUrl)));
public SplitLayoutDemo(Component demo, String sourceUrl, SourcePosition sourcePosition) {
this(demo, Arrays.asList(new SourceCodeTab(sourceUrl, sourcePosition)));
}

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

code = new MultiSourceCodeViewer(tabs, properties);
getContent().addToPrimary(demo);
getContent().addToSecondary(code);

this.sourcePosition = code.getSourcePosition();
switch (this.sourcePosition) {
case PRIMARY:
getContent().addToPrimary(code);
getContent().addToSecondary(demo);
break;
case SECONDARY:
default:
getContent().addToPrimary(demo);
getContent().addToSecondary(code);
}

getContent().setSizeFull();
}

public void switchSourcePosition(SourcePosition position) {
if (!this.sourcePosition.equals(position)) {
toggleSourcePosition();
}
}

public void toggleSourcePosition() {
Component primary = getContent().getPrimaryComponent();
Component secondary = getContent().getSecondaryComponent();
getContent().removeAll();
getContent().addToPrimary(secondary);
getContent().addToSecondary(primary);
this.sourcePosition = this.sourcePosition.toggle();
}

public void setOrientation(Orientation o) {
getContent().setOrientation(o);
getContent()
Expand All @@ -71,4 +101,20 @@ public void setSplitterPosition(int pos) {
public void setSizeFull() {
getContent().setSizeFull();
}

public void showSourceCode() {
getContent().setSplitterPosition(50);
}

public void hideSourceCode() {
switch (sourcePosition) {
case PRIMARY:
getContent().setSplitterPosition(0);
break;
case SECONDARY:
getContent().setSplitterPosition(100);
break;
}
}

}
25 changes: 23 additions & 2 deletions src/main/java/com/flowingcode/vaadin/addons/demo/TabbedDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class TabbedDemo extends VerticalLayout implements RouterLayout {
private SplitLayoutDemo currentLayout;
private Checkbox orientationCB;
private Checkbox codeCB;
private Checkbox codePositionCB;
private Checkbox themeCB;
private Orientation splitOrientation;
private Button helperButton;
Expand All @@ -83,6 +84,10 @@ public TabbedDemo() {
codeCB.setValue(true);
codeCB.addClassName("smallcheckbox");
codeCB.addValueChangeListener(ev -> updateSplitterPosition());
codePositionCB = new Checkbox("Toggle Code Position");
codePositionCB.setValue(true);
codePositionCB.addClassName("smallcheckbox");
codePositionCB.addValueChangeListener(ev -> toggleSourcePosition());
themeCB = new Checkbox("Dark Theme");
themeCB.setValue(false);
themeCB.addClassName("smallcheckbox");
Expand All @@ -92,7 +97,7 @@ public TabbedDemo() {
footer = new HorizontalLayout();
footer.setWidthFull();
footer.setJustifyContentMode(JustifyContentMode.END);
footer.add(codeCB, orientationCB, themeCB);
footer.add(codeCB, codePositionCB, orientationCB, themeCB);
footer.setClassName("demo-footer");

Package pkg = this.getClass().getPackage();
Expand Down Expand Up @@ -242,6 +247,8 @@ private Optional<SourceCodeTab> createSourceCodeTab(Class<?> annotatedClass, Dem
if (!annotation.language().equals(DemoSource.DEFAULT_VALUE)) {
builder.language(annotation.caption());
}

builder.sourcePosition(annotation.sourcePosition());

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

private void updateSplitterPosition() {
if (currentLayout != null) {
currentLayout.setSplitterPosition(codeCB.getValue() ? 50 : 100);
if (codeCB.getValue()) {
currentLayout.showSourceCode();
} else {
currentLayout.hideSourceCode();
}
orientationCB.setEnabled(codeCB.getValue());
codePositionCB.setEnabled(codeCB.getValue());
}
}

public void setSourceVisible(boolean visible) {
codeCB.setValue(visible);
codePositionCB.setVisible(visible);
}

public void toggleSourcePosition() {
if (currentLayout != null) {
currentLayout.toggleSourcePosition();
}
}

private void toggleSplitterOrientation() {
Expand Down Expand Up @@ -307,6 +326,7 @@ private void updateFooterButtonsVisibility() {
ComponentUtil.fireEvent(this, new TabbedDemoSourceEvent(this, hasSourceCode));
orientationCB.setVisible(hasSourceCode);
codeCB.setVisible(hasSourceCode);
codePositionCB.setVisible(hasSourceCode);
}

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

boolean portraitOrientation = receiver.getBodyClientHeight() > receiver.getBodyClientWidth();
adjustSplitOrientation(portraitOrientation);
Expand Down