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.

TestForStyledUpload.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package com.vaadin.tests;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9. import java.lang.management.ManagementFactory;
  10. import java.lang.management.MemoryMXBean;
  11. import com.vaadin.server.LegacyApplication;
  12. import com.vaadin.server.StreamResource;
  13. import com.vaadin.shared.ui.ContentMode;
  14. import com.vaadin.ui.Button;
  15. import com.vaadin.ui.Label;
  16. import com.vaadin.ui.Layout;
  17. import com.vaadin.ui.LegacyWindow;
  18. import com.vaadin.ui.Link;
  19. import com.vaadin.ui.Panel;
  20. import com.vaadin.ui.Upload;
  21. import com.vaadin.ui.Upload.FailedEvent;
  22. import com.vaadin.ui.Upload.FailedListener;
  23. import com.vaadin.ui.Upload.FinishedEvent;
  24. import com.vaadin.ui.Upload.StartedEvent;
  25. import com.vaadin.ui.Upload.StartedListener;
  26. import com.vaadin.ui.Upload.SucceededEvent;
  27. import com.vaadin.ui.Upload.SucceededListener;
  28. import com.vaadin.ui.VerticalLayout;
  29. import com.vaadin.v7.ui.ProgressIndicator;
  30. public class TestForStyledUpload extends LegacyApplication
  31. implements Upload.FinishedListener, FailedListener, SucceededListener,
  32. StartedListener {
  33. Layout main = new VerticalLayout();
  34. TmpFileBuffer buffer = new TmpFileBuffer();
  35. VerticalLayout statusLayout = new VerticalLayout();
  36. Panel status = new Panel("Uploaded file:", statusLayout);
  37. private final Upload up;
  38. private final Label l;
  39. private final Label transferred = new Label("");
  40. private final ProgressIndicator pi = new ProgressIndicator();
  41. private final Label memoryStatus;
  42. public TestForStyledUpload() {
  43. main.addComponent(new Label(
  44. "Clicking on button b updates information about upload components status or same with garbage collector."));
  45. up = new Upload(null, buffer);
  46. up.setButtonCaption("Select file");
  47. up.setImmediateMode(true);
  48. up.addFinishedListener(this);
  49. up.addFailedListener(this);
  50. up.addSucceededListener(this);
  51. up.addStartedListener(this);
  52. up.addProgressListener((readBytes, contentLenght) -> {
  53. pi.setValue(new Float(readBytes / (float) contentLenght));
  54. refreshMemUsage();
  55. transferred.setValue(
  56. "Transferred " + readBytes + " of " + contentLenght);
  57. });
  58. final Button b = new Button("Update status", event -> readState());
  59. final Button c = new Button("Update status with gc", evenet -> gc());
  60. main.addComponent(up);
  61. l = new Label("Idle");
  62. main.addComponent(l);
  63. pi.setVisible(false);
  64. pi.setPollingInterval(300);
  65. main.addComponent(pi);
  66. main.addComponent(transferred);
  67. memoryStatus = new Label();
  68. main.addComponent(memoryStatus);
  69. statusLayout.setMargin(true);
  70. status.setVisible(false);
  71. main.addComponent(status);
  72. Button cancel = new Button("Cancel current upload");
  73. cancel.addClickListener(event -> buffer.cancel());
  74. main.addComponent(cancel);
  75. final Button restart = new Button("Restart demo application");
  76. restart.addClickListener(event -> TestForStyledUpload.this.close());
  77. main.addComponent(restart);
  78. main.addComponent(b);
  79. main.addComponent(c);
  80. }
  81. public void gc() {
  82. Runtime.getRuntime().gc();
  83. readState();
  84. }
  85. public void readState() {
  86. final StringBuilder sb = new StringBuilder();
  87. if (up.isUploading()) {
  88. sb.append("Uploading...");
  89. sb.append(up.getBytesRead());
  90. sb.append('/');
  91. sb.append(up.getUploadSize());
  92. sb.append(' ');
  93. sb.append(Math.round(
  94. 100 * up.getBytesRead() / (double) up.getUploadSize()));
  95. sb.append('%');
  96. } else {
  97. sb.append("Idle");
  98. }
  99. l.setValue(sb.toString());
  100. refreshMemUsage();
  101. }
  102. @Override
  103. public void uploadFinished(FinishedEvent event) {
  104. statusLayout.removeAllComponents();
  105. final InputStream stream = buffer.getStream();
  106. if (stream == null) {
  107. statusLayout.addComponent(
  108. new Label("Upload finished, but output buffer is null!!"));
  109. } else {
  110. statusLayout.addComponent(new Label(
  111. "<b>Name:</b> " + event.getFilename(), ContentMode.HTML));
  112. statusLayout.addComponent(
  113. new Label("<b>Mimetype:</b> " + event.getMIMEType(),
  114. ContentMode.HTML));
  115. statusLayout.addComponent(
  116. new Label("<b>Size:</b> " + event.getLength() + " bytes.",
  117. ContentMode.HTML));
  118. statusLayout
  119. .addComponent(new Link("Download " + buffer.getFileName(),
  120. new StreamResource(buffer, buffer.getFileName())));
  121. status.setVisible(true);
  122. }
  123. }
  124. public interface Buffer
  125. extends StreamResource.StreamSource, Upload.Receiver {
  126. String getFileName();
  127. }
  128. public class TmpFileBuffer implements Buffer {
  129. String mimeType;
  130. String fileName;
  131. private File file;
  132. private FileInputStream stream;
  133. public TmpFileBuffer() {
  134. final String tempFileName = "upload_tmpfile_"
  135. + System.currentTimeMillis();
  136. try {
  137. file = File.createTempFile(tempFileName, null);
  138. } catch (final IOException e) {
  139. e.printStackTrace();
  140. }
  141. }
  142. public void cancel() {
  143. up.interruptUpload();
  144. }
  145. @Override
  146. public InputStream getStream() {
  147. if (file == null) {
  148. return null;
  149. }
  150. try {
  151. stream = new FileInputStream(file);
  152. return stream;
  153. } catch (final FileNotFoundException e) {
  154. e.printStackTrace();
  155. }
  156. return null;
  157. }
  158. /**
  159. * @see com.vaadin.ui.Upload.Receiver#receiveUpload(String, String)
  160. */
  161. @Override
  162. public OutputStream receiveUpload(String filename, String MIMEType) {
  163. fileName = filename;
  164. mimeType = MIMEType;
  165. try {
  166. return new FileOutputStream(file);
  167. } catch (final FileNotFoundException e) {
  168. e.printStackTrace();
  169. }
  170. return null;
  171. }
  172. /**
  173. * Returns the fileName.
  174. *
  175. * @return String
  176. */
  177. @Override
  178. public String getFileName() {
  179. return fileName;
  180. }
  181. /**
  182. * Returns the mimeType.
  183. *
  184. * @return String
  185. */
  186. public String getMimeType() {
  187. return mimeType;
  188. }
  189. }
  190. @Override
  191. public void uploadFailed(FailedEvent event) {
  192. pi.setVisible(false);
  193. l.setValue("Upload was interrupted");
  194. }
  195. @Override
  196. public void uploadSucceeded(SucceededEvent event) {
  197. pi.setVisible(false);
  198. l.setValue("Finished upload, idle");
  199. System.out.println(event);
  200. }
  201. private void refreshMemUsage() {
  202. // memoryStatus.setValue("Not available in Java 1.4");
  203. StringBuilder mem = new StringBuilder();
  204. MemoryMXBean mmBean = ManagementFactory.getMemoryMXBean();
  205. mem.append("Heap (M):");
  206. mem.append(mmBean.getHeapMemoryUsage().getUsed() / 1048576);
  207. mem.append(" | Non-Heap (M):");
  208. mem.append(mmBean.getNonHeapMemoryUsage().getUsed() / 1048576);
  209. memoryStatus.setValue(mem.toString());
  210. }
  211. @Override
  212. public void uploadStarted(StartedEvent event) {
  213. pi.setVisible(true);
  214. l.setValue("Started uploading file " + event.getFilename());
  215. }
  216. @Override
  217. public void init() {
  218. LegacyWindow w = new LegacyWindow();
  219. setTheme("runo");
  220. w.addComponent(main);
  221. setMainWindow(w);
  222. }
  223. }