PDA

View Full Version : [CLOSED] "are you sure" logic puzzle???



gslender
3 Jul 2008, 2:38 AM
Possible logic bug in the event system - maybe around the message box implementation?

I'm trying to provide a "are you sure" dialog before the window is closed... can't seem to figure out the event chains to make this happen - so it may not be a bug, but nothing elegant or simple is obvious.....would appreciate any help if possible :-)

Example of problem...


package com.mycompany.gxt.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;

import com.extjs.gxt.ui.client.Events;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.event.WindowEvent;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;

public class WindowCancelCloseTest implements EntryPoint {

public void onModuleLoad() {
final Window w = new Window();
w.setHeading("close me");
w.addText("close me and I should ask \"are you sure\" before window is closed?");
w.setSize(300, 300);
w.addListener(Events.BeforeHide, new Listener<WindowEvent>() {
public void handleEvent(final WindowEvent wbe) {
wbe.doit = false; /// <<< this cancels the window hide
MessageBox.confirm("Cancel", "Are you sure you wish to cancel?", new Listener<WindowEvent>() {
public void handleEvent(WindowEvent mbe) {
if (mbe.buttonClicked.getId().equals(Dialog.YES)) {
// wbe.doit = false; /// <<< this doesn't work as the wbe event is gone by now??
w.hide(); /// <<< this doesn't work? How do I hide the window?
}
}
});
}
});

Button test = new Button("click me");
SelectionListener<ComponentEvent> listener = new SelectionListener<ComponentEvent>() {
public void componentSelected(ComponentEvent be) {
w.show();
}
};
test.addSelectionListener(listener);

final Viewport vp = new Viewport();
vp.add(test);
vp.layout();

RootPanel.get().add(vp);
}
}

gslender
3 Jul 2008, 2:08 PM
solved it.... added/removed listeners - someone clever gave me the idea ;-)


package com.mycompany.gxt.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;

import com.extjs.gxt.ui.client.Events;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.event.WindowEvent;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;

public class WindowCancelCloseTest implements EntryPoint {
private Listener<WindowEvent> windowListener = null;
public void onModuleLoad() {
final Window w = new Window();
w.setHeading("close me");
w.addText("close me and I should ask \"are you sure\" before window is closed?");
w.setSize(300, 300);

windowListener = new Listener<WindowEvent>() {
public void handleEvent(final WindowEvent wbe) {
wbe.doit = false;
MessageBox.confirm("Cancel", "Are you sure you wish to cancel?", new Listener<WindowEvent>() {
public void handleEvent(WindowEvent mbe) {
if (mbe.buttonClicked.getItemId().equals(Dialog.YES)) {
w.removeListener(Events.BeforeHide, windowListener);
w.hide();
w.addListener(Events.BeforeHide, windowListener);
}
}
});
}
};
w.addListener(Events.BeforeHide, windowListener);

Button test = new Button("click me");
SelectionListener<ComponentEvent> listener = new SelectionListener<ComponentEvent>() {
public void componentSelected(ComponentEvent be) {
w.show();
}
};
test.addSelectionListener(listener);

final Viewport vp = new Viewport();
vp.add(test);
vp.layout();

RootPanel.get().add(vp);
}
}