8074286: Add getSelectedIndices() to ListSelectionModel
authorpbansal
Thu, 29 Mar 2018 17:52:32 +0530
changeset 49501 5daa8ef17089
parent 49500 3c68768d3904
child 49502 f9e81b6bfc20
child 49504 8a23dbe5ed1b
8074286: Add getSelectedIndices() to ListSelectionModel Reviewed-by: serb, psadhukhan
src/java.desktop/share/classes/javax/swing/JList.java
src/java.desktop/share/classes/javax/swing/JTable.java
src/java.desktop/share/classes/javax/swing/ListSelectionModel.java
src/java.desktop/share/classes/javax/swing/table/DefaultTableColumnModel.java
--- a/src/java.desktop/share/classes/javax/swing/JList.java	Thu Mar 29 12:03:40 2018 +0530
+++ b/src/java.desktop/share/classes/javax/swing/JList.java	Thu Mar 29 17:52:32 2018 +0530
@@ -2180,24 +2180,7 @@
      */
     @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 @@
      */
     @BeanProperty(bound = false)
     public List<E> getSelectedValuesList() {
-        ListSelectionModel sm = getSelectionModel();
         ListModel<E> dm = getModel();
-
-        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)) {
+        int[] selectedIndices = getSelectedIndices();
+
+        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();
     }
 
 
--- a/src/java.desktop/share/classes/javax/swing/JTable.java	Thu Mar 29 12:03:40 2018 +0530
+++ b/src/java.desktop/share/classes/javax/swing/JTable.java	Thu Mar 29 17:52:32 2018 +0530
@@ -2268,23 +2268,7 @@
      */
     @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 @@
      */
     @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();
     }
 
     /**
--- a/src/java.desktop/share/classes/javax/swing/ListSelectionModel.java	Thu Mar 29 12:03:40 2018 +0530
+++ b/src/java.desktop/share/classes/javax/swing/ListSelectionModel.java	Thu Mar 29 17:52:32 2018 +0530
@@ -332,4 +332,61 @@
      * @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;
+    }
 }
--- a/src/java.desktop/share/classes/javax/swing/table/DefaultTableColumnModel.java	Thu Mar 29 12:03:40 2018 +0530
+++ b/src/java.desktop/share/classes/javax/swing/table/DefaultTableColumnModel.java	Thu Mar 29 17:52:32 2018 +0530
@@ -427,23 +427,7 @@
      */
     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 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;
     }