PDA

View Full Version : Window BeforeClose event doens't work on 2.0 M2



edumrf
28 May 2009, 6:27 AM
Hi, after i migrated to 2.0 m2 all my BeforeClose events on the Window are going off only after the window is already closed.
Any clue why this stoped working?



//Creates the widget
Window w = new Window();
w.setCloseAction(CloseAction.CLOSE);

Listener closeListener = null;
closeListener = new Listener<WindowEvent>() {
public void handleEvent(WindowEvent we) {
MessageBox.confirm("Close Process", "Are you sure?", new Listener<MessageBoxEvent>() {
public void handleEvent(MessageBoxEvent ce) {
Dialog dialog = (Dialog) ce.getComponent();
Button btn = ce.getButtonClicked();
if ( btn.getText().equalsIgnoreCase("yes") ){
//now remove the listener and close the window
w.removeListener(Events.BeforeClose, closeListener);
w.close();
}
}
});
//can close only if user answer "yes";
//used to be we.doit = false;
//guess i'm suposed to change that after upgrading...
//instead of throwing away the old stuff this could have
//been marked as deprecated, would save a lot of time on migration :)
we.stopEvent();
}
};

//add close event handler
this.addListener(Events.BeforeClose, closeListener);
Thanks in advance.

Jose Jeria
28 May 2009, 7:43 AM
The solution to this can be found in the 2.0 migration guide:



final Window w = new Window();
w.setHeading("WFM");
w.setSize(300, 300);
w.setCloseAction(CloseAction.CLOSE);

Listener<WindowEvent> closeListener = new Listener<WindowEvent>() {
public void handleEvent(WindowEvent we) {
we.setCancelled(true);

MessageBox.confirm("Close Process", "Are you sure?", new Listener<MessageBoxEvent>() {
public void handleEvent(MessageBoxEvent ce) {
Button btn = ce.getButtonClicked();

if ( Dialog.YES == btn.getItemId() ){
w.removeAllListeners();
w.close();
}
}
});
}
};


w.addListener(Events.BeforeClose, closeListener);
w.show();

edumrf
28 May 2009, 9:11 AM
It worked!
That's true, it was in the migration guide, next time i'll pay more attention to it.
Thanks a lot.