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.

Upload.java 34KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  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.ui;
  17. import java.io.OutputStream;
  18. import java.io.Serializable;
  19. import java.lang.reflect.Method;
  20. import java.util.Collections;
  21. import java.util.Iterator;
  22. import java.util.LinkedHashSet;
  23. import java.util.Map;
  24. import com.vaadin.server.NoInputStreamException;
  25. import com.vaadin.server.NoOutputStreamException;
  26. import com.vaadin.server.PaintException;
  27. import com.vaadin.server.PaintTarget;
  28. import com.vaadin.server.StreamVariable.StreamingProgressEvent;
  29. import com.vaadin.shared.EventId;
  30. import com.vaadin.shared.ui.upload.UploadClientRpc;
  31. import com.vaadin.shared.ui.upload.UploadServerRpc;
  32. import com.vaadin.util.ReflectTools;
  33. /**
  34. * Component for uploading files from client to server.
  35. *
  36. * <p>
  37. * The visible component consists of a file name input box and a browse button
  38. * and an upload submit button to start uploading.
  39. *
  40. * <p>
  41. * The Upload component needs a java.io.OutputStream to write the uploaded data.
  42. * You need to implement the Upload.Receiver interface and return the output
  43. * stream in the receiveUpload() method.
  44. *
  45. * <p>
  46. * You can get an event regarding starting (StartedEvent), progress
  47. * (ProgressEvent), and finishing (FinishedEvent) of upload by implementing
  48. * StartedListener, ProgressListener, and FinishedListener, respectively. The
  49. * FinishedListener is called for both failed and succeeded uploads. If you wish
  50. * to separate between these two cases, you can use SucceededListener
  51. * (SucceededEvenet) and FailedListener (FailedEvent).
  52. *
  53. * <p>
  54. * The upload component does not itself show upload progress, but you can use
  55. * the ProgressIndicator for providing progress feedback by implementing
  56. * ProgressListener and updating the indicator in updateProgress().
  57. *
  58. * <p>
  59. * Setting upload component immediate initiates the upload as soon as a file is
  60. * selected, instead of the common pattern of file selection field and upload
  61. * button.
  62. *
  63. * <p>
  64. * Note! Because of browser dependent implementations of <input type="file">
  65. * element, setting size for Upload component is not supported. For some
  66. * browsers setting size may work to some extend.
  67. *
  68. * @author Vaadin Ltd.
  69. * @since 3.0
  70. */
  71. @SuppressWarnings("serial")
  72. public class Upload extends AbstractComponent implements Component.Focusable,
  73. LegacyComponent {
  74. /**
  75. * Should the field be focused on next repaint?
  76. */
  77. private final boolean focus = false;
  78. /**
  79. * The tab order number of this field.
  80. */
  81. private int tabIndex = 0;
  82. /**
  83. * The output of the upload is redirected to this receiver.
  84. */
  85. private Receiver receiver;
  86. private boolean isUploading;
  87. private long contentLength = -1;
  88. private int totalBytes;
  89. private String buttonCaption = "Upload";
  90. /**
  91. * ProgressListeners to which information about progress is sent during
  92. * upload
  93. */
  94. private LinkedHashSet<ProgressListener> progressListeners;
  95. private boolean interrupted = false;
  96. private boolean notStarted;
  97. private int nextid;
  98. /**
  99. * Creates a new instance of Upload.
  100. *
  101. * The receiver must be set before performing an upload.
  102. */
  103. public Upload() {
  104. registerRpc(new UploadServerRpc() {
  105. @Override
  106. public void change(String filename) {
  107. fireEvent(new ChangeEvent(Upload.this, filename));
  108. }
  109. @Override
  110. public void poll() {
  111. // Nothing to do, called only to visit the server
  112. }
  113. });
  114. }
  115. public Upload(String caption, Receiver uploadReceiver) {
  116. this();
  117. setCaption(caption);
  118. receiver = uploadReceiver;
  119. }
  120. /**
  121. * Invoked when the value of a variable has changed.
  122. *
  123. * @see com.vaadin.ui.AbstractComponent#changeVariables(java.lang.Object,
  124. * java.util.Map)
  125. */
  126. @Override
  127. public void changeVariables(Object source, Map<String, Object> variables) {
  128. if (variables.containsKey("pollForStart")) {
  129. int id = (Integer) variables.get("pollForStart");
  130. if (!isUploading && id == nextid) {
  131. notStarted = true;
  132. markAsDirty();
  133. } else {
  134. }
  135. }
  136. }
  137. /**
  138. * Paints the content of this component.
  139. *
  140. * @param target
  141. * Target to paint the content on.
  142. * @throws PaintException
  143. * if the paint operation failed.
  144. */
  145. @Override
  146. public void paintContent(PaintTarget target) throws PaintException {
  147. if (notStarted) {
  148. target.addAttribute("notStarted", true);
  149. notStarted = false;
  150. return;
  151. }
  152. // The field should be focused
  153. if (focus) {
  154. target.addAttribute("focus", true);
  155. }
  156. // The tab ordering number
  157. if (tabIndex >= 0) {
  158. target.addAttribute("tabindex", tabIndex);
  159. }
  160. target.addAttribute("state", isUploading);
  161. if (buttonCaption != null) {
  162. target.addAttribute("buttoncaption", buttonCaption);
  163. }
  164. target.addAttribute("nextid", nextid);
  165. // Post file to this strean variable
  166. target.addVariable(this, "action", getStreamVariable());
  167. }
  168. /**
  169. * Interface that must be implemented by the upload receivers to provide the
  170. * Upload component an output stream to write the uploaded data.
  171. *
  172. * @author Vaadin Ltd.
  173. * @since 3.0
  174. */
  175. public interface Receiver extends Serializable {
  176. /**
  177. * Invoked when a new upload arrives.
  178. *
  179. * @param filename
  180. * the desired filename of the upload, usually as specified
  181. * by the client.
  182. * @param mimeType
  183. * the MIME type of the uploaded file.
  184. * @return Stream to which the uploaded file should be written.
  185. */
  186. public OutputStream receiveUpload(String filename, String mimeType);
  187. }
  188. /* Upload events */
  189. private static final Method UPLOAD_FINISHED_METHOD;
  190. private static final Method UPLOAD_FAILED_METHOD;
  191. private static final Method UPLOAD_SUCCEEDED_METHOD;
  192. private static final Method UPLOAD_STARTED_METHOD;
  193. static {
  194. try {
  195. UPLOAD_FINISHED_METHOD = FinishedListener.class.getDeclaredMethod(
  196. "uploadFinished", new Class[] { FinishedEvent.class });
  197. UPLOAD_FAILED_METHOD = FailedListener.class.getDeclaredMethod(
  198. "uploadFailed", new Class[] { FailedEvent.class });
  199. UPLOAD_STARTED_METHOD = StartedListener.class.getDeclaredMethod(
  200. "uploadStarted", new Class[] { StartedEvent.class });
  201. UPLOAD_SUCCEEDED_METHOD = SucceededListener.class
  202. .getDeclaredMethod("uploadSucceeded",
  203. new Class[] { SucceededEvent.class });
  204. } catch (final java.lang.NoSuchMethodException e) {
  205. // This should never happen
  206. throw new java.lang.RuntimeException(
  207. "Internal error finding methods in Upload");
  208. }
  209. }
  210. /**
  211. * Upload.FinishedEvent is sent when the upload receives a file, regardless
  212. * of whether the reception was successful or failed. If you wish to
  213. * distinguish between the two cases, use either SucceededEvent or
  214. * FailedEvent, which are both subclasses of the FinishedEvent.
  215. *
  216. * @author Vaadin Ltd.
  217. * @since 3.0
  218. */
  219. public static class FinishedEvent extends Component.Event {
  220. /**
  221. * Length of the received file.
  222. */
  223. private final long length;
  224. /**
  225. * MIME type of the received file.
  226. */
  227. private final String type;
  228. /**
  229. * Received file name.
  230. */
  231. private final String filename;
  232. /**
  233. *
  234. * @param source
  235. * the source of the file.
  236. * @param filename
  237. * the received file name.
  238. * @param MIMEType
  239. * the MIME type of the received file.
  240. * @param length
  241. * the length of the received file.
  242. */
  243. public FinishedEvent(Upload source, String filename, String MIMEType,
  244. long length) {
  245. super(source);
  246. type = MIMEType;
  247. this.filename = filename;
  248. this.length = length;
  249. }
  250. /**
  251. * Uploads where the event occurred.
  252. *
  253. * @return the Source of the event.
  254. */
  255. public Upload getUpload() {
  256. return (Upload) getSource();
  257. }
  258. /**
  259. * Gets the file name.
  260. *
  261. * @return the filename.
  262. */
  263. public String getFilename() {
  264. return filename;
  265. }
  266. /**
  267. * Gets the MIME Type of the file.
  268. *
  269. * @return the MIME type.
  270. */
  271. public String getMIMEType() {
  272. return type;
  273. }
  274. /**
  275. * Gets the length of the file.
  276. *
  277. * @return the length.
  278. */
  279. public long getLength() {
  280. return length;
  281. }
  282. }
  283. /**
  284. * Upload.FailedEvent event is sent when the upload is received, but the
  285. * reception is interrupted for some reason.
  286. *
  287. * @author Vaadin Ltd.
  288. * @since 3.0
  289. */
  290. public static class FailedEvent extends FinishedEvent {
  291. private Exception reason = null;
  292. /**
  293. *
  294. * @param source
  295. * @param filename
  296. * @param MIMEType
  297. * @param length
  298. * @param exception
  299. */
  300. public FailedEvent(Upload source, String filename, String MIMEType,
  301. long length, Exception reason) {
  302. this(source, filename, MIMEType, length);
  303. this.reason = reason;
  304. }
  305. /**
  306. *
  307. * @param source
  308. * @param filename
  309. * @param MIMEType
  310. * @param length
  311. * @param exception
  312. */
  313. public FailedEvent(Upload source, String filename, String MIMEType,
  314. long length) {
  315. super(source, filename, MIMEType, length);
  316. }
  317. /**
  318. * Gets the exception that caused the failure.
  319. *
  320. * @return the exception that caused the failure, null if n/a
  321. */
  322. public Exception getReason() {
  323. return reason;
  324. }
  325. }
  326. /**
  327. * FailedEvent that indicates that an output stream could not be obtained.
  328. */
  329. public static class NoOutputStreamEvent extends FailedEvent {
  330. /**
  331. *
  332. * @param source
  333. * @param filename
  334. * @param MIMEType
  335. * @param length
  336. */
  337. public NoOutputStreamEvent(Upload source, String filename,
  338. String MIMEType, long length) {
  339. super(source, filename, MIMEType, length);
  340. }
  341. }
  342. /**
  343. * FailedEvent that indicates that an input stream could not be obtained.
  344. */
  345. public static class NoInputStreamEvent extends FailedEvent {
  346. /**
  347. *
  348. * @param source
  349. * @param filename
  350. * @param MIMEType
  351. * @param length
  352. */
  353. public NoInputStreamEvent(Upload source, String filename,
  354. String MIMEType, long length) {
  355. super(source, filename, MIMEType, length);
  356. }
  357. }
  358. /**
  359. * Upload.SucceededEvent event is sent when the upload is received
  360. * successfully.
  361. *
  362. * @author Vaadin Ltd.
  363. * @since 3.0
  364. */
  365. public static class SucceededEvent extends FinishedEvent {
  366. /**
  367. *
  368. * @param source
  369. * @param filename
  370. * @param MIMEType
  371. * @param length
  372. */
  373. public SucceededEvent(Upload source, String filename, String MIMEType,
  374. long length) {
  375. super(source, filename, MIMEType, length);
  376. }
  377. }
  378. /**
  379. * Upload.StartedEvent event is sent when the upload is started to received.
  380. *
  381. * @author Vaadin Ltd.
  382. * @since 5.0
  383. */
  384. public static class StartedEvent extends Component.Event {
  385. private final String filename;
  386. private final String type;
  387. /**
  388. * Length of the received file.
  389. */
  390. private final long length;
  391. /**
  392. *
  393. * @param source
  394. * @param filename
  395. * @param MIMEType
  396. * @param length
  397. */
  398. public StartedEvent(Upload source, String filename, String MIMEType,
  399. long contentLength) {
  400. super(source);
  401. this.filename = filename;
  402. type = MIMEType;
  403. length = contentLength;
  404. }
  405. /**
  406. * Uploads where the event occurred.
  407. *
  408. * @return the Source of the event.
  409. */
  410. public Upload getUpload() {
  411. return (Upload) getSource();
  412. }
  413. /**
  414. * Gets the file name.
  415. *
  416. * @return the filename.
  417. */
  418. public String getFilename() {
  419. return filename;
  420. }
  421. /**
  422. * Gets the MIME Type of the file.
  423. *
  424. * @return the MIME type.
  425. */
  426. public String getMIMEType() {
  427. return type;
  428. }
  429. /**
  430. * @return the length of the file that is being uploaded
  431. */
  432. public long getContentLength() {
  433. return length;
  434. }
  435. }
  436. /**
  437. * Upload.ChangeEvent event is sent when the value (filename) of the upload
  438. * changes.
  439. *
  440. * @since 7.2
  441. */
  442. public static class ChangeEvent extends Component.Event {
  443. private final String filename;
  444. public ChangeEvent(Upload source, String filename) {
  445. super(source);
  446. this.filename = filename;
  447. }
  448. /**
  449. * Uploads where the event occurred.
  450. *
  451. * @return the Source of the event.
  452. */
  453. @Override
  454. public Upload getSource() {
  455. return (Upload) super.getSource();
  456. }
  457. /**
  458. * Gets the file name.
  459. *
  460. * @return the filename.
  461. */
  462. public String getFilename() {
  463. return filename;
  464. }
  465. }
  466. /**
  467. * Receives the events when the upload starts.
  468. *
  469. * @author Vaadin Ltd.
  470. * @since 5.0
  471. */
  472. public interface StartedListener extends Serializable {
  473. /**
  474. * Upload has started.
  475. *
  476. * @param event
  477. * the Upload started event.
  478. */
  479. public void uploadStarted(StartedEvent event);
  480. }
  481. /**
  482. * Receives the events when the uploads are ready.
  483. *
  484. * @author Vaadin Ltd.
  485. * @since 3.0
  486. */
  487. public interface FinishedListener extends Serializable {
  488. /**
  489. * Upload has finished.
  490. *
  491. * @param event
  492. * the Upload finished event.
  493. */
  494. public void uploadFinished(FinishedEvent event);
  495. }
  496. /**
  497. * Receives events when the uploads are finished, but unsuccessful.
  498. *
  499. * @author Vaadin Ltd.
  500. * @since 3.0
  501. */
  502. public interface FailedListener extends Serializable {
  503. /**
  504. * Upload has finished unsuccessfully.
  505. *
  506. * @param event
  507. * the Upload failed event.
  508. */
  509. public void uploadFailed(FailedEvent event);
  510. }
  511. /**
  512. * Receives events when the uploads are successfully finished.
  513. *
  514. * @author Vaadin Ltd.
  515. * @since 3.0
  516. */
  517. public interface SucceededListener extends Serializable {
  518. /**
  519. * Upload successfull..
  520. *
  521. * @param event
  522. * the Upload successfull event.
  523. */
  524. public void uploadSucceeded(SucceededEvent event);
  525. }
  526. /**
  527. * Listener for {@link ChangeEvent}
  528. *
  529. * @since 7.2
  530. */
  531. public interface ChangeListener extends Serializable {
  532. Method FILENAME_CHANGED = ReflectTools.findMethod(ChangeListener.class,
  533. "filenameChanged", ChangeEvent.class);
  534. /**
  535. * A file has been selected but upload has not yet started.
  536. *
  537. * @param event
  538. * the change event
  539. */
  540. public void filenameChanged(ChangeEvent event);
  541. }
  542. /**
  543. * Adds the upload started event listener.
  544. *
  545. * @param listener
  546. * the Listener to be added.
  547. */
  548. public void addStartedListener(StartedListener listener) {
  549. addListener(StartedEvent.class, listener, UPLOAD_STARTED_METHOD);
  550. }
  551. /**
  552. * @deprecated As of 7.0, replaced by
  553. * {@link #addStartedListener(StartedListener)}
  554. **/
  555. @Deprecated
  556. public void addListener(StartedListener listener) {
  557. addStartedListener(listener);
  558. }
  559. /**
  560. * Removes the upload started event listener.
  561. *
  562. * @param listener
  563. * the Listener to be removed.
  564. */
  565. public void removeStartedListener(StartedListener listener) {
  566. removeListener(StartedEvent.class, listener, UPLOAD_STARTED_METHOD);
  567. }
  568. /**
  569. * @deprecated As of 7.0, replaced by
  570. * {@link #removeStartedListener(StartedListener)}
  571. **/
  572. @Deprecated
  573. public void removeListener(StartedListener listener) {
  574. removeStartedListener(listener);
  575. }
  576. /**
  577. * Adds the upload received event listener.
  578. *
  579. * @param listener
  580. * the Listener to be added.
  581. */
  582. public void addFinishedListener(FinishedListener listener) {
  583. addListener(FinishedEvent.class, listener, UPLOAD_FINISHED_METHOD);
  584. }
  585. /**
  586. * @deprecated As of 7.0, replaced by
  587. * {@link #addFinishedListener(FinishedListener)}
  588. **/
  589. @Deprecated
  590. public void addListener(FinishedListener listener) {
  591. addFinishedListener(listener);
  592. }
  593. /**
  594. * Removes the upload received event listener.
  595. *
  596. * @param listener
  597. * the Listener to be removed.
  598. */
  599. public void removeFinishedListener(FinishedListener listener) {
  600. removeListener(FinishedEvent.class, listener, UPLOAD_FINISHED_METHOD);
  601. }
  602. /**
  603. * @deprecated As of 7.0, replaced by
  604. * {@link #removeFinishedListener(FinishedListener)}
  605. **/
  606. @Deprecated
  607. public void removeListener(FinishedListener listener) {
  608. removeFinishedListener(listener);
  609. }
  610. /**
  611. * Adds the upload interrupted event listener.
  612. *
  613. * @param listener
  614. * the Listener to be added.
  615. */
  616. public void addFailedListener(FailedListener listener) {
  617. addListener(FailedEvent.class, listener, UPLOAD_FAILED_METHOD);
  618. }
  619. /**
  620. * @deprecated As of 7.0, replaced by
  621. * {@link #addFailedListener(FailedListener)}
  622. **/
  623. @Deprecated
  624. public void addListener(FailedListener listener) {
  625. addFailedListener(listener);
  626. }
  627. /**
  628. * Removes the upload interrupted event listener.
  629. *
  630. * @param listener
  631. * the Listener to be removed.
  632. */
  633. public void removeFailedListener(FailedListener listener) {
  634. removeListener(FailedEvent.class, listener, UPLOAD_FAILED_METHOD);
  635. }
  636. /**
  637. * @deprecated As of 7.0, replaced by
  638. * {@link #removeFailedListener(FailedListener)}
  639. **/
  640. @Deprecated
  641. public void removeListener(FailedListener listener) {
  642. removeFailedListener(listener);
  643. }
  644. /**
  645. * Adds the upload success event listener.
  646. *
  647. * @param listener
  648. * the Listener to be added.
  649. */
  650. public void addSucceededListener(SucceededListener listener) {
  651. addListener(SucceededEvent.class, listener, UPLOAD_SUCCEEDED_METHOD);
  652. }
  653. /**
  654. * @deprecated As of 7.0, replaced by
  655. * {@link #addSucceededListener(SucceededListener)}
  656. **/
  657. @Deprecated
  658. public void addListener(SucceededListener listener) {
  659. addSucceededListener(listener);
  660. }
  661. /**
  662. * Removes the upload success event listener.
  663. *
  664. * @param listener
  665. * the Listener to be removed.
  666. */
  667. public void removeSucceededListener(SucceededListener listener) {
  668. removeListener(SucceededEvent.class, listener, UPLOAD_SUCCEEDED_METHOD);
  669. }
  670. /**
  671. * @deprecated As of 7.0, replaced by
  672. * {@link #removeSucceededListener(SucceededListener)}
  673. **/
  674. @Deprecated
  675. public void removeListener(SucceededListener listener) {
  676. removeSucceededListener(listener);
  677. }
  678. /**
  679. * Adds the upload progress event listener.
  680. *
  681. * @param listener
  682. * the progress listener to be added
  683. */
  684. public void addProgressListener(ProgressListener listener) {
  685. if (progressListeners == null) {
  686. progressListeners = new LinkedHashSet<ProgressListener>();
  687. }
  688. progressListeners.add(listener);
  689. }
  690. /**
  691. * @deprecated As of 7.0, replaced by
  692. * {@link #addProgressListener(ProgressListener)}
  693. **/
  694. @Deprecated
  695. public void addListener(ProgressListener listener) {
  696. addProgressListener(listener);
  697. }
  698. /**
  699. * Removes the upload progress event listener.
  700. *
  701. * @param listener
  702. * the progress listener to be removed
  703. */
  704. public void removeProgressListener(ProgressListener listener) {
  705. if (progressListeners != null) {
  706. progressListeners.remove(listener);
  707. }
  708. }
  709. /**
  710. * Adds a filename change event listener
  711. *
  712. * @param listener
  713. * the Listener to add
  714. */
  715. public void addChangeListener(ChangeListener listener) {
  716. super.addListener(EventId.CHANGE, ChangeEvent.class, listener,
  717. ChangeListener.FILENAME_CHANGED);
  718. }
  719. /**
  720. * Removes a filename change event listener
  721. *
  722. * @param listener
  723. * the listener to be removed
  724. */
  725. public void removeChangeListener(ChangeListener listener) {
  726. super.removeListener(EventId.CHANGE, ChangeEvent.class, listener);
  727. }
  728. /**
  729. * @deprecated As of 7.0, replaced by
  730. * {@link #removeProgressListener(ProgressListener)}
  731. **/
  732. @Deprecated
  733. public void removeListener(ProgressListener listener) {
  734. removeProgressListener(listener);
  735. }
  736. /**
  737. * Emit upload received event.
  738. *
  739. * @param filename
  740. * @param MIMEType
  741. * @param length
  742. */
  743. protected void fireStarted(String filename, String MIMEType) {
  744. fireEvent(new Upload.StartedEvent(this, filename, MIMEType,
  745. contentLength));
  746. }
  747. /**
  748. * Emits the upload failed event.
  749. *
  750. * @param filename
  751. * @param MIMEType
  752. * @param length
  753. */
  754. protected void fireUploadInterrupted(String filename, String MIMEType,
  755. long length) {
  756. fireEvent(new Upload.FailedEvent(this, filename, MIMEType, length));
  757. }
  758. protected void fireNoInputStream(String filename, String MIMEType,
  759. long length) {
  760. fireEvent(new Upload.NoInputStreamEvent(this, filename, MIMEType,
  761. length));
  762. }
  763. protected void fireNoOutputStream(String filename, String MIMEType,
  764. long length) {
  765. fireEvent(new Upload.NoOutputStreamEvent(this, filename, MIMEType,
  766. length));
  767. }
  768. protected void fireUploadInterrupted(String filename, String MIMEType,
  769. long length, Exception e) {
  770. fireEvent(new Upload.FailedEvent(this, filename, MIMEType, length, e));
  771. }
  772. /**
  773. * Emits the upload success event.
  774. *
  775. * @param filename
  776. * @param MIMEType
  777. * @param length
  778. *
  779. */
  780. protected void fireUploadSuccess(String filename, String MIMEType,
  781. long length) {
  782. fireEvent(new Upload.SucceededEvent(this, filename, MIMEType, length));
  783. }
  784. /**
  785. * Emits the progress event.
  786. *
  787. * @param totalBytes
  788. * bytes received so far
  789. * @param contentLength
  790. * actual size of the file being uploaded, if known
  791. *
  792. */
  793. protected void fireUpdateProgress(long totalBytes, long contentLength) {
  794. // this is implemented differently than other listeners to maintain
  795. // backwards compatibility
  796. if (progressListeners != null) {
  797. for (Iterator<ProgressListener> it = progressListeners.iterator(); it
  798. .hasNext();) {
  799. ProgressListener l = it.next();
  800. l.updateProgress(totalBytes, contentLength);
  801. }
  802. }
  803. }
  804. /**
  805. * Returns the current receiver.
  806. *
  807. * @return the StreamVariable.
  808. */
  809. public Receiver getReceiver() {
  810. return receiver;
  811. }
  812. /**
  813. * Sets the receiver.
  814. *
  815. * @param receiver
  816. * the receiver to set.
  817. */
  818. public void setReceiver(Receiver receiver) {
  819. this.receiver = receiver;
  820. }
  821. /**
  822. * {@inheritDoc}
  823. */
  824. @Override
  825. public void focus() {
  826. super.focus();
  827. }
  828. /**
  829. * Gets the Tabulator index of this Focusable component.
  830. *
  831. * @see com.vaadin.ui.Component.Focusable#getTabIndex()
  832. */
  833. @Override
  834. public int getTabIndex() {
  835. return tabIndex;
  836. }
  837. /**
  838. * Sets the Tabulator index of this Focusable component.
  839. *
  840. * @see com.vaadin.ui.Component.Focusable#setTabIndex(int)
  841. */
  842. @Override
  843. public void setTabIndex(int tabIndex) {
  844. this.tabIndex = tabIndex;
  845. }
  846. /**
  847. * Go into upload state. This is to prevent double uploading on same
  848. * component.
  849. *
  850. * Warning: this is an internal method used by the framework and should not
  851. * be used by user of the Upload component. Using it results in the Upload
  852. * component going in wrong state and not working. It is currently public
  853. * because it is used by another class.
  854. */
  855. public void startUpload() {
  856. if (isUploading) {
  857. throw new IllegalStateException("uploading already started");
  858. }
  859. isUploading = true;
  860. nextid++;
  861. }
  862. /**
  863. * Interrupts the upload currently being received. The interruption will be
  864. * done by the receiving thread so this method will return immediately and
  865. * the actual interrupt will happen a bit later.
  866. */
  867. public void interruptUpload() {
  868. if (isUploading) {
  869. interrupted = true;
  870. }
  871. }
  872. /**
  873. * Go into state where new uploading can begin.
  874. *
  875. * Warning: this is an internal method used by the framework and should not
  876. * be used by user of the Upload component.
  877. */
  878. private void endUpload() {
  879. isUploading = false;
  880. contentLength = -1;
  881. interrupted = false;
  882. markAsDirty();
  883. }
  884. public boolean isUploading() {
  885. return isUploading;
  886. }
  887. /**
  888. * Gets read bytes of the file currently being uploaded.
  889. *
  890. * @return bytes
  891. */
  892. public long getBytesRead() {
  893. return totalBytes;
  894. }
  895. /**
  896. * Returns size of file currently being uploaded. Value sane only during
  897. * upload.
  898. *
  899. * @return size in bytes
  900. */
  901. public long getUploadSize() {
  902. return contentLength;
  903. }
  904. /**
  905. * ProgressListener receives events to track progress of upload.
  906. */
  907. public interface ProgressListener extends Serializable {
  908. /**
  909. * Updates progress to listener
  910. *
  911. * @param readBytes
  912. * bytes transferred
  913. * @param contentLength
  914. * total size of file currently being uploaded, -1 if unknown
  915. */
  916. public void updateProgress(long readBytes, long contentLength);
  917. }
  918. /**
  919. * @return String to be rendered into button that fires uploading
  920. */
  921. public String getButtonCaption() {
  922. return buttonCaption;
  923. }
  924. /**
  925. * In addition to the actual file chooser, upload components have button
  926. * that starts actual upload progress. This method is used to set text in
  927. * that button.
  928. * <p>
  929. * In case the button text is set to null, the button is hidden. In this
  930. * case developer must explicitly initiate the upload process with
  931. * {@link #submitUpload()}.
  932. * <p>
  933. * In case the Upload is used in immediate mode using
  934. * {@link #setImmediate(boolean)}, the file choose (html input with type
  935. * "file") is hidden and only the button with this text is shown.
  936. * <p>
  937. *
  938. * <p>
  939. * <strong>Note</strong> the string given is set as is to the button. HTML
  940. * formatting is not stripped. Be sure to properly validate your value
  941. * according to your needs.
  942. *
  943. * @param buttonCaption
  944. * text for upload components button.
  945. */
  946. public void setButtonCaption(String buttonCaption) {
  947. this.buttonCaption = buttonCaption;
  948. markAsDirty();
  949. }
  950. /**
  951. * Forces the upload the send selected file to the server.
  952. * <p>
  953. * In case developer wants to use this feature, he/she will most probably
  954. * want to hide the uploads internal submit button by setting its caption to
  955. * null with {@link #setButtonCaption(String)} method.
  956. * <p>
  957. * Note, that the upload runs asynchronous. Developer should use normal
  958. * upload listeners to trac the process of upload. If the field is empty
  959. * uploaded the file name will be empty string and file length 0 in the
  960. * upload finished event.
  961. * <p>
  962. * Also note, that the developer should not remove or modify the upload in
  963. * the same user transaction where the upload submit is requested. The
  964. * upload may safely be hidden or removed once the upload started event is
  965. * fired.
  966. */
  967. public void submitUpload() {
  968. markAsDirty();
  969. getRpcProxy(UploadClientRpc.class).submitUpload();
  970. }
  971. @Override
  972. public void markAsDirty() {
  973. super.markAsDirty();
  974. }
  975. /*
  976. * Handle to terminal via Upload monitors and controls the upload during it
  977. * is being streamed.
  978. */
  979. private com.vaadin.server.StreamVariable streamVariable;
  980. protected com.vaadin.server.StreamVariable getStreamVariable() {
  981. if (streamVariable == null) {
  982. streamVariable = new com.vaadin.server.StreamVariable() {
  983. private StreamingStartEvent lastStartedEvent;
  984. @Override
  985. public boolean listenProgress() {
  986. return (progressListeners != null && !progressListeners
  987. .isEmpty());
  988. }
  989. @Override
  990. public void onProgress(StreamingProgressEvent event) {
  991. fireUpdateProgress(event.getBytesReceived(),
  992. event.getContentLength());
  993. }
  994. @Override
  995. public boolean isInterrupted() {
  996. return interrupted;
  997. }
  998. @Override
  999. public OutputStream getOutputStream() {
  1000. if (getReceiver() == null) {
  1001. throw new IllegalStateException(
  1002. "Upload cannot be performed without a receiver set");
  1003. }
  1004. OutputStream receiveUpload = getReceiver().receiveUpload(
  1005. lastStartedEvent.getFileName(),
  1006. lastStartedEvent.getMimeType());
  1007. lastStartedEvent = null;
  1008. return receiveUpload;
  1009. }
  1010. @Override
  1011. public void streamingStarted(StreamingStartEvent event) {
  1012. startUpload();
  1013. contentLength = event.getContentLength();
  1014. fireStarted(event.getFileName(), event.getMimeType());
  1015. lastStartedEvent = event;
  1016. }
  1017. @Override
  1018. public void streamingFinished(StreamingEndEvent event) {
  1019. fireUploadSuccess(event.getFileName(), event.getMimeType(),
  1020. event.getContentLength());
  1021. endUpload();
  1022. }
  1023. @Override
  1024. public void streamingFailed(StreamingErrorEvent event) {
  1025. try {
  1026. Exception exception = event.getException();
  1027. if (exception instanceof NoInputStreamException) {
  1028. fireNoInputStream(event.getFileName(),
  1029. event.getMimeType(), 0);
  1030. } else if (exception instanceof NoOutputStreamException) {
  1031. fireNoOutputStream(event.getFileName(),
  1032. event.getMimeType(), 0);
  1033. } else {
  1034. fireUploadInterrupted(event.getFileName(),
  1035. event.getMimeType(), 0, exception);
  1036. }
  1037. } finally {
  1038. endUpload();
  1039. }
  1040. }
  1041. };
  1042. }
  1043. return streamVariable;
  1044. }
  1045. @Override
  1046. public java.util.Collection<?> getListeners(java.lang.Class<?> eventType) {
  1047. if (StreamingProgressEvent.class.isAssignableFrom(eventType)) {
  1048. if (progressListeners == null) {
  1049. return Collections.EMPTY_LIST;
  1050. } else {
  1051. return Collections.unmodifiableCollection(progressListeners);
  1052. }
  1053. }
  1054. return super.getListeners(eventType);
  1055. }
  1056. }