*
- * JList bigDataList = new JList(bigData);
+ * {@code
+ * JList bigDataList = new JList(bigData);
*
* // We don't want the JList implementation to compute the width
* // or height of all of the list cells, so we give it a string
@@ -204,6 +224,7 @@
* // properties.
*
* bigDataList.setPrototypeCellValue("Index 1234567890");
+ * }
*
*
* {@code JList} doesn't implement scrolling directly. To create a list that
@@ -260,13 +281,15 @@
* @see ListCellRenderer
* @see DefaultListCellRenderer
*
+ * @param the type of the elements of this list
+ *
* @beaninfo
* attribute: isContainer false
* description: A component which allows for the selection of one or more objects from a list.
*
* @author Hans Muller
*/
-public class JList extends JComponent implements Scrollable, Accessible
+public class JList extends JComponent implements Scrollable, Accessible
{
/**
* @see #getUIClassID
@@ -301,15 +324,15 @@
private int fixedCellWidth = -1;
private int fixedCellHeight = -1;
private int horizontalScrollIncrement = -1;
- private Object prototypeCellValue;
+ private E prototypeCellValue;
private int visibleRowCount = 8;
private Color selectionForeground;
private Color selectionBackground;
private boolean dragEnabled;
private ListSelectionModel selectionModel;
- private ListModel dataModel;
- private ListCellRenderer cellRenderer;
+ private ListModel dataModel;
+ private ListCellRenderer super E> cellRenderer;
private ListSelectionListener selectionListener;
/**
@@ -402,7 +425,7 @@
* @param dataModel the model for the list
* @exception IllegalArgumentException if the model is {@code null}
*/
- public JList(ListModel dataModel)
+ public JList(ListModel dataModel)
{
if (dataModel == null) {
throw new IllegalArgumentException("dataModel must be non null");
@@ -437,12 +460,12 @@
* @param listData the array of Objects to be loaded into the data model,
* {@code non-null}
*/
- public JList(final Object[] listData)
+ public JList(final E[] listData)
{
this (
- new AbstractListModel() {
+ new AbstractListModel() {
public int getSize() { return listData.length; }
- public Object getElementAt(int i) { return listData[i]; }
+ public E getElementAt(int i) { return listData[i]; }
}
);
}
@@ -462,11 +485,11 @@
* @param listData the Vector
to be loaded into the
* data model, {@code non-null}
*/
- public JList(final Vector> listData) {
+ public JList(final Vector extends E> listData) {
this (
- new AbstractListModel() {
+ new AbstractListModel() {
public int getSize() { return listData.size(); }
- public Object getElementAt(int i) { return listData.elementAt(i); }
+ public E getElementAt(int i) { return listData.elementAt(i); }
}
);
}
@@ -477,9 +500,9 @@
*/
public JList() {
this (
- new AbstractListModel() {
+ new AbstractListModel() {
public int getSize() { return 0; }
- public Object getElementAt(int i) { return "No Data Model"; }
+ public E getElementAt(int i) { throw new IndexOutOfBoundsException("No Data Model"); }
}
);
}
@@ -526,7 +549,7 @@
public void updateUI() {
setUI((ListUI)UIManager.getUI(this));
- ListCellRenderer renderer = getCellRenderer();
+ ListCellRenderer super E> renderer = getCellRenderer();
if (renderer instanceof Component) {
SwingUtilities.updateComponentTreeUI((Component)renderer);
}
@@ -560,8 +583,8 @@
*/
private void updateFixedCellSize()
{
- ListCellRenderer cr = getCellRenderer();
- Object value = getPrototypeCellValue();
+ ListCellRenderer super E> cr = getCellRenderer();
+ E value = getPrototypeCellValue();
if ((cr != null) && (value != null)) {
Component c = cr.getListCellRendererComponent(this, value, 0, false, false);
@@ -592,7 +615,7 @@
* @return the value of the {@code prototypeCellValue} property
* @see #setPrototypeCellValue
*/
- public Object getPrototypeCellValue() {
+ public E getPrototypeCellValue() {
return prototypeCellValue;
}
@@ -632,8 +655,8 @@
* attribute: visualUpdate true
* description: The cell prototype value, used to compute cell width and height.
*/
- public void setPrototypeCellValue(Object prototypeCellValue) {
- Object oldValue = this.prototypeCellValue;
+ public void setPrototypeCellValue(E prototypeCellValue) {
+ E oldValue = this.prototypeCellValue;
this.prototypeCellValue = prototypeCellValue;
/* If the prototypeCellValue has changed and is non-null,
@@ -727,7 +750,7 @@
* @see #setCellRenderer
*/
@Transient
- public ListCellRenderer getCellRenderer() {
+ public ListCellRenderer super E> getCellRenderer() {
return cellRenderer;
}
@@ -755,8 +778,8 @@
* attribute: visualUpdate true
* description: The component used to draw the cells.
*/
- public void setCellRenderer(ListCellRenderer cellRenderer) {
- ListCellRenderer oldValue = this.cellRenderer;
+ public void setCellRenderer(ListCellRenderer super E> cellRenderer) {
+ ListCellRenderer super E> oldValue = this.cellRenderer;
this.cellRenderer = cellRenderer;
/* If the cellRenderer has changed and prototypeCellValue
@@ -1455,7 +1478,7 @@
* @since 1.4
*/
public int getNextMatch(String prefix, int startIndex, Position.Bias bias) {
- ListModel model = getModel();
+ ListModel model = getModel();
int max = model.getSize();
if (prefix == null) {
throw new IllegalArgumentException();
@@ -1469,16 +1492,16 @@
int increment = (bias == Position.Bias.Forward) ? 1 : -1;
int index = startIndex;
do {
- Object o = model.getElementAt(index);
-
- if (o != null) {
+ E element = model.getElementAt(index);
+
+ if (element != null) {
String string;
- if (o instanceof String) {
- string = ((String)o).toUpperCase();
+ if (element instanceof String) {
+ string = ((String)element).toUpperCase();
}
else {
- string = o.toString();
+ string = element.toString();
if (string != null) {
string = string.toUpperCase();
}
@@ -1516,7 +1539,7 @@
if(event != null) {
Point p = event.getPoint();
int index = locationToIndex(p);
- ListCellRenderer r = getCellRenderer();
+ ListCellRenderer super E> r = getCellRenderer();
Rectangle cellBounds;
if (index != -1 && r != null && (cellBounds =
@@ -1634,7 +1657,7 @@
* list of items
* @see #setModel
*/
- public ListModel getModel() {
+ public ListModel getModel() {
return dataModel;
}
@@ -1656,11 +1679,11 @@
* attribute: visualUpdate true
* description: The object that contains the data to be drawn by this JList.
*/
- public void setModel(ListModel model) {
+ public void setModel(ListModel model) {
if (model == null) {
throw new IllegalArgumentException("model must be non null");
}
- ListModel oldValue = dataModel;
+ ListModel oldValue = dataModel;
dataModel = model;
firePropertyChange("model", oldValue, dataModel);
clearSelection();
@@ -1668,7 +1691,7 @@
/**
- * Constructs a read-only ListModel
from an array of objects,
+ * Constructs a read-only ListModel
from an array of items,
* and calls {@code setModel} with this model.
*
* Attempts to pass a {@code null} value to this method results in
@@ -1676,15 +1699,15 @@
* references the given array directly. Attempts to modify the array
* after invoking this method results in undefined behavior.
*
- * @param listData an array of {@code Objects} containing the items to
+ * @param listData an array of {@code E} containing the items to
* display in the list
* @see #setModel
*/
- public void setListData(final Object[] listData) {
+ public void setListData(final E[] listData) {
setModel (
- new AbstractListModel() {
+ new AbstractListModel() {
public int getSize() { return listData.length; }
- public Object getElementAt(int i) { return listData[i]; }
+ public E getElementAt(int i) { return listData[i]; }
}
);
}
@@ -1703,11 +1726,11 @@
* display in the list
* @see #setModel
*/
- public void setListData(final Vector> listData) {
+ public void setListData(final Vector extends E> listData) {
setModel (
- new AbstractListModel() {
+ new AbstractListModel() {
public int getSize() { return listData.size(); }
- public Object getElementAt(int i) { return listData.elementAt(i); }
+ public E getElementAt(int i) { return listData.elementAt(i); }
}
);
}
@@ -2235,10 +2258,13 @@
* @see #isSelectedIndex
* @see #getModel
* @see #addListSelectionListener
+ *
+ * @deprecated As of JDK 1.7, replaced by {@link #getSelectedValuesList()}
*/
+ @Deprecated
public Object[] getSelectedValues() {
ListSelectionModel sm = getSelectionModel();
- ListModel dm = getModel();
+ ListModel dm = getModel();
int iMin = sm.getMinSelectionIndex();
int iMax = sm.getMaxSelectionIndex();
@@ -2259,6 +2285,37 @@
return rv;
}
+ /**
+ * Returns a list of all the selected items, in increasing order based
+ * on their indices in the list.
+ *
+ * @return the selected items, or an empty list if nothing is selected
+ * @see #isSelectedIndex
+ * @see #getModel
+ * @see #addListSelectionListener
+ *
+ * @since 1.7
+ */
+ public List getSelectedValuesList() {
+ ListSelectionModel sm = getSelectionModel();
+ ListModel dm = getModel();
+
+ int iMin = sm.getMinSelectionIndex();
+ int iMax = sm.getMaxSelectionIndex();
+
+ if ((iMin < 0) || (iMax < 0)) {
+ return Collections.emptyList();
+ }
+
+ List selectedItems = new ArrayList();
+ for(int i = iMin; i <= iMax; i++) {
+ if (sm.isSelectedIndex(i)) {
+ selectedItems.add(dm.getElementAt(i));
+ }
+ }
+ return selectedItems;
+ }
+
/**
* Returns the smallest selected cell index; the selection when only
@@ -2291,7 +2348,7 @@
* @see #getModel
* @see #addListSelectionListener
*/
- public Object getSelectedValue() {
+ public E getSelectedValue() {
int i = getMinSelectionIndex();
return (i == -1) ? null : getModel().getElementAt(i);
}
@@ -2309,7 +2366,7 @@
setSelectedIndex(-1);
else if(!anObject.equals(getSelectedValue())) {
int i,c;
- ListModel dm = getModel();
+ ListModel dm = getModel();
for(i=0,c=dm.getSize();i parent = null;
private int indexInParent;
private Component component = null;
private AccessibleContext accessibleContext = null;
- private ListModel listModel;
- private ListCellRenderer cellRenderer = null;
-
- public AccessibleJListChild(JList parent, int indexInParent) {
+ private ListModel listModel;
+ private ListCellRenderer super E> cellRenderer = null;
+
+ public AccessibleJListChild(JList parent, int indexInParent) {
this.parent = parent;
this.setAccessibleParent(parent);
this.indexInParent = indexInParent;
@@ -3175,7 +3232,7 @@
if ((parent != null)
&& (listModel != null)
&& cellRenderer != null) {
- Object value = listModel.getElementAt(index);
+ E value = listModel.getElementAt(index);
boolean isSelected = parent.isSelectedIndex(index);
boolean isFocussed = parent.isFocusOwner()
&& (index == parent.getLeadSelectionIndex());
diff -r 06d5378aaff3 -r ef21a120cb18 jdk/src/share/classes/javax/swing/ListCellRenderer.java
--- a/jdk/src/share/classes/javax/swing/ListCellRenderer.java Wed Nov 18 17:36:46 2009 +0300
+++ b/jdk/src/share/classes/javax/swing/ListCellRenderer.java Mon Nov 23 20:57:17 2009 +0300
@@ -33,12 +33,13 @@
* the cells in a JList. For example, to use a JLabel as a
* ListCellRenderer, you would write something like this:
*
- * class MyCellRenderer extends JLabel implements ListCellRenderer {
+ * {@code
+ * class MyCellRenderer extends JLabel implements ListCellRenderer
*
+ * @param the type of values this renderer can be used for
+ *
* @see JList
* @see DefaultListCellRenderer
*
* @author Hans Muller
*/
-public interface ListCellRenderer
+public interface ListCellRenderer
{
/**
* Return a component that has been configured to display the specified
@@ -104,8 +108,8 @@
* @see ListModel
*/
Component getListCellRendererComponent(
- JList list,
- Object value,
+ JList extends E> list,
+ E value,
int index,
boolean isSelected,
boolean cellHasFocus);
diff -r 06d5378aaff3 -r ef21a120cb18 jdk/src/share/classes/javax/swing/ListModel.java
--- a/jdk/src/share/classes/javax/swing/ListModel.java Wed Nov 18 17:36:46 2009 +0300
+++ b/jdk/src/share/classes/javax/swing/ListModel.java Mon Nov 23 20:57:17 2009 +0300
@@ -35,10 +35,12 @@
* length of the data model must be reported to all of the
* ListDataListeners.
*
+ * @param the type of the elements of this model
+ *
* @author Hans Muller
* @see JList
*/
-public interface ListModel
+public interface ListModel
{
/**
* Returns the length of the list.
@@ -51,7 +53,7 @@
* @param index the requested index
* @return the value at index
*/
- Object getElementAt(int index);
+ E getElementAt(int index);
/**
* Adds a listener to the list that's notified each time a change
diff -r 06d5378aaff3 -r ef21a120cb18 jdk/test/javax/swing/JList/6823603/bug6823603.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/JList/6823603/bug6823603.java Mon Nov 23 20:57:17 2009 +0300
@@ -0,0 +1,247 @@
+/*
+ * Copyright 2009 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/* @test
+ @bug 6823603
+ @summary Generics: JList
+ @author Florian Brunner
+ @run main bug6823603
+ */
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Vector;
+import java.util.Enumeration;
+import java.awt.*;
+import javax.swing.*;
+
+public class bug6823603 {
+
+ private static final String TEST_ELEMENT = "Test1";
+
+ /**
+ * @param args the command line arguments
+ */
+ @SuppressWarnings("unchecked")
+ public static void main(String[] args) {
+ testRawSignatures();
+ testGenericSignatures();
+
+ testGetSelectedValuesList(); // new method
+ }
+
+ @SuppressWarnings("unchecked")
+ private static void testRawSignatures() {
+ // Test JList
+ ListModel rawTestModel = new DefaultListModel();
+
+ new JList();
+ new JList(rawTestModel);
+ new JList(new Object[]{TEST_ELEMENT});
+ JList rawTestList = new JList(new Vector());
+ rawTestList.getPrototypeCellValue();
+ rawTestList.setPrototypeCellValue(TEST_ELEMENT);
+ rawTestList.getCellRenderer();
+ rawTestList.setCellRenderer(new DefaultListCellRenderer());
+ rawTestList.getModel();
+ rawTestList.setModel(rawTestModel);
+ rawTestList.setListData(new Object[]{TEST_ELEMENT});
+ rawTestList.setListData(new Vector());
+
+ @SuppressWarnings("deprecation")
+ Object[] selectedValues = rawTestList.getSelectedValues();
+ rawTestList.getSelectedValue();
+
+ // Test ListCellRenderer
+ ListCellRenderer rawTestCellRenderer = new DefaultListCellRenderer();
+ String testEntry = "Test";
+ @SuppressWarnings("unchecked")
+ JList rawJList = new JList(new Object[]{testEntry});
+
+ rawTestCellRenderer.getListCellRendererComponent(rawJList,
+ testEntry, 0, true, true);
+
+ // Test ListModel
+ DefaultListModel testModel = new DefaultListModel();
+ testModel.addElement(TEST_ELEMENT);
+ rawTestModel = testModel;
+ rawTestModel.getElementAt(0);
+
+ // Test DefaultListModel
+ DefaultListModel defaultListModel = new DefaultListModel();
+
+ defaultListModel.addElement(TEST_ELEMENT);
+ defaultListModel.getElementAt(0);
+ defaultListModel.elements();
+ defaultListModel.elementAt(0);
+ defaultListModel.firstElement();
+ defaultListModel.lastElement();
+
+ String testElement2 = "Test2";
+
+ defaultListModel.setElementAt(testElement2, 0);
+ defaultListModel.insertElementAt(TEST_ELEMENT, 0);
+ defaultListModel.get(0);
+ defaultListModel.set(0, testElement2);
+ defaultListModel.add(0, TEST_ELEMENT);
+ defaultListModel.remove(0);
+
+ // Test AbstractListModel
+ @SuppressWarnings("serial")
+ ListModel abstractListModel = new AbstractListModel() {
+ public int getSize() {
+ throw new UnsupportedOperationException("Not supported yet.");
+ }
+
+ public Object getElementAt(int index) {
+ throw new UnsupportedOperationException("Not supported yet.");
+ }
+ };
+
+ // Test DefaultListCellRenderer
+ DefaultListCellRenderer cellRenderer = new DefaultListCellRenderer();
+
+ @SuppressWarnings("unchecked")
+ JList list = new JList(new Object[]{testEntry});
+
+ cellRenderer.getListCellRendererComponent(rawJList, testEntry, 0, true, true);
+ }
+
+ private static void testGenericSignatures() {
+ // Test JList
+ ListModel stringListModel = new DefaultListModel();
+
+ new JList();
+ new JList(stringListModel);
+ new JList(new String[]{TEST_ELEMENT});
+
+ JList stringTestList = new JList(new Vector());
+
+ stringTestList.getPrototypeCellValue();
+ stringTestList.setPrototypeCellValue(TEST_ELEMENT);
+
+ ListCellRenderer super String> cellRenderer = stringTestList.getCellRenderer();
+
+ stringTestList.setCellRenderer(new DefaultListCellRenderer());
+
+ ListModel model = stringTestList.getModel();
+
+ stringTestList.setModel(stringListModel);
+ stringTestList.setListData(new String[]{TEST_ELEMENT});
+ stringTestList.setListData(new Vector());
+
+ @SuppressWarnings("deprecation")
+ Object[] selectedValues = stringTestList.getSelectedValues();
+
+ stringTestList.getSelectedValue();
+
+ // Test ListCellRenderer
+ ListCellRenderer