< prev index next >

src/share/classes/javax/swing/JList.java

Print this page
rev 14331 : 8074286: Add getSelectedIndices() to ListSelectionModel
Reviewed-by: serb, psadhukhan

*** 2172,2199 **** * @see #removeSelectionInterval * @see #addListSelectionListener */ @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; } /** * Selects a single cell. Does nothing if the given index is greater --- 2172,2182 ---- * @see #removeSelectionInterval * @see #addListSelectionListener */ @Transient public int[] getSelectedIndices() { ! return getSelectionModel().getSelectedIndices(); } /** * Selects a single cell. Does nothing if the given index is greater
*** 2289,2316 **** * @see #addListSelectionListener * * @since 1.7 */ public List<E> getSelectedValuesList() { - ListSelectionModel sm = getSelectionModel(); ListModel<E> dm = getModel(); ! int iMin = sm.getMinSelectionIndex(); ! int iMax = sm.getMaxSelectionIndex(); ! ! if ((iMin < 0) || (iMax < 0)) { return Collections.emptyList(); } - List<E> selectedItems = new ArrayList<E>(); ! for(int i = iMin; i <= iMax; i++) { ! if (sm.isSelectedIndex(i)) { selectedItems.add(dm.getElementAt(i)); } - } return selectedItems; } /** * Returns the smallest selected cell index; <i>the selection</i> when only * a single item is selected in the list. When multiple items are selected, --- 2272,2299 ---- * @see #addListSelectionListener * * @since 1.7 */ public List<E> getSelectedValuesList() { ListModel<E> dm = getModel(); + 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 Collections.emptyList(); + } /** * Returns the smallest selected cell index; <i>the selection</i> when only * a single item is selected in the list. When multiple items are selected,
< prev index next >