ListSelectionModel.java 10.9 KB
Newer Older
1
/* ListSelectionModel.java --
2
   Copyright (C) 2002, 2006, Free Software Foundation, Inc.
Tom Tromey committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath 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 for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */


package javax.swing;

41
import javax.swing.event.ListSelectionEvent;
Tom Tromey committed
42 43 44
import javax.swing.event.ListSelectionListener;

/**
45
 * A model that tracks the selection status of a list of items.  Each item in
46
 * the list is identified by a zero-based index only, so the model can be used
47
 * to track the selection status of any type of list.  The model
48 49
 * supports three modes:
 * <ul>
50
 * <li><code>SINGLE_SELECTION</code> - only one item in the list may be
51
 *     selected;</li>
52
 * <li><code>SINGLE_INTERVAL_SELECTION</code> - only one interval in the list
53
 *     may be selected;</li>
54
 * <li><code>MULTIPLE_INTERVAL_SELECTION</code> - any combination of items in
55 56
 *     the list may be selected.</li>
 * </ul>
57
 * The model uses an event notification mechanism to notify listeners (see
58 59 60 61
 * {@link ListSelectionListener}) about updates to the selection model.
 * <p>
 * This model is used to track row selections in the {@link JList} component,
 * and row and column selections in the {@link JTable} component.
Tom Tromey committed
62 63 64
 */
public interface ListSelectionModel
{
65

66 67
  /**
   * A selection mode in which only one item can be selected.
68
   *
69 70
   * @see #setSelectionMode(int)
   */
Tom Tromey committed
71
  int SINGLE_SELECTION = 0;
72

73 74 75
  /**
   * A selection mode in which a single interval can be selected (an interval
   * is a range containing one or more contiguous items).
76
   *
77 78
   * @see #setSelectionMode(int)
   */
Tom Tromey committed
79
  int SINGLE_INTERVAL_SELECTION = 1;
80

81 82
  /**
   * A selection mode in which any combination of items can be selected.
83
   *
84 85
   * @see #setSelectionMode(int)
   */
Tom Tromey committed
86 87
  int MULTIPLE_INTERVAL_SELECTION = 2;

88 89 90 91 92
  /**
   * Sets the selection mode.
   * <p>
   * FIXME: The spec is silent about what happens to existing selections, for
   * example when changing from an interval selection to single selection.
93 94 95
   *
   * @param mode  one of {@link #SINGLE_SELECTION},
   *     {@link #SINGLE_INTERVAL_SELECTION} and
96
   *     {@link #MULTIPLE_INTERVAL_SELECTION}.
97
   *
98
   * @see #getSelectionMode()
99
   *
100 101 102 103 104 105
   * @throws IllegalArgumentException if <code>mode</code> is not one of the
   *     specified values.
   */
  void setSelectionMode(int mode);

  /**
106 107
   * Returns the selection mode, which is one of {@link #SINGLE_SELECTION},
   * {@link #SINGLE_INTERVAL_SELECTION} and
108
   * {@link #MULTIPLE_INTERVAL_SELECTION}.
109
   *
110
   * @return The selection mode.
111
   *
112 113
   * @see #setSelectionMode(int)
   */
Tom Tromey committed
114
  int getSelectionMode();
115

116
  /**
117 118
   * Clears the current selection from the model.  If the selection state
   * changes (that is, the existing selection is non-empty) a
119 120 121 122 123 124 125
   * {@link ListSelectionEvent} should be sent to all registered listeners.
   * <p>
   * FIXME: what happens to the anchor and lead selection indices (the spec
   * is silent about this)?  See:
   * <p>
   * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4334792
   */
Tom Tromey committed
126
  void clearSelection();
127

128
  /**
129
   * Returns the lowest selected index, or <code>-1</code> if there is no
130
   * selection.
131
   *
132
   * @return The lowest selected index.
133
   *
134 135
   * @see #getMaxSelectionIndex()
   */
Tom Tromey committed
136
  int getMinSelectionIndex();
137

138 139 140
  /**
   * Returns the highest selected index, or <code>-1</code> if there is no
   * selection.
141
   *
142
   * @return The highest selected index.
143
   *
144 145
   * @see #getMinSelectionIndex()
   */
Tom Tromey committed
146 147
  int getMaxSelectionIndex();

148
  /**
149 150 151
   * Returns <code>true</code> if the specified item is selected, and
   * <code>false</code> otherwise.  Special note: if <code>index</code> is
   * negative, this method should return <code>false</code> (no exception
152
   * should be thrown).
153
   *
154
   * @param index  the item index (zero-based).
155 156
   *
   * @return <code>true</code> if the specified item is selected, and
157 158 159 160 161 162 163
   *     <code>false</code> otherwise.
   */
  boolean isSelectedIndex(int index);

  /**
   * Returns <code>true</code> if there is no selection, and <code>false</code>
   * otherwise.
164 165
   *
   * @return  <code>true</code> if there is no selection, and
166 167
   *     <code>false</code> otherwise.
   */
Tom Tromey committed
168
  boolean isSelectionEmpty();
169

170
  /**
171 172 173
   * Sets the selection interval to the specified range (note that
   * <code>anchor</code> can be less than, equal to, or greater than
   * <code>lead</code>).  If this results in the selection being changed,
174 175
   * a {@link ListSelectionEvent} is sent to all registered listeners.
   * <p>
176
   * If the selection mode is {@link #SINGLE_SELECTION}, only the
177
   * <code>lead</code> item is selected.
178
   *
179 180 181 182 183 184
   * @param anchor  the anchor index.
   * @param lead  the lead index.
   */
  void setSelectionInterval(int anchor, int lead);

  /**
185
   * Marks the items in the specified interval as selected.  The behaviour of
186 187
   * this method depends on the selection mode:
   * <ul>
188
   * <li><code>SINGLE_SELECTION</code> - only the <code>lead</code> item is
189
   *     selected;</li>
190
   * <li><code>SINGLE_INTERVAL_SELECTION</code> - the existing selection
191
   *     interval is replaced by the specified interval;</li>
192
   * <li><code>MULTIPLE_INTERVAL_SELECTION</code> - the specified interval is
193 194
   *     merged into the currently selected intervals.</li>
   * </ul>
195
   * Note that <code>anchor</code> can be less than, equal to, or greater than
196
   * <code>lead</code>.
197
   *
198 199 200 201 202 203
   * @param anchor  the index of the anchor item
   * @param lead  the index of the lead item.
   */
  void addSelectionInterval(int anchor, int lead);

  /**
204
   * Marks the items in the specified interval as not selected.  The behaviour
205 206 207 208 209 210
   * of this method depends on the selection mode:
   * <ul>
   * <li><code>SINGLE_SELECTION</code> - XXX;</li>
   * <li><code>SINGLE_INTERVAL_SELECTION</code> - XXX;</li>
   * <li><code>MULTIPLE_INTERVAL_SELECTION</code> - XXX.</li>
   * </ul>
211
   * Note that <code>anchor</code> can be less than, equal to, or greater than
212
   * <code>lead</code>.
213
   *
214 215 216 217 218 219 220 221 222 223
   * @param anchor  the index of the anchor item
   * @param lead  the index of the lead item.
   */
  void removeSelectionInterval(int anchor, int lead);

  /**
   * Inserts a new interval containing <code>length</code> items at the
   * specified <code>index</code> (the <code>before</code> flag indicates
   * whether the range is inserted before or after the existing item at
   * <code>index</code>).
224
   *
225 226
   * FIXME: What is the selection status of the new items? Bug 4870694.
   * FIXME: What event is generated?
227 228
   *
   * @param index  the index of the item.
229
   * @param length  the number of items in the interval to be inserted.
230
   * @param before  if <code>true</code>, the interval should be inserted
231
   *     before <code>index</code>, otherwise it is inserted after.
232
   *
233 234
   * @see #removeIndexInterval(int, int)
   */
235 236
  void insertIndexInterval(int index, int length, boolean before);

237 238
  /**
   * Removes the items in the specified range (inclusive) from the selection
239
   * model.  This method should be called when an interval is deleted from
240
   * the underlying list.
241 242 243
   *
   * FIXME: what happens to the lead and anchor indices if they are part of
   * the range that is removed?
244
   * FIXME: what event is generated
245
   *
246 247
   * @param index0  XXX
   * @param index1  XXX
248
   *
249 250
   * @see #insertIndexInterval(int, int, boolean)
   */
251
  void removeIndexInterval(int index0, int index1);
Tom Tromey committed
252

253
  /**
254 255
   * Returns the index of the anchor item.
   *
256
   * @return The index of the anchor item.
257
   *
258 259
   * @see #setAnchorSelectionIndex(int)
   */
Tom Tromey committed
260
  int getAnchorSelectionIndex();
261

262 263
  /**
   * Sets the index of the anchor item.
264
   *
265
   * @param index  the item index.
266
   *
267 268
   * @see #getAnchorSelectionIndex()
   */
Tom Tromey committed
269
  void setAnchorSelectionIndex(int index);
270

271 272
  /**
   * Returns the index of the lead item.
273
   *
274
   * @return The index of the lead item.
275
   *
276 277
   * @see #setLeadSelectionIndex(int)
   */
Tom Tromey committed
278
  int getLeadSelectionIndex();
279

280 281
  /**
   * Sets the index of the lead item.
282
   *
283
   * @param index  the item index.
284
   *
285 286
   * @see #getLeadSelectionIndex()
   */
Tom Tromey committed
287 288
  void setLeadSelectionIndex(int index);

289 290 291
  /**
   * Sets the flag that is passed to listeners for each change notification.
   * If a sequence of changes is made to the selection model, this flag should
292
   * be set to <code>true</code> at the start of the sequence, and
293 294
   * <code>false</code> for the last change - this gives listeners the option
   * to ignore interim changes if that is more efficient.
295
   *
296
   * @param valueIsAdjusting  the flag value.
297
   *
298 299
   * @see #getValueIsAdjusting()
   */
Tom Tromey committed
300
  void setValueIsAdjusting(boolean valueIsAdjusting);
301

302 303
  /**
   * Returns a flag that is passed to registered listeners when changes are
304
   * made to the model.  See the description for
305
   * {@link #setValueIsAdjusting(boolean)} for more information.
306
   *
307 308
   * @return The flag.
   */
Tom Tromey committed
309 310
  boolean getValueIsAdjusting();

311 312 313
  /**
   * Registers a listener with the model so that it receives notification
   * of changes to the model.
314
   *
315
   * @param listener  the listener (<code>null</code> ignored).
316
   *
317 318
   * @see #removeListSelectionListener(ListSelectionListener)
   */
Tom Tromey committed
319
  void addListSelectionListener(ListSelectionListener listener);
320

321 322 323 324
  /**
   * Deregisters a listener so that it no longer receives notification of
   * changes to the model.  If the specified listener is not registered with
   * the model, or is <code>null</code>, this method does nothing.
325
   *
326
   * @param listener  the listener (<code>null</code> ignored).
327
   *
328 329
   * @see #addListSelectionListener(ListSelectionListener)
   */
330
  void removeListSelectionListener(ListSelectionListener listener);
Tom Tromey committed
331 332

}