GWT Single Selection Model to prevent having all records unchecked in Cell List.

163 views
Skip to first unread message

BM

unread,
Jul 23, 2018, 12:17:58 PM7/23/18
to GWT Users
I have GWT cell list with single selection model and checkbox which is one of the selection column. So it does toggle between rows since it acquires single section model. 

The toggle should function AS-IS but what I want is if one clicks on an already selected row, the selection should not uncheck that row. If they click on second row, the selection should move to second row which is fine. 

How do we do this?


Thomas Broyer

unread,
Jul 24, 2018, 4:30:24 AM7/24/18
to GWT Users
You'd need a specific SelectionModel that would "refuse" to unselect the selected item.
It might be as simple as extending SingleSelectionModel, overriding setSelected(T,boolean) to do nothing when 'selected' is 'false'; or you may have to write your own SelectionModel (inspired by the SingleSelectionModel)

Alternatively, I suppose you could also achieve your goal with a specific EventTranslator that wouldn't toggle the selection on an already selected item (instead of using DefaultSelectionEventManager.createCheckboxManager(), use a new DefaultSelectionEventManager(new MyEventTranslator()); you could possibly wrap a CheckboxEventTranslator and return IGNORE on an already-selected item –event.getDisplay().getSelectionModel().isSelected(event.getValue())– instead of delegating to the CheckboxEventTranslator, which could then only return TOGGLE on an unchecked checkbox)

BM

unread,
Aug 2, 2018, 10:23:08 AM8/2/18
to GWT Users
Hi Thomas,
Thanks for the suggestions. I tried both of them and didn't really work. Am I missing something here? Any help would be appreciated.

1) I tried using Custom Selection Model and overriding setSelected. When checkbox is unchecked, it doesn't go to setSelected method. 

private class CustomSingleSelectionModel<T> extends SingleSelectionModel<T> {
public CustomSingleSelectionModel() {
super();
}

public CustomSingleSelectionModel(final ProvidesKey<T> keyProvider) {
super(keyProvider);
}


@Override
public void setSelected(final T object, final boolean selected) {
if (selected) {
Window.alert("Selected is true");
super.setSelected(object, selected);
}
else {
Window.alert("Selected is false");
//Do nothing. To skip already selected cell to uncheck.
}
}
}

2) I tried using customEventTranslator. I had breakpoint in translateSelectionEvent() method. But it doesn't go there. 

DefaultSelectionEventManager.<EnrollmentSummaryDTO> createCustomManager(
new CustomCheckboxEventTranslator<>(5))

private class CustomCheckboxEventTranslator<T> extends DefaultSelectionEventManager.CheckboxEventTranslator<T> {
private int column;

public CustomCheckboxEventTranslator() {
super();
}

public CustomCheckboxEventTranslator(final int column) {
super(column);
this.column = column;
}

@Override
public DefaultSelectionEventManager.SelectAction translateSelectionEvent(final CellPreviewEvent<T> event) {
final boolean isSelected = event.getDisplay().getSelectionModel().isSelected(event.getValue());
if (isSelected) {
return DefaultSelectionEventManager.SelectAction.IGNORE;
}
else {
return DefaultSelectionEventManager.SelectAction.TOGGLE;
}

}
}

BM

unread,
Aug 3, 2018, 5:25:10 AM8/3/18
to GWT Users
Ok It finally worked. The original problem was I had keyboard policy bound to selection which was preventing the calls to setSelected.


On Tuesday, July 24, 2018 at 3:30:24 AM UTC-5, Thomas Broyer wrote:
Reply all
Reply to author
Forward
0 new messages