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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 StringBuffer sb = new StringBuffer();
  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. // TODO Auto-generated catch block
  181. e.printStackTrace();
  182. }
  183. }
  184. public void cancel() {
  185. up.interruptUpload();
  186. }
  187. @Override
  188. public InputStream getStream() {
  189. if (file == null) {
  190. return null;
  191. }
  192. try {
  193. stream = new FileInputStream(file);
  194. return stream;
  195. } catch (final FileNotFoundException e) {
  196. // TODO Auto-generated catch block
  197. e.printStackTrace();
  198. }
  199. return null;
  200. }
  201. /**
  202. * @see com.vaadin.ui.Upload.Receiver#receiveUpload(String, String)
  203. */
  204. @Override
  205. public OutputStream receiveUpload(String filename, String MIMEType) {
  206. fileName = filename;
  207. mimeType = MIMEType;
  208. try {
  209. return new FileOutputStream(file);
  210. } catch (final FileNotFoundException e) {
  211. // TODO Auto-generated catch block
  212. e.printStackTrace();
  213. }
  214. return null;
  215. }
  216. /**
  217. * Returns the fileName.
  218. *
  219. * @return String
  220. */
  221. @Override
  222. public String getFileName() {
  223. return fileName;
  224. }
  225. /**
  226. * Returns the mimeType.
  227. *
  228. * @return String
  229. */
  230. public String getMimeType() {
  231. return mimeType;
  232. }
  233. }
  234. @Override
  235. public void uploadFailed(FailedEvent event) {
  236. pi.setVisible(false);
  237. l.setValue("Upload was interrupted");
  238. }
  239. @Override
  240. public void uploadSucceeded(SucceededEvent event) {
  241. pi.setVisible(false);
  242. l.setValue("Finished upload, idle");
  243. System.out.println(event);
  244. }
  245. private void refreshMemUsage() {
  246. // memoryStatus.setValue("Not available in Java 1.4");
  247. StringBuffer mem = new StringBuffer();
  248. MemoryMXBean mmBean = ManagementFactory.getMemoryMXBean();
  249. mem.append("Heap (M):");
  250. mem.append(mmBean.getHeapMemoryUsage().getUsed() / 1048576);
  251. mem.append(" | Non-Heap (M):");
  252. mem.append(mmBean.getNonHeapMemoryUsage().getUsed() / 1048576);
  253. memoryStatus.setValue(mem.toString());
  254. }
  255. @Override
  256. public void uploadStarted(StartedEvent event) {
  257. pi.setVisible(true);
  258. l.setValue("Started uploading file " + event.getFilename());
  259. }
  260. @Override
  261. public void init() {
  262. LegacyWindow w = new LegacyWindow();
  263. setTheme("runo");
  264. w.addComponent(main);
  265. setMainWindow(w);
  266. }
  267. }