8074286: Add getSelectedIndices() to ListSelectionModel

Reviewed-by: serb, psadhukhan
This commit is contained in:
Pankaj Bansal 2018-03-29 17:52:32 +05:30
parent 0c29ff3488
commit 70e6bad08d
4 changed files with 74 additions and 86 deletions

View File

@ -2180,24 +2180,7 @@ public class JList<E> extends JComponent implements Scrollable, Accessible
*/
@Transient
public int[] getSelectedIndices() {
ListSelectionModel sm = getSelectionModel();
int iMin = sm.getMinSelectionIndex();
int iMax = sm.getMaxSelectionIndex();
if ((iMin < 0) || (iMax < 0)) {
return new int[0];
}
int[] rvTmp = new int[1+ (iMax - iMin)];
int n = 0;
for(int i = iMin; i <= iMax; i++) {
if (sm.isSelectedIndex(i)) {
rvTmp[n++] = i;
}
}
int[] rv = new int[n];
System.arraycopy(rvTmp, 0, rv, 0, n);
return rv;
return getSelectionModel().getSelectedIndices();
}
@ -2301,25 +2284,23 @@ public class JList<E> extends JComponent implements Scrollable, Accessible
*/
@BeanProperty(bound = false)
public List<E> getSelectedValuesList() {
ListSelectionModel sm = getSelectionModel();
ListModel<E> dm = getModel();
int[] selectedIndices = getSelectedIndices();
int iMin = sm.getMinSelectionIndex();
int iMax = sm.getMaxSelectionIndex();
int size = dm.getSize();
if ((iMin < 0) || (iMax < 0) || (iMin >= size)) {
return Collections.emptyList();
}
iMax = iMax < size ? iMax : size - 1;
List<E> selectedItems = new ArrayList<E>();
for(int i = iMin; i <= iMax; i++) {
if (sm.isSelectedIndex(i)) {
if (selectedIndices.length > 0) {
int size = dm.getSize();
if (selectedIndices[0] >= size) {
return Collections.emptyList();
}
List<E> selectedItems = new ArrayList<E>();
for (int i : selectedIndices) {
if (i >= size)
break;
selectedItems.add(dm.getElementAt(i));
}
return selectedItems;
}
return selectedItems;
return Collections.emptyList();
}

View File

@ -2268,23 +2268,7 @@ public class JTable extends JComponent implements TableModelListener, Scrollable
*/
@BeanProperty(bound = false)
public int[] getSelectedRows() {
int iMin = selectionModel.getMinSelectionIndex();
int iMax = selectionModel.getMaxSelectionIndex();
if ((iMin == -1) || (iMax == -1)) {
return new int[0];
}
int[] rvTmp = new int[1+ (iMax - iMin)];
int n = 0;
for(int i = iMin; i <= iMax; i++) {
if (selectionModel.isSelectedIndex(i)) {
rvTmp[n++] = i;
}
}
int[] rv = new int[n];
System.arraycopy(rvTmp, 0, rv, 0, n);
return rv;
return selectionModel.getSelectedIndices();
}
/**
@ -2306,16 +2290,7 @@ public class JTable extends JComponent implements TableModelListener, Scrollable
*/
@BeanProperty(bound = false)
public int getSelectedRowCount() {
int iMin = selectionModel.getMinSelectionIndex();
int iMax = selectionModel.getMaxSelectionIndex();
int count = 0;
for(int i = iMin; i <= iMax; i++) {
if (selectionModel.isSelectedIndex(i)) {
count++;
}
}
return count;
return selectionModel.getSelectedItemsCount();
}
/**

View File

@ -332,4 +332,61 @@ public interface ListSelectionModel
* @see #addListSelectionListener
*/
void removeListSelectionListener(ListSelectionListener x);
/**
* Returns an array of all of the selected indices in the selection model,
* in increasing order.
*
* @return all of the selected indices, in increasing order,
* or an empty array if nothing is selected
* @see #removeSelectionInterval
* @see #addListSelectionListener
* @since 11
* @implSpec The default implementation iterates from minimum selected
* index {@link #getMinSelectionIndex()} to maximum selected index {@link
* #getMaxSelectionIndex()} and returns the selected indices {@link
* #isSelectedIndex(int)} in a newly allocated int array.
*/
default int[] getSelectedIndices() {
int iMin = getMinSelectionIndex();
int iMax = getMaxSelectionIndex();
if ((iMin < 0) || (iMax < 0)) {
return new int[0];
}
int[] rvTmp = new int[1+ (iMax - iMin)];
int n = 0;
for(int i = iMin; i <= iMax; i++) {
if (isSelectedIndex(i)) {
rvTmp[n++] = i;
}
}
int[] rv = new int[n];
System.arraycopy(rvTmp, 0, rv, 0, n);
return rv;
}
/**
* Returns the number of selected items.
*
* @return the number of selected items, 0 if no items are selected
* @since 11
* @implSpec The default implementation iterates from minimum selected
* index {@link #getMinSelectionIndex()} to maximum selected index {@link
* #getMaxSelectionIndex()} and returns the number of selected indices
* {@link #isSelectedIndex(int)}
*/
default int getSelectedItemsCount() {
int iMin = getMinSelectionIndex();
int iMax = getMaxSelectionIndex();
int count = 0;
for(int i = iMin; i <= iMax; i++) {
if (isSelectedIndex(i)) {
count++;
}
}
return count;
}
}

View File

@ -427,23 +427,7 @@ public class DefaultTableColumnModel implements TableColumnModel,
*/
public int[] getSelectedColumns() {
if (selectionModel != null) {
int iMin = selectionModel.getMinSelectionIndex();
int iMax = selectionModel.getMaxSelectionIndex();
if ((iMin == -1) || (iMax == -1)) {
return new int[0];
}
int[] rvTmp = new int[1+ (iMax - iMin)];
int n = 0;
for(int i = iMin; i <= iMax; i++) {
if (selectionModel.isSelectedIndex(i)) {
rvTmp[n++] = i;
}
}
int[] rv = new int[n];
System.arraycopy(rvTmp, 0, rv, 0, n);
return rv;
return selectionModel.getSelectedIndices();
}
return new int[0];
}
@ -455,16 +439,7 @@ public class DefaultTableColumnModel implements TableColumnModel,
*/
public int getSelectedColumnCount() {
if (selectionModel != null) {
int iMin = selectionModel.getMinSelectionIndex();
int iMax = selectionModel.getMaxSelectionIndex();
int count = 0;
for(int i = iMin; i <= iMax; i++) {
if (selectionModel.isSelectedIndex(i)) {
count++;
}
}
return count;
return selectionModel.getSelectedItemsCount();
}
return 0;
}