2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / javax / swing / JList.java
blob22d35a5a2c04800e9548e2547e6abd6c6c024202
1 /* JList.java --
2 Copyright (C) 2002, 2003 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing;
41 import java.awt.Color;
42 import java.awt.Dimension;
43 import java.awt.Rectangle;
44 import java.util.Vector;
45 import javax.accessibility.Accessible;
46 import javax.accessibility.AccessibleContext;
47 import javax.swing.event.ListDataEvent;
48 import javax.swing.event.ListDataListener;
49 import javax.swing.event.ListSelectionListener;
50 import javax.swing.plaf.ListUI;
52 public class JList extends JComponent implements Accessible, Scrollable
54 Color select_back, select_fore;
55 ListCellRenderer render;
56 int visibles = 8;
58 ListModel model;
59 ListSelectionModel sel_model;
61 public JList()
63 init();
66 public JList(Object[] listData)
68 init();
69 setListData(listData);
73 public JList(Vector listData)
75 init();
76 setListData(listData);
80 public JList(ListModel listData)
82 init();
83 setModel(listData);
85 void init()
87 render = new DefaultCellRenderer();
89 sel_model = new DefaultListSelectionModel();
90 setModel(new DefaultListModel());
92 select_back = new Color(0,0,255);
93 select_fore = new Color(255,255,255);
95 updateUI();
99 public int getVisibleRowCount()
100 { return visibles; }
101 public void setVisibleRowCount(int visibleRowCount)
103 visibles = visibleRowCount;
104 invalidate();
105 repaint();
108 void addListSelectionListener(ListSelectionListener listener)
109 { sel_model.addListSelectionListener(listener); }
110 void removeListSelectionListener(ListSelectionListener listener)
111 { sel_model.removeListSelectionListener(listener); }
113 void setSelectionMode(int a)
114 { sel_model.setSelectionMode(a); }
115 void setSelectedIndex(int a)
116 { sel_model.setSelectionInterval(a,a); }
117 int getSelectedIndex()
118 { return sel_model.getMinSelectionIndex(); }
119 Object getSelectedValue()
121 int index = getSelectedIndex();
122 if (index == -1)
123 return null;
124 return getModel().getElementAt(index);
127 Color getSelectionBackground()
128 { return select_back; }
129 Color getSelectionForeground()
130 { return select_fore; }
133 public void setListData(final Object[] listData)
135 class AL extends AbstractListModel
137 public int getSize() { return listData.length; }
138 public Object getElementAt(int i) { return listData[i]; }
141 setModel (new AL());
144 public void setListData(final Vector listData)
146 class AL extends AbstractListModel
148 public int getSize() { return listData.size(); }
149 public Object getElementAt(int i) { return listData.elementAt(i); }
152 setModel (new AL());
156 public ListCellRenderer getCellRenderer()
157 { return render; }
158 public void setCellRenderer(ListCellRenderer cellRenderer)
160 render = cellRenderer;
161 invalidate();
162 repaint();
165 public void setModel(ListModel model)
167 ListDataListener l = new ListDataListener()
169 public void intervalAdded(ListDataEvent e) {
170 repaint();
172 public void intervalRemoved(ListDataEvent e) {
173 repaint();
175 public void contentsChanged(ListDataEvent e) {
176 repaint();
180 this.model = model;
181 model.addListDataListener(l);
184 public ListModel getModel()
185 { return model; }
188 public ListUI getUI()
189 { return (ListUI) ui; }
190 public void setUI(ListUI ui)
191 { super.setUI(ui); }
193 public void updateUI()
195 setUI((ListUI)UIManager.getUI(this));
198 public String getUIClassID()
200 return "JList";
204 public AccessibleContext getAccessibleContext()
206 return null;
209 public Dimension getPreferredScrollableViewportSize()
211 return null;
214 public int getScrollableUnitIncrement(Rectangle visibleRect,
215 int orientation,
216 int direction)
218 return 1;
221 public int getScrollableBlockIncrement(Rectangle visibleRect,
222 int orientation,
223 int direction)
225 return 1;
228 public boolean getScrollableTracksViewportWidth()
230 return false;
233 public boolean getScrollableTracksViewportHeight()
235 return false;