diff options
Diffstat (limited to 'client')
-rw-r--r-- | client/src/main/java/com/vaadin/client/ui/VUpload.java | 62 | ||||
-rw-r--r-- | client/src/main/java/com/vaadin/client/ui/upload/UploadConnector.java | 14 |
2 files changed, 54 insertions, 22 deletions
diff --git a/client/src/main/java/com/vaadin/client/ui/VUpload.java b/client/src/main/java/com/vaadin/client/ui/VUpload.java index e72fe3e838..543cc1d2e6 100644 --- a/client/src/main/java/com/vaadin/client/ui/VUpload.java +++ b/client/src/main/java/com/vaadin/client/ui/VUpload.java @@ -58,8 +58,7 @@ public class VUpload extends SimplePanel { public void onBrowserEvent(Event event) { super.onBrowserEvent(event); if (event.getTypeInt() == Event.ONCHANGE) { - if (isImmediateMode() && fu.getFilename() != null - && !fu.getFilename().isEmpty()) { + if (isImmediateMode() && hasFilename()) { submit(); } } else if (BrowserInfo.get().isIE() @@ -122,6 +121,15 @@ public class VUpload extends SimplePanel { private boolean immediateMode; + /** + * Just-in-case option to override the default assumption that if no file + * has been selected, no upload attempt should happen. Not part of public + * API so can be removed without a warning -- if you have an actual need for + * this feature (which is currently only accessible through violator + * pattern), let us know. + */ + private boolean allowUploadWithoutFilename = false; + private String acceptMimeTypes; private Hidden maxfilesize = new Hidden(); @@ -144,6 +152,19 @@ public class VUpload extends SimplePanel { setWidget(panel); panel.add(maxfilesize); panel.add(fu); + fu.addChangeHandler(event -> { + if (!isImmediateMode()) { + updateEnabledForSubmitButton(); + } + if (client != null) { + UploadConnector connector = ((UploadConnector) ConnectorMap + .get(client).getConnector(VUpload.this)); + if (connector.hasEventListener(EventId.CHANGE)) { + connector.getRpcProxy(UploadServerRpc.class) + .change(fu.getFilename()); + } + } + }); submitButton = new VButton(); submitButton.addClickHandler(event -> { if (isImmediateMode()) { @@ -180,6 +201,7 @@ public class VUpload extends SimplePanel { fu.unsinkEvents(Event.ONCHANGE); fu.unsinkEvents(Event.ONFOCUS); } + updateEnabledForSubmitButton(); } setStyleName(getElement(), CLASSNAME + "-immediate", immediateMode); } @@ -205,20 +227,20 @@ public class VUpload extends SimplePanel { /** For internal use only. May be removed or replaced in the future. */ public void disableUpload() { - setEnabledForSubmitButton(false); if (!submitted) { // Cannot disable the fileupload while submitting or the file won't // be submitted at all fu.getElement().setPropertyBoolean("disabled", true); } enabled = false; + updateEnabledForSubmitButton(); } /** For internal use only. May be removed or replaced in the future. */ public void enableUpload() { - setEnabledForSubmitButton(true); fu.getElement().setPropertyBoolean("disabled", false); enabled = true; + updateEnabledForSubmitButton(); if (submitted) { /* * An old request is still in progress (most likely cancelled), @@ -242,9 +264,27 @@ public class VUpload extends SimplePanel { } } - private void setEnabledForSubmitButton(boolean enabled) { - submitButton.setEnabled(enabled); - submitButton.setStyleName(StyleConstants.DISABLED, !enabled); + /** + * Updates the enabled status for submit button. If the widget itself is + * disabled, so is the submit button. It must also follow overall enabled + * status in immediate mode, otherwise you cannot select a file at all. In + * non-immediate mode there is another button for selecting the file, so the + * submit button should be disabled until a file has been selected, unless + * upload without selection has been specifically allowed. + */ + private void updateEnabledForSubmitButton() { + if (enabled && (isImmediateMode() || hasFilename() + || allowUploadWithoutFilename)) { + submitButton.setEnabled(true); + submitButton.setStyleName(StyleConstants.DISABLED, false); + } else { + submitButton.setEnabled(false); + submitButton.setStyleName(StyleConstants.DISABLED, true); + } + } + + private boolean hasFilename() { + return fu.getFilename() != null && !fu.getFilename().isEmpty(); } /** @@ -266,6 +306,9 @@ public class VUpload extends SimplePanel { fu.sinkEvents(Event.ONCHANGE); } fu.addChangeHandler(event -> { + if (!isImmediateMode()) { + updateEnabledForSubmitButton(); + } if (client != null) { UploadConnector connector = ((UploadConnector) ConnectorMap .get(client).getConnector(VUpload.this)); @@ -350,7 +393,10 @@ public class VUpload extends SimplePanel { .info("Submit cancelled (disabled or already submitted)"); return; } - if (fu.getFilename().isEmpty()) { + if (!hasFilename()) { + if (!allowUploadWithoutFilename) { + return; + } getLogger().info("Submitting empty selection (no file)"); } // flush possibly pending variable changes, so they will be handled diff --git a/client/src/main/java/com/vaadin/client/ui/upload/UploadConnector.java b/client/src/main/java/com/vaadin/client/ui/upload/UploadConnector.java index 400c61a4f5..87f726eb15 100644 --- a/client/src/main/java/com/vaadin/client/ui/upload/UploadConnector.java +++ b/client/src/main/java/com/vaadin/client/ui/upload/UploadConnector.java @@ -22,10 +22,8 @@ import com.vaadin.client.UIDL; import com.vaadin.client.communication.StateChangeEvent; import com.vaadin.client.ui.AbstractComponentConnector; import com.vaadin.client.ui.VUpload; -import com.vaadin.shared.EventId; import com.vaadin.shared.ui.Connect; import com.vaadin.shared.ui.upload.UploadClientRpc; -import com.vaadin.shared.ui.upload.UploadServerRpc; import com.vaadin.shared.ui.upload.UploadState; import com.vaadin.ui.Upload; @@ -38,18 +36,6 @@ public class UploadConnector extends AbstractComponentConnector } @Override - protected void init() { - super.init(); - - getWidget().fu.addChangeHandler(event -> { - if (hasEventListener(EventId.CHANGE)) { - getRpcProxy(UploadServerRpc.class) - .change(getWidget().fu.getFilename()); - } - }); - } - - @Override public void updateFromUIDL(UIDL uidl, ApplicationConnection client) { if (!isRealUpdate(uidl)) { return; |