diff --git a/javafx-ui-controls/src/javafx/scene/control/ListView.java b/javafx-ui-controls/src/javafx/scene/control/ListView.java --- a/javafx-ui-controls/src/javafx/scene/control/ListView.java +++ b/javafx-ui-controls/src/javafx/scene/control/ListView.java @@ -1033,15 +1033,11 @@ // (6) if (!newIndices.isEmpty()) { - if (newIndices.size() == 1) { - select(newIndices.get(0)); - } else { - int[] ints = new int[newIndices.size() - 1]; - for (int i = 0; i < newIndices.size() - 1; i++) { - ints[i] = newIndices.get(i + 1); - } - selectIndices(newIndices.get(0), ints); + int[] ints = new int[newIndices.size()]; + for (int i = 0; i < newIndices.size(); i++) { + ints[i] = newIndices.get(i); } + selectIndices(ints); } } } diff --git a/javafx-ui-controls/src/javafx/scene/control/MultipleSelectionModel.java b/javafx-ui-controls/src/javafx/scene/control/MultipleSelectionModel.java --- a/javafx-ui-controls/src/javafx/scene/control/MultipleSelectionModel.java +++ b/javafx-ui-controls/src/javafx/scene/control/MultipleSelectionModel.java @@ -129,7 +129,7 @@ public abstract ObservableList getSelectedItems(); /** - *

This method allows for one or more selections to be set at the same time. + *

This method allows for zero or more selections to be set at the same time. * It will ignore any value that is not within the valid range (i.e. greater * than or equal to zero, and less than the total number of items in the * underlying data model). Any duplication of indices will be ignored. @@ -141,7 +141,7 @@ *

The last valid value given will become the selected index / selected * item. */ - public abstract void selectIndices(int index, int... indices); + public abstract void selectIndices(int... indices); /** *

Selects all indices from the given start index to the item before the @@ -163,16 +163,15 @@ final boolean asc = start < end; final int low = asc ? start : end; // Math.min(start, end); final int high = asc ? end : start; //Math.max(start, end); - final int arrayLength = high - low - 1; + final int arrayLength = high - low; int[] indices = new int[arrayLength]; int startValue = asc ? low : high; - int firstVal = asc ? startValue++ : startValue--; for (int i = 0; i < arrayLength; i++) { indices[i] = asc ? startValue++ : startValue--; } - selectIndices(firstVal, indices); + selectIndices(indices); } /** diff --git a/javafx-ui-controls/src/javafx/scene/control/MultipleSelectionModelBase.java b/javafx-ui-controls/src/javafx/scene/control/MultipleSelectionModelBase.java --- a/javafx-ui-controls/src/javafx/scene/control/MultipleSelectionModelBase.java +++ b/javafx-ui-controls/src/javafx/scene/control/MultipleSelectionModelBase.java @@ -305,9 +305,8 @@ setSelectedItem(obj); } - @Override public void selectIndices(int row, int... rows) { - if (rows == null) { - select(row); + @Override public void selectIndices(int... indices) { + if (indices == null || indices.length == 0) { return; } @@ -321,8 +320,8 @@ if (getSelectionMode() == SINGLE) { quietClearSelection(); - for (int i = rows.length - 1; i >= 0; i--) { - int index = rows[i]; + for (int i = indices.length - 1; i >= 0; i--) { + int index = indices[i]; if (index >= 0 && index < rowCount) { selectedIndices.set((int) index); select(index); @@ -330,23 +329,12 @@ } } - if (selectedIndices.isEmpty()) { - if (row > 0 && row < rowCount) { - selectedIndices.set((int) row); - select((int) row); - } - } - selectedIndicesSeq.callObservers(new NonIterableChange.SimpleAddChange(0, 1, selectedItemsSeq)); } else { int lastIndex = -1; - if (row >= 0 && row < rowCount) { - lastIndex = row; - selectedIndices.set((int) row); - } - for (int i = 0; i < rows.length; i++) { - int index = rows[i]; + for (int i = 0; i < indices.length; i++) { + int index = indices[i]; if (index < 0 || index >= rowCount) continue; lastIndex = index; selectedIndices.set((int) index); @@ -356,13 +344,8 @@ select(lastIndex); } - if (rows.length == 0) { - // TODO this isn't accurate - selectedIndicesSeq.callObservers(new NonIterableChange.SimpleAddChange((int) row, (int) row, selectedIndicesSeq)); - } else { - // TODO this isn't accurate - selectedIndicesSeq.callObservers(new NonIterableChange.SimpleAddChange((int) row, (int) rows[rows.length - 1], selectedIndicesSeq)); - } + // TODO this isn't accurate + selectedIndicesSeq.callObservers(new NonIterableChange.SimpleAddChange(indices[0], indices[indices.length - 1], selectedIndicesSeq)); } } diff --git a/javafx-ui-controls/src/javafx/scene/control/TableView.java b/javafx-ui-controls/src/javafx/scene/control/TableView.java --- a/javafx-ui-controls/src/javafx/scene/control/TableView.java +++ b/javafx-ui-controls/src/javafx/scene/control/TableView.java @@ -1982,9 +1982,8 @@ setSelectedItem(obj); } - @Override public void selectIndices(int row, int... rows) { - if (rows == null) { - select(row); + @Override public void selectIndices(int... indices) { + if (indices == null || indices.length == 0) { return; } @@ -1997,30 +1996,19 @@ if (getSelectionMode() == SelectionMode.SINGLE) { quietClearSelection(); - for (int i = rows.length - 1; i >= 0; i--) { - int index = rows[i]; + for (int i = indices.length - 1; i >= 0; i--) { + int index = indices[i]; if (index >= 0 && index < rowCount) { select(index); break; } } - - if (selectedCells.isEmpty()) { - if (row > 0 && row < rowCount) { - select(row); - } - } } else { int lastIndex = -1; List positions = new ArrayList(); - if (row >= 0 && row < rowCount) { - positions.add(new TablePosition(getTableView(), row, null)); - lastIndex = row; - } - - for (int i = 0; i < rows.length; i++) { - int index = rows[i]; + for (int i = 0; i < indices.length; i++) { + int index = indices[i]; if (index < 0 || index >= rowCount) continue; lastIndex = index; TablePosition pos = new TablePosition(getTableView(), index, null); diff --git a/javafx-ui-controls/test/javafx/scene/control/MultipleSelectionModelImplTest.java b/javafx-ui-controls/test/javafx/scene/control/MultipleSelectionModelImplTest.java --- a/javafx-ui-controls/test/javafx/scene/control/MultipleSelectionModelImplTest.java +++ b/javafx-ui-controls/test/javafx/scene/control/MultipleSelectionModelImplTest.java @@ -513,16 +513,9 @@ @Test public void testMultipleSelectionWithEmptyArray() { msModel().setSelectionMode(SelectionMode.MULTIPLE); - msModel().selectIndices(3,new int[] { }); - assertEquals(3, model.getSelectedIndex()); - assertEquals(1, msModel().getSelectedIndices().size()); - assertEquals(1, msModel().getSelectedItems().size()); - - if (model instanceof TreeView.TreeViewBitSetSelectionModel) { - assertEquals(root.getChildren().get(2), model.getSelectedItem()); - } else { - assertEquals(data.get(3), model.getSelectedItem()); - } + msModel().selectIndices(new int[] { }); + assertEquals(0, msModel().getSelectedIndices().size()); + assertEquals(0, msModel().getSelectedItems().size()); } @Test public void selectOnlyValidIndicesInSingleSelection() { @@ -559,7 +552,7 @@ @Test public void testNullArrayInMultipleSelection() { msModel().setSelectionMode(SelectionMode.MULTIPLE); - msModel().selectIndices(-20, null); + msModel().selectIndices(null); assertEquals(-1, model.getSelectedIndex()); assertNull(model.getSelectedItem()); assertEquals(indices(msModel()), 0, msModel().getSelectedIndices().size()); @@ -577,7 +570,7 @@ @Test public void testInvalidSelection() { msModel().setSelectionMode(SelectionMode.SINGLE); - msModel().selectIndices(-20, null); + msModel().selectIndices(null); assertEquals(-1, model.getSelectedIndex()); assertNull(model.getSelectedItem()); assertEquals(indices(msModel()), 0, msModel().getSelectedIndices().size());