< prev index next >

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

Print this page
rev 1527 : 6727662: Code improvement and warnings removing from swing packages
Summary: Removed unnecessary castings and other warnings
Reviewed-by: malenkov


1828      *
1829      * @param listener the {@code ListSelectionListener} to remove
1830      * @see #addListSelectionListener
1831      * @see #getSelectionModel
1832      */
1833     public void removeListSelectionListener(ListSelectionListener listener) {
1834         listenerList.remove(ListSelectionListener.class, listener);
1835     }
1836 
1837 
1838     /**
1839      * Returns an array of all the {@code ListSelectionListener}s added
1840      * to this {@code JList} by way of {@code addListSelectionListener}.
1841      *
1842      * @return all of the {@code ListSelectionListener}s on this list, or
1843      *         an empty array if no listeners have been added
1844      * @see #addListSelectionListener
1845      * @since 1.4
1846      */
1847     public ListSelectionListener[] getListSelectionListeners() {
1848         return (ListSelectionListener[])listenerList.getListeners(
1849                 ListSelectionListener.class);
1850     }
1851 
1852 
1853     /**
1854      * Sets the <code>selectionModel</code> for the list to a
1855      * non-<code>null</code> <code>ListSelectionModel</code>
1856      * implementation. The selection model handles the task of making single
1857      * selections, selections of contiguous ranges, and non-contiguous
1858      * selections.
1859      * <p>
1860      * This is a JavaBeans bound property.
1861      *
1862      * @param selectionModel  the <code>ListSelectionModel</code> that
1863      *                          implements the selections
1864      * @exception IllegalArgumentException   if <code>selectionModel</code>
1865      *                                          is <code>null</code>
1866      * @see #getSelectionModel
1867      * @beaninfo
1868      *       bound: true
1869      * description: The selection model, recording which cells are selected.


2199 
2200     /**
2201      * Changes the selection to be the set of indices specified by the given
2202      * array. Indices greater than or equal to the model size are ignored.
2203      * This is a convenience method that clears the selection and then uses
2204      * {@code addSelectionInterval} on the selection model to add the indices.
2205      * Refer to the documentation of the selection model class being used for
2206      * details on how values less than {@code 0} are handled.
2207      *
2208      * @param indices an array of the indices of the cells to select,
2209      *                {@code non-null}
2210      * @see ListSelectionModel#addSelectionInterval
2211      * @see #isSelectedIndex
2212      * @see #addListSelectionListener
2213      * @throws NullPointerException if the given array is {@code null}
2214      */
2215     public void setSelectedIndices(int[] indices) {
2216         ListSelectionModel sm = getSelectionModel();
2217         sm.clearSelection();
2218         int size = getModel().getSize();
2219         for(int i = 0; i < indices.length; i++) {
2220             if (indices[i] < size) {
2221                 sm.addSelectionInterval(indices[i], indices[i]);
2222             }
2223         }
2224     }
2225 
2226 
2227     /**
2228      * Returns an array of all the selected values, in increasing order based
2229      * on their indices in the list.
2230      *
2231      * @return the selected values, or an empty array if nothing is selected
2232      * @see #isSelectedIndex
2233      * @see #getModel
2234      * @see #addListSelectionListener
2235      */
2236     public Object[] getSelectedValues() {
2237         ListSelectionModel sm = getSelectionModel();
2238         ListModel dm = getModel();
2239 
2240         int iMin = sm.getMinSelectionIndex();
2241         int iMax = sm.getMaxSelectionIndex();


2703     /**
2704      * Returns {@code true} if this {@code JList} is displayed in a
2705      * {@code JViewport} and the viewport is wider than the list's
2706      * preferred width, or if the layout orientation is {@code HORIZONTAL_WRAP}
2707      * and {@code visibleRowCount <= 0}; otherwise returns {@code false}.
2708      * <p>
2709      * If {@code false}, then don't track the viewport's width. This allows
2710      * horizontal scrolling if the {@code JViewport} is itself embedded in a
2711      * {@code JScrollPane}.
2712      *
2713      * @return whether or not an enclosing viewport should force the list's
2714      *         width to match its own
2715      * @see Scrollable#getScrollableTracksViewportWidth
2716      */
2717     public boolean getScrollableTracksViewportWidth() {
2718         if (getLayoutOrientation() == HORIZONTAL_WRAP &&
2719                                       getVisibleRowCount() <= 0) {
2720             return true;
2721         }
2722         if (getParent() instanceof JViewport) {
2723             return (((JViewport)getParent()).getWidth() > getPreferredSize().width);
2724         }
2725         return false;
2726     }
2727 
2728     /**
2729      * Returns {@code true} if this {@code JList} is displayed in a
2730      * {@code JViewport} and the viewport is taller than the list's
2731      * preferred height, or if the layout orientation is {@code VERTICAL_WRAP}
2732      * and {@code visibleRowCount <= 0}; otherwise returns {@code false}.
2733      * <p>
2734      * If {@code false}, then don't track the viewport's height. This allows
2735      * vertical scrolling if the {@code JViewport} is itself embedded in a
2736      * {@code JScrollPane}.
2737      *
2738      * @return whether or not an enclosing viewport should force the list's
2739      *         height to match its own
2740      * @see Scrollable#getScrollableTracksViewportHeight
2741      */
2742     public boolean getScrollableTracksViewportHeight() {
2743         if (getLayoutOrientation() == VERTICAL_WRAP &&
2744                      getVisibleRowCount() <= 0) {
2745             return true;
2746         }
2747         if (getParent() instanceof JViewport) {
2748             return (((JViewport)getParent()).getHeight() > getPreferredSize().height);
2749         }
2750         return false;
2751     }
2752 
2753 
2754     /*
2755      * See {@code readObject} and {@code writeObject} in {@code JComponent}
2756      * for more information about serialization in Swing.
2757      */
2758     private void writeObject(ObjectOutputStream s) throws IOException {
2759         s.defaultWriteObject();
2760         if (getUIClassID().equals(uiClassID)) {
2761             byte count = JComponent.getWriteObjCounter(this);
2762             JComponent.setWriteObjCounter(this, --count);
2763             if (count == 0 && ui != null) {
2764                 ui.installUI(this);
2765             }
2766         }
2767     }
2768 


3140             private ListModel listModel;
3141             private ListCellRenderer cellRenderer = null;
3142 
3143             public AccessibleJListChild(JList parent, int indexInParent) {
3144                 this.parent = parent;
3145                 this.setAccessibleParent(parent);
3146                 this.indexInParent = indexInParent;
3147                 if (parent != null) {
3148                     listModel = parent.getModel();
3149                     cellRenderer = parent.getCellRenderer();
3150                 }
3151             }
3152 
3153             private Component getCurrentComponent() {
3154                 return getComponentAtIndex(indexInParent);
3155             }
3156 
3157             private AccessibleContext getCurrentAccessibleContext() {
3158                 Component c = getComponentAtIndex(indexInParent);
3159                 if (c instanceof Accessible) {
3160                     return ((Accessible) c).getAccessibleContext();
3161                 } else {
3162                     return null;
3163                 }
3164             }
3165 
3166             private Component getComponentAtIndex(int index) {
3167                 if (index < 0 || index >= listModel.getSize()) {
3168                     return null;
3169                 }
3170                 if ((parent != null)
3171                         && (listModel != null)
3172                         && cellRenderer != null) {
3173                     Object value = listModel.getElementAt(index);
3174                     boolean isSelected = parent.isSelectedIndex(index);
3175                     boolean isFocussed = parent.isFocusOwner()
3176                             && (index == parent.getLeadSelectionIndex());
3177                     return cellRenderer.getListCellRendererComponent(
3178                             parent,
3179                             value,
3180                             index,




1828      *
1829      * @param listener the {@code ListSelectionListener} to remove
1830      * @see #addListSelectionListener
1831      * @see #getSelectionModel
1832      */
1833     public void removeListSelectionListener(ListSelectionListener listener) {
1834         listenerList.remove(ListSelectionListener.class, listener);
1835     }
1836 
1837 
1838     /**
1839      * Returns an array of all the {@code ListSelectionListener}s added
1840      * to this {@code JList} by way of {@code addListSelectionListener}.
1841      *
1842      * @return all of the {@code ListSelectionListener}s on this list, or
1843      *         an empty array if no listeners have been added
1844      * @see #addListSelectionListener
1845      * @since 1.4
1846      */
1847     public ListSelectionListener[] getListSelectionListeners() {
1848         return listenerList.getListeners(ListSelectionListener.class);

1849     }
1850 
1851 
1852     /**
1853      * Sets the <code>selectionModel</code> for the list to a
1854      * non-<code>null</code> <code>ListSelectionModel</code>
1855      * implementation. The selection model handles the task of making single
1856      * selections, selections of contiguous ranges, and non-contiguous
1857      * selections.
1858      * <p>
1859      * This is a JavaBeans bound property.
1860      *
1861      * @param selectionModel  the <code>ListSelectionModel</code> that
1862      *                          implements the selections
1863      * @exception IllegalArgumentException   if <code>selectionModel</code>
1864      *                                          is <code>null</code>
1865      * @see #getSelectionModel
1866      * @beaninfo
1867      *       bound: true
1868      * description: The selection model, recording which cells are selected.


2198 
2199     /**
2200      * Changes the selection to be the set of indices specified by the given
2201      * array. Indices greater than or equal to the model size are ignored.
2202      * This is a convenience method that clears the selection and then uses
2203      * {@code addSelectionInterval} on the selection model to add the indices.
2204      * Refer to the documentation of the selection model class being used for
2205      * details on how values less than {@code 0} are handled.
2206      *
2207      * @param indices an array of the indices of the cells to select,
2208      *                {@code non-null}
2209      * @see ListSelectionModel#addSelectionInterval
2210      * @see #isSelectedIndex
2211      * @see #addListSelectionListener
2212      * @throws NullPointerException if the given array is {@code null}
2213      */
2214     public void setSelectedIndices(int[] indices) {
2215         ListSelectionModel sm = getSelectionModel();
2216         sm.clearSelection();
2217         int size = getModel().getSize();
2218         for (int i : indices) {
2219             if (i < size) {
2220                 sm.addSelectionInterval(i, i);
2221             }
2222         }
2223     }
2224 
2225 
2226     /**
2227      * Returns an array of all the selected values, in increasing order based
2228      * on their indices in the list.
2229      *
2230      * @return the selected values, or an empty array if nothing is selected
2231      * @see #isSelectedIndex
2232      * @see #getModel
2233      * @see #addListSelectionListener
2234      */
2235     public Object[] getSelectedValues() {
2236         ListSelectionModel sm = getSelectionModel();
2237         ListModel dm = getModel();
2238 
2239         int iMin = sm.getMinSelectionIndex();
2240         int iMax = sm.getMaxSelectionIndex();


2702     /**
2703      * Returns {@code true} if this {@code JList} is displayed in a
2704      * {@code JViewport} and the viewport is wider than the list's
2705      * preferred width, or if the layout orientation is {@code HORIZONTAL_WRAP}
2706      * and {@code visibleRowCount <= 0}; otherwise returns {@code false}.
2707      * <p>
2708      * If {@code false}, then don't track the viewport's width. This allows
2709      * horizontal scrolling if the {@code JViewport} is itself embedded in a
2710      * {@code JScrollPane}.
2711      *
2712      * @return whether or not an enclosing viewport should force the list's
2713      *         width to match its own
2714      * @see Scrollable#getScrollableTracksViewportWidth
2715      */
2716     public boolean getScrollableTracksViewportWidth() {
2717         if (getLayoutOrientation() == HORIZONTAL_WRAP &&
2718                                       getVisibleRowCount() <= 0) {
2719             return true;
2720         }
2721         if (getParent() instanceof JViewport) {
2722             return (getParent().getWidth() > getPreferredSize().width);
2723         }
2724         return false;
2725     }
2726 
2727     /**
2728      * Returns {@code true} if this {@code JList} is displayed in a
2729      * {@code JViewport} and the viewport is taller than the list's
2730      * preferred height, or if the layout orientation is {@code VERTICAL_WRAP}
2731      * and {@code visibleRowCount <= 0}; otherwise returns {@code false}.
2732      * <p>
2733      * If {@code false}, then don't track the viewport's height. This allows
2734      * vertical scrolling if the {@code JViewport} is itself embedded in a
2735      * {@code JScrollPane}.
2736      *
2737      * @return whether or not an enclosing viewport should force the list's
2738      *         height to match its own
2739      * @see Scrollable#getScrollableTracksViewportHeight
2740      */
2741     public boolean getScrollableTracksViewportHeight() {
2742         if (getLayoutOrientation() == VERTICAL_WRAP &&
2743                      getVisibleRowCount() <= 0) {
2744             return true;
2745         }
2746         if (getParent() instanceof JViewport) {
2747             return (getParent().getHeight() > getPreferredSize().height);
2748         }
2749         return false;
2750     }
2751 
2752 
2753     /*
2754      * See {@code readObject} and {@code writeObject} in {@code JComponent}
2755      * for more information about serialization in Swing.
2756      */
2757     private void writeObject(ObjectOutputStream s) throws IOException {
2758         s.defaultWriteObject();
2759         if (getUIClassID().equals(uiClassID)) {
2760             byte count = JComponent.getWriteObjCounter(this);
2761             JComponent.setWriteObjCounter(this, --count);
2762             if (count == 0 && ui != null) {
2763                 ui.installUI(this);
2764             }
2765         }
2766     }
2767 


3139             private ListModel listModel;
3140             private ListCellRenderer cellRenderer = null;
3141 
3142             public AccessibleJListChild(JList parent, int indexInParent) {
3143                 this.parent = parent;
3144                 this.setAccessibleParent(parent);
3145                 this.indexInParent = indexInParent;
3146                 if (parent != null) {
3147                     listModel = parent.getModel();
3148                     cellRenderer = parent.getCellRenderer();
3149                 }
3150             }
3151 
3152             private Component getCurrentComponent() {
3153                 return getComponentAtIndex(indexInParent);
3154             }
3155 
3156             private AccessibleContext getCurrentAccessibleContext() {
3157                 Component c = getComponentAtIndex(indexInParent);
3158                 if (c instanceof Accessible) {
3159                     return c.getAccessibleContext();
3160                 } else {
3161                     return null;
3162                 }
3163             }
3164 
3165             private Component getComponentAtIndex(int index) {
3166                 if (index < 0 || index >= listModel.getSize()) {
3167                     return null;
3168                 }
3169                 if ((parent != null)
3170                         && (listModel != null)
3171                         && cellRenderer != null) {
3172                     Object value = listModel.getElementAt(index);
3173                     boolean isSelected = parent.isSelectedIndex(index);
3174                     boolean isFocussed = parent.isFocusOwner()
3175                             && (index == parent.getLeadSelectionIndex());
3176                     return cellRenderer.getListCellRendererComponent(
3177                             parent,
3178                             value,
3179                             index,


< prev index next >