You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Html5FileDragAndDropUpload.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package com.vaadin.tests.dnd;
  2. import java.io.IOException;
  3. import java.io.OutputStream;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.concurrent.atomic.AtomicBoolean;
  7. import com.vaadin.server.StreamVariable;
  8. import com.vaadin.server.VaadinRequest;
  9. import com.vaadin.shared.ui.dnd.FileParameters;
  10. import com.vaadin.shared.ui.grid.DropMode;
  11. import com.vaadin.tests.components.AbstractTestUIWithLog;
  12. import com.vaadin.ui.Button;
  13. import com.vaadin.ui.Grid;
  14. import com.vaadin.ui.Notification;
  15. import com.vaadin.ui.ProgressBar;
  16. import com.vaadin.ui.UI;
  17. import com.vaadin.ui.VerticalLayout;
  18. import com.vaadin.ui.components.grid.GridDropTarget;
  19. import com.vaadin.ui.dnd.FileDropTarget;
  20. public class Html5FileDragAndDropUpload extends AbstractTestUIWithLog {
  21. private static final int FILE_SIZE_LIMIT = 1024 * 1024 * 5; // 5 MB
  22. private final boolean pushManually;
  23. public Html5FileDragAndDropUpload() {
  24. this(false);
  25. }
  26. public Html5FileDragAndDropUpload(boolean pushManually) {
  27. this.pushManually = pushManually;
  28. }
  29. @Override
  30. protected void setup(VaadinRequest request) {
  31. Grid<FileParameters> grid = new Grid<>(
  32. "Drop files or text on the Grid");
  33. grid.addColumn(FileParameters::getName).setCaption("File name");
  34. grid.addColumn(FileParameters::getSize).setCaption("File size");
  35. grid.addColumn(FileParameters::getMime).setCaption("Mime type");
  36. List<FileParameters> gridItems = new ArrayList<>();
  37. AtomicBoolean cancelled = new AtomicBoolean(false);
  38. ProgressBar progressBar = new ProgressBar(0.0f);
  39. new FileDropTarget<Grid<FileParameters>>(grid,
  40. event -> event.getFiles().forEach(html5File -> {
  41. if (html5File.getFileSize() > FILE_SIZE_LIMIT) {
  42. Notification.show(html5File.getFileName()
  43. + " is too large (max 5 MB)");
  44. return;
  45. }
  46. UI.getCurrent().setPollInterval(200);
  47. html5File.setStreamVariable(new StreamVariable() {
  48. @Override
  49. public OutputStream getOutputStream() {
  50. return new OutputStream() {
  51. @Override
  52. public void write(int b) throws IOException {
  53. // NOP
  54. }
  55. };
  56. }
  57. @Override
  58. public boolean listenProgress() {
  59. return true;
  60. }
  61. @Override
  62. public void onProgress(StreamingProgressEvent event) {
  63. float progress = (float) event.getBytesReceived()
  64. / event.getContentLength();
  65. progressBar.setValue(progress);
  66. log("Progress, bytesReceived="
  67. + event.getBytesReceived());
  68. pushIfManual();
  69. }
  70. @Override
  71. public void streamingStarted(
  72. StreamingStartEvent event) {
  73. cancelled.set(false);
  74. progressBar.setValue(0.0f);
  75. log("Stream started, fileName="
  76. + event.getFileName());
  77. pushIfManual();
  78. }
  79. @Override
  80. public void streamingFinished(StreamingEndEvent event) {
  81. progressBar.setValue(1.0f);
  82. gridItems
  83. .add(new FileParameters(event.getFileName(),
  84. event.getContentLength(),
  85. event.getMimeType()));
  86. grid.setItems(gridItems);
  87. log("Stream finished, fileName="
  88. + event.getFileName());
  89. pushIfManual();
  90. UI.getCurrent().setPollInterval(-1);
  91. }
  92. @Override
  93. public void streamingFailed(StreamingErrorEvent event) {
  94. progressBar.setValue(0.0f);
  95. log("Stream failed, fileName="
  96. + event.getFileName());
  97. pushIfManual();
  98. UI.getCurrent().setPollInterval(-1);
  99. }
  100. @Override
  101. public boolean isInterrupted() {
  102. return cancelled.get();
  103. }
  104. });
  105. }));
  106. GridDropTarget<FileParameters> dropTarget = new GridDropTarget<>(grid,
  107. DropMode.ON_TOP);
  108. dropTarget.addGridDropListener(event -> {
  109. log("dataTransferText=" + event.getDataTransferText());
  110. Notification.show(event.getDataTransferText());
  111. });
  112. Button cancelButton = new Button("Cancel",
  113. click -> cancelled.set(true));
  114. VerticalLayout layout = new VerticalLayout(grid, cancelButton,
  115. progressBar);
  116. addComponent(layout);
  117. }
  118. private void pushIfManual() {
  119. if (pushManually) {
  120. push();
  121. }
  122. }
  123. @Override
  124. protected String getTestDescription() {
  125. return "Drop files onto the Grid to upload them or text";
  126. }
  127. }