From 1a79420132a8fd7d403ead631e237a54a9eac4ed Mon Sep 17 00:00:00 2001 From: Marco Collovati Date: Fri, 19 Aug 2016 22:03:42 +0200 Subject: [PATCH] use StreamSource directly as content generator --- .../ewopener/DynamicContentGenerator.java | 33 -------- .../ewopener/EnhancedBrowserWindowOpener.java | 11 +-- enhanced-window-opener-demo/pom.xml | 11 +++ .../vaadin/addon/ewopener/demo/DemoUI.java | 81 ++++++++++++------- .../addon/ewopener/demo/DummyService.java | 37 +++++++++ 5 files changed, 108 insertions(+), 65 deletions(-) delete mode 100644 enhanced-window-opener-addon/src/main/java/org/vaadin/addon/ewopener/DynamicContentGenerator.java create mode 100644 enhanced-window-opener-demo/src/main/java/org/vaadin/addon/ewopener/demo/DummyService.java diff --git a/enhanced-window-opener-addon/src/main/java/org/vaadin/addon/ewopener/DynamicContentGenerator.java b/enhanced-window-opener-addon/src/main/java/org/vaadin/addon/ewopener/DynamicContentGenerator.java deleted file mode 100644 index 3c17f27..0000000 --- a/enhanced-window-opener-addon/src/main/java/org/vaadin/addon/ewopener/DynamicContentGenerator.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (C) 2016 Marco Collovati (mcollovati@gmail.com) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.vaadin.addon.ewopener; - -import com.vaadin.server.StreamResource; - -/** - * Dynamic content generator. - * - * Provides dynamic filename and content of the downloadable resource. - */ -@FunctionalInterface -public interface DynamicContentGenerator { - /** - * Stream source for the downloadable resource. - * - * @return StreamSource for the downloadable resource - */ - StreamResource.StreamSource streamSource(); -} diff --git a/enhanced-window-opener-addon/src/main/java/org/vaadin/addon/ewopener/EnhancedBrowserWindowOpener.java b/enhanced-window-opener-addon/src/main/java/org/vaadin/addon/ewopener/EnhancedBrowserWindowOpener.java index 93c0dfd..7b2d179 100644 --- a/enhanced-window-opener-addon/src/main/java/org/vaadin/addon/ewopener/EnhancedBrowserWindowOpener.java +++ b/enhanced-window-opener-addon/src/main/java/org/vaadin/addon/ewopener/EnhancedBrowserWindowOpener.java @@ -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); }); } @@ -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); diff --git a/enhanced-window-opener-demo/pom.xml b/enhanced-window-opener-demo/pom.xml index 2f5e919..d369197 100644 --- a/enhanced-window-opener-demo/pom.xml +++ b/enhanced-window-opener-demo/pom.xml @@ -60,6 +60,17 @@ 3.0.1 provided + + com.oblac + nomen-est-omen + 1.2 + + + org.projectlombok + lombok + 1.16.8 + provided + diff --git a/enhanced-window-opener-demo/src/main/java/org/vaadin/addon/ewopener/demo/DemoUI.java b/enhanced-window-opener-demo/src/main/java/org/vaadin/addon/ewopener/demo/DemoUI.java index 8f5bca5..3379b15 100644 --- a/enhanced-window-opener-demo/src/main/java/org/vaadin/addon/ewopener/demo/DemoUI.java +++ b/enhanced-window-opener-demo/src/main/java/org/vaadin/addon/ewopener/demo/DemoUI.java @@ -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; @@ -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) { @@ -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); @@ -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"); @@ -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); @@ -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 data = (Collection)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) { diff --git a/enhanced-window-opener-demo/src/main/java/org/vaadin/addon/ewopener/demo/DummyService.java b/enhanced-window-opener-demo/src/main/java/org/vaadin/addon/ewopener/demo/DummyService.java new file mode 100644 index 0000000..9fcddff --- /dev/null +++ b/enhanced-window-opener-demo/src/main/java/org/vaadin/addon/ewopener/demo/DummyService.java @@ -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 data; + + static { + data = IntStream.range(0, 20) + .mapToObj(i -> new Person(name(), random.nextInt(80) + 20)) + .collect(Collectors.toList()); + } + + public static List 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; + } +}