< prev index next >

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

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


 292      *
 293      * @param x the ListSelectionListener
 294      * @see #removeListSelectionListener
 295      * @see #setSelectionInterval
 296      * @see #addSelectionInterval
 297      * @see #removeSelectionInterval
 298      * @see #clearSelection
 299      * @see #insertIndexInterval
 300      * @see #removeIndexInterval
 301      */
 302     void addListSelectionListener(ListSelectionListener x);
 303 
 304     /**
 305      * Remove a listener from the list that's notified each time a
 306      * change to the selection occurs.
 307      *
 308      * @param x the ListSelectionListener
 309      * @see #addListSelectionListener
 310      */
 311     void removeListSelectionListener(ListSelectionListener x);

























































 312 }


 292      *
 293      * @param x the ListSelectionListener
 294      * @see #removeListSelectionListener
 295      * @see #setSelectionInterval
 296      * @see #addSelectionInterval
 297      * @see #removeSelectionInterval
 298      * @see #clearSelection
 299      * @see #insertIndexInterval
 300      * @see #removeIndexInterval
 301      */
 302     void addListSelectionListener(ListSelectionListener x);
 303 
 304     /**
 305      * Remove a listener from the list that's notified each time a
 306      * change to the selection occurs.
 307      *
 308      * @param x the ListSelectionListener
 309      * @see #addListSelectionListener
 310      */
 311     void removeListSelectionListener(ListSelectionListener x);
 312 
 313     /**
 314      * Returns an array of all of the selected indices in the selection model,
 315      * in increasing order.
 316      *
 317      * @return all of the selected indices, in increasing order,
 318      *         or an empty array if nothing is selected
 319      * @see #removeSelectionInterval
 320      * @see #addListSelectionListener
 321      * @since 11
 322      * @implSpec The default implementation iterates from minimum selected
 323      * index {@link #getMinSelectionIndex()} to maximum selected index {@link
 324      * #getMaxSelectionIndex()} and returns the selected indices {@link
 325      * #isSelectedIndex(int)} in a newly allocated int array.
 326      */
 327     default int[] getSelectedIndices() {
 328         int iMin = getMinSelectionIndex();
 329         int iMax = getMaxSelectionIndex();
 330 
 331         if ((iMin < 0) || (iMax < 0)) {
 332             return new int[0];
 333         }
 334 
 335         int[] rvTmp = new int[1+ (iMax - iMin)];
 336         int n = 0;
 337         for(int i = iMin; i <= iMax; i++) {
 338             if (isSelectedIndex(i)) {
 339                 rvTmp[n++] = i;
 340             }
 341         }
 342         int[] rv = new int[n];
 343         System.arraycopy(rvTmp, 0, rv, 0, n);
 344         return rv;
 345     }
 346 
 347     /**
 348      * Returns the number of selected items.
 349      *
 350      * @return the number of selected items, 0 if no items are selected
 351      * @since 11
 352      * @implSpec The default implementation iterates from minimum selected
 353      * index {@link #getMinSelectionIndex()} to maximum selected index {@link
 354      * #getMaxSelectionIndex()} and returns the number of selected indices
 355      * {@link #isSelectedIndex(int)}
 356      */
 357     default int getSelectedItemsCount() {
 358         int iMin = getMinSelectionIndex();
 359         int iMax = getMaxSelectionIndex();
 360         int count = 0;
 361 
 362         for(int i = iMin; i <= iMax; i++) {
 363             if (isSelectedIndex(i)) {
 364                 count++;
 365             }
 366         }
 367         return count;
 368     }
 369 }
< prev index next >