如何在使用vaadin下载文件之前通过StreamResource提供等待图像/弹出窗口

如何在使用vaadin下载文件之前通过StreamResource提供等待图像/弹出窗口,vaadin,Vaadin,我使用streamResource动态创建文件(在用户单击链接后),然后允许用户下载文件。有时创建文件需要10秒以上的时间,因此我希望提供一个等待的图像或相关消息,并使链接处于非活动状态。 但是,我提供的所有更改和新窗口仅在文件准备好下载时执行,而不是更早 有没有办法在流程开始时向用户提供消息? 我用的是瓦丁v8 private StreamResource createResource(ExportItem exportItem, String exportType) { retu

我使用streamResource动态创建文件(在用户单击链接后),然后允许用户下载文件。有时创建文件需要10秒以上的时间,因此我希望提供一个等待的图像或相关消息,并使链接处于非活动状态。 但是,我提供的所有更改和新窗口仅在文件准备好下载时执行,而不是更早
有没有办法在流程开始时向用户提供消息? 我用的是瓦丁v8

private StreamResource createResource(ExportItem exportItem, String exportType) {


    return new StreamResource(new StreamResource.StreamSource() {

        @Override
        public InputStream getStream()
        {
            //Provide a new wait Popup window to let the user know about the delay
            getView().showWaitPopup(true, "Your file is being generated!Please be patient.");

            //the following getFile() method needs about 10-20 seconds to generate the file.
            File file = getFile(exportItem, exportType);

            try {
                return new FileInputStream(file);
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
    }, exportItem.getFilename());
}
你需要的是

没有它(默认情况下),只有在“主”请求线程完成后,才会执行对UI的任何更改

为了立即“推送”对UI的更改,您需要首先在视图上启用
@push
,然后将更改UI的所需代码放入
UI.access(()->{…})中呼叫。(实际上,当在请求线程中时,不能100%确定Vaadin 8中是否需要ui.access)

正如您可能在代码中注意到的,我添加了一个TODO,您通常会恢复用于显示等待微调器的组件的原始状态


我个人不会显示一个弹出窗口来告诉用户他必须等待,因为你没有好的选择来告诉他现在已经准备好了。我通常喜欢在进程开始时将downloadbutton的图标更改为
vaadincon.HOURGLASS
,然后在进程结束时将其更改回

谢谢你的回答,我会查一查的。在我的情况下,我有下载链接,没有任何按钮。你有什么建议让用户现在必须等待吗?没有。这个答案告诉你怎么做,但是你怎么做完全取决于你自己。基本上没有边界,你可以让任何事情发生-无论是CSS、旋转轮、通知,你可以说。或者弹出对话框,我不会阻止你。
@Push
public class SomeUI extends UI {

    ...

    private StreamResource createResource(ExportItem exportItem, String exportType) {
        return new StreamResource(new StreamResource.StreamSource() {

            @Override
            public InputStream getStream()
            {
                // *Immediately* provide a new wait Popup window to let the user know about the delay
                access(() -> {
                    getView().showWaitPopup(true, "Your file is being generated!Please be patient.");
                });

                //the following getFile() method needs about 10-20 seconds to generate the file.
                File file = getFile(exportItem, exportType);

                // TODO: restore original state, or make user know it's ready (close popup?)

                try {
                    return new FileInputStream(file);
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
            }
        }, exportItem.getFilename());
    }
}