Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* *************************************************************************
  2. IT Mill Toolkit
  3. Development of Browser User Interfaces Made Easy
  4. Copyright (C) 2000-2006 IT Mill Ltd
  5. *************************************************************************
  6. This product is distributed under commercial license that can be found
  7. from the product package on license.pdf. Use of this product might
  8. require purchasing a commercial license from IT Mill Ltd. For guidelines
  9. on usage, see licensing-guidelines.html
  10. *************************************************************************
  11. For more information, contact:
  12. IT Mill Ltd phone: +358 2 4802 7180
  13. Ruukinkatu 2-4 fax: +358 2 4802 7181
  14. 20540, Turku email: info@itmill.com
  15. Finland company www: www.itmill.com
  16. Primary source for information and releases: www.itmill.com
  17. ********************************************************************** */
  18. package com.itmill.toolkit.demo.features;
  19. import java.io.ByteArrayInputStream;
  20. import java.io.ByteArrayOutputStream;
  21. import java.io.InputStream;
  22. import java.io.OutputStream;
  23. import com.itmill.toolkit.terminal.StreamResource;
  24. import com.itmill.toolkit.ui.Component;
  25. import com.itmill.toolkit.ui.Label;
  26. import com.itmill.toolkit.ui.Link;
  27. import com.itmill.toolkit.ui.OrderedLayout;
  28. import com.itmill.toolkit.ui.Panel;
  29. import com.itmill.toolkit.ui.Upload;
  30. import com.itmill.toolkit.ui.Upload.FinishedEvent;
  31. public class FeatureUpload extends Feature implements Upload.FinishedListener {
  32. Buffer buffer = new Buffer();
  33. Panel status = new Panel("Uploaded file:");
  34. public FeatureUpload() {
  35. super();
  36. }
  37. protected Component getDemoComponent() {
  38. OrderedLayout l = new OrderedLayout();
  39. // Example panel
  40. Panel show = new Panel("Upload component");
  41. Upload up = new Upload("Upload a file:", buffer);
  42. up.addListener(this);
  43. show.addComponent(up);
  44. status.setVisible(false);
  45. l.addComponent(status);
  46. l.addComponent(show);
  47. // Properties
  48. propertyPanel = new PropertyPanel(up);
  49. return l;
  50. }
  51. protected String getExampleSrc() {
  52. return "Upload u = new Upload(\"Upload a file:\", uploadReceiver);\n\n"
  53. + "public class uploadReceiver \n"
  54. + "implements Upload.receiver, Upload.FinishedListener { \n"
  55. + "\n" + " java.io.File file;\n"
  56. + " java.io.FileOutputStream fos;\n"
  57. + " public uploadReceiver() {\n" + " }";
  58. }
  59. protected String getDescriptionXHTML() {
  60. return "This demonstrates the use of the Upload component together with the Link component. "
  61. + "This implementation does not actually store the file to disk, it only keeps it in a buffer. "
  62. + "The example given on the example-tab on the other hand stores the file to disk and binds the link to that file.<br/>"
  63. + "<br/>On the demo tab you can try out how the different properties affect the presentation of the component.";
  64. }
  65. protected String getImage() {
  66. return "filetransfer.jpg";
  67. }
  68. protected String getTitle() {
  69. return "Upload";
  70. }
  71. public void uploadFinished(FinishedEvent event) {
  72. status.removeAllComponents();
  73. if (buffer.getStream() == null)
  74. status.addComponent(new Label(
  75. "Upload finished, but output buffer is null!!"));
  76. else {
  77. status
  78. .addComponent(new Label("<b>Name:</b> "
  79. + event.getFilename(), Label.CONTENT_XHTML));
  80. status.addComponent(new Label("<b>Mimetype:</b> "
  81. + event.getMIMEType(), Label.CONTENT_XHTML));
  82. status.addComponent(new Label("<b>Size:</b> " + event.getLength()
  83. + " bytes.", Label.CONTENT_XHTML));
  84. status.addComponent(new Link("Download " + buffer.getFileName(),
  85. new StreamResource(buffer, buffer.getFileName(),
  86. getApplication())));
  87. status.setVisible(true);
  88. }
  89. }
  90. public class Buffer implements StreamResource.StreamSource, Upload.Receiver {
  91. ByteArrayOutputStream outputBuffer = null;
  92. String mimeType;
  93. String fileName;
  94. public Buffer() {
  95. }
  96. public InputStream getStream() {
  97. if (outputBuffer == null)
  98. return null;
  99. return new ByteArrayInputStream(outputBuffer.toByteArray());
  100. }
  101. /**
  102. * @see com.itmill.toolkit.ui.Upload.Receiver#receiveUpload(String,
  103. * String)
  104. */
  105. public OutputStream receiveUpload(String filename, String MIMEType) {
  106. fileName = filename;
  107. mimeType = MIMEType;
  108. outputBuffer = new ByteArrayOutputStream();
  109. return outputBuffer;
  110. }
  111. /**
  112. * Returns the fileName.
  113. *
  114. * @return String
  115. */
  116. public String getFileName() {
  117. return fileName;
  118. }
  119. /**
  120. * Returns the mimeType.
  121. *
  122. * @return String
  123. */
  124. public String getMimeType() {
  125. return mimeType;
  126. }
  127. }
  128. }