summaryrefslogtreecommitdiff
path: root/javax/swing/JList.java
diff options
context:
space:
mode:
Diffstat (limited to 'javax/swing/JList.java')
-rw-r--r--javax/swing/JList.java180
1 files changed, 132 insertions, 48 deletions
diff --git a/javax/swing/JList.java b/javax/swing/JList.java
index 04f658361..04f3881a2 100644
--- a/javax/swing/JList.java
+++ b/javax/swing/JList.java
@@ -1,5 +1,5 @@
/* JList.java --
- Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
+ Copyright (C) 2002, 2003, 2004, 2005, 2006, Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -1022,48 +1022,56 @@ public class JList extends JComponent implements Accessible, Scrollable
/**
- * Creates a new JList object.
+ * Creates a new <code>JList</code> object.
*/
public JList()
{
- init();
+ init(new DefaultListModel());
}
/**
- * Creates a new JList object.
+ * Creates a new <code>JList</code> object.
*
- * @param listData Initial data to populate the list with
+ * @param items the initial list items.
*/
- public JList(Object[] listData)
+ public JList(Object[] items)
{
- init();
- setListData(listData);
+ init(createListModel(items));
}
/**
- * Creates a new JList object.
+ * Creates a new <code>JList</code> object.
*
- * @param listData Initial data to populate the list with
+ * @param items the initial list items.
*/
- public JList(Vector<?> listData)
+ public JList(Vector<?> items)
{
- init();
- setListData(listData);
+ init(createListModel(items));
}
/**
- * Creates a new JList object.
+ * Creates a new <code>JList</code> object.
*
- * @param listData Initial data to populate the list with
+ * @param model a model containing the list items (<code>null</code> not
+ * permitted).
+ *
+ * @throws IllegalArgumentException if <code>model</code> is
+ * <code>null</code>.
*/
- public JList(ListModel listData)
+ public JList(ListModel model)
{
- init();
- setModel(listData);
+ init(model);
}
- void init()
+ /**
+ * Initializes the list.
+ *
+ * @param m the list model (<code>null</code> not permitted).
+ */
+ private void init(ListModel m)
{
+ if (m == null)
+ throw new IllegalArgumentException("Null model not permitted.");
dragEnabled = false;
fixedCellHeight = -1;
fixedCellWidth = -1;
@@ -1075,9 +1083,17 @@ public class JList extends JComponent implements Accessible, Scrollable
cellRenderer = new DefaultListCellRenderer();
listListener = new ListListener();
- setModel(new DefaultListModel());
- setSelectionModel(createSelectionModel());
- setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
+ model = m;
+ if (model != null)
+ model.addListDataListener(listListener);
+
+ selectionModel = createSelectionModel();
+ if (selectionModel != null)
+ {
+ selectionModel.addListSelectionListener(listListener);
+ selectionModel.setSelectionMode
+ (ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
+ }
setLayout(null);
updateUI();
@@ -1117,6 +1133,8 @@ public class JList extends JComponent implements Accessible, Scrollable
* #prototypeCellValue} property is set, but setting it explicitly
* overrides the height computed from {@link #prototypeCellValue}.
*
+ * @param h the height.
+ *
* @see #getFixedCellHeight
* @see #getPrototypeCellValue
*/
@@ -1127,7 +1145,7 @@ public class JList extends JComponent implements Accessible, Scrollable
int old = fixedCellHeight;
fixedCellHeight = h;
- firePropertyChange("fixedCellWidth", old, h);
+ firePropertyChange("fixedCellHeight", old, h);
}
@@ -1549,20 +1567,39 @@ public class JList extends JComponent implements Accessible, Scrollable
* @param listData The object array to build a new list model on
* @see #setModel
*/
- public void setListData(final Object[] listData)
+ public void setListData(Object[] listData)
{
- setModel(new AbstractListModel()
- {
- public int getSize()
- {
- return listData.length;
- }
+ setModel(createListModel(listData));
+ }
- public Object getElementAt(int i)
- {
- return listData[i];
- }
- });
+ private ListModel createListModel(final Object[] items)
+ {
+ return new AbstractListModel()
+ {
+ public int getSize()
+ {
+ return items.length;
+ }
+ public Object getElementAt(int i)
+ {
+ return items[i];
+ }
+ };
+ }
+
+ private ListModel createListModel(final Vector items)
+ {
+ return new AbstractListModel()
+ {
+ public int getSize()
+ {
+ return items.size();
+ }
+ public Object getElementAt(int i)
+ {
+ return items.get(i);
+ }
+ };
}
/**
@@ -1576,17 +1613,17 @@ public class JList extends JComponent implements Accessible, Scrollable
public void setListData(final Vector<?> listData)
{
setModel(new AbstractListModel()
- {
- public int getSize()
- {
- return listData.size();
- }
-
- public Object getElementAt(int i)
- {
- return listData.elementAt(i);
- }
- });
+ {
+ public int getSize()
+ {
+ return listData.size();
+ }
+
+ public Object getElementAt(int i)
+ {
+ return listData.elementAt(i);
+ }
+ });
}
/**
@@ -2022,11 +2059,27 @@ public class JList extends JComponent implements Accessible, Scrollable
return selectionModel.getLeadSelectionIndex();
}
+ /**
+ * Returns the lowest item index in the current selection, or <code>-1</code>
+ * if there is no selection.
+ *
+ * @return The index.
+ *
+ * @see #getMaxSelectionIndex()
+ */
public int getMinSelectionIndex()
{
- return selectionModel.getMaxSelectionIndex();
+ return selectionModel.getMinSelectionIndex();
}
+ /**
+ * Returns the highest item index in the current selection, or
+ * <code>-1</code> if there is no selection.
+ *
+ * @return The index.
+ *
+ * @see #getMinSelectionIndex()
+ */
public int getMaxSelectionIndex()
{
return selectionModel.getMaxSelectionIndex();
@@ -2140,7 +2193,14 @@ public class JList extends JComponent implements Accessible, Scrollable
*/
public Rectangle getCellBounds(int index0, int index1)
{
- return getUI().getCellBounds(this, index0, index1);
+ ListUI ui = getUI();
+ Rectangle bounds = null;
+ if (ui != null)
+ {
+ bounds = ui.getCellBounds(this, index0, index1);
+ }
+ // When the UI is null, this method also returns null in the RI.
+ return bounds;
}
/**
@@ -2203,4 +2263,28 @@ public class JList extends JComponent implements Accessible, Scrollable
}
return index;
}
+
+ /**
+ * Returns a string describing the attributes for the <code>JList</code>
+ * component, for use in debugging. The return value is guaranteed to be
+ * non-<code>null</code>, but the format of the string may vary between
+ * implementations.
+ *
+ * @return A string describing the attributes of the <code>JList</code>.
+ */
+ protected String paramString()
+ {
+ StringBuffer sb = new StringBuffer(super.paramString());
+ sb.append(",fixedCellHeight=").append(getFixedCellHeight());
+ sb.append(",fixedCellWidth=").append(getFixedCellWidth());
+ sb.append(",selectionBackground=");
+ if (getSelectionBackground() != null)
+ sb.append(getSelectionBackground());
+ sb.append(",selectionForeground=");
+ if (getSelectionForeground() != null)
+ sb.append(getSelectionForeground());
+ sb.append(",visibleRowCount=").append(getVisibleRowCount());
+ sb.append(",layoutOrientation=").append(getLayoutOrientation());
+ return sb.toString();
+ }
}