2017-09-06 72 views
0

我想將ConfirmDialog的標題「確認」更改爲我的語言。我看着this,但代碼對我來說沒有意義,因爲我的vaadin插件中的show方法是一種靜態方法,而且似乎無法顯示工廠返回的對話框。Vaadin ConfirmDialog更改標題

我用這個代碼來包裝對話框:

package com.xxx.dialog; 

import org.vaadin.dialogs.ConfirmDialog; 

import com.xxx.message.MessageByLocaleService; 
import com.vaadin.ui.Label; 
import com.vaadin.ui.UI; 

public class DeleteDialog { 
    private ConfirmDialog cd; 

    public DeleteDialog(MessageByLocaleService messageByLocaleService) { 
     cd = ConfirmDialog.getFactory() 
       .create(messageByLocaleService.getMessage("caption.confirm"), 
       "", "OK", "Cancel", ""); 
    } 

    public abstract static class MyListener { 
     public abstract void receive(); 
    } 

    public void show(UI ui, MessageByLocaleService messageByLocaleService, MyListener yes, MyListener cancel) { 
     //this.setCaption(messageByLocaleService.getMessage("caption.confirm")); 
     //this.setMessageLabel(new Label(messageByLocaleService.getMessage("caption.confirm"))); 
     cd.show(ui, messageByLocaleService.getMessage("confirm.delete"), 
       new ConfirmDialog.Listener() { 
        private static final long serialVersionUID = 1L; 

        public void onClose(ConfirmDialog dialog) { 
         if (dialog.isConfirmed()) { 
          // Confirmed to continue 
          yes.receive(); 
         } else { 
          // User did not confirm 
          cancel.receive(); 
         } 
        } 
       }); 
     } 
} 

我有這個class DeleteDialogextends ConfirmDialog了一段時間,這就是爲什麼一些我的企圖被註釋掉沒有意義的。

+0

您是否檢查messageByLocaleService.getMessage(「caption.confirm」)是否返回null? – nyg

+0

它不應該爲空,翻譯是在翻譯文件中。問題是'show'是一個靜態方法,我需要一個類方法。也許我會將插件版本降級爲具有非靜態方法的版本。 – Adder

回答

0

我設法實現了我的目標,設置了一個新的默認工廠。

package com.xxx.dialog; 

import org.vaadin.dialogs.ConfirmDialog; 

import com.xxx.message.MessageByLocaleService; 
import com.vaadin.ui.UI; 

public class DeleteDialog { 
    ConfirmDialog.Factory oldFactory; 
    ConfirmDialog.Factory factory; 

    public DeleteDialog(MessageByLocaleService messageByLocaleService) { 
     oldFactory = ConfirmDialog.getFactory(); 
     factory = new ConfirmDialog.Factory() { 

      private static final long serialVersionUID = 1L; 

      @Override 
      public ConfirmDialog create(String arg0, String arg1, String arg2, String arg3, String arg4) { 
      ConfirmDialog cd = oldFactory.create(messageByLocaleService.getMessage("caption.confirm"), 
        "", messageByLocaleService.getMessage("message.ok"), 
         messageByLocaleService.getMessage("message.cancel"), null); 
      cd.setMessage(messageByLocaleService.getMessage("confirm.delete")); 
      return cd; 
      } 
     }; 
    } 

    public abstract static class MyListener { 
     public abstract void receive(); 
    } 

    public void show(UI ui, MessageByLocaleService messageByLocaleService, MyListener yes, MyListener cancel) { 

     ConfirmDialog.setFactory(factory); 
     ConfirmDialog.show(ui, messageByLocaleService.getMessage("confirm.delete"), 
       new ConfirmDialog.Listener() { 
        private static final long serialVersionUID = 1L; 

        public void onClose(ConfirmDialog dialog) { 
         if (dialog.isConfirmed()) { 
          // Confirmed to continue 
          yes.receive(); 
         } else { 
          // User did not confirm 
          cancel.receive(); 
         } 
        } 
       }); 
     } 
}