2013-08-05 32 views
2

我遇到了PopupViews的下列問題:我的應用程序在UI中設置了一個錯誤處理程序,並且此錯誤處理程序在接收到錯誤事件時通過調用Notification.show(...)顯示一些錯誤通知。在彈出視圖中,我有一個按鈕,它執行一些操作。當單擊按鈕時,彈出視圖關閉(通過調用setPopupVisible(false))並執行操作。但是,如果操作無法運行並引發異常,我希望異常由UI處理,並且錯誤消息將顯示在屏幕上。不幸的是,處理程序接收到錯誤事件並調用Notification.show,但沒有顯示消息。如何關閉PopupView並在同一請求中顯示通知?

有人遇到過類似的問題嗎?

回答

1

您可以使用其他通知系統並運行異步JavaScript代碼。 您也可以在10分鐘內(實現Vaadin AbstractJavaScriptComponent)包裝任何其他通知系統,例如noty2(使用應包括jQuery的LIB):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" 
       }); 
      } 

     } 

它應該是這樣的:

noty2 jQuery plugin in Vaadin7 UI

0

也許處理程序是在不同的線程,而不是在UI線程。我得到了同樣的奇怪的行爲,試圖啓用一個禁用的按鈕,直到我用過了才工作的

Button button = new Button("foo") 

// ... 

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