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

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