Skip to content

Commit

Permalink
#214 seems done (#215)
Browse files Browse the repository at this point in the history
demo page now caches generated components
  • Loading branch information
vaadin-miki authored Aug 24, 2020
1 parent 92f74a1 commit 8e1fc7b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.vaadin.miki.superfields.text.SuperTextField;
import org.vaadin.miki.superfields.unload.UnloadObserver;

import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Collection;
Expand All @@ -71,7 +72,7 @@
* @author miki
* @since 2020-07-04
*/
public final class DemoComponentFactory {
public final class DemoComponentFactory implements Serializable {

private static final int NOTIFICATION_TIME = 1500;

Expand Down
24 changes: 17 additions & 7 deletions demo-v14/src/main/java/org/vaadin/miki/DemoPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import com.vaadin.flow.router.HasUrlParameter;
import com.vaadin.flow.router.Route;

import java.util.HashMap;
import java.util.Map;

/**
* Page that shows a demo of a component.
* @author miki
Expand All @@ -18,24 +21,31 @@ public class DemoPage extends VerticalLayout implements HasUrlParameter<String>,

private final DemoComponentFactory demoComponentFactory = DemoComponentFactory.get();

private final Map<String, Component> pages = new HashMap<>();

private Class<? extends Component> componentType;

@Override
public void setParameter(BeforeEvent event, String parameter) {
this.removeAll();
this.demoComponentFactory.getDemoableComponentTypes().stream()
.filter(type -> type.getSimpleName().equalsIgnoreCase(parameter))
.findFirst().ifPresentOrElse(this::buildDemoPageFor, this::buildErrorPage);

this.add(this.pages.computeIfAbsent(parameter, s ->
this.demoComponentFactory.getDemoableComponentTypes().stream()
.filter(type -> type.getSimpleName().equalsIgnoreCase(s))
.findFirst()
.map(this::buildDemoPageFor)
.orElseGet(this::buildErrorPage)
));
}

private void buildDemoPageFor(Class<? extends Component> type) {
private Component buildDemoPageFor(Class<? extends Component> type) {
this.componentType = type;
this.add(this.demoComponentFactory.buildDemoPageFor(type));
return this.demoComponentFactory.buildDemoPageFor(type);
}

private void buildErrorPage() {
private Component buildErrorPage() {
this.componentType = null;
this.add(new Span("You are seeing this because there was a problem in navigating to the demo page for your selected component."));
return new Span("You are seeing this because there was a problem in navigating to the demo page for your selected component.");
}

@Override
Expand Down

0 comments on commit 8e1fc7b

Please sign in to comment.