GWT确认对话框 [英] GWT confirmation dialog box
问题描述
我正在尝试创建一个模式确认对话框。我希望它能像 Window.confirm()
那样工作,在那里我可以调用它,并获得布尔响应。
I'm trying to create a modal confirmation dialog box. I'd like it to work like Window.confirm("")
, where I can just call it, and get a boolean response.
我的麻烦是我不知道该怎么做。我试图在我的应用程序中使用MVP。这里是我到目前为止的代码:
My trouble is I'm not sure how to do it. I'm trying to use MVP in my application. Here is the code I have so far:
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());
}
}
和我的看法:
and my view:
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()
返回。如果您要为对话创建一个类似的方法,只要它正在等待该方法返回,则不会处理用户生成的事件,因此您的对话将不起作用。我希望这是有道理的。
You're not going to be able to have it work in exactly the same way as Window.confirm()
. The problem is that all of the javascript in a web page runs in a single thread. You'll notice that as long as a standard confirm dialog is open, the rest of the page goes dead. That's because the one javascript thread is blocked, waiting for confirm()
to return. If you were to create a similar method for your dialog, as long as it was waiting for that method to return no user generated events would be processed and so your dialog wouldn't work. I hope that makes sense.
您能够做的最好的事情与GWT库为RPC调用做的类似 - AsyncCallback
界面。您甚至可以自己重新使用该接口,或者您可能更愿意推出自己的接口:
The best you will be able to do is similar to what the GWT library does for RPC calls -- the AsyncCallback
interface. You could even reuse that interface yourself, or you might prefer to roll your own:
public interface DialogCallback {
void onOk();
void onCancel();
}
而不是 Window.confirm(String) code>,你的方法签名会更像
Dialog.confirm(String,DialogCallback)
。然后你的对话框只保留一个对你传入的回调的引用,以及你调用 onOk 时在你的代码中执行
/ code>和
onCancel
。
Instead of Window.confirm(String)
, your method signature will be more like Dialog.confirm(String,DialogCallback)
. Then your dialog just keeps a reference to the callback that's passed in, and where you have // do something
in your code you make calls to onOk
and onCancel
.
这篇关于GWT确认对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!