2

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

有人遇到过类似的问题吗?

4

2 回答 2

1

您可以使用其他通知系统并运行异步 JavaScript 代码。您也可以在 10 分钟内包装任何其他通知系统(实现 Vaadin AbstractJavaScriptComponent),例如noty2(使用时应包含 jQuery 库):http ://needim.github.io/noty/ 并在执行操作时显示通知,例如这个:

        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"
                });
            }

        }

它应该是这样的:

Vaadin7 UI 中的 noty2 jQuery 插件

于 2013-09-24T06:13:30.267 回答
0

也许处理程序在不同的线程中,而不是在 UI 线程中。我有同样奇怪的行为试图启用一个禁用的按钮,直到我使用它才起作用

Button button = new Button("foo")

// ...

getUI().access(new Runnable(){
   public void run() {
     button.setEnabled(true)
   }
})
于 2015-01-16T17:18:41.687 回答