< prev index next >

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

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


2157      *
2158      * @see #setValueIsAdjusting
2159      * @see ListSelectionModel#getValueIsAdjusting
2160      */
2161     public boolean getValueIsAdjusting() {
2162         return getSelectionModel().getValueIsAdjusting();
2163     }
2164 
2165 
2166     /**
2167      * Returns an array of all of the selected indices, in increasing
2168      * order.
2169      *
2170      * @return all of the selected indices, in increasing order,
2171      *         or an empty array if nothing is selected
2172      * @see #removeSelectionInterval
2173      * @see #addListSelectionListener
2174      */
2175     @Transient
2176     public int[] getSelectedIndices() {
2177         ListSelectionModel sm = getSelectionModel();
2178         int iMin = sm.getMinSelectionIndex();
2179         int iMax = sm.getMaxSelectionIndex();
2180 
2181         if ((iMin < 0) || (iMax < 0)) {
2182             return new int[0];
2183         }
2184 
2185         int[] rvTmp = new int[1+ (iMax - iMin)];
2186         int n = 0;
2187         for(int i = iMin; i <= iMax; i++) {
2188             if (sm.isSelectedIndex(i)) {
2189                 rvTmp[n++] = i;
2190             }
2191         }
2192         int[] rv = new int[n];
2193         System.arraycopy(rvTmp, 0, rv, 0, n);
2194         return rv;
2195     }
2196 
2197 
2198     /**
2199      * Selects a single cell. Does nothing if the given index is greater
2200      * than or equal to the model size. This is a convenience method that uses
2201      * {@code setSelectionInterval} on the selection model. Refer to the
2202      * documentation for the selection model class being used for details on
2203      * how values less than {@code 0} are handled.
2204      *
2205      * @param index the index of the cell to select
2206      * @see ListSelectionModel#setSelectionInterval
2207      * @see #isSelectedIndex
2208      * @see #addListSelectionListener
2209      * @beaninfo
2210      * description: The index of the selected cell.
2211      */
2212     public void setSelectedIndex(int index) {
2213         if (index >= getModel().getSize()) {
2214             return;


2274                 rvTmp[n++] = dm.getElementAt(i);
2275             }
2276         }
2277         Object[] rv = new Object[n];
2278         System.arraycopy(rvTmp, 0, rv, 0, n);
2279         return rv;
2280     }
2281 
2282     /**
2283      * Returns a list of all the selected items, in increasing order based
2284      * on their indices in the list.
2285      *
2286      * @return the selected items, or an empty list if nothing is selected
2287      * @see #isSelectedIndex
2288      * @see #getModel
2289      * @see #addListSelectionListener
2290      *
2291      * @since 1.7
2292      */
2293     public List<E> getSelectedValuesList() {
2294         ListSelectionModel sm = getSelectionModel();
2295         ListModel<E> dm = getModel();

2296 
2297         int iMin = sm.getMinSelectionIndex();
2298         int iMax = sm.getMaxSelectionIndex();
2299 
2300         if ((iMin < 0) || (iMax < 0)) {
2301             return Collections.emptyList();
2302         }
2303 
2304         List<E> selectedItems = new ArrayList<E>();
2305         for(int i = iMin; i <= iMax; i++) {
2306             if (sm.isSelectedIndex(i)) {

2307                 selectedItems.add(dm.getElementAt(i));
2308             }
2309         }
2310         return selectedItems;


2311     }
2312 
2313 
2314     /**
2315      * Returns the smallest selected cell index; <i>the selection</i> when only
2316      * a single item is selected in the list. When multiple items are selected,
2317      * it is simply the smallest selected index. Returns {@code -1} if there is
2318      * no selection.
2319      * <p>
2320      * This method is a cover that delegates to {@code getMinSelectionIndex}.
2321      *
2322      * @return the smallest selected cell index
2323      * @see #getMinSelectionIndex
2324      * @see #addListSelectionListener
2325      */
2326     public int getSelectedIndex() {
2327         return getMinSelectionIndex();
2328     }
2329 
2330 




2157      *
2158      * @see #setValueIsAdjusting
2159      * @see ListSelectionModel#getValueIsAdjusting
2160      */
2161     public boolean getValueIsAdjusting() {
2162         return getSelectionModel().getValueIsAdjusting();
2163     }
2164 
2165 
2166     /**
2167      * Returns an array of all of the selected indices, in increasing
2168      * order.
2169      *
2170      * @return all of the selected indices, in increasing order,
2171      *         or an empty array if nothing is selected
2172      * @see #removeSelectionInterval
2173      * @see #addListSelectionListener
2174      */
2175     @Transient
2176     public int[] getSelectedIndices() {
2177         return getSelectionModel().getSelectedIndices();

















2178     }
2179 
2180 
2181     /**
2182      * Selects a single cell. Does nothing if the given index is greater
2183      * than or equal to the model size. This is a convenience method that uses
2184      * {@code setSelectionInterval} on the selection model. Refer to the
2185      * documentation for the selection model class being used for details on
2186      * how values less than {@code 0} are handled.
2187      *
2188      * @param index the index of the cell to select
2189      * @see ListSelectionModel#setSelectionInterval
2190      * @see #isSelectedIndex
2191      * @see #addListSelectionListener
2192      * @beaninfo
2193      * description: The index of the selected cell.
2194      */
2195     public void setSelectedIndex(int index) {
2196         if (index >= getModel().getSize()) {
2197             return;


2257                 rvTmp[n++] = dm.getElementAt(i);
2258             }
2259         }
2260         Object[] rv = new Object[n];
2261         System.arraycopy(rvTmp, 0, rv, 0, n);
2262         return rv;
2263     }
2264 
2265     /**
2266      * Returns a list of all the selected items, in increasing order based
2267      * on their indices in the list.
2268      *
2269      * @return the selected items, or an empty list if nothing is selected
2270      * @see #isSelectedIndex
2271      * @see #getModel
2272      * @see #addListSelectionListener
2273      *
2274      * @since 1.7
2275      */
2276     public List<E> getSelectedValuesList() {

2277         ListModel<E> dm = getModel();
2278         int[] selectedIndices = getSelectedIndices();
2279 
2280         if (selectedIndices.length > 0) {
2281             int size = dm.getSize();
2282             if (selectedIndices[0] >= size) {

2283                 return Collections.emptyList();
2284             }

2285             List<E> selectedItems = new ArrayList<E>();
2286             for (int i : selectedIndices) {
2287                 if (i >= size)
2288                     break;
2289                 selectedItems.add(dm.getElementAt(i));
2290             }

2291             return selectedItems;
2292         }
2293         return Collections.emptyList();
2294     }
2295 
2296 
2297     /**
2298      * Returns the smallest selected cell index; <i>the selection</i> when only
2299      * a single item is selected in the list. When multiple items are selected,
2300      * it is simply the smallest selected index. Returns {@code -1} if there is
2301      * no selection.
2302      * <p>
2303      * This method is a cover that delegates to {@code getMinSelectionIndex}.
2304      *
2305      * @return the smallest selected cell index
2306      * @see #getMinSelectionIndex
2307      * @see #addListSelectionListener
2308      */
2309     public int getSelectedIndex() {
2310         return getMinSelectionIndex();
2311     }
2312 
2313 


< prev index next >