PDA

View Full Version : Grid.Select event can be "canceled" only in parts



Reghin
21 Aug 2009, 9:47 AM
Hi again, folks.

I have this problem, and I don't know what else I can do to solve it.

I have this grid which has a CheckBoxSelectionModel. I need to prevent any row to be selected or changed from if the form is dirty.

Both the BeforeEvent and SelectionChanged events are being fired correctly. The problem is that I can not prevent the already selected row (the form's dirty record) from being deselected, I mean, the previous selection is cleared.



mainGrid.getGridRef().getSelectionModel().addListener(Events.BeforeSelect, new Listener<SelectionEvent<ModelData>>() {
@Override
public void handleEvent(SelectionEvent<ModelData> be) {
Info.display("MessageBox", "BeforeSelect");
// verifica se o form teve alguma alteracao que precise ser gravada antes de movimentar o registro
if (mainForm != null && mainForm.getFormRef().isDirty()) {
Info.display("MessageBox", "O FORM ESTÁ SUJO !!!");
be.setCancelled(true);
}
}
});

mainGrid.getGridRef().getSelectionModel().addListener(Events.SelectionChange, new Listener<SelectionChangedEvent<BeanModel>>() {
public void handleEvent(SelectionChangedEvent<BeanModel> be) {
if (be.getSelection().size() == 1) {
mainBindings.bind((BeanModel) be.getSelection().get(0));
footer.clearStatus("");
}
else if(be.getSelection().size() > 1) {
mainForm.getFormRef().reset();
}
else if(be.getSelection().size() == 0) {
}
else {
mainForm.getFormRef().reset();
}
};
});

sven
21 Aug 2009, 11:20 AM
You could simple lock the selectionmodel and it cannot be changed anymore.

Reghin
24 Aug 2009, 3:52 AM
Well, okay, but don't you agree this still is a strange behavior??

The right behavior would be NOT to unselect any row before knowing if the event was cancelled or not.

Do you think I should add this issue to the bug list?

Arno.Nyhm
24 Aug 2009, 4:17 AM
is there not an additional event with selection change where the unselection event is fired?

Reghin
24 Aug 2009, 4:32 AM
I couldn't find any unselect event. Is there any?

Arno.Nyhm
24 Aug 2009, 5:24 AM
did this one is not fired for each row if the item is deselected?


mainGrid.getGridRef().getSelectionModel().addListener(Events.SelectionChange

but i testet it and no this only once is fired :-(




but with this i get each changed items also the deselected ones, but here you can not cancel the event:


grid.setSelectionModel(new GridSelectionModel<Stock>() {

@Override
protected void onSelectChange(Stock model, boolean select) {
int idx = listStore.indexOf(model);
GWT.log("idx:" + idx, null);
GWT.log("select:" + select, null);
super.onSelectChange(model, select);
// some debug info
List<Stock> selectedItems = getSelectedItems();
for (Stock stock : selectedItems) {
GWT.log(stock.getName(), null);
}
}
});

Reghin
24 Aug 2009, 8:09 AM
Thank you, Arno!!! :))

This "selection" problem I've already solved keeping a reference to the previously selected bean. This way:


private BeanModel editingBean = null;


mainGrid.getGridRef().getSelectionModel().addListener(Events.BeforeSelect, new Listener<SelectionEvent<ModelData>>() {
@Override
public void handleEvent(SelectionEvent<ModelData> be) {
if (be.getModel() != null && be.getModel() != editingBean) {
if (mainForm != null) {
if (mainForm.getFormRef().isDirty()) {
mainGrid.getGridRef().getSelectionModel().select(editingBean, true);
be.setCancelled(true);
}
else {
editingBean = (BeanModel) be.getModel();
mainBindings.bind(editingBean);
}
}
}
}
});

The problem now is that the form is always considered dirty. I don't know how to fix that. :(