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 9.4KB

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