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.

VUpload.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import com.google.gwt.user.client.Timer;
  6. import com.google.gwt.user.client.ui.Button;
  7. import com.google.gwt.user.client.ui.ClickListener;
  8. import com.google.gwt.user.client.ui.FileUpload;
  9. import com.google.gwt.user.client.ui.FlowPanel;
  10. import com.google.gwt.user.client.ui.FormHandler;
  11. import com.google.gwt.user.client.ui.FormPanel;
  12. import com.google.gwt.user.client.ui.FormSubmitCompleteEvent;
  13. import com.google.gwt.user.client.ui.FormSubmitEvent;
  14. import com.google.gwt.user.client.ui.Panel;
  15. import com.google.gwt.user.client.ui.Widget;
  16. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  17. import com.vaadin.terminal.gwt.client.Paintable;
  18. import com.vaadin.terminal.gwt.client.UIDL;
  19. public class VUpload extends FormPanel implements Paintable, ClickListener,
  20. FormHandler {
  21. public static final String CLASSNAME = "i-upload";
  22. /**
  23. * FileUpload component that opens native OS dialog to select file.
  24. */
  25. FileUpload fu = new FileUpload();
  26. Panel panel = new FlowPanel();
  27. ApplicationConnection client;
  28. private String paintableId;
  29. /**
  30. * Button that initiates uploading
  31. */
  32. private final Button submitButton;
  33. /**
  34. * When expecting big files, programmer may initiate some UI changes when
  35. * uploading the file starts. Bit after submitting file we'll visit the
  36. * server to check possible changes.
  37. */
  38. private Timer t;
  39. /**
  40. * some browsers tries to send form twice if submit is called in button
  41. * click handler, some don't submit at all without it, so we need to track
  42. * if form is already being submitted
  43. */
  44. private boolean submitted = false;
  45. private boolean enabled = true;
  46. public VUpload() {
  47. super();
  48. setEncoding(FormPanel.ENCODING_MULTIPART);
  49. setMethod(FormPanel.METHOD_POST);
  50. setWidget(panel);
  51. panel.add(fu);
  52. submitButton = new Button();
  53. submitButton.addClickListener(this);
  54. panel.add(submitButton);
  55. addFormHandler(this);
  56. setStyleName(CLASSNAME);
  57. }
  58. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  59. if (client.updateComponent(this, uidl, true)) {
  60. return;
  61. }
  62. this.client = client;
  63. paintableId = uidl.getId();
  64. setAction(client.getAppUri());
  65. submitButton.setText(uidl.getStringAttribute("buttoncaption"));
  66. fu.setName(paintableId + "_file");
  67. if (uidl.hasAttribute("disabled") || uidl.hasAttribute("readonly")) {
  68. disableUpload();
  69. } else if (uidl.getBooleanAttribute("state")) {
  70. enableUploaod();
  71. }
  72. }
  73. public void onClick(Widget sender) {
  74. submit();
  75. }
  76. public void onSubmit(FormSubmitEvent event) {
  77. if (fu.getFilename().length() == 0 || submitted || !enabled) {
  78. event.setCancelled(true);
  79. ApplicationConnection
  80. .getConsole()
  81. .log(
  82. "Submit cancelled (disabled, no file or already submitted)");
  83. return;
  84. }
  85. // flush possibly pending variable changes, so they will be handled
  86. // before upload
  87. client.sendPendingVariableChanges();
  88. submitted = true;
  89. ApplicationConnection.getConsole().log("Submitted form");
  90. disableUpload();
  91. /*
  92. * Visit server a moment after upload has started to see possible
  93. * changes from UploadStarted event. Will be cleared on complete.
  94. */
  95. t = new Timer() {
  96. @Override
  97. public void run() {
  98. client.sendPendingVariableChanges();
  99. }
  100. };
  101. t.schedule(800);
  102. }
  103. protected void disableUpload() {
  104. submitButton.setEnabled(false);
  105. fu.setVisible(false);
  106. enabled = false;
  107. }
  108. protected void enableUploaod() {
  109. submitButton.setEnabled(true);
  110. fu.setVisible(true);
  111. enabled = true;
  112. }
  113. public void onSubmitComplete(FormSubmitCompleteEvent event) {
  114. if (client != null) {
  115. if (t != null) {
  116. t.cancel();
  117. }
  118. ApplicationConnection.getConsole().log("Submit complete");
  119. client.sendPendingVariableChanges();
  120. }
  121. submitted = false;
  122. enableUploaod();
  123. }
  124. }