123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- package com.itmill.toolkit.terminal.gwt.client.ui;
-
- import com.google.gwt.user.client.Timer;
- import com.google.gwt.user.client.ui.Button;
- import com.google.gwt.user.client.ui.ClickListener;
- import com.google.gwt.user.client.ui.FileUpload;
- import com.google.gwt.user.client.ui.FlowPanel;
- import com.google.gwt.user.client.ui.FormHandler;
- import com.google.gwt.user.client.ui.FormPanel;
- import com.google.gwt.user.client.ui.FormSubmitCompleteEvent;
- import com.google.gwt.user.client.ui.FormSubmitEvent;
- import com.google.gwt.user.client.ui.Panel;
- import com.google.gwt.user.client.ui.Widget;
- import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
- import com.itmill.toolkit.terminal.gwt.client.Paintable;
- import com.itmill.toolkit.terminal.gwt.client.UIDL;
-
- public class IUpload extends FormPanel implements Paintable, ClickListener,
- FormHandler {
-
- /**
- * FileUpload component that opens native OS dialog to select file.
- */
- FileUpload fu = new FileUpload();
-
- Panel panel = new FlowPanel();
-
- ApplicationConnection client;
-
- private String paintableId;
-
- /**
- * Button that initiates uploading
- */
- private Button b;
-
- /**
- * When expecting big files, programmer may initiate some UI changes when
- * uploading the file starts. Bit after submitting file we'll visit the
- * server to check possible changes.
- */
- private Timer t;
-
- /**
- * some browsers tries to send form twice if submit is called in button
- * click handler, some don't submit at all without it, so we need to track
- * if form is already being submitted
- */
- private boolean submitted = false;
-
- public IUpload() {
- super();
- setEncoding(FormPanel.ENCODING_MULTIPART);
- setMethod(FormPanel.METHOD_POST);
-
- setWidget(panel);
- panel.add(fu);
- // TODO
- b = new Button("Click to Upload");
- b.addClickListener(this);
- panel.add(b);
-
- addFormHandler(this);
- }
-
- public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
- this.client = client;
- this.paintableId = uidl.getId();
- setAction(client.getAppUri());
-
- if (uidl.hasAttribute("caption"))
- b.setText(uidl.getStringAttribute("caption"));
- fu.setName(paintableId + "_file");
-
- }
-
- public void onClick(Widget sender) {
- this.submit();
- }
-
- public void onSubmit(FormSubmitEvent event) {
- if (fu.getFilename().length() == 0 || submitted) {
- event.setCancelled(true);
- ApplicationConnection.getConsole().log(
- "Submit cancelled (no file or already submitted)");
- return;
- }
- submitted = true;
- ApplicationConnection.getConsole().log("Submitted form");
-
- disableUpload();
-
- /*
- * visit server after upload to see possible changes from UploadStarted
- * event
- */
- t = new Timer() {
- public void run() {
- client.sendPendingVariableChanges();
- }
- };
- t.schedule(800);
- }
-
- protected void disableUpload() {
- b.setEnabled(false);
- fu.setVisible(false);
- }
-
- protected void enableUploaod() {
- b.setEnabled(true);
- fu.setVisible(true);
- }
-
- public void onSubmitComplete(FormSubmitCompleteEvent event) {
- if (client != null) {
- if (t != null)
- t.cancel();
- ApplicationConnection.getConsole().log("Submit complete");
- client.sendPendingVariableChanges();
- }
- submitted = false;
- enableUploaod();
- }
-
- }
|