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.

Upload.java 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. /*
  2. * @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.ui;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.io.Serializable;
  9. import java.lang.reflect.Method;
  10. import java.util.Iterator;
  11. import java.util.LinkedHashSet;
  12. import java.util.Map;
  13. import com.vaadin.Application;
  14. import com.vaadin.terminal.PaintException;
  15. import com.vaadin.terminal.PaintTarget;
  16. import com.vaadin.terminal.UploadStream;
  17. import com.vaadin.terminal.gwt.client.ui.VUpload;
  18. /**
  19. * Component for uploading files from client to server.
  20. *
  21. * <p>
  22. * The visible component consists of a file name input box and a browse button
  23. * and an upload submit button to start uploading.
  24. *
  25. * <p>
  26. * The Upload component needs a java.io.OutputStream to write the uploaded data.
  27. * You need to implement the Upload.Receiver interface and return the output
  28. * stream in the receiveUpload() method.
  29. *
  30. * <p>
  31. * You can get an event regarding starting (StartedEvent), progress
  32. * (ProgressEvent), and finishing (FinishedEvent) of upload by implementing
  33. * StartedListener, ProgressListener, and FinishedListener, respectively. The
  34. * FinishedListener is called for both failed and succeeded uploads. If you wish
  35. * to separate between these two cases, you can use SucceededListener
  36. * (SucceededEvenet) and FailedListener (FailedEvent).
  37. *
  38. * <p>
  39. * The upload component does not itself show upload progress, but you can use
  40. * the ProgressIndicator for providing progress feedback by implementing
  41. * ProgressListener and updating the indicator in updateProgress().
  42. *
  43. * <p>
  44. * Setting upload component immediate initiates the upload as soon as a file is
  45. * selected, instead of the common pattern of file selection field and upload
  46. * button.
  47. *
  48. * @author IT Mill Ltd.
  49. * @version
  50. * @VERSION@
  51. * @since 3.0
  52. */
  53. @SuppressWarnings("serial")
  54. @ClientWidget(VUpload.class)
  55. public class Upload extends AbstractComponent implements Component.Focusable {
  56. private boolean delayedFocus;
  57. /**
  58. * Upload buffer size.
  59. */
  60. private static final int BUFFER_SIZE = 64 * 1024; // 64k
  61. /**
  62. * Should the field be focused on next repaint?
  63. */
  64. private final boolean focus = false;
  65. /**
  66. * The tab order number of this field.
  67. */
  68. private int tabIndex = 0;
  69. /**
  70. * The output of the upload is redirected to this receiver.
  71. */
  72. private Receiver receiver;
  73. private boolean isUploading;
  74. private long contentLength = -1;
  75. private int totalBytes;
  76. private String buttonCaption = "Upload";
  77. /**
  78. * ProgressListener to which information about progress is sent during
  79. * upload
  80. */
  81. private ProgressListener progressListener;
  82. private LinkedHashSet<ProgressListener> progressListeners;
  83. private boolean interrupted = false;
  84. /* TODO: Add a default constructor, receive to temp file. */
  85. /**
  86. * Creates a new instance of Upload that redirects the uploaded data to
  87. * stream given by the Receiver.
  88. *
  89. * @param caption
  90. * Normal component caption. You can set the caption of the
  91. * upload submit button with setButtonCaption().
  92. * @param uploadReceiver
  93. * Receiver to call to retrieve output stream when upload starts.
  94. */
  95. public Upload(String caption, Receiver uploadReceiver) {
  96. setCaption(caption);
  97. receiver = uploadReceiver;
  98. }
  99. /**
  100. * Gets the component type.
  101. *
  102. * @return Component type as string.
  103. */
  104. @Override
  105. public String getTag() {
  106. return "upload";
  107. }
  108. /**
  109. * This method is called by terminal when upload is received.
  110. *
  111. * Note, this method is called outside synchronized (Application) block, so
  112. * overriding this may be dangerous.
  113. *
  114. * @param upload
  115. */
  116. public void receiveUpload(UploadStream upload) {
  117. if (!isUploading) {
  118. throw new IllegalStateException("uploading not started");
  119. }
  120. // Gets file properties
  121. final String filename = upload.getContentName();
  122. final String type = upload.getContentType();
  123. final Application application = getApplication();
  124. synchronized (application) {
  125. fireStarted(filename, type);
  126. }
  127. // Gets the output target stream
  128. final OutputStream out = receiver.receiveUpload(filename, type);
  129. if (out == null) {
  130. synchronized (application) {
  131. fireNoOutputStream(filename, type, 0);
  132. endUpload();
  133. }
  134. return;
  135. }
  136. final InputStream in = upload.getStream();
  137. if (null == in) {
  138. // No file, for instance non-existent filename in html upload
  139. synchronized (application) {
  140. fireNoInputStream(filename, type, 0);
  141. endUpload();
  142. }
  143. return;
  144. }
  145. final byte buffer[] = new byte[BUFFER_SIZE];
  146. int bytesRead = 0;
  147. totalBytes = 0;
  148. try {
  149. while ((bytesRead = in.read(buffer)) > 0) {
  150. out.write(buffer, 0, bytesRead);
  151. totalBytes += bytesRead;
  152. if (contentLength > 0
  153. && (progressListeners != null || progressListener != null)) {
  154. // update progress if listener set and contentLength
  155. // received
  156. synchronized (application) {
  157. fireUpdateProgress(totalBytes, contentLength);
  158. }
  159. }
  160. if (interrupted) {
  161. throw new Exception("Upload interrupted by other thread");
  162. }
  163. }
  164. // upload successful
  165. out.close();
  166. synchronized (application) {
  167. fireUploadSuccess(filename, type, totalBytes);
  168. endUpload();
  169. requestRepaint();
  170. }
  171. } catch (final Exception e) {
  172. synchronized (application) {
  173. // Download interrupted
  174. try {
  175. // still try to close output stream
  176. out.close();
  177. } catch (IOException ignored) {
  178. }
  179. fireUploadInterrupted(filename, type, totalBytes, e);
  180. endUpload();
  181. interrupted = false;
  182. // throw cause ahead
  183. throw new IllegalStateException("Uploading failed", e);
  184. }
  185. }
  186. }
  187. /**
  188. * Invoked when the value of a variable has changed.
  189. *
  190. * @see com.vaadin.ui.AbstractComponent#changeVariables(java.lang.Object,
  191. * java.util.Map)
  192. */
  193. @Override
  194. public void changeVariables(Object source, Map variables) {
  195. // NOP
  196. }
  197. /**
  198. * Paints the content of this component.
  199. *
  200. * @param target
  201. * Target to paint the content on.
  202. * @throws PaintException
  203. * if the paint operation failed.
  204. */
  205. @Override
  206. public void paintContent(PaintTarget target) throws PaintException {
  207. // The field should be focused
  208. if (focus) {
  209. target.addAttribute("focus", true);
  210. }
  211. // The tab ordering number
  212. if (tabIndex >= 0) {
  213. target.addAttribute("tabindex", tabIndex);
  214. }
  215. target.addAttribute("state", isUploading);
  216. target.addAttribute("buttoncaption", buttonCaption);
  217. target.addVariable(this, "fake", true);
  218. target.addUploadStreamVariable(this, "stream");
  219. }
  220. /**
  221. * Interface that must be implemented by the upload receivers to provide the
  222. * Upload component an output stream to write the uploaded data.
  223. *
  224. * @author IT Mill Ltd.
  225. * @version
  226. * @VERSION@
  227. * @since 3.0
  228. */
  229. public interface Receiver extends Serializable {
  230. /**
  231. * Invoked when a new upload arrives.
  232. *
  233. * @param filename
  234. * the desired filename of the upload, usually as specified
  235. * by the client.
  236. * @param MIMEType
  237. * the MIME type of the uploaded file.
  238. * @return Stream to which the uploaded file should be written.
  239. */
  240. public OutputStream receiveUpload(String filename, String MIMEType);
  241. }
  242. /* Upload events */
  243. private static final Method UPLOAD_FINISHED_METHOD;
  244. private static final Method UPLOAD_FAILED_METHOD;
  245. private static final Method UPLOAD_SUCCEEDED_METHOD;
  246. private static final Method UPLOAD_STARTED_METHOD;
  247. static {
  248. try {
  249. UPLOAD_FINISHED_METHOD = FinishedListener.class.getDeclaredMethod(
  250. "uploadFinished", new Class[] { FinishedEvent.class });
  251. UPLOAD_FAILED_METHOD = FailedListener.class.getDeclaredMethod(
  252. "uploadFailed", new Class[] { FailedEvent.class });
  253. UPLOAD_STARTED_METHOD = StartedListener.class.getDeclaredMethod(
  254. "uploadStarted", new Class[] { StartedEvent.class });
  255. UPLOAD_SUCCEEDED_METHOD = SucceededListener.class
  256. .getDeclaredMethod("uploadSucceeded",
  257. new Class[] { SucceededEvent.class });
  258. } catch (final java.lang.NoSuchMethodException e) {
  259. // This should never happen
  260. throw new java.lang.RuntimeException(
  261. "Internal error finding methods in Upload");
  262. }
  263. }
  264. /**
  265. * Upload.Received event is sent when the upload receives a file, regardless
  266. * of whether the reception was successful or failed. If you wish to
  267. * distinguish between the two cases, use either SucceededEvent or
  268. * FailedEvent, which are both subclasses of the FinishedEvent.
  269. *
  270. * @author IT Mill Ltd.
  271. * @version
  272. * @VERSION@
  273. * @since 3.0
  274. */
  275. public class FinishedEvent extends Component.Event {
  276. /**
  277. * Length of the received file.
  278. */
  279. private final long length;
  280. /**
  281. * MIME type of the received file.
  282. */
  283. private final String type;
  284. /**
  285. * Received file name.
  286. */
  287. private final String filename;
  288. /**
  289. *
  290. * @param source
  291. * the source of the file.
  292. * @param filename
  293. * the received file name.
  294. * @param MIMEType
  295. * the MIME type of the received file.
  296. * @param length
  297. * the length of the received file.
  298. */
  299. public FinishedEvent(Upload source, String filename, String MIMEType,
  300. long length) {
  301. super(source);
  302. type = MIMEType;
  303. this.filename = filename;
  304. this.length = length;
  305. }
  306. /**
  307. * Uploads where the event occurred.
  308. *
  309. * @return the Source of the event.
  310. */
  311. public Upload getUpload() {
  312. return (Upload) getSource();
  313. }
  314. /**
  315. * Gets the file name.
  316. *
  317. * @return the filename.
  318. */
  319. public String getFilename() {
  320. return filename;
  321. }
  322. /**
  323. * Gets the MIME Type of the file.
  324. *
  325. * @return the MIME type.
  326. */
  327. public String getMIMEType() {
  328. return type;
  329. }
  330. /**
  331. * Gets the length of the file.
  332. *
  333. * @return the length.
  334. */
  335. public long getLength() {
  336. return length;
  337. }
  338. }
  339. /**
  340. * Upload.Interrupted event is sent when the upload is received, but the
  341. * reception is interrupted for some reason.
  342. *
  343. * @author IT Mill Ltd.
  344. * @version
  345. * @VERSION@
  346. * @since 3.0
  347. */
  348. public class FailedEvent extends FinishedEvent {
  349. private Exception reason = null;
  350. /**
  351. *
  352. * @param source
  353. * @param filename
  354. * @param MIMEType
  355. * @param length
  356. * @param exception
  357. */
  358. public FailedEvent(Upload source, String filename, String MIMEType,
  359. long length, Exception reason) {
  360. this(source, filename, MIMEType, length);
  361. this.reason = reason;
  362. }
  363. /**
  364. *
  365. * @param source
  366. * @param filename
  367. * @param MIMEType
  368. * @param length
  369. * @param exception
  370. */
  371. public FailedEvent(Upload source, String filename, String MIMEType,
  372. long length) {
  373. super(source, filename, MIMEType, length);
  374. }
  375. /**
  376. * Gets the exception that caused the failure.
  377. *
  378. * @return the exception that caused the failure, null if n/a
  379. */
  380. public Exception getReason() {
  381. return reason;
  382. }
  383. }
  384. /**
  385. * FailedEvent that indicates that an output stream could not be obtained.
  386. */
  387. public class NoOutputStreamEvent extends FailedEvent {
  388. /**
  389. *
  390. * @param source
  391. * @param filename
  392. * @param MIMEType
  393. * @param length
  394. */
  395. public NoOutputStreamEvent(Upload source, String filename,
  396. String MIMEType, long length) {
  397. super(source, filename, MIMEType, length);
  398. }
  399. }
  400. /**
  401. * FailedEvent that indicates that an input stream could not be obtained.
  402. */
  403. public class NoInputStreamEvent extends FailedEvent {
  404. /**
  405. *
  406. * @param source
  407. * @param filename
  408. * @param MIMEType
  409. * @param length
  410. */
  411. public NoInputStreamEvent(Upload source, String filename,
  412. String MIMEType, long length) {
  413. super(source, filename, MIMEType, length);
  414. }
  415. }
  416. /**
  417. * Upload.Success event is sent when the upload is received successfully.
  418. *
  419. * @author IT Mill Ltd.
  420. * @version
  421. * @VERSION@
  422. * @since 3.0
  423. */
  424. public class SucceededEvent extends FinishedEvent {
  425. /**
  426. *
  427. * @param source
  428. * @param filename
  429. * @param MIMEType
  430. * @param length
  431. */
  432. public SucceededEvent(Upload source, String filename, String MIMEType,
  433. long length) {
  434. super(source, filename, MIMEType, length);
  435. }
  436. }
  437. /**
  438. * Upload.Started event is sent when the upload is started to received.
  439. *
  440. * @author IT Mill Ltd.
  441. * @version
  442. * @VERSION@
  443. * @since 5.0
  444. */
  445. public class StartedEvent extends Component.Event {
  446. private final String filename;
  447. private final String type;
  448. /**
  449. *
  450. * @param source
  451. * @param filename
  452. * @param MIMEType
  453. * @param length
  454. */
  455. public StartedEvent(Upload source, String filename, String MIMEType) {
  456. super(source);
  457. this.filename = filename;
  458. type = MIMEType;
  459. }
  460. /**
  461. * Uploads where the event occurred.
  462. *
  463. * @return the Source of the event.
  464. */
  465. public Upload getUpload() {
  466. return (Upload) getSource();
  467. }
  468. /**
  469. * Gets the file name.
  470. *
  471. * @return the filename.
  472. */
  473. public String getFilename() {
  474. return filename;
  475. }
  476. /**
  477. * Gets the MIME Type of the file.
  478. *
  479. * @return the MIME type.
  480. */
  481. public String getMIMEType() {
  482. return type;
  483. }
  484. }
  485. /**
  486. * Receives the events when the upload starts.
  487. *
  488. * @author IT Mill Ltd.
  489. * @version
  490. * @VERSION@
  491. * @since 5.0
  492. */
  493. public interface StartedListener extends Serializable {
  494. /**
  495. * Upload has started.
  496. *
  497. * @param event
  498. * the Upload started event.
  499. */
  500. public void uploadStarted(StartedEvent event);
  501. }
  502. /**
  503. * Receives the events when the uploads are ready.
  504. *
  505. * @author IT Mill Ltd.
  506. * @version
  507. * @VERSION@
  508. * @since 3.0
  509. */
  510. public interface FinishedListener extends Serializable {
  511. /**
  512. * Upload has finished.
  513. *
  514. * @param event
  515. * the Upload finished event.
  516. */
  517. public void uploadFinished(FinishedEvent event);
  518. }
  519. /**
  520. * Receives events when the uploads are finished, but unsuccessful.
  521. *
  522. * @author IT Mill Ltd.
  523. * @version
  524. * @VERSION@
  525. * @since 3.0
  526. */
  527. public interface FailedListener extends Serializable {
  528. /**
  529. * Upload has finished unsuccessfully.
  530. *
  531. * @param event
  532. * the Upload failed event.
  533. */
  534. public void uploadFailed(FailedEvent event);
  535. }
  536. /**
  537. * Receives events when the uploads are successfully finished.
  538. *
  539. * @author IT Mill Ltd.
  540. * @version
  541. * @VERSION@
  542. * @since 3.0
  543. */
  544. public interface SucceededListener extends Serializable {
  545. /**
  546. * Upload successfull..
  547. *
  548. * @param event
  549. * the Upload successfull event.
  550. */
  551. public void uploadSucceeded(SucceededEvent event);
  552. }
  553. /**
  554. * Adds the upload started event listener.
  555. *
  556. * @param listener
  557. * the Listener to be added.
  558. */
  559. public void addListener(StartedListener listener) {
  560. addListener(StartedEvent.class, listener, UPLOAD_STARTED_METHOD);
  561. }
  562. /**
  563. * Removes the upload started event listener.
  564. *
  565. * @param listener
  566. * the Listener to be removed.
  567. */
  568. public void removeListener(StartedListener listener) {
  569. removeListener(StartedEvent.class, listener, UPLOAD_STARTED_METHOD);
  570. }
  571. /**
  572. * Adds the upload received event listener.
  573. *
  574. * @param listener
  575. * the Listener to be added.
  576. */
  577. public void addListener(FinishedListener listener) {
  578. addListener(FinishedEvent.class, listener, UPLOAD_FINISHED_METHOD);
  579. }
  580. /**
  581. * Removes the upload received event listener.
  582. *
  583. * @param listener
  584. * the Listener to be removed.
  585. */
  586. public void removeListener(FinishedListener listener) {
  587. removeListener(FinishedEvent.class, listener, UPLOAD_FINISHED_METHOD);
  588. }
  589. /**
  590. * Adds the upload interrupted event listener.
  591. *
  592. * @param listener
  593. * the Listener to be added.
  594. */
  595. public void addListener(FailedListener listener) {
  596. addListener(FailedEvent.class, listener, UPLOAD_FAILED_METHOD);
  597. }
  598. /**
  599. * Removes the upload interrupted event listener.
  600. *
  601. * @param listener
  602. * the Listener to be removed.
  603. */
  604. public void removeListener(FailedListener listener) {
  605. removeListener(FailedEvent.class, listener, UPLOAD_FAILED_METHOD);
  606. }
  607. /**
  608. * Adds the upload success event listener.
  609. *
  610. * @param listener
  611. * the Listener to be added.
  612. */
  613. public void addListener(SucceededListener listener) {
  614. addListener(SucceededEvent.class, listener, UPLOAD_SUCCEEDED_METHOD);
  615. }
  616. /**
  617. * Removes the upload success event listener.
  618. *
  619. * @param listener
  620. * the Listener to be removed.
  621. */
  622. public void removeListener(SucceededListener listener) {
  623. removeListener(SucceededEvent.class, listener, UPLOAD_SUCCEEDED_METHOD);
  624. }
  625. /**
  626. * Adds the upload success event listener.
  627. *
  628. * @param listener
  629. * the Listener to be added.
  630. */
  631. public void addListener(ProgressListener listener) {
  632. if (progressListeners == null) {
  633. progressListeners = new LinkedHashSet<ProgressListener>();
  634. }
  635. progressListeners.add(listener);
  636. }
  637. /**
  638. * Removes the upload success event listener.
  639. *
  640. * @param listener
  641. * the Listener to be removed.
  642. */
  643. public void removeListener(ProgressListener listener) {
  644. if (progressListeners != null) {
  645. progressListeners.remove(listener);
  646. }
  647. }
  648. /**
  649. * Emit upload received event.
  650. *
  651. * @param filename
  652. * @param MIMEType
  653. * @param length
  654. */
  655. protected void fireStarted(String filename, String MIMEType) {
  656. fireEvent(new Upload.StartedEvent(this, filename, MIMEType));
  657. }
  658. /**
  659. * Emit upload finished event.
  660. *
  661. * @param filename
  662. * @param MIMEType
  663. * @param length
  664. */
  665. protected void fireUploadReceived(String filename, String MIMEType,
  666. long length) {
  667. fireEvent(new Upload.FinishedEvent(this, filename, MIMEType, length));
  668. }
  669. /**
  670. * Emits the upload failed event.
  671. *
  672. * @param filename
  673. * @param MIMEType
  674. * @param length
  675. */
  676. protected void fireUploadInterrupted(String filename, String MIMEType,
  677. long length) {
  678. fireEvent(new Upload.FailedEvent(this, filename, MIMEType, length));
  679. }
  680. protected void fireNoInputStream(String filename, String MIMEType,
  681. long length) {
  682. fireEvent(new Upload.NoInputStreamEvent(this, filename, MIMEType,
  683. length));
  684. }
  685. protected void fireNoOutputStream(String filename, String MIMEType,
  686. long length) {
  687. fireEvent(new Upload.NoOutputStreamEvent(this, filename, MIMEType,
  688. length));
  689. }
  690. protected void fireUploadInterrupted(String filename, String MIMEType,
  691. long length, Exception e) {
  692. fireEvent(new Upload.FailedEvent(this, filename, MIMEType, length, e));
  693. }
  694. /**
  695. * Emits the upload success event.
  696. *
  697. * @param filename
  698. * @param MIMEType
  699. * @param length
  700. *
  701. */
  702. protected void fireUploadSuccess(String filename, String MIMEType,
  703. long length) {
  704. fireEvent(new Upload.SucceededEvent(this, filename, MIMEType, length));
  705. }
  706. /**
  707. * Emits the progress event.
  708. *
  709. * @param totalBytes
  710. * bytes received so far
  711. * @param contentLength
  712. * actual size of the file being uploaded, if known
  713. *
  714. */
  715. protected void fireUpdateProgress(long totalBytes, long contentLength) {
  716. // this is implemented differently than other listeners to maintain
  717. // backwards compatibility
  718. if (progressListeners != null) {
  719. for (Iterator<ProgressListener> it = progressListeners.iterator(); it
  720. .hasNext();) {
  721. ProgressListener l = it.next();
  722. l.updateProgress(totalBytes, contentLength);
  723. }
  724. }
  725. // deprecated:
  726. if (progressListener != null) {
  727. progressListener.updateProgress(totalBytes, contentLength);
  728. }
  729. }
  730. /**
  731. * Returns the current receiver.
  732. *
  733. * @return the Receiver.
  734. */
  735. public Receiver getReceiver() {
  736. return receiver;
  737. }
  738. /**
  739. * Sets the receiver.
  740. *
  741. * @param receiver
  742. * the receiver to set.
  743. */
  744. public void setReceiver(Receiver receiver) {
  745. this.receiver = receiver;
  746. }
  747. /*
  748. * (non-Javadoc)
  749. *
  750. * @see com.vaadin.ui.Component.Focusable#focus()
  751. */
  752. public void focus() {
  753. final Application app = getApplication();
  754. if (app != null) {
  755. getWindow().setFocusedComponent(this);
  756. delayedFocus = false;
  757. } else {
  758. delayedFocus = true;
  759. }
  760. }
  761. /**
  762. * Gets the Tabulator index of this Focusable component.
  763. *
  764. * @see com.vaadin.ui.Component.Focusable#getTabIndex()
  765. */
  766. public int getTabIndex() {
  767. return tabIndex;
  768. }
  769. /**
  770. * Sets the Tabulator index of this Focusable component.
  771. *
  772. * @see com.vaadin.ui.Component.Focusable#setTabIndex(int)
  773. */
  774. public void setTabIndex(int tabIndex) {
  775. this.tabIndex = tabIndex;
  776. }
  777. /**
  778. * Sets the size of the file currently being uploaded.
  779. *
  780. * @param contentLength
  781. */
  782. public void setUploadSize(long contentLength) {
  783. this.contentLength = contentLength;
  784. }
  785. /**
  786. * Go into upload state. This is to prevent double uploading on same
  787. * component.
  788. *
  789. * Warning: this is an internal method used by the framework and should not
  790. * be used by user of the Upload component. Using it results in the Upload
  791. * component going in wrong state and not working. It is currently public
  792. * because it is used by another class.
  793. */
  794. public void startUpload() {
  795. if (isUploading) {
  796. throw new IllegalStateException("uploading already started");
  797. }
  798. isUploading = true;
  799. }
  800. /**
  801. * Interrupts the upload currently being received. The interruption will be
  802. * done by the receiving tread so this method will return immediately and
  803. * the actual interrupt will happen a bit later.
  804. */
  805. public void interruptUpload() {
  806. if (isUploading) {
  807. interrupted = true;
  808. }
  809. }
  810. /**
  811. * Go into state where new uploading can begin.
  812. *
  813. * Warning: this is an internal method used by the framework and should not
  814. * be used by user of the Upload component.
  815. */
  816. private void endUpload() {
  817. isUploading = false;
  818. contentLength = -1;
  819. }
  820. public boolean isUploading() {
  821. return isUploading;
  822. }
  823. /**
  824. * Gets read bytes of the file currently being uploaded.
  825. *
  826. * @return bytes
  827. */
  828. public long getBytesRead() {
  829. return totalBytes;
  830. }
  831. /**
  832. * Returns size of file currently being uploaded. Value sane only during
  833. * upload.
  834. *
  835. * @return size in bytes
  836. */
  837. public long getUploadSize() {
  838. return contentLength;
  839. }
  840. /**
  841. * This method is deprecated, use addListener(ProgressListener) instead.
  842. *
  843. * @deprecated Use addListener(ProgressListener) instead.
  844. * @param progressListener
  845. */
  846. @Deprecated
  847. public void setProgressListener(ProgressListener progressListener) {
  848. this.progressListener = progressListener;
  849. }
  850. /**
  851. * This method is deprecated.
  852. *
  853. * @deprecated Replaced with addListener/removeListener
  854. * @return listener
  855. *
  856. */
  857. @Deprecated
  858. public ProgressListener getProgressListener() {
  859. return progressListener;
  860. }
  861. /**
  862. * ProgressListener receives events to track progress of upload.
  863. */
  864. public interface ProgressListener extends Serializable {
  865. /**
  866. * Updates progress to listener
  867. *
  868. * @param readBytes
  869. * bytes transferred
  870. * @param contentLength
  871. * total size of file currently being uploaded, -1 if unknown
  872. */
  873. public void updateProgress(long readBytes, long contentLength);
  874. }
  875. /**
  876. * @return String to be rendered into button that fires uploading
  877. */
  878. public String getButtonCaption() {
  879. return buttonCaption;
  880. }
  881. /**
  882. * In addition to the actual file chooser, upload components have button
  883. * that starts actual upload progress. This method is used to set text in
  884. * that button.
  885. *
  886. * <p>
  887. * <strong>Note</strong> the string given is set as is to the button. HTML
  888. * formatting is not stripped. Be sure to properly validate your value
  889. * according to your needs.
  890. *
  891. * @param buttonCaption
  892. * text for upload components button.
  893. */
  894. public void setButtonCaption(String buttonCaption) {
  895. this.buttonCaption = buttonCaption;
  896. }
  897. /**
  898. * Notifies the component that it is connected to an application.
  899. *
  900. * @see com.vaadin.ui.Component#attach()
  901. */
  902. @Override
  903. public void attach() {
  904. super.attach();
  905. if (delayedFocus) {
  906. focus();
  907. }
  908. }
  909. }