mirror of
https://gitee.com/jmix/cuba.git
synced 2024-12-04 20:28:00 +08:00
PL-8178 Before/After listeners on ClearButton click action for FileUploadField
This commit is contained in:
parent
135ff826fe
commit
611f8ad18d
@ -81,6 +81,10 @@ public class DesktopFileUploadField extends DesktopAbstractUploadField<CubaFileU
|
|||||||
protected List<FileUploadFinishListener> fileUploadFinishListeners; // lazily initialized list
|
protected List<FileUploadFinishListener> fileUploadFinishListeners; // lazily initialized list
|
||||||
protected List<FileUploadSucceedListener> fileUploadSucceedListeners; // lazily initialized list
|
protected List<FileUploadSucceedListener> fileUploadSucceedListeners; // lazily initialized list
|
||||||
protected List<FileUploadErrorListener> fileUploadErrorListeners; // lazily initialized list
|
protected List<FileUploadErrorListener> fileUploadErrorListeners; // lazily initialized list
|
||||||
|
|
||||||
|
protected List<BeforeValueClearListener> beforeValueClearListeners; // lazily initialized list
|
||||||
|
protected List<AfterValueClearListener> afterValueClearListeners; // lazily initialized list
|
||||||
|
|
||||||
protected FileDescriptor prevValue;
|
protected FileDescriptor prevValue;
|
||||||
|
|
||||||
public DesktopFileUploadField() {
|
public DesktopFileUploadField() {
|
||||||
@ -121,10 +125,30 @@ public class DesktopFileUploadField extends DesktopAbstractUploadField<CubaFileU
|
|||||||
exportDisplay.show(value);
|
exportDisplay.show(value);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
impl.setClearButtonListener(() -> {
|
impl.setClearButtonListener(this::clearButtonClicked);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void clearButtonClicked() {
|
||||||
|
boolean preventClearAction = false;
|
||||||
|
if (beforeValueClearListeners != null) {
|
||||||
|
BeforeValueClearEvent beforeValueClearEvent = new BeforeValueClearEvent(this);
|
||||||
|
for (BeforeValueClearListener listener : new ArrayList<>(beforeValueClearListeners)) {
|
||||||
|
listener.beforeValueClearPerformed(beforeValueClearEvent);
|
||||||
|
}
|
||||||
|
preventClearAction = beforeValueClearEvent.isClearPrevented();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!preventClearAction) {
|
||||||
setValue(null);
|
setValue(null);
|
||||||
fileName = null;
|
fileName = null;
|
||||||
});
|
}
|
||||||
|
|
||||||
|
if (afterValueClearListeners != null) {
|
||||||
|
AfterValueClearEvent afterValueClearEvent = new AfterValueClearEvent(this, !preventClearAction);
|
||||||
|
for (AfterValueClearListener listener : new ArrayList<>(afterValueClearListeners)) {
|
||||||
|
listener.afterValueClearPerformed(afterValueClearEvent);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void uploadFile(File file) {
|
protected void uploadFile(File file) {
|
||||||
@ -719,4 +743,38 @@ public class DesktopFileUploadField extends DesktopAbstractUploadField<CubaFileU
|
|||||||
public String getClearButtonDescription() {
|
public String getClearButtonDescription() {
|
||||||
return impl.getClearButtonDescription();
|
return impl.getClearButtonDescription();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addBeforeValueClearListener(BeforeValueClearListener listener) {
|
||||||
|
if (beforeValueClearListeners == null) {
|
||||||
|
beforeValueClearListeners = new ArrayList<>();
|
||||||
|
}
|
||||||
|
if (!beforeValueClearListeners.contains(listener)) {
|
||||||
|
beforeValueClearListeners.add(listener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeBeforeValueClearListener(BeforeValueClearListener listener) {
|
||||||
|
if (beforeValueClearListeners != null) {
|
||||||
|
beforeValueClearListeners.remove(listener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addAfterValueClearListener(AfterValueClearListener listener) {
|
||||||
|
if (afterValueClearListeners == null) {
|
||||||
|
afterValueClearListeners = new ArrayList<>();
|
||||||
|
}
|
||||||
|
if (!afterValueClearListeners.contains(listener)) {
|
||||||
|
afterValueClearListeners.add(listener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeAfterValueClearListener(AfterValueClearListener listener) {
|
||||||
|
if (afterValueClearListeners != null) {
|
||||||
|
afterValueClearListeners.remove(listener);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -142,16 +142,15 @@ public interface FileUploadField extends UploadField, Field {
|
|||||||
* Enable or disable displaying name of uploaded file next to upload button.
|
* Enable or disable displaying name of uploaded file next to upload button.
|
||||||
*/
|
*/
|
||||||
void setShowFileName(boolean showFileName);
|
void setShowFileName(boolean showFileName);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return true if name of uploaded file is shown.
|
* @return true if name of uploaded file is shown.
|
||||||
*/
|
*/
|
||||||
boolean isShowFileName();
|
boolean isShowFileName();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setup caption of upload button.
|
* Setup caption of upload button.
|
||||||
*/
|
*/
|
||||||
void setUploadButtonCaption(String caption);
|
void setUploadButtonCaption(String caption);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return upload button caption.
|
* @return upload button caption.
|
||||||
*/
|
*/
|
||||||
@ -161,7 +160,6 @@ public interface FileUploadField extends UploadField, Field {
|
|||||||
* Setup upload button icon.
|
* Setup upload button icon.
|
||||||
*/
|
*/
|
||||||
void setUploadButtonIcon(String icon);
|
void setUploadButtonIcon(String icon);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return upload button icon.
|
* @return upload button icon.
|
||||||
*/
|
*/
|
||||||
@ -171,7 +169,6 @@ public interface FileUploadField extends UploadField, Field {
|
|||||||
* Setup upload button description.
|
* Setup upload button description.
|
||||||
*/
|
*/
|
||||||
void setUploadButtonDescription(String description);
|
void setUploadButtonDescription(String description);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return upload button description.
|
* @return upload button description.
|
||||||
*/
|
*/
|
||||||
@ -181,7 +178,6 @@ public interface FileUploadField extends UploadField, Field {
|
|||||||
* Enable or disable displaying name of clear button.
|
* Enable or disable displaying name of clear button.
|
||||||
*/
|
*/
|
||||||
void setShowClearButton(boolean showClearButton);
|
void setShowClearButton(boolean showClearButton);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return true if clear button is shown.
|
* @return true if clear button is shown.
|
||||||
*/
|
*/
|
||||||
@ -191,7 +187,6 @@ public interface FileUploadField extends UploadField, Field {
|
|||||||
* Setup clear button caption.
|
* Setup clear button caption.
|
||||||
*/
|
*/
|
||||||
void setClearButtonCaption(String caption);
|
void setClearButtonCaption(String caption);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return clear button caption.
|
* @return clear button caption.
|
||||||
*/
|
*/
|
||||||
@ -201,7 +196,6 @@ public interface FileUploadField extends UploadField, Field {
|
|||||||
* Setup clear button icon.
|
* Setup clear button icon.
|
||||||
*/
|
*/
|
||||||
void setClearButtonIcon(String icon);
|
void setClearButtonIcon(String icon);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return clear button icon.
|
* @return clear button icon.
|
||||||
*/
|
*/
|
||||||
@ -211,14 +205,77 @@ public interface FileUploadField extends UploadField, Field {
|
|||||||
* Setup clear button description.
|
* Setup clear button description.
|
||||||
*/
|
*/
|
||||||
void setClearButtonDescription(String description);
|
void setClearButtonDescription(String description);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return clear button description.
|
* @return clear button description.
|
||||||
*/
|
*/
|
||||||
String getClearButtonDescription();
|
String getClearButtonDescription();
|
||||||
|
|
||||||
|
class BeforeValueClearEvent {
|
||||||
|
private FileUploadField target;
|
||||||
|
private boolean clearPrevented = false;
|
||||||
|
|
||||||
|
public BeforeValueClearEvent(FileUploadField target) {
|
||||||
|
this.target = target;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isClearPrevented() {
|
||||||
|
return clearPrevented;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void preventClearAction() {
|
||||||
|
this.clearPrevented = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public FileUploadField getTarget() {
|
||||||
|
return target;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set mode which determines when {@link FileDescriptor} will be commited.
|
* Callback interface which is invoked by the {@link FileUploadField} before value clearing when user use clear button. <br/>
|
||||||
|
* Listener can prevent value clearing using {@link BeforeValueClearEvent#preventClearAction()}.
|
||||||
|
*
|
||||||
|
* @see #setShowClearButton(boolean)
|
||||||
|
*/
|
||||||
|
interface BeforeValueClearListener {
|
||||||
|
void beforeValueClearPerformed(BeforeValueClearEvent event);
|
||||||
|
}
|
||||||
|
|
||||||
|
void addBeforeValueClearListener(BeforeValueClearListener listener);
|
||||||
|
void removeBeforeValueClearListener(BeforeValueClearListener listener);
|
||||||
|
|
||||||
|
class AfterValueClearEvent {
|
||||||
|
private FileUploadField target;
|
||||||
|
private boolean valueCleared;
|
||||||
|
|
||||||
|
public AfterValueClearEvent(FileUploadField target, boolean valueCleared) {
|
||||||
|
this.target = target;
|
||||||
|
this.valueCleared = valueCleared;
|
||||||
|
}
|
||||||
|
|
||||||
|
public FileUploadField getTarget() {
|
||||||
|
return target;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isValueCleared() {
|
||||||
|
return valueCleared;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Callback interface which is invoked by the {@link FileUploadField} after value has been cleared using clear button.
|
||||||
|
*
|
||||||
|
* @see #setShowClearButton(boolean)
|
||||||
|
*/
|
||||||
|
interface AfterValueClearListener {
|
||||||
|
void afterValueClearPerformed(AfterValueClearEvent event);
|
||||||
|
}
|
||||||
|
|
||||||
|
void addAfterValueClearListener(AfterValueClearListener listener);
|
||||||
|
void removeAfterValueClearListener(AfterValueClearListener listener);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set mode which determines when {@link FileDescriptor} will be committed.
|
||||||
*/
|
*/
|
||||||
void setMode(FileStoragePutMode mode);
|
void setMode(FileStoragePutMode mode);
|
||||||
/**
|
/**
|
||||||
@ -231,10 +288,9 @@ public interface FileUploadField extends UploadField, Field {
|
|||||||
* <p>Passed content provider will be used for downloading by clicking the link with file name
|
* <p>Passed content provider will be used for downloading by clicking the link with file name
|
||||||
* or as source for {@link FileUploadField#getFileContent()} method.</p>
|
* or as source for {@link FileUploadField#getFileContent()} method.</p>
|
||||||
*
|
*
|
||||||
* @param contentProvider
|
* @param contentProvider content provider
|
||||||
*/
|
*/
|
||||||
void setContentProvider(FileContentProvider contentProvider);
|
void setContentProvider(FileContentProvider contentProvider);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return FileContentProvider which can be used to read data from field
|
* @return FileContentProvider which can be used to read data from field
|
||||||
*/
|
*/
|
||||||
|
@ -292,7 +292,7 @@ public class CubaFileUploadWrapper extends CustomField {
|
|||||||
return clearButton.getIcon().toString();
|
return clearButton.getIcon().toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setClearButtonAction(Button.ClickListener listener) {
|
public void setClearButtonListener(Button.ClickListener listener) {
|
||||||
clearButton.addClickListener(listener);
|
clearButton.addClickListener(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,6 +74,9 @@ public class WebFileUploadField extends WebAbstractUploadField<CubaFileUploadWra
|
|||||||
protected List<FileUploadErrorListener> fileUploadErrorListeners; // lazily initialized list
|
protected List<FileUploadErrorListener> fileUploadErrorListeners; // lazily initialized list
|
||||||
protected List<FileUploadSucceedListener> fileUploadSucceedListeners; // lazily initialized list
|
protected List<FileUploadSucceedListener> fileUploadSucceedListeners; // lazily initialized list
|
||||||
|
|
||||||
|
protected List<BeforeValueClearListener> beforeValueClearListeners; // lazily initialized list
|
||||||
|
protected List<AfterValueClearListener> afterValueClearListeners; // lazily initialized list
|
||||||
|
|
||||||
public WebFileUploadField() {
|
public WebFileUploadField() {
|
||||||
fileUploading = AppBeans.get(FileUploadingAPI.NAME);
|
fileUploading = AppBeans.get(FileUploadingAPI.NAME);
|
||||||
exportDisplay = AppBeans.get(ExportDisplay.NAME);
|
exportDisplay = AppBeans.get(ExportDisplay.NAME);
|
||||||
@ -105,10 +108,30 @@ public class WebFileUploadField extends WebAbstractUploadField<CubaFileUploadWra
|
|||||||
exportDisplay.show(value);
|
exportDisplay.show(value);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
component.setClearButtonAction((Button.ClickListener) event -> {
|
component.setClearButtonListener((Button.ClickListener) this::clearButtonClicked);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void clearButtonClicked(Button.ClickEvent clickEvent) {
|
||||||
|
boolean preventClearAction = false;
|
||||||
|
if (beforeValueClearListeners != null) {
|
||||||
|
BeforeValueClearEvent beforeValueClearEvent = new BeforeValueClearEvent(this);
|
||||||
|
for (BeforeValueClearListener listener : new ArrayList<>(beforeValueClearListeners)) {
|
||||||
|
listener.beforeValueClearPerformed(beforeValueClearEvent);
|
||||||
|
}
|
||||||
|
preventClearAction = beforeValueClearEvent.isClearPrevented();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!preventClearAction) {
|
||||||
setValue(null);
|
setValue(null);
|
||||||
fileName = null;
|
fileName = null;
|
||||||
});
|
}
|
||||||
|
|
||||||
|
if (afterValueClearListeners != null) {
|
||||||
|
AfterValueClearEvent afterValueClearEvent = new AfterValueClearEvent(this, !preventClearAction);
|
||||||
|
for (AfterValueClearListener listener : new ArrayList<>(afterValueClearListeners)) {
|
||||||
|
listener.afterValueClearPerformed(afterValueClearEvent);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void saveFile(FileDescriptor fileDescriptor) {
|
protected void saveFile(FileDescriptor fileDescriptor) {
|
||||||
@ -578,10 +601,12 @@ public class WebFileUploadField extends WebAbstractUploadField<CubaFileUploadWra
|
|||||||
this.mode = mode;
|
this.mode = mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public boolean isShowFileName() {
|
public boolean isShowFileName() {
|
||||||
return component.isShowFileName();
|
return component.isShowFileName();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void setShowFileName(boolean showFileName) {
|
public void setShowFileName(boolean showFileName) {
|
||||||
component.setShowFileName(showFileName);
|
component.setShowFileName(showFileName);
|
||||||
if (showFileName && StringUtils.isNotEmpty(fileName)) {
|
if (showFileName && StringUtils.isNotEmpty(fileName)) {
|
||||||
@ -645,6 +670,40 @@ public class WebFileUploadField extends WebAbstractUploadField<CubaFileUploadWra
|
|||||||
return component.getClearButtonDescription();
|
return component.getClearButtonDescription();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addBeforeValueClearListener(BeforeValueClearListener listener) {
|
||||||
|
if (beforeValueClearListeners == null) {
|
||||||
|
beforeValueClearListeners = new ArrayList<>();
|
||||||
|
}
|
||||||
|
if (!beforeValueClearListeners.contains(listener)) {
|
||||||
|
beforeValueClearListeners.add(listener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeBeforeValueClearListener(BeforeValueClearListener listener) {
|
||||||
|
if (beforeValueClearListeners != null) {
|
||||||
|
beforeValueClearListeners.remove(listener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addAfterValueClearListener(AfterValueClearListener listener) {
|
||||||
|
if (afterValueClearListeners == null) {
|
||||||
|
afterValueClearListeners = new ArrayList<>();
|
||||||
|
}
|
||||||
|
if (!afterValueClearListeners.contains(listener)) {
|
||||||
|
afterValueClearListeners.add(listener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeAfterValueClearListener(AfterValueClearListener listener) {
|
||||||
|
if (afterValueClearListeners != null) {
|
||||||
|
afterValueClearListeners.remove(listener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Upload button
|
* Upload button
|
||||||
* */
|
* */
|
||||||
|
Loading…
Reference in New Issue
Block a user