Buggy code:
void heightChanged ( final int delta ) { } void rowCountChanged ( ) { } void filtersChanged ( ) { } void elementsAdded ( final Collection < T > elements ) { } void elementsUpdated ( final Collection < T > elements ) { } void elementsRemoved ( final Collection < T > elements ) { } void columnResized ( final Column column ) { } void columnMoved ( final Column column ) { } void columnSorted ( final Column column ) { } void rowNumbersVisibilityChanged ( final boolean visible ) { } } public GridModel ( final Grid < T > grid , final ResourceManager resourceManager , final GC gc ) { } public void enableEvents ( final boolean enable ) { } public boolean isEventsSuppressed ( ) { } public GroupRenderStyle getGroupRenderStyle ( ) { } public void setGroupRenderStyle ( final GroupRenderStyle groupRenderStyle ) { } public StyleRegistry < T > getStyleRegistry ( ) { } public SelectionModel < T > getSelectionModel ( ) { } public SortModel < T > getSortModel ( ) { } public FilterModel < T > getFilterModel ( ) { } public List < Column > getColumns ( ) { } public List < Column > getAllColumns ( ) { } public List < Column > getGroupByColumns ( ) { } public Row < T > getColumnHeaderRow ( ) { } public Column getRowNumberColumn ( ) { } public List < T > getElements ( ) { } public Collection < T > getSelection ( ) { } public int getDetailedRowCount ( final boolean visible , final RowCountScope scope ) { } public List < Row < T > > getRows ( ) { } public Collection < Row < T > > getHiddenRows ( ) { } public Collection < Row < T > > getAllRows ( ) { } public List < Row < T > > getColumnHeaderRows ( ) { } public List < Column > getPinnedColumns ( ) { } public Row < T > getRow ( final T element ) { } Map < T , Row < T > > getRowsByElement ( ) { } public void setLabelProvider ( final IGridLabelProvider < T > labelProvider ) { } public void setContentProvider ( final IGridContentProvider < T > contentProvider ) { } public void clearFilters ( ) { } public IGridContentProvider < T > getContentProvider ( ) { } private void addColumn ( final Column column ) { } public void addColumns ( final List < Column > columns ) { } public void clearColumns ( ) { } private void removeColumn ( final Column column ) { } public void removeColumns ( final List < Column > columns ) { } public void rebuildVisibleColumns ( ) { } public void moveColumn ( final Column columnToMove , final Column insertBefore ) { } public Column getColumnById ( final String columnId ) { } public Collection < Row < T > > addElements ( final Collection < T > elements ) { } private boolean addRow ( final Row < T > row ) { } public void removeElements ( final Collection < T > elements ) { int heightDelta = 0 ; int lastSelectedIndex = - 1 ; boolean selectionChanged = false ; for ( final T element : elements ) { final Row < T > row = rowsByElement . get ( element ) ; heightDelta -= getRowHeight ( row ) ; rows . remove ( row ) ; hiddenRows . remove ( row ) ; rowsByElement . remove ( element ) ; if ( row . isSelected ( ) ) { selectionChanged |= selectionModel . removeRow ( row ) ; lastSelectedIndex = Math . max ( lastSelectedIndex , row . getRowIndex ( ) ) ; } if ( row . isPinned ( ) ) { columnHeaderRows . remove ( row ) ; } } <START_BUG> if ( ( lastSelectedIndex != ( - 1 ) ) && ( selectionModel . getSelectedElements ( ) . isEmpty ( ) ) ) { <END_BUG> final int nextIndex = ( lastSelectedIndex - ( elements . size ( ) ) ) + 1 ; if ( ( nextIndex >= 0 ) && ( nextIndex <= ( ( rows . size ( ) ) - 1 ) ) ) { final Row < T > row = rows . get ( nextIndex ) ; final List < Row < T > > rowsToSelect = ( isGroupRow ( row ) ) ? getWholeGroup ( row ) : Collections . singletonList ( row ) ; selectionModel . setSelectedRows ( rowsToSelect ) ; } } reindex ( ) ; if ( heightDelta != 0 ) { fireHeightChangeEvent ( heightDelta ) ; } fireElementsRemovedEvent ( elements ) ; fireRowCountChangedEvent ( ) ; if ( selectionChanged ) { fireSelectionChangedEvent ( ) ; } } public void updateElements ( final Collection < T > elements ) { } public void reindex ( ) { } public void clearElements ( ) { } public void showRow ( final Row < T > row ) { } public void hideRow ( final Row < T > row ) { } public void groupBy ( final List < Column > columns ) { } public void ungroupBy ( final List < Column > columns ) { } public void ungroupAll ( ) { } public void addListener ( final GridModel . IModelListener < T > listener ) { } public void removeListener ( final GridModel . IModelListener < T > listener ) { } public void fireChangeEvent ( ) { } public void fireHeightChangeEvent ( final int delta ) { } public void fireRowCountChangedEvent ( ) { } public void fireElementsAddedEvent ( final Collection < T > elements ) { } public void fireElementsUpdatedEvent ( final Collection < T > elements ) { } public void fireElementsRemovedEvent ( final Collection < T > elements ) { } void fireSelectionChangedEvent ( ) { } public void fireFiltersChangedEvent ( ) { }

Fix:
if ( ( ( selectionModel . isSelectNextOnRemove ( ) ) && ( lastSelectedIndex != ( - 1 ) ) ) && ( selectionModel . getSelectedElements ( ) . isEmpty ( ) ) ) {