| 1 | /* JList.java --
|
|---|
| 2 | Copyright (C) 2002 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | This file is part of GNU Classpath.
|
|---|
| 5 |
|
|---|
| 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.
|
|---|
| 10 |
|
|---|
| 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.
|
|---|
| 15 |
|
|---|
| 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.
|
|---|
| 20 |
|
|---|
| 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.
|
|---|
| 25 |
|
|---|
| 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. */
|
|---|
| 37 |
|
|---|
| 38 | package javax.swing;
|
|---|
| 39 |
|
|---|
| 40 | import javax.swing.event.*;
|
|---|
| 41 |
|
|---|
| 42 | import java.awt.*;
|
|---|
| 43 | import javax.swing.plaf.*;
|
|---|
| 44 | import java.util.*;
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 | import javax.accessibility.AccessibleContext;
|
|---|
| 48 | import javax.accessibility.AccessibleRole;
|
|---|
| 49 | import javax.accessibility.AccessibleState;
|
|---|
| 50 | import javax.accessibility.AccessibleStateSet;
|
|---|
| 51 |
|
|---|
| 52 | public class JList extends JComponent implements Scrollable
|
|---|
| 53 | {
|
|---|
| 54 | Color select_back, select_fore;
|
|---|
| 55 | ListCellRenderer render;
|
|---|
| 56 | int visibles = 8;
|
|---|
| 57 |
|
|---|
| 58 | ListModel model;
|
|---|
| 59 | ListSelectionModel sel_model;
|
|---|
| 60 |
|
|---|
| 61 | public JList()
|
|---|
| 62 | {
|
|---|
| 63 | init();
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | public JList(Object[] listData)
|
|---|
| 67 | {
|
|---|
| 68 | init();
|
|---|
| 69 | setListData(listData);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 | public JList(Vector listData)
|
|---|
| 74 | {
|
|---|
| 75 | init();
|
|---|
| 76 | setListData(listData);
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 | public JList(ListModel listData)
|
|---|
| 81 | {
|
|---|
| 82 | init();
|
|---|
| 83 | setModel(listData);
|
|---|
| 84 | }
|
|---|
| 85 | void init()
|
|---|
| 86 | {
|
|---|
| 87 | render = new DefaultCellRenderer();
|
|---|
| 88 |
|
|---|
| 89 | sel_model = new DefaultListSelectionModel();
|
|---|
| 90 | setModel(new DefaultListModel());
|
|---|
| 91 |
|
|---|
| 92 | select_back = new Color(0,0,255);
|
|---|
| 93 | select_fore = new Color(255,255,255);
|
|---|
| 94 |
|
|---|
| 95 | updateUI();
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 | public int getVisibleRowCount()
|
|---|
| 100 | { return visibles; }
|
|---|
| 101 | public void setVisibleRowCount(int visibleRowCount)
|
|---|
| 102 | {
|
|---|
| 103 | visibles = visibleRowCount;
|
|---|
| 104 | invalidate();
|
|---|
| 105 | repaint();
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | void addListSelectionListener(ListSelectionListener listener)
|
|---|
| 109 | { sel_model.addListSelectionListener(listener); }
|
|---|
| 110 | void removeListSelectionListener(ListSelectionListener listener)
|
|---|
| 111 | { sel_model.removeListSelectionListener(listener); }
|
|---|
| 112 |
|
|---|
| 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()
|
|---|
| 120 | {
|
|---|
| 121 | int index = getSelectedIndex();
|
|---|
| 122 | if (index == -1)
|
|---|
| 123 | return null;
|
|---|
| 124 | return getModel().getElementAt(index);
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | Color getSelectionBackground()
|
|---|
| 128 | { return select_back; }
|
|---|
| 129 | Color getSelectionForeground()
|
|---|
| 130 | { return select_fore; }
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 | public void setListData(final Object[] listData)
|
|---|
| 134 | {
|
|---|
| 135 | class AL extends AbstractListModel
|
|---|
| 136 | {
|
|---|
| 137 | public int getSize() { return listData.length; }
|
|---|
| 138 | public Object getElementAt(int i) { return listData[i]; }
|
|---|
| 139 | };
|
|---|
| 140 |
|
|---|
| 141 | setModel (new AL());
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | public void setListData(final Vector listData)
|
|---|
| 145 | {
|
|---|
| 146 | // XXX - FIXME Don't also name this AL, workaround for gcj 3.1.
|
|---|
| 147 | class ALData extends AbstractListModel
|
|---|
| 148 | {
|
|---|
| 149 | public int getSize() { return listData.size(); }
|
|---|
| 150 | public Object getElementAt(int i) { return listData.elementAt(i); }
|
|---|
| 151 | };
|
|---|
| 152 |
|
|---|
| 153 | setModel (new ALData());
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 | public ListCellRenderer getCellRenderer()
|
|---|
| 158 | { return render; }
|
|---|
| 159 | public void setCellRenderer(ListCellRenderer cellRenderer)
|
|---|
| 160 | {
|
|---|
| 161 | render = cellRenderer;
|
|---|
| 162 | invalidate();
|
|---|
| 163 | repaint();
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | public void setModel(ListModel model)
|
|---|
| 167 | {
|
|---|
| 168 | ListDataListener l = new ListDataListener()
|
|---|
| 169 | {
|
|---|
| 170 | public void intervalAdded(ListDataEvent e) {
|
|---|
| 171 | repaint();
|
|---|
| 172 | }
|
|---|
| 173 | public void intervalRemoved(ListDataEvent e) {
|
|---|
| 174 | repaint();
|
|---|
| 175 | }
|
|---|
| 176 | public void contentsChanged(ListDataEvent e) {
|
|---|
| 177 | repaint();
|
|---|
| 178 | }
|
|---|
| 179 | };
|
|---|
| 180 |
|
|---|
| 181 | this.model = model;
|
|---|
| 182 | model.addListDataListener(l);
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | public ListModel getModel()
|
|---|
| 186 | { return model; }
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 | public ListUI getUI()
|
|---|
| 190 | { return (ListUI) ui; }
|
|---|
| 191 | public void setUI(ListUI ui)
|
|---|
| 192 | { super.setUI(ui); }
|
|---|
| 193 |
|
|---|
| 194 | public void updateUI()
|
|---|
| 195 | {
|
|---|
| 196 | setUI((ListUI)UIManager.getUI(this));
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | public String getUIClassID()
|
|---|
| 200 | {
|
|---|
| 201 | return "JList";
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 |
|
|---|
| 205 | public AccessibleContext getAccessibleContext()
|
|---|
| 206 | {
|
|---|
| 207 | return null;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | public Dimension getPreferredScrollableViewportSize()
|
|---|
| 211 | {
|
|---|
| 212 | return null;
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | public int getScrollableUnitIncrement(Rectangle visibleRect,
|
|---|
| 216 | int orientation,
|
|---|
| 217 | int direction)
|
|---|
| 218 | {
|
|---|
| 219 | return 1;
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | public int getScrollableBlockIncrement(Rectangle visibleRect,
|
|---|
| 223 | int orientation,
|
|---|
| 224 | int direction)
|
|---|
| 225 | {
|
|---|
| 226 | return 1;
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | public boolean getScrollableTracksViewportWidth()
|
|---|
| 230 | {
|
|---|
| 231 | return false;
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | public boolean getScrollableTracksViewportHeight()
|
|---|
| 235 | {
|
|---|
| 236 | return false;
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | }
|
|---|