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.

IUpload.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package com.itmill.toolkit.terminal.gwt.client.ui;
  2. import com.google.gwt.user.client.Timer;
  3. import com.google.gwt.user.client.ui.Button;
  4. import com.google.gwt.user.client.ui.ClickListener;
  5. import com.google.gwt.user.client.ui.FileUpload;
  6. import com.google.gwt.user.client.ui.FlowPanel;
  7. import com.google.gwt.user.client.ui.FormHandler;
  8. import com.google.gwt.user.client.ui.FormPanel;
  9. import com.google.gwt.user.client.ui.FormSubmitCompleteEvent;
  10. import com.google.gwt.user.client.ui.FormSubmitEvent;
  11. import com.google.gwt.user.client.ui.Panel;
  12. import com.google.gwt.user.client.ui.Widget;
  13. import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
  14. import com.itmill.toolkit.terminal.gwt.client.Paintable;
  15. import com.itmill.toolkit.terminal.gwt.client.UIDL;
  16. public class IUpload extends FormPanel implements Paintable, ClickListener,
  17. FormHandler {
  18. /**
  19. * FileUpload component that opens native OS dialog to select file.
  20. */
  21. FileUpload fu = new FileUpload();
  22. Panel panel = new FlowPanel();
  23. ApplicationConnection client;
  24. private String paintableId;
  25. /**
  26. * Button that initiates uploading
  27. */
  28. private Button b;
  29. /**
  30. * When expecting big files, programmer may initiate some UI changes when
  31. * uploading the file starts. Bit after submitting file we'll visit the
  32. * server to check possible changes.
  33. */
  34. private Timer t;
  35. /**
  36. * some browsers tries to send form twice if submit is called in button
  37. * click handler, some don't submit at all without it, so we need to track
  38. * if form is already being submitted
  39. */
  40. private boolean submitted = false;
  41. public IUpload() {
  42. super();
  43. setEncoding(FormPanel.ENCODING_MULTIPART);
  44. setMethod(FormPanel.METHOD_POST);
  45. setWidget(panel);
  46. panel.add(fu);
  47. // TODO
  48. b = new Button("Click to Upload");
  49. b.addClickListener(this);
  50. panel.add(b);
  51. addFormHandler(this);
  52. }
  53. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  54. this.client = client;
  55. this.paintableId = uidl.getId();
  56. setAction(client.getAppUri());
  57. if (uidl.hasAttribute("caption"))
  58. b.setText(uidl.getStringAttribute("caption"));
  59. fu.setName(paintableId + "_file");
  60. }
  61. public void onClick(Widget sender) {
  62. this.submit();
  63. }
  64. public void onSubmit(FormSubmitEvent event) {
  65. if (fu.getFilename().length() == 0 || submitted) {
  66. event.setCancelled(true);
  67. ApplicationConnection.getConsole().log(
  68. "Submit cancelled (no file or already submitted)");
  69. return;
  70. }
  71. submitted = true;
  72. ApplicationConnection.getConsole().log("Submitted form");
  73. disableUpload();
  74. /*
  75. * visit server after upload to see possible changes from UploadStarted
  76. * event
  77. */
  78. t = new Timer() {
  79. public void run() {
  80. client.sendPendingVariableChanges();
  81. }
  82. };
  83. t.schedule(800);
  84. }
  85. protected void disableUpload() {
  86. b.setEnabled(false);
  87. fu.setVisible(false);
  88. }
  89. protected void enableUploaod() {
  90. b.setEnabled(true);
  91. fu.setVisible(true);
  92. }
  93. public void onSubmitComplete(FormSubmitCompleteEvent event) {
  94. if (client != null) {
  95. if (t != null)
  96. t.cancel();
  97. ApplicationConnection.getConsole().log("Submit complete");
  98. client.sendPendingVariableChanges();
  99. }
  100. submitted = false;
  101. enableUploaod();
  102. }
  103. }