首页 > 解决方案 > Vaadin ConfirmDialog 自定义

问题描述

我对对话框的问题是有时第三个按钮不显示。它在测试期间显示出来,但让代码在 git 中腐烂一天并重新测试它,它只显示两个按钮。

package com.xxx.bpspkpibpcheck.dialog;

import org.vaadin.dialogs.ConfirmDialog;

import com.xxx.bpspkpibpcheck.message.UIMessageByLocaleService;
import com.vaadin.ui.UI;

/**
 * Class representing the save as disabled dialog
 */
public class SaveAsDisabledDialog {
    //translation prefixes
    //private static final String DIALOG = "Dialog.";
    private static final String SAVEASDISABLEDDIALOG = "SaveAsDisabledDialog.";

    //translation postfixes
    //private static final String MESSAGE_DISACTIVATE = "message.disactivate";
    private static final String MESSAGE_CANCEL = "message.cancel";
    private static final String CAPTION_SAVEASDISABLED = "caption.saveasdisabled";
    protected static final String MESSAGE_YES = "message.yes";
    protected static final String MESSAGE_NO = "message.no";
    private static final String MESSAGE_SAVEASDISABLED = "message.saveasdisabled";

    protected static final String TOOLTIP_YES = "tooltip.yes";
    protected static final String TOOLTIP_NO = "tooltip.no";
    protected static final String TOOLTIP_CANCEL = "tooltip.cancel";

    //factories
    ConfirmDialog.Factory oldFactory;
    ConfirmDialog.Factory factory;

    /**
     * Construct a SaveAsDisabledDialog
     * @param messageByLocaleService
     */
    public SaveAsDisabledDialog(UIMessageByLocaleService 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(SAVEASDISABLEDDIALOG + CAPTION_SAVEASDISABLED),
                        "message",
                          "xxx", //messageByLocaleService.getMessage(DIALOG + MESSAGE_CANCEL),
                          "yyy", //messageByLocaleService.getMessage(SAVEASDISABLEDDIALOG + MESSAGE_YES),
                          "zzz");
                cd.setMessage(messageByLocaleService.getMessage(SAVEASDISABLEDDIALOG + MESSAGE_SAVEASDISABLED));

                /*
                System.err.println("cd.getCancelButton()=" + cd.getCancelButton());
                System.err.println("cd.getNotOkButton()=" + cd.getNotOkButton());
                */
                cd.getOkButton().setCaption(messageByLocaleService.getMessage(SAVEASDISABLEDDIALOG + MESSAGE_YES));
                cd.getOkButton().setDescription(messageByLocaleService.getMessage(SAVEASDISABLEDDIALOG + TOOLTIP_YES));
                if(cd.getNotOkButton() != null) {
                    cd.getNotOkButton().setCaption(messageByLocaleService.getMessage(SAVEASDISABLEDDIALOG + MESSAGE_NO));
                    cd.getNotOkButton().setDescription(messageByLocaleService.getMessage(SAVEASDISABLEDDIALOG + TOOLTIP_NO));
                }
                cd.getCancelButton().setCaption(messageByLocaleService.getMessage(SAVEASDISABLEDDIALOG + MESSAGE_CANCEL));
                cd.getCancelButton().setDescription(messageByLocaleService.getMessage(SAVEASDISABLEDDIALOG + TOOLTIP_CANCEL));
                return cd;
            }
        };
    }

    public interface MyListener {
        /**
         * Receive an event
         */
        public void receive();
    }

    /**
     * Show the save as disabled dialog
     * @param ui UI 
     * @param messageByLocaleService UIMessageByLocaleService
     * @param yes SaveAsDisabledDialog.MyListener
     * @param no SaveAsDisabledDialog.MyListener
     * @param cancel SaveAsDisabledDialog.MyListener
     */
    public void show(UI ui, UIMessageByLocaleService messageByLocaleService, MyListener yes, MyListener no, MyListener cancel) {
        ConfirmDialog.setFactory(factory);
        ConfirmDialog.show(ui, 
                messageByLocaleService.getMessage(SAVEASDISABLEDDIALOG + CAPTION_SAVEASDISABLED),
                messageByLocaleService.getMessage(SAVEASDISABLEDDIALOG + MESSAGE_SAVEASDISABLED),
                messageByLocaleService.getMessage(SAVEASDISABLEDDIALOG + MESSAGE_YES),
                messageByLocaleService.getMessage(SAVEASDISABLEDDIALOG + MESSAGE_NO),
                messageByLocaleService.getMessage(SAVEASDISABLEDDIALOG + MESSAGE_CANCEL),
                new ConfirmDialog.Listener() {
                    private static final long serialVersionUID = 1L;

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

标签: javadialogvaadin

解决方案


推荐阅读