diff options
Diffstat (limited to 'server/src/main/java')
-rw-r--r-- | server/src/main/java/com/vaadin/event/dnd/FileDropEvent.java | 15 | ||||
-rw-r--r-- | server/src/main/java/com/vaadin/ui/FileDropTarget.java | 38 |
2 files changed, 35 insertions, 18 deletions
diff --git a/server/src/main/java/com/vaadin/event/dnd/FileDropEvent.java b/server/src/main/java/com/vaadin/event/dnd/FileDropEvent.java index 42d270ebe4..7ad5f410bf 100644 --- a/server/src/main/java/com/vaadin/event/dnd/FileDropEvent.java +++ b/server/src/main/java/com/vaadin/event/dnd/FileDropEvent.java @@ -15,6 +15,7 @@ */ package com.vaadin.event.dnd; +import java.util.Collection; import java.util.List; import com.vaadin.ui.AbstractComponent; @@ -22,7 +23,7 @@ import com.vaadin.ui.Component; import com.vaadin.ui.Html5File; /** - * File drop event that contains the list of files dropped on a file drop + * File drop event that contains the collection of files dropped on a file drop * target. * * @param <T> @@ -34,7 +35,7 @@ import com.vaadin.ui.Html5File; public class FileDropEvent<T extends AbstractComponent> extends Component.Event { - private final List<Html5File> files; + private final Collection<Html5File> files; /** * Creates a file drop event. @@ -42,21 +43,21 @@ public class FileDropEvent<T extends AbstractComponent> extends * @param target * The file drop target component. * @param files - * List of files. + * Collection of files. */ - public FileDropEvent(T target, List<Html5File> files) { + public FileDropEvent(T target, Collection<Html5File> files) { super(target); this.files = files; } /** - * Gets the list of files dropped onto the file drop target component. + * Gets the collection of files dropped onto the file drop target component. * - * @return List of files that were dropped onto the file drop target + * @return Collection of files that were dropped onto the file drop target * component. */ - public List<Html5File> getFiles() { + public Collection<Html5File> getFiles() { return files; } } diff --git a/server/src/main/java/com/vaadin/ui/FileDropTarget.java b/server/src/main/java/com/vaadin/ui/FileDropTarget.java index 831d6723d9..88d2550691 100644 --- a/server/src/main/java/com/vaadin/ui/FileDropTarget.java +++ b/server/src/main/java/com/vaadin/ui/FileDropTarget.java @@ -16,9 +16,7 @@ package com.vaadin.ui; import java.io.OutputStream; -import java.util.ArrayList; import java.util.HashMap; -import java.util.List; import java.util.Map; import com.vaadin.event.dnd.DropTargetExtension; @@ -72,22 +70,40 @@ public class FileDropTarget<T extends AbstractComponent> extends registerRpc(new FileDropTargetRpc() { @Override public void drop(Map<String, FileParameters> fileParams) { - List<Html5File> files = new ArrayList<>(); + Map<String, Html5File> files = new HashMap<>(); Map<String, String> urls = new HashMap<>(); + // Create a collection of html5 files fileParams.forEach((id, fileParameters) -> { - Html5File html5File = new Html5File(fileParameters.getName(), - fileParameters.getSize(), fileParameters.getMime()); - String url = createUrl(html5File, id); + Html5File html5File = new Html5File( + fileParameters.getName(), fileParameters.getSize(), + fileParameters.getMime()); + files.put(id, html5File); + }); + + // Call drop handler with the collection of dropped files + FileDropEvent<T> event = new FileDropEvent<>(target, + files.values()); + fileDropHandler.drop(event); + + // Create upload URLs for the files that the drop handler + // attached stream variable to + files.entrySet().stream() + .filter(entry -> entry.getValue().getStreamVariable() + != null).forEach(entry -> { + String id = entry.getKey(); + Html5File file = entry.getValue(); - files.add(html5File); + String url = createUrl(file, id); urls.put(id, url); }); - getRpcProxy(FileDropTargetClientRpc.class).sendUploadUrl(urls); - - FileDropEvent<T> event = new FileDropEvent<>(target, files); - fileDropHandler.drop(event); + // Send upload URLs to the client if there are files to be + // uploaded + if (urls.size() > 0) { + getRpcProxy(FileDropTargetClientRpc.class) + .sendUploadUrl(urls); + } } @Override |