diff options
-rw-r--r-- | src/com/vaadin/terminal/gwt/client/ui/VUpload.java | 11 | ||||
-rw-r--r-- | src/com/vaadin/ui/Upload.java | 51 | ||||
-rw-r--r-- | tests/src/com/vaadin/tests/components/upload/ForceSubmit.java | 73 |
3 files changed, 133 insertions, 2 deletions
diff --git a/src/com/vaadin/terminal/gwt/client/ui/VUpload.java b/src/com/vaadin/terminal/gwt/client/ui/VUpload.java index f267f97860..ddca44d7d4 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VUpload.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VUpload.java @@ -143,12 +143,21 @@ public class VUpload extends SimplePanel implements Paintable { t.schedule(400); return; } + if(uidl.hasAttribute("forceSubmit")) { + element.submit(); + return; + } setImmediate(uidl.getBooleanAttribute("immediate")); this.client = client; paintableId = uidl.getId(); nextUploadId = uidl.getIntAttribute("nextid"); element.setAction(uidl.getStringVariable("action")); - submitButton.setText(uidl.getStringAttribute("buttoncaption")); + if(uidl.hasAttribute("buttoncaption")) { + submitButton.setText(uidl.getStringAttribute("buttoncaption")); + submitButton.setVisible(true); + } else { + submitButton.setVisible(false); + } fu.setName(paintableId + "_file"); if (uidl.hasAttribute("disabled") || uidl.hasAttribute("readonly")) { diff --git a/src/com/vaadin/ui/Upload.java b/src/com/vaadin/ui/Upload.java index 51de029108..3928f809f3 100644 --- a/src/com/vaadin/ui/Upload.java +++ b/src/com/vaadin/ui/Upload.java @@ -98,6 +98,11 @@ public class Upload extends AbstractComponent implements Component.Focusable { private int nextid; /** + * Flag to indicate that submitting file has been requested. + */ + private boolean forceSubmit; + + /** * Creates a new instance of Upload. * * The receiver must be set before performing an upload. @@ -143,6 +148,11 @@ public class Upload extends AbstractComponent implements Component.Focusable { notStarted = false; return; } + if (forceSubmit) { + target.addAttribute("forceSubmit", true); + forceSubmit = true; + return; + } // The field should be focused if (focus) { target.addAttribute("focus", true); @@ -155,7 +165,9 @@ public class Upload extends AbstractComponent implements Component.Focusable { target.addAttribute("state", isUploading); - target.addAttribute("buttoncaption", buttonCaption); + if (buttonCaption != null) { + target.addAttribute("buttoncaption", buttonCaption); + } target.addAttribute("nextid", nextid); @@ -901,6 +913,15 @@ public class Upload extends AbstractComponent implements Component.Focusable { * In addition to the actual file chooser, upload components have button * that starts actual upload progress. This method is used to set text in * that button. + * <p> + * In case the button text is set to null, the button is hidden. In this + * case developer must explicitly initiate the upload process with + * {@link #submitUpload()}. + * <p> + * In case the Upload is used in immediate mode using + * {@link #setImmediate(boolean)}, the file choose (html input with type + * "file") is hidden and only the button with this text is shown. + * <p> * * <p> * <strong>Note</strong> the string given is set as is to the button. HTML @@ -915,6 +936,34 @@ public class Upload extends AbstractComponent implements Component.Focusable { requestRepaint(); } + /** + * Forces the upload the send selected file to the server. + * <p> + * In case developer wants to use this feature, he/she will most probably + * want to hide the uploads internal submit button by setting its caption to + * null with {@link #setButtonCaption(String)} method. + * <p> + * Note, that the upload runs asynchronous. Developer should use normal + * upload listeners to trac the process of upload. If the field is empty + * uploaded the file name will be empty string and file length 0 in the + * upload finished event. + * <p> + * Also note, that the developer should not remove or modify the upload in + * the same user transaction where the upload submit is requested. The + * upload may safely be hidden or removed once the upload started event is + * fired. + */ + public void submitUpload() { + requestRepaint(); + forceSubmit = true; + } + + @Override + public void requestRepaint() { + forceSubmit = false; + super.requestRepaint(); + } + /* * Handle to terminal via Upload monitors and controls the upload during it * is being streamed. diff --git a/tests/src/com/vaadin/tests/components/upload/ForceSubmit.java b/tests/src/com/vaadin/tests/components/upload/ForceSubmit.java new file mode 100644 index 0000000000..fef82dd962 --- /dev/null +++ b/tests/src/com/vaadin/tests/components/upload/ForceSubmit.java @@ -0,0 +1,73 @@ +package com.vaadin.tests.components.upload;
+
+import java.io.ByteArrayOutputStream;
+import java.io.OutputStream;
+
+import com.vaadin.tests.components.TestBase;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.Button.ClickListener;
+import com.vaadin.ui.Upload;
+import com.vaadin.ui.Button.ClickEvent;
+import com.vaadin.ui.Upload.FailedEvent;
+import com.vaadin.ui.Upload.FinishedEvent;
+import com.vaadin.ui.Upload.Receiver;
+
+public class ForceSubmit extends TestBase implements Receiver {
+
+ @Override
+ protected Integer getTicketNumber() {
+ return 6630;
+ }
+
+ public OutputStream receiveUpload(String filename, String MIMEType) {
+ return new ByteArrayOutputStream();
+ }
+
+ @Override
+ protected void setup() {
+
+ final Upload u;
+
+ u = new Upload("Upload", this);
+
+ u.setButtonCaption(null);
+
+ addComponent(u);
+
+ u.addListener(new Upload.FinishedListener() {
+ public void uploadFinished(FinishedEvent event) {
+ String filename = event.getFilename();
+ long length = event.getLength();
+ getMainWindow().showNotification(
+ "Done. Filename : " + filename + " Lenght: " + length);
+ }
+ });
+
+ u.addListener(new Upload.FailedListener() {
+ public void uploadFailed(FailedEvent event) {
+ getMainWindow().showNotification("Failed. No file selected?");
+ }
+ });
+
+ Button button = new Button(
+ "I'm an external button (not the uploads builtin), hit me to start upload.");
+ button.addListener(new ClickListener() {
+ public void buttonClick(ClickEvent event) {
+ u.submitUpload();
+ }
+ });
+
+ addComponent(button);
+
+ }
+
+ @Override
+ protected String getDescription() {
+ return "Some wireframists are just so web 1.0. If requirements " +
+ "say the upload must not start until the whole form " +
+ "is 'Oukeyd', that is what we gotta do. In these cases " +
+ "developers most probably also want to hide the uploads" +
+ " internal button by setting its caption to null.";
+ }
+
+}
|