Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

TestForStyledUpload.java 8.6KB

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