8191436: ListSelectionModel.setSelectionMode() underspecified

Reviewed-by: serb, ssadetsky
This commit is contained in:
Pankaj Bansal 2017-12-08 15:57:30 +05:30
parent 81c1d53cdb
commit 2940eb8a25
2 changed files with 158 additions and 7 deletions

View file

@ -95,14 +95,37 @@ public class DefaultListSelectionModel implements ListSelectionModel, Cloneable,
* @throws IllegalArgumentException {@inheritDoc}
*/
public void setSelectionMode(int selectionMode) {
int oldMode = this.selectionMode;
switch (selectionMode) {
case SINGLE_SELECTION:
case SINGLE_INTERVAL_SELECTION:
case MULTIPLE_INTERVAL_SELECTION:
this.selectionMode = selectionMode;
break;
default:
throw new IllegalArgumentException("invalid selectionMode");
case SINGLE_SELECTION:
case SINGLE_INTERVAL_SELECTION:
case MULTIPLE_INTERVAL_SELECTION:
this.selectionMode = selectionMode;
break;
default:
throw new IllegalArgumentException("invalid selectionMode");
}
/*
This code will only be executed when selection needs to be updated on
changing selection mode. It will happen only if selection mode is changed
from MULTIPLE_INTERVAL to SINGLE_INTERVAL or SINGLE or from
SINGLE_INTERVAL to SINGLE
*/
if (oldMode > this.selectionMode) {
if (this.selectionMode == SINGLE_SELECTION) {
if (!isSelectionEmpty()) {
setSelectionInterval(minIndex, minIndex);
}
} else if (this.selectionMode == SINGLE_INTERVAL_SELECTION) {
if(!isSelectionEmpty()) {
int selectionEndindex = minIndex;
while (value.get(selectionEndindex + 1)) {
selectionEndindex++;
}
setSelectionInterval(minIndex, selectionEndindex);
}
}
}
}