GWT: How to prevent a handler from receiving an event until user has selected a choice in a confirmation box?
This will take some explaining.
In our project we have a popup which has quite a few fields that can be edited in it as well as Save and Cancel buttons at the bottom. Clicking Save obviously will save any changes that you've made, or just close the popup if there are no changes. Cancel will also close the popup if there are no changes. If you cancel when there are changes, a confirmation dialog pops up asking if you are sure you want to discard the changes you have made.
The idea is that if you click No, the confirmation dialog will close, but the popup won't, since the user has indicated they don't want to discard those changes.
The problem right now is that the popup is closing right away when you click the Cancel button, even if there are changes made. The confirmation dialog also pops up, but it isn't much use since the popup is already gone.
The reason this happens is that the handlers for closing the popup and showing the confirmation dialog are both attached as click handlers to the Cancel button. This used to work before GWT switched to handlers because we passed in the list of listeners as a parameter to the method that showed the confirmation dialog. We would then only fire those listeners if the user decided that it was ok for the changes to be discarded. Since I don't have control over handlers since the event model change (they are held in GWT Widget code) I can't do that anymore.
My idea for fixing this was to simply add the handler that closes the popup to a different event that would only be fired when the correct option is selected in the confirmation dialog. The only reason I'm asking this question is to see if there is any other way to do this because this whole thing where the popup closing handler is added is handled in our xml layouts and it would require some interesting architectural changes to make this work properly.
: BeforePopupClosedEvent is fired which opens the confirmation dialog. If the user indicates the changes should not be discarded, the BeforePopupClosedEvent is cancelled. Unfortunately, code keeps exe开发者_开发知识库cuting while waiting for user response and the handler which closes the popup goes ahead and does that because the user has not selected a choice yet.
This is an early draft of a widget I made to solve this problem. I later made a more generic wrapping subclass of SimplePanel
that would wrap the click handlers of any widget.
It works by switching in a substitute HandlerManager when addClickHandler is called, and only firing events along that HandlerManager when the confirmation dialog returns.
public class ConfirmButton extends Button
{
private HandlerManager handlerManager = new HandlerManager(this);
private String message = "Do you really want to do this?";
private ClickHandler myRealClickHandler = new ClickHandler() {
@Override
public void onClick(ClickEvent event)
{
if (Window.confirm(message)) handlerManager.fireEvent(event);
}
};
public ConfirmButton()
{
super();
addDomHandler(myRealClickHandler, ClickEvent.getType());
}
public ConfirmButton(String text)
{
this();
setText(text);
}
public void setConfirmationPrompt(String prompt)
{
message = prompt;
}
@Override
public HandlerRegistration addClickHandler(ClickHandler handler)
{
HandlerRegistration ret = handlerManager.addHandler(ClickEvent.getType(), handler);
return ret;
}
}
精彩评论