如何关闭PopupView并在同一请求中显示通知?

时间:2013-08-05 06:26:23

标签: vaadin vaadin7

我遇到了PopupViews的以下问题:我的应用程序在UI中设置了错误处理程序,并且此错误处理程序在收到错误事件时通过调用Notification.show(...)来显示一些错误通知。在弹出视图中,我有一个按钮,它执行一些操作。单击该按钮时,将关闭弹出视图(通过调用setPopupVisible(false))并执行该操作。但是,如果操作无法运行并抛出异常,我希望UI处理该异常,并在屏幕上显示错误消息。不幸的是,处理程序接收错误事件并调用Notification.show,但不显示任何消息。

有人遇到类似问题吗?

2 个答案:

答案 0 :(得分:1)

您可以使用其他通知系统并运行异步JavaScript代码。 您还可以在10分钟内包装任何其他通知系统(实现Vaadin AbstractJavaScriptComponent ),例如 noty2 (使用时应包括jQuery lib):http://needim.github.io/noty/ 当你按照这样执行动作时显示notif:

        new Notty("WARNING<br>description... (static)", Notty.Type.WARNING).show(UI.getCurrent());
        new Notty("WARNING<br>description... (close in 3000ms)", Notty.Type.WARNING, 3000).show(UI.getCurrent());
        new Notty("WARNING<br>description... (modal)", Notty.Type.WARNING, 0, true).show(UI.getCurrent());
        new Notty("WARNING<br>description... (modal force, close in 3000ms)", Notty.Type.WARNING, 3000, true, true).show(UI.getCurrent());
        new Notty("asd", Notty.Type.ERROR, 5000).show(UI.getCurrent());

        package user_interface.util.ext;

        import com.vaadin.annotations.JavaScript;
        import com.vaadin.ui.AbstractJavaScriptComponent;
        import com.vaadin.ui.AbstractOrderedLayout;
        import com.vaadin.ui.UI;
        import java.io.Serializable;
        import java.util.Iterator;

        public class Notty implements Serializable {

            private String message;
            private String type;
            private int closeIn = 0;
            private boolean force = false;
            private boolean modal = false;

            public Notty(String message, Notty.Type type) {
                this.message = message;
                this.type = type.value;
            }

            public Notty(String message, Notty.Type type, int closeIn) {
                this.message = message;
                this.type = type.value;
                this.closeIn = closeIn;
            }

            public Notty(String message, Notty.Type type, int closeIn, boolean modal) {
                this.message = message;
                this.type = type.value;
                this.closeIn = closeIn;
                this.modal = modal;
            }    

            public Notty(String message, Notty.Type type, int closeIn, boolean modal, boolean force) {
                this.message = message;
                this.type = type.value;
                this.closeIn = closeIn;
                this.modal = modal;
                this.force = force;
            }

            public void show(UI ui) {

                AbstractOrderedLayout aol = (AbstractOrderedLayout) ui.getContent();

                NottyMessage nm = null;
                boolean wasAdded = false;
                Iterator itr = aol.iterator();
                while(itr.hasNext()) {
                    Object o = itr.next();
                    if (o instanceof NottyMessage) {
                        nm = (NottyMessage) o;
                        wasAdded = true;
                    }
                }

                if (!wasAdded) {
                    nm = new NottyMessage();
                    aol.addComponent(nm);            
                }

                nm.show(message, type, closeIn, force, modal);
            }

            @JavaScript({"NottyMessage.js"})
            private class NottyMessage extends AbstractJavaScriptComponent {

                public NottyMessage() {
                    setImmediate(true);
                }

                @Override
                protected NottyMessageState getState() {
                    return (NottyMessageState) super.getState();
                }        

                private void show(String mess, String type, int closeIn, boolean force, boolean modal) {
                    callFunction("show", mess, type, closeIn, force, modal);
                }

            }

            public static enum Type {

                ALERT("alert"),
                INFORMATION("information"),
                ERROR("error"),
                WARNING("warning"),
                NOTIFICATION("notification"),
                SUCCESS("success");

                private String value;

                private Type(String val) {
                    this.value = val;
                }
            }

        }

<强> NottyMessageState.java

        package user_interface.util.ext;

        import com.vaadin.shared.ui.JavaScriptComponentState;

        public class NottyMessageState extends JavaScriptComponentState {
            public String xhtml;
            public String type;
        }

<强> NottyMessage.js

        user_interface_util_ext_Notty_NottyMessage = function() {

            var e = this.getElement();

            this.onStateChange = function() {
                // change state callb
            }

            this.show = function(message, messageType, closeInMs, isForce, isModal) {
                var n = noty({
                    text: message,
                    type: messageType,
                    dismissQueue: true,
                    timeout: closeInMs,
                    force: isForce,
                    modal: isModal,
                    maxVisible: 7,
                    layout: "bottomLeft",
                    theme: "defaultTheme"
                });
            }

        }

它应该是这样的:

noty2 jQuery plugin in Vaadin7 UI

答案 1 :(得分:0)

也许处理程序位于不同的线程中而不是UI线程中。我得到了同样奇怪的行为试图启用一个禁用的按钮,直到我使用

后才能工作
Button button = new Button("foo")

// ...

getUI().access(new Runnable(){
   public void run() {
     button.setEnabled(true)
   }
})