您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

VUpload.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import com.google.gwt.core.client.GWT;
  6. import com.google.gwt.core.client.Scheduler;
  7. import com.google.gwt.dom.client.DivElement;
  8. import com.google.gwt.dom.client.Document;
  9. import com.google.gwt.dom.client.FormElement;
  10. import com.google.gwt.event.dom.client.ClickEvent;
  11. import com.google.gwt.event.dom.client.ClickHandler;
  12. import com.google.gwt.user.client.Command;
  13. import com.google.gwt.user.client.Element;
  14. import com.google.gwt.user.client.Event;
  15. import com.google.gwt.user.client.Timer;
  16. import com.google.gwt.user.client.ui.FileUpload;
  17. import com.google.gwt.user.client.ui.FlowPanel;
  18. import com.google.gwt.user.client.ui.FormPanel;
  19. import com.google.gwt.user.client.ui.Hidden;
  20. import com.google.gwt.user.client.ui.Panel;
  21. import com.google.gwt.user.client.ui.SimplePanel;
  22. import com.google.gwt.user.client.ui.Widget;
  23. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  24. import com.vaadin.terminal.gwt.client.BrowserInfo;
  25. import com.vaadin.terminal.gwt.client.VPaintableWidget;
  26. import com.vaadin.terminal.gwt.client.UIDL;
  27. import com.vaadin.terminal.gwt.client.VConsole;
  28. import com.vaadin.terminal.gwt.client.VTooltip;
  29. /**
  30. *
  31. * Note, we are not using GWT FormPanel as we want to listen submitcomplete
  32. * events even though the upload component is already detached.
  33. *
  34. */
  35. public class VUpload extends SimplePanel implements VPaintableWidget {
  36. private final class MyFileUpload extends FileUpload {
  37. @Override
  38. public void onBrowserEvent(Event event) {
  39. super.onBrowserEvent(event);
  40. if (event.getTypeInt() == Event.ONCHANGE) {
  41. if (immediate && fu.getFilename() != null
  42. && !"".equals(fu.getFilename())) {
  43. submit();
  44. }
  45. } else if (BrowserInfo.get().isIE()
  46. && event.getTypeInt() == Event.ONFOCUS) {
  47. // IE and user has clicked on hidden textarea part of upload
  48. // field. Manually open file selector, other browsers do it by
  49. // default.
  50. fireNativeClick(fu.getElement());
  51. // also remove focus to enable hack if user presses cancel
  52. // button
  53. fireNativeBlur(fu.getElement());
  54. }
  55. }
  56. }
  57. public static final String CLASSNAME = "v-upload";
  58. /**
  59. * FileUpload component that opens native OS dialog to select file.
  60. */
  61. FileUpload fu = new MyFileUpload();
  62. Panel panel = new FlowPanel();
  63. UploadIFrameOnloadStrategy onloadstrategy = GWT
  64. .create(UploadIFrameOnloadStrategy.class);
  65. ApplicationConnection client;
  66. private String paintableId;
  67. /**
  68. * Button that initiates uploading
  69. */
  70. private final VButton submitButton;
  71. /**
  72. * When expecting big files, programmer may initiate some UI changes when
  73. * uploading the file starts. Bit after submitting file we'll visit the
  74. * server to check possible changes.
  75. */
  76. private Timer t;
  77. /**
  78. * some browsers tries to send form twice if submit is called in button
  79. * click handler, some don't submit at all without it, so we need to track
  80. * if form is already being submitted
  81. */
  82. private boolean submitted = false;
  83. private boolean enabled = true;
  84. private boolean immediate;
  85. private Hidden maxfilesize = new Hidden();
  86. private FormElement element;
  87. private com.google.gwt.dom.client.Element synthesizedFrame;
  88. private int nextUploadId;
  89. public VUpload() {
  90. super(com.google.gwt.dom.client.Document.get().createFormElement());
  91. element = getElement().cast();
  92. setEncoding(getElement(), FormPanel.ENCODING_MULTIPART);
  93. element.setMethod(FormPanel.METHOD_POST);
  94. setWidget(panel);
  95. panel.add(maxfilesize);
  96. panel.add(fu);
  97. submitButton = new VButton();
  98. submitButton.addClickHandler(new ClickHandler() {
  99. public void onClick(ClickEvent event) {
  100. if (immediate) {
  101. // fire click on upload (eg. focused button and hit space)
  102. fireNativeClick(fu.getElement());
  103. } else {
  104. submit();
  105. }
  106. }
  107. });
  108. panel.add(submitButton);
  109. setStyleName(CLASSNAME);
  110. sinkEvents(VTooltip.TOOLTIP_EVENTS);
  111. }
  112. @Override
  113. public void onBrowserEvent(Event event) {
  114. if ((event.getTypeInt() & VTooltip.TOOLTIP_EVENTS) > 0) {
  115. client.handleTooltipEvent(event, this);
  116. }
  117. super.onBrowserEvent(event);
  118. }
  119. private static native void setEncoding(Element form, String encoding)
  120. /*-{
  121. form.enctype = encoding;
  122. }-*/;
  123. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  124. if (client.updateComponent(this, uidl, true)) {
  125. return;
  126. }
  127. if (uidl.hasAttribute("notStarted")) {
  128. t.schedule(400);
  129. return;
  130. }
  131. if (uidl.hasAttribute("forceSubmit")) {
  132. submit();
  133. return;
  134. }
  135. setImmediate(uidl.getBooleanAttribute("immediate"));
  136. this.client = client;
  137. paintableId = uidl.getId();
  138. nextUploadId = uidl.getIntAttribute("nextid");
  139. final String action = client.translateVaadinUri(uidl
  140. .getStringVariable("action"));
  141. element.setAction(action);
  142. if (uidl.hasAttribute("buttoncaption")) {
  143. submitButton.setText(uidl.getStringAttribute("buttoncaption"));
  144. submitButton.setVisible(true);
  145. } else {
  146. submitButton.setVisible(false);
  147. }
  148. fu.setName(paintableId + "_file");
  149. if (uidl.hasAttribute("disabled") || uidl.hasAttribute("readonly")) {
  150. disableUpload();
  151. } else if (!uidl.getBooleanAttribute("state")) {
  152. // Enable the button only if an upload is not in progress
  153. enableUpload();
  154. ensureTargetFrame();
  155. }
  156. }
  157. private void setImmediate(boolean booleanAttribute) {
  158. if (immediate != booleanAttribute) {
  159. immediate = booleanAttribute;
  160. if (immediate) {
  161. fu.sinkEvents(Event.ONCHANGE);
  162. fu.sinkEvents(Event.ONFOCUS);
  163. }
  164. }
  165. setStyleName(getElement(), CLASSNAME + "-immediate", immediate);
  166. }
  167. private static native void fireNativeClick(Element element)
  168. /*-{
  169. element.click();
  170. }-*/;
  171. private static native void fireNativeBlur(Element element)
  172. /*-{
  173. element.blur();
  174. }-*/;
  175. protected void disableUpload() {
  176. submitButton.setEnabled(false);
  177. if (!submitted) {
  178. // Cannot disable the fileupload while submitting or the file won't
  179. // be submitted at all
  180. fu.getElement().setPropertyBoolean("disabled", true);
  181. }
  182. enabled = false;
  183. }
  184. protected void enableUpload() {
  185. submitButton.setEnabled(true);
  186. fu.getElement().setPropertyBoolean("disabled", false);
  187. enabled = true;
  188. if (submitted) {
  189. /*
  190. * An old request is still in progress (most likely cancelled),
  191. * ditching that target frame to make it possible to send a new
  192. * file. A new target frame is created later."
  193. */
  194. cleanTargetFrame();
  195. submitted = false;
  196. }
  197. }
  198. /**
  199. * Re-creates file input field and populates panel. This is needed as we
  200. * want to clear existing values from our current file input field.
  201. */
  202. private void rebuildPanel() {
  203. panel.remove(submitButton);
  204. panel.remove(fu);
  205. fu = new MyFileUpload();
  206. fu.setName(paintableId + "_file");
  207. fu.getElement().setPropertyBoolean("disabled", !enabled);
  208. panel.add(fu);
  209. panel.add(submitButton);
  210. if (immediate) {
  211. fu.sinkEvents(Event.ONCHANGE);
  212. }
  213. }
  214. /**
  215. * Called by JSNI (hooked via {@link #onloadstrategy})
  216. */
  217. private void onSubmitComplete() {
  218. /* Needs to be run dereferred to avoid various browser issues. */
  219. Scheduler.get().scheduleDeferred(new Command() {
  220. public void execute() {
  221. if (submitted) {
  222. if (client != null) {
  223. if (t != null) {
  224. t.cancel();
  225. }
  226. VConsole.log("VUpload:Submit complete");
  227. client.sendPendingVariableChanges();
  228. }
  229. rebuildPanel();
  230. submitted = false;
  231. enableUpload();
  232. if (!isAttached()) {
  233. /*
  234. * Upload is complete when upload is already abandoned.
  235. */
  236. cleanTargetFrame();
  237. }
  238. }
  239. }
  240. });
  241. }
  242. private void submit() {
  243. if (fu.getFilename().length() == 0 || submitted || !enabled) {
  244. VConsole.log("Submit cancelled (disabled, no file or already submitted)");
  245. return;
  246. }
  247. // flush possibly pending variable changes, so they will be handled
  248. // before upload
  249. client.sendPendingVariableChanges();
  250. element.submit();
  251. submitted = true;
  252. VConsole.log("Submitted form");
  253. disableUpload();
  254. /*
  255. * Visit server a moment after upload has started to see possible
  256. * changes from UploadStarted event. Will be cleared on complete.
  257. */
  258. t = new Timer() {
  259. @Override
  260. public void run() {
  261. VConsole.log("Visiting server to see if upload started event changed UI.");
  262. client.updateVariable(paintableId, "pollForStart",
  263. nextUploadId, true);
  264. }
  265. };
  266. t.schedule(800);
  267. }
  268. @Override
  269. protected void onAttach() {
  270. super.onAttach();
  271. if (client != null) {
  272. ensureTargetFrame();
  273. }
  274. }
  275. private void ensureTargetFrame() {
  276. if (synthesizedFrame == null) {
  277. // Attach a hidden IFrame to the form. This is the target iframe to
  278. // which
  279. // the form will be submitted. We have to create the iframe using
  280. // innerHTML,
  281. // because setting an iframe's 'name' property dynamically doesn't
  282. // work on
  283. // most browsers.
  284. DivElement dummy = Document.get().createDivElement();
  285. dummy.setInnerHTML("<iframe src=\"javascript:''\" name='"
  286. + getFrameName()
  287. + "' style='position:absolute;width:0;height:0;border:0'>");
  288. synthesizedFrame = dummy.getFirstChildElement();
  289. Document.get().getBody().appendChild(synthesizedFrame);
  290. element.setTarget(getFrameName());
  291. onloadstrategy.hookEvents(synthesizedFrame, this);
  292. }
  293. }
  294. private String getFrameName() {
  295. return paintableId + "_TGT_FRAME";
  296. }
  297. @Override
  298. protected void onDetach() {
  299. super.onDetach();
  300. if (!submitted) {
  301. cleanTargetFrame();
  302. }
  303. }
  304. private void cleanTargetFrame() {
  305. if (synthesizedFrame != null) {
  306. Document.get().getBody().removeChild(synthesizedFrame);
  307. onloadstrategy.unHookEvents(synthesizedFrame);
  308. synthesizedFrame = null;
  309. }
  310. }
  311. public Widget getWidgetForPaintable() {
  312. return this;
  313. }
  314. }