GWT 确认对话框

标签 gwt

我正在尝试创建一个模式确认对话框。我希望它像 Window.confirm("") 一样工作,我可以在这里调用它,并得到一个 bool 响应。

我的麻烦是我不知道该怎么做。我正在尝试在我的应用程序中使用 MVP。这是我到目前为止的代码:

public class DialogBoxPresenter implements Presenter {

    public interface Display {

        Label getDialogText();

        Button getAffirmativeButton();

        Button getCancelButton();

        Widget asWidget();

        public void center();

        public void hide();

        public void setHeader(String text);
    }
    private Display display;
    private String header;
    private String dialogText;
    private String cancelButtonText;
    private String affirmativeButtonText;

    protected DialogBoxPresenter() {
    }

    public DialogBoxPresenter(Display display, String header, String dialogText, String cancelButtonText, String affirmativeButtonText) {
        this.display = display;
        this.header = header;
        this.dialogText = dialogText;
        this.cancelButtonText = cancelButtonText;
        this.affirmativeButtonText = affirmativeButtonText;

        bind();
    }

    public DialogBoxPresenter(Display display, String header, String dialogText) {
        this.display = display;
        this.header = header;
        this.dialogText = dialogText;
        this.cancelButtonText = "Cancel";
        this.affirmativeButtonText = "OK";

        bind();
    }

    private void bind() {

        this.display.getDialogText().setText(dialogText);
        this.display.getAffirmativeButton().setText(affirmativeButtonText);
        this.display.getCancelButton().setText(cancelButtonText);
        this.display.setHeader(header);

        addClickHandlers();

    }

    private void addClickHandlers() {
        this.display.getAffirmativeButton().addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                doAffirmative();
            }
        });

        this.display.getCancelButton().addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                doCancel();
            }
        });
    }

    private void doAffirmative() {
        //do something
        display.hide();
    }

    private void doCancel() {
        //do something
        display.hide();
    }

    public void init() {
        display.center();
    }

    @Override
    public void go(HasWidgets container) {
        container.clear();
        container.add(display.asWidget());
    }
}

和我的观点:
public class DialogBoxView extends DialogBox implements DialogBoxPresenter.Display {

    private Label dialogText;
    private Button affirmativeButton;
    private Button cancelButton;
    private VerticalPanel container;

    public DialogBoxView() {
        //init items
        dialogText = new Label();

        affirmativeButton = new Button();
        cancelButton = new Button();

        container = new VerticalPanel();

        setGlassEnabled(true);
        setAnimationEnabled(true);
        setModal(false);

        init();
    }

    private void init() {
        //add items
        container.add(dialogText);

        HorizontalPanel hp = new HorizontalPanel();
        hp.add(affirmativeButton);
        hp.add(cancelButton);

        container.add(hp);
        this.add(container);
    }

    @Override
    public Widget asWidget() {
        return this;
    }

    @Override
    public Label getDialogText() {
       return dialogText;
    }

    @Override
    public Button getAffirmativeButton() {
        return affirmativeButton;
    }

    @Override
    public Button getCancelButton() {
       return cancelButton;
    }

    @Override
    public void setHeader(String text) {
       this.setText(text);
    }

}

最佳答案

您将无法让它以与 Window.confirm() 完全相同的方式工作。 .问题是网页中的所有javascript都在一个线程中运行。您会注意到,只要打开标准确认对话框,页面的其余部分就会失效。那是因为一个javascript线程被阻塞,等待confirm()返回。如果您要为您的对话框创建一个类似的方法,只要它正在等待该方法返回,就不会处理任何用户生成的事件,因此您的对话框将无法工作。我希望这是有道理的。

您能做的最好的事情类似于 GWT 库对 RPC 调用所做的事情——AsyncCallback。界面。您甚至可以自己重用该接口(interface),或者您可能更喜欢自己滚动:

public interface DialogCallback {
    void onOk();
    void onCancel();
}

而不是 Window.confirm(String) ,您的方法签名将更像 Dialog.confirm(String,DialogCallback) .然后您的对话框只保留对传入回调的引用,以及您拥有// do something 的位置。在您的代码中调用 onOkonCancel .

关于GWT 确认对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3162399/

相关文章:

java - 如何配置 SmartGWT TreeGrid 以使用不同类型的对象?

class - 自定义类 : No class matching "..." in urn:import: 的 GWT 延迟绑定(bind)失败

java - GWT javascript 后台加载

c# 将包含 HTML 特殊字符的字符串解析为 XElement

GWT:在服务器端获取常量

java - 当我输入旧日期(例如 20/03/1961 (dd/mm/yyyy))时,会存储前一天的值

java - 如何将 Maven 管理的依赖项复制到 war\web-inf\lib 中,以便我可以在 Eclipse 中以 Debug模式运行我的 GWT 2.0 应用程序?

android - GWT 应用程序在 Android 浏览器中运行良好吗?

java - JDO分离对象,然后获取该对象的arraylist

gwt - GWT:UiBinder还是GWT Designer?