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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Copyright 2000-2014 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.label.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.ProgressIndicator;
  37. import com.vaadin.ui.Upload;
  38. import com.vaadin.ui.Upload.FailedEvent;
  39. import com.vaadin.ui.Upload.FailedListener;
  40. import com.vaadin.ui.Upload.FinishedEvent;
  41. import com.vaadin.ui.Upload.FinishedListener;
  42. import com.vaadin.ui.Upload.StartedEvent;
  43. import com.vaadin.ui.Upload.StartedListener;
  44. import com.vaadin.ui.Upload.SucceededEvent;
  45. import com.vaadin.ui.Upload.SucceededListener;
  46. import com.vaadin.ui.VerticalLayout;
  47. public class TestForStyledUpload extends LegacyApplication implements
  48. Upload.FinishedListener, FailedListener, SucceededListener,
  49. StartedListener {
  50. Layout main = new VerticalLayout();
  51. TmpFileBuffer buffer = new TmpFileBuffer();
  52. VerticalLayout statusLayout = new VerticalLayout();
  53. Panel status = new Panel("Uploaded file:", statusLayout);
  54. private final Upload up;
  55. private final Label l;
  56. private final Label transferred = new Label("");
  57. private final ProgressIndicator pi = new ProgressIndicator();
  58. private final Label memoryStatus;
  59. public TestForStyledUpload() {
  60. main.addComponent(new Label(
  61. "Clicking on button b updates information about upload components status or same with garbage collector."));
  62. up = new Upload(null, buffer);
  63. up.setButtonCaption("Select file");
  64. up.setImmediate(true);
  65. up.addListener((FinishedListener) this);
  66. up.addListener((FailedListener) this);
  67. up.addListener((SucceededListener) this);
  68. up.addListener((StartedListener) this);
  69. up.addListener(new Upload.ProgressListener() {
  70. @Override
  71. public void updateProgress(long readBytes, long contentLenght) {
  72. pi.setValue(new Float(readBytes / (float) contentLenght));
  73. refreshMemUsage();
  74. transferred.setValue("Transferred " + readBytes + " of "
  75. + contentLenght);
  76. }
  77. });
  78. final Button b = new Button("Update status",
  79. new Button.ClickListener() {
  80. @Override
  81. public void buttonClick(ClickEvent event) {
  82. readState();
  83. }
  84. });
  85. final Button c = new Button("Update status with gc",
  86. new Button.ClickListener() {
  87. @Override
  88. public void buttonClick(ClickEvent event) {
  89. gc();
  90. }
  91. });
  92. main.addComponent(up);
  93. l = new Label("Idle");
  94. main.addComponent(l);
  95. pi.setVisible(false);
  96. pi.setPollingInterval(300);
  97. main.addComponent(pi);
  98. main.addComponent(transferred);
  99. memoryStatus = new Label();
  100. main.addComponent(memoryStatus);
  101. statusLayout.setMargin(true);
  102. status.setVisible(false);
  103. main.addComponent(status);
  104. Button cancel = new Button("Cancel current upload");
  105. cancel.addListener(new Button.ClickListener() {
  106. @Override
  107. public void buttonClick(ClickEvent event) {
  108. buffer.cancel();
  109. }
  110. });
  111. main.addComponent(cancel);
  112. final Button restart = new Button("Restart demo application");
  113. restart.addListener(new Button.ClickListener() {
  114. @Override
  115. public void buttonClick(ClickEvent event) {
  116. TestForStyledUpload.this.close();
  117. }
  118. });
  119. main.addComponent(restart);
  120. main.addComponent(b);
  121. main.addComponent(c);
  122. }
  123. public void gc() {
  124. Runtime.getRuntime().gc();
  125. readState();
  126. }
  127. public void readState() {
  128. final StringBuffer sb = new StringBuffer();
  129. if (up.isUploading()) {
  130. sb.append("Uploading...");
  131. sb.append(up.getBytesRead());
  132. sb.append("/");
  133. sb.append(up.getUploadSize());
  134. sb.append(" ");
  135. sb.append(Math.round(100 * up.getBytesRead()
  136. / (double) up.getUploadSize()));
  137. sb.append("%");
  138. } else {
  139. sb.append("Idle");
  140. }
  141. l.setValue(sb.toString());
  142. refreshMemUsage();
  143. }
  144. @Override
  145. public void uploadFinished(FinishedEvent event) {
  146. statusLayout.removeAllComponents();
  147. final InputStream stream = buffer.getStream();
  148. if (stream == null) {
  149. statusLayout.addComponent(new Label(
  150. "Upload finished, but output buffer is null!!"));
  151. } else {
  152. statusLayout.addComponent(new Label("<b>Name:</b> "
  153. + event.getFilename(), ContentMode.HTML));
  154. statusLayout.addComponent(new Label("<b>Mimetype:</b> "
  155. + event.getMIMEType(), ContentMode.HTML));
  156. statusLayout.addComponent(new Label("<b>Size:</b> "
  157. + event.getLength() + " bytes.", ContentMode.HTML));
  158. statusLayout.addComponent(new Link("Download "
  159. + buffer.getFileName(), new StreamResource(buffer, buffer
  160. .getFileName())));
  161. status.setVisible(true);
  162. }
  163. }
  164. public interface Buffer extends StreamResource.StreamSource,
  165. Upload.Receiver {
  166. String getFileName();
  167. }
  168. public class TmpFileBuffer implements Buffer {
  169. String mimeType;
  170. String fileName;
  171. private File file;
  172. private FileInputStream stream;
  173. public TmpFileBuffer() {
  174. final String tempFileName = "upload_tmpfile_"
  175. + System.currentTimeMillis();
  176. try {
  177. file = File.createTempFile(tempFileName, null);
  178. } catch (final IOException e) {
  179. // TODO Auto-generated catch block
  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. // TODO Auto-generated catch block
  196. e.printStackTrace();
  197. }
  198. return null;
  199. }
  200. /**
  201. * @see com.vaadin.ui.Upload.Receiver#receiveUpload(String, String)
  202. */
  203. @Override
  204. public OutputStream receiveUpload(String filename, String MIMEType) {
  205. fileName = filename;
  206. mimeType = MIMEType;
  207. try {
  208. return new FileOutputStream(file);
  209. } catch (final FileNotFoundException e) {
  210. // TODO Auto-generated catch block
  211. e.printStackTrace();
  212. }
  213. return null;
  214. }
  215. /**
  216. * Returns the fileName.
  217. *
  218. * @return String
  219. */
  220. @Override
  221. public String getFileName() {
  222. return fileName;
  223. }
  224. /**
  225. * Returns the mimeType.
  226. *
  227. * @return String
  228. */
  229. public String getMimeType() {
  230. return mimeType;
  231. }
  232. }
  233. @Override
  234. public void uploadFailed(FailedEvent event) {
  235. pi.setVisible(false);
  236. l.setValue("Upload was interrupted");
  237. }
  238. @Override
  239. public void uploadSucceeded(SucceededEvent event) {
  240. pi.setVisible(false);
  241. l.setValue("Finished upload, idle");
  242. System.out.println(event);
  243. }
  244. private void refreshMemUsage() {
  245. // memoryStatus.setValue("Not available in Java 1.4");
  246. StringBuffer mem = new StringBuffer();
  247. MemoryMXBean mmBean = ManagementFactory.getMemoryMXBean();
  248. mem.append("Heap (M):");
  249. mem.append(mmBean.getHeapMemoryUsage().getUsed() / 1048576);
  250. mem.append(" | Non-Heap (M):");
  251. mem.append(mmBean.getNonHeapMemoryUsage().getUsed() / 1048576);
  252. memoryStatus.setValue(mem.toString());
  253. }
  254. @Override
  255. public void uploadStarted(StartedEvent event) {
  256. pi.setVisible(true);
  257. l.setValue("Started uploading file " + event.getFilename());
  258. }
  259. @Override
  260. public void init() {
  261. LegacyWindow w = new LegacyWindow();
  262. setTheme("runo");
  263. w.addComponent(main);
  264. setMainWindow(w);
  265. }
  266. }