Skip to content

Commit

Permalink
use StreamSource directly as content generator
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollovati committed Aug 19, 2016
1 parent a0c1574 commit 1a79420
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 65 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,15 @@ public EnhancedBrowserWindowOpener popupBlockerWorkaround(boolean active) {
* Sets a {@code resource} for this instance whose content will be generated
* when the window will be opened.
*
* The content will be generated through {@code generator} instance.
* The content will be generated through {@code generator} instance and the cache will be disabled.
*
* @param filename The filename for the generated resource.
* @param generator The content generator.
* @return current object for further customization
*/
public EnhancedBrowserWindowOpener withGeneratedContent(String filename, DynamicContentGenerator generator) {
public EnhancedBrowserWindowOpener withGeneratedContent(String filename, StreamResource.StreamSource generator) {
return withGeneratedContent(filename, generator, r -> {
r.setCacheTime(0);
});
}

Expand All @@ -185,14 +186,14 @@ public EnhancedBrowserWindowOpener withGeneratedContent(String filename, Dynamic
* @param resourceCustomizer The {@link StreamResource} customizer
* @return current object for further customization
*/
public EnhancedBrowserWindowOpener withGeneratedContent(String filename, DynamicContentGenerator generator,
public EnhancedBrowserWindowOpener withGeneratedContent(String filename, StreamResource.StreamSource generator,
StreamResourceCustomizer resourceCustomizer) {
Objects.requireNonNull(generator);
Objects.requireNonNull(resourceCustomizer);
StreamResource resource = new StreamResource(null, filename) {
@Override
public StreamSource getStreamSource() {
return generator.streamSource();
public StreamResource.StreamSource getStreamSource() {
return generator::getStream;
}
};
resourceCustomizer.customize(resource);
Expand Down
11 changes: 11 additions & 0 deletions enhanced-window-opener-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.oblac</groupId>
<artifactId>nomen-est-omen</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.8</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,37 @@
import com.vaadin.annotations.Title;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.annotations.Viewport;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.server.ClassResource;
import com.vaadin.server.Resource;
import com.vaadin.server.StreamResource;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.shared.ui.MultiSelectMode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.Layout;
import com.vaadin.ui.Link;
import com.vaadin.ui.Table;
import com.vaadin.ui.UI;
import com.vaadin.ui.themes.ValoTheme;
import org.jsoup.safety.Whitelist;
import org.vaadin.addon.ewopener.EnhancedBrowserWindowOpener;
import org.vaadin.viritin.fields.MTable;
import org.vaadin.viritin.label.MLabel;
import org.vaadin.viritin.label.RichText;
import org.vaadin.viritin.layouts.MCssLayout;
import org.vaadin.viritin.layouts.MHorizontalLayout;
import org.vaadin.viritin.layouts.MVerticalLayout;

import javax.servlet.annotation.WebServlet;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.Scanner;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicInteger;
Expand All @@ -62,6 +68,7 @@ public static class Servlet extends VaadinServlet {
}

private final AtomicInteger downloadCounter = new AtomicInteger(0);
private Table table;

@Override
protected void init(VaadinRequest request) {
Expand All @@ -78,7 +85,7 @@ protected void init(VaadinRequest request) {
.popupBlockerWorkaround(true);
Button button4 = new Button("Nothing to open here");
button4.addClickListener(e -> {
opener4.open((Resource)null);
opener4.open((Resource) null);
});
opener4.extend(button4);

Expand All @@ -92,20 +99,20 @@ protected void init(VaadinRequest request) {
Button button3 = new Button("Click me");
EnhancedBrowserWindowOpener opener3 = new EnhancedBrowserWindowOpener()
.popupBlockerWorkaround(true)
.withGeneratedContent("myFileName.txt", this::makeStreamSource)
.withGeneratedContent("myFileName.txt", this::generateContent)
.doExtend(button3);
button3.addClickListener(opener3::open);

Link link = new Link("Click me", null);
new EnhancedBrowserWindowOpener()
.clientSide(true)
.withGeneratedContent("myFileName.txt", this::makeStreamSource)
.withGeneratedContent("myFileName.txt", this::generateContent)
.doExtend(link);

Link link2 = new Link("Click me", null);
new EnhancedBrowserWindowOpener()
.clientSide(true)
.withGeneratedContent("myFileName.txt", this::makeStreamSource,
.withGeneratedContent("myFileName.txt", this::generateContent,
resource -> {
resource.setCacheTime(0);
resource.setFilename("runtimeFileName-" + Instant.now().getEpochSecond() + ".txt");
Expand All @@ -119,28 +126,40 @@ protected void init(VaadinRequest request) {
CompletableFuture.runAsync(this::doSomeLongProcessing)
.thenRun(() -> getUI().access(opener5::open));

table = new Table("Select items to download", new BeanItemContainer<>(DummyService.Person.class, DummyService.data()));
table.setImmediate(true);
table.setVisibleColumns("name", "age");
table.setColumnHeaders("Name", "Age");
table.setWidth("100%");
table.setPageLength(20);
table.setMultiSelectMode(MultiSelectMode.DEFAULT);
table.setMultiSelect(true);
table.setSelectable(true);


// Show it in the middle of the screen
final Layout layout = new MVerticalLayout(
new MLabel("Enhanced Window Opener Demo")
.withStyleName(ValoTheme.LABEL_COLORED, ValoTheme.LABEL_H1),
new MCssLayout(
new MVerticalLayout(readMarkdown("code1.md"), button1)
.alignAll(Alignment.MIDDLE_CENTER).withWidthUndefined()
.withMargin(false),
new MVerticalLayout(readMarkdown("code2.md"), button2)
.alignAll(Alignment.MIDDLE_CENTER).withWidthUndefined().withMargin(false),
new MVerticalLayout(readMarkdown("code7.md"), button3)
.alignAll(Alignment.MIDDLE_CENTER).withWidthUndefined().withMargin(false),
new MVerticalLayout(readMarkdown("code5.md"), link)
.alignAll(Alignment.MIDDLE_CENTER).withWidthUndefined().withMargin(false),
new MVerticalLayout(readMarkdown("code6.md"), link2)
.alignAll(Alignment.MIDDLE_CENTER).withWidthUndefined().withMargin(false),
new MVerticalLayout(readMarkdown("code3.md"), button4)
.alignAll(Alignment.MIDDLE_CENTER).withWidthUndefined().withMargin(false),
new MVerticalLayout(readMarkdown("code4.md"), hiddenComponent)
.alignAll(Alignment.MIDDLE_CENTER).withWidthUndefined().withMargin(false)
).withFullWidth().withStyleName("demo-samples")
new MHorizontalLayout().add(table, 1).add(
new MCssLayout(
new MVerticalLayout(readMarkdown("code1.md"), button1)
.alignAll(Alignment.MIDDLE_CENTER).withWidthUndefined()
.withMargin(false),
new MVerticalLayout(readMarkdown("code2.md"), button2)
.alignAll(Alignment.MIDDLE_CENTER).withWidthUndefined().withMargin(false),
new MVerticalLayout(readMarkdown("code7.md"), button3)
.alignAll(Alignment.MIDDLE_CENTER).withWidthUndefined().withMargin(false),
new MVerticalLayout(readMarkdown("code5.md"), link)
.alignAll(Alignment.MIDDLE_CENTER).withWidthUndefined().withMargin(false),
new MVerticalLayout(readMarkdown("code6.md"), link2)
.alignAll(Alignment.MIDDLE_CENTER).withWidthUndefined().withMargin(false),
new MVerticalLayout(readMarkdown("code3.md"), button4)
.alignAll(Alignment.MIDDLE_CENTER).withWidthUndefined().withMargin(false),
new MVerticalLayout(readMarkdown("code4.md"), hiddenComponent)
.alignAll(Alignment.MIDDLE_CENTER).withWidthUndefined().withMargin(false)
).withFullWidth().withStyleName("demo-samples")
, 5).withFullWidth()
).withFullWidth().withMargin(true);

setContent(layout);
Expand All @@ -162,12 +181,20 @@ private StreamResource generateResource() {
return streamResource;
}

@SuppressWarnings("unchecked")
private InputStream generateContent() {
StringBuilder content = new StringBuilder()
.append(String.format("File %d downloaded at %s", downloadCounter.incrementAndGet(),
DateTimeFormatter.ISO_DATE_TIME.format(LocalDateTime.now())));
Collection<DummyService.Person> data = (Collection<DummyService.Person>)table.getValue();
data.stream().map(DummyService.Person::toString)
.peek(s -> content.append(System.lineSeparator()))
.forEach(content::append);
return new ByteArrayInputStream(content.toString().getBytes());
}

private StreamResource.StreamSource makeStreamSource() {
return () -> {
String content = String.format("File %d downloaded at %s", downloadCounter.incrementAndGet(),
DateTimeFormatter.ISO_DATE_TIME.format(LocalDateTime.now()));
return new ByteArrayInputStream(content.getBytes());
};
return this::generateContent;
}

private static RichText readMarkdown(String markdown) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.vaadin.addon.ewopener.demo;

import com.oblac.nomen.Nomen;
import lombok.Data;

import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class DummyService {

private static Random random = new Random();
private static final List<Person> data;

static {
data = IntStream.range(0, 20)
.mapToObj(i -> new Person(name(), random.nextInt(80) + 20))
.collect(Collectors.toList());
}

public static List<Person> data() {
return Collections.unmodifiableList(data);
}

private static String name() {
return Nomen.est().superb().separator(" ").space(" ").person().get();
}


@Data
public static class Person {
private final String name;
private final int age;
}
}

0 comments on commit 1a79420

Please sign in to comment.