GWT CellList Click to Toggle selection (Multi-Selection)

Nick Siderakis picture Nick Siderakis · Oct 26, 2011 · Viewed 7.6k times · Source

I'd like to setup a CellList so that clicking a row will toggle the selection. Such that multiple rows can be selected with out the need for holding the ctrl key.

What do I need to change to get it working?

class ToggleEventTranslator<T> implements DefaultSelectionEventManager.EventTranslator<T> {
    @Override
    public boolean clearCurrentSelection(final CellPreviewEvent<T> event) {
        return false;
    }

    @Override
    public SelectAction translateSelectionEvent(final CellPreviewEvent<T> event) {
        return SelectAction.TOGGLE;
    }

}


MultiSelectionModel<ObjProxy> multiSelectionModel = new MultiSelectionModel<ObjProxy>();

    ocjCellList.setSelectionModel(multiSelectionModel, DefaultSelectionEventManager
            .<ObjProxy> createCustomManager(new ToggleEventTranslator<ObjProxy>()));

Answer

Nick Siderakis picture Nick Siderakis · Dec 12, 2011
list.addCellPreviewHandler(new Handler<T>() {

        @Override
        public void onCellPreview(final CellPreviewEvent<T> event) {

            if (BrowserEvents.CLICK.equals(event.getNativeEvent().getType())) {

                final T value = event.getValue();
                final Boolean state = !event.getDisplay().getSelectionModel().isSelected(value);
                event.getDisplay().getSelectionModel().setSelected(value, state);
                event.setCanceled(true);
            }
        }
});


private final MultiSelectionModel<T> selectModel = new MultiSelectionModel<T>();

final Handler<T> selectionEventManager = DefaultSelectionEventManager.createCheckboxManager();
list.setSelectionModel(selectModel, selectionEventManager);