This is a application-wide thing that you can access statically from anywhere. But generally, of course, you'll only have one or two listeners at any one time.
onWindowClosing() Your application can use this to warn the user that some
dire consequences will ensue. The system then allows the user to cancel. Simply return
a string to get the message up, null if you don't want it. A typical use would be to warn
of loss of data and ask the user to cancel and save before exiting.
onWindowClose() It's too late now to stop the window closing, but you do
have the chance here to do a last, final-fling bit of processing - logging out of the
server or saving settings to a cookie.
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.WindowCloseListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.ToggleButton;
import com.google.gwt.user.client.ui.Widget;
import com.roughian.rxf.client.ui.AbstractRemotePage;
import com.roughian.rxf.client.ui.RootPanelUncached;
public class Listeners__WindowCloseListener extends
AbstractRemotePage implements ClickListener,
WindowCloseListener
{
public void buildPage()
{
ToggleButton b = new ToggleButton("WindowCloseListener Off",
"WindowCloseListener On");
/*
* Mostly, you'd style this button in CSS, but so you can see what's
* going on (and because I'm lazy and it's more trouble the other way)
* I'll do it here
*/
DOM.setStyleAttribute(b.getElement(), "border",
"1px solid #999");
DOM.setStyleAttribute(b.getElement(), "width", "200px");
DOM.setStyleAttribute(b.getElement(), "margin", "5px auto");
DOM.setStyleAttribute(b.getElement(), "fontSize", "80%");
DOM.setStyleAttribute(b.getElement(), "textAlign", "center");
DOM.setStyleAttribute(b.getElement(), "cursor", "default");
b.addClickListener(this);
RootPanelUncached.get("demo").add(b);
}
public void onClick(Widget sender)
{
if (((ToggleButton) sender).isDown())
{
Window.addWindowCloseListener(this);
}
else
{
Window.removeWindowCloseListener(this);
}
}
public void onWindowClosed()
{
Window.alert("Aaaarrrggghhhhhh!");
}
public String onWindowClosing()
{
return "Please Don't Kill Me! I Don't Want To Die!";
}
}