1 2 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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
|
/* DefaultListSelectionModel.java --
Copyright (C) 2002, 2004 Free Software Foundation, Inc.
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., 59 Temple Place, Suite 330, Boston, MA
02111-1307 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;
import java.io.Serializable;
import java.util.BitSet;
import java.util.EventListener;
import javax.swing.event.EventListenerList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
/**
* <p>This class provides a default implementation of {@link
* ListSelectioModel}, which is used by {@link javax.swing.JList} and
* similar classes to manage the selection status of a number of data
* elements. </p>
*
* <p>The class is organized <em>abstractly</em> as a set of intervals of
* integers. Each interval indicates an inclusive range of indices in a
* list -- held by some other object and unknown to this class -- which is
* considered "selected". There are various accessors for querying and
* modifying the set of intervals, with simplified forms accepting a single
* index, representing an interval with only one element. </p>
*/
public class DefaultListSelectionModel implements Cloneable,
ListSelectionModel,
Serializable
{
private static final long serialVersionUID = -5718799865110415860L;
/** The list of ListSelectionListeners subscribed to this selection model. */
protected EventListenerList listenerList = new EventListenerList();
/**
* The current list selection mode. Must be one of the numeric constants
* <code>SINGLE_SELECTION</code>, <code>SINGLE_INTERVAL_SELECTION</code>
* or <code>MULTIPLE_INTERVAL_SELECTION</code> from {@link
* ListSelectionModel}. The default value is
* <code>MULTIPLE_INTERVAL_SELECTION</code>.
*/
int selectionMode = MULTIPLE_INTERVAL_SELECTION;
/**
* The index of the "lead" of the most recent selection. The lead is the
* second argument in any call to {@link #setSelectionInterval}, {@link
* #addSelectionInterval} or {@link #removeSelectionInterval}. Generally
* the lead refers to the most recent position a user dragged their mouse
* over.
*/
int leadSelectionIndex = -1;
/**
* The index of the "anchor" of the most recent selection. The anchor is
* the first argument in any call to {@link #setSelectionInterval},
* {@link #addSelectionInterval} or {@link
* #removeSelectionInterval}. Generally the anchor refers to the first
* recent position a user clicks when they begin to drag their mouse over
* a list.
*
* @see #getAnchorSelectionIndex
* @see #setAnchorSelectionIndex
*/
int anchorSelectionIndex = -1;
/**
* controls the range of indices provided in any {@link
* ListSelectionEvent} fired by the selectionModel. Let
* <code>[A,L]</code> be the range of indices between {@link
* anchorSelectionIndex} and {@link leadSelectionIndex} inclusive, and
* let <code>[i0,i1]</code> be the range of indices changed in a given
* call which generates a {@link ListSelectionEvent}. Then when this
* property is <code>true</code>, the {@link ListSelectionEvent} contains
* the range <code>[A,L] union [i0,i1]</code>; when <code>false</code> it
* will contain only <code>[i0,i1]</code>. The default is
* <code>true</code>.
*
* @see #isLeadAnchorNotificationEnabled
* @see #setLeadAnchorNotificationEnabled
*/
protected boolean leadAnchorNotificationEnabled = true;
/**
* Whether the selection is currently "adjusting". Any {@link
* ListSelectionEvent} events constructed in response to changes in this
* list selection model will have their {@link
* ListSelectionEvent#isAdjusting} field set to this value.
*
* @see #getValueIsAdjusting
* @see #setValueIsAdjusting
*/
boolean valueIsAdjusting = false;
/**
* The current set of "intervals", represented simply by a {@link
* java.util.BitSet}. A set bit indicates a selected index, whereas a
* cleared bit indicates a non-selected index.
*/
BitSet sel = new BitSet();
/**
* Gets the value of the {@link #selectionMode} property.
*
* @return The current value of the property
*/
public int getSelectionMode()
{
return selectionMode;
}
/**
* Sets the value of the {@link #selectionMode} property.
*
* @param a The new value of the property
*/
public void setSelectionMode(int a)
{
selectionMode = a;
}
/**
* Gets the value of the {@link #anchorSelectionIndex} property.
*
* @return The current property value
*
* @see #setAnchorSelectionIndex
*/
public int getAnchorSelectionIndex()
{
return anchorSelectionIndex;
}
/**
* Sets the value of the {@link #anchorSelectionIndex} property.
*
* @param anchorIndex The new property value
*
* @see #getAnchorSelectionIndex
*/
public void setAnchorSelectionIndex(int anchorIndex)
{
anchorSelectionIndex = anchorIndex;
}
/**
* Gets the value of the {@link #leadSelectionIndex} property.
*
* @return The current property value
*
* @see #setLeadSelectionIndex
*/
public int getLeadSelectionIndex()
{
return leadSelectionIndex;
}
/**
* <p>Sets the value of the {@link #anchorSelectionIndex} property. As a
* side effect, alters the selection status of two ranges of indices. Let
* <code>OL</code> be the old lead selection index, <code>NL</code> be
* the new lead selection index, and <code>A</code> be the anchor
* selection index. Then if <code>A</code> is a valid selection index,
* one of two things happens depending on the seleciton status of
* <code>A</code>:</p>
*
* <ul>
*
* <li><code>isSelectedIndex(A) == true</code>: set <code>[A,OL]</code>
* to <em>deselected</em>, then set <code>[A,NL]</code> to
* <em>selected</em>.</li>
*
* <li><code>isSelectedIndex(A) == false</code>: set <code>[A,OL]</code>
* to <em>selected</em>, then set <code>[A,NL]</code> to
* <em>deselected</em>.</li>
*
* </ul>
*
* <p>This method generates at most a single {@link ListSelectionEvent}
* despite changing multiple ranges. The range of values provided to the
* {@link ListSelectionEvent} includes only the minimum range of values
* which changed selection status between the beginning and end of the
* method.</p>
*
* @param anchorIndex The new property value
*
* @see #getAnchorSelectionIndex
*/
public void setLeadSelectionIndex(int leadIndex)
{
int oldLeadIndex = leadSelectionIndex;
leadSelectionIndex = leadIndex;
if (anchorSelectionIndex == -1)
return;
int R1 = Math.min(anchorSelectionIndex, oldLeadIndex);
int R2 = Math.max(anchorSelectionIndex, oldLeadIndex);
int S1 = Math.min(anchorSelectionIndex, leadIndex);
int S2 = Math.max(anchorSelectionIndex, leadIndex);
int lo = Math.min(R1, S1);
int hi = Math.max(R2, S2);
BitSet oldRange = sel.get(lo, hi+1);
if (isSelectedIndex(anchorSelectionIndex))
{
sel.clear(R1, R2+1);
sel.set(S1, S2+1);
}
else
{
sel.set(R1, R2+1);
sel.clear(S1, S2+1);
}
BitSet newRange = sel.get(lo, hi+1);
newRange.xor(oldRange);
int beg = sel.nextSetBit(0), end = -1;
for(int i=beg; i >= 0; i=sel.nextSetBit(i+1))
{
end = i;
}
fireValueChanged(beg, end, valueIsAdjusting);
}
/**
* Gets the value of the {@link #leadAnchorNotificationEnabled} property.
*
* @return The current property value
*
* @see #setLeadAnchorNotificationEnabled
*/
public boolean isLeadAnchorNotificationEnabled()
{
return leadAnchorNotificationEnabled;
}
/**
* Sets the value of the {@link #leadAnchorNotificationEnabled} property.
*
* @param flag The new property value
*
* @see #getLeadAnchorNotificationEnabled
*/
public void setLeadAnchorNotificationEnabled(boolean l)
{
leadAnchorNotificationEnabled = l;
}
/**
* Gets the value of the {@link #valueIsAdjusting} property.
*
* @return The current property value
*
* @see #setValueIsAdjusting
*/
public boolean getValueIsAdjusting()
{
return valueIsAdjusting;
}
/**
* Sets the value of the {@link #valueIsAdjusting} property.
*
* @param v The new property value
*
* @see #getValueIsAdjusting
*/
public void setValueIsAdjusting(boolean v)
{
valueIsAdjusting = v;
}
/**
* Determines whether the selection is empty.
*
* @return <code>true</code> if the selection is empty, otherwise
* <code>false</code>
*/
public boolean isSelectionEmpty()
{
return sel.isEmpty();
}
/**
* Gets the smallest index which is currently a member of a selection
* interval.
*
* @return The least integer <code>i</code> such that <code>i >=
* 0</code> and <code>i</code> is a member of a selected interval, or
* <code>-1</code> if there are no selected intervals
*
* @see #getMaxSelectionIndex
*/
public int getMinSelectionIndex()
{
if (isSelectionEmpty())
return -1;
return sel.nextSetBit(0);
}
/**
* Gets the largest index which is currently a member of a selection
* interval.
*
* @return The greatest integer <code>i</code> such that <code>i >=
* 0</code> and <code>i</code> is a member of a selected interval, or
* <code>-1</code> if there are no selected intervals
*
* @see #getMinSelectionIndex
*/
public int getMaxSelectionIndex()
{
if (isSelectionEmpty())
return -1;
int mx = -1;
for(int i=sel.nextSetBit(0); i >= 0; i=sel.nextSetBit(i+1))
{
mx = i;
}
return mx;
}
/**
* Determines whether a particular index is a member of a selection
* interval.
*
* @param a The index to search for
*
* @return <code>true</code> if the index is a member of a selection interval,
* otherwise <code>false</code>
*/
public boolean isSelectedIndex(int a)
{
return sel.get(a);
}
/**
* If the {@link #selectionMode} property is equal to
* <code>SINGLE_SELECTION</code> or
* <code>SINGLE_INTERVAL_SELECTION</code>, equivalent to calling
* <code>setSelectionInterval(index1, index2)</code>; otherwise adds the
* range <code>[index0, index1]</code> to the selection interval set.
*
* @param index0 The beginning of the range of indices to select
* @param index1 The end of the range of indices to select
*
* @see #setSelectionInterval
* @see #removeSelectionInterval
*/
public void addSelectionInterval(int index0, int index1)
{
if (selectionMode == SINGLE_SELECTION
|| selectionMode == SINGLE_INTERVAL_SELECTION)
sel.clear();
if (selectionMode == SINGLE_SELECTION)
index0 = index1;
int lo = Math.min(index0, index1);
int hi = Math.max(index0, index1);
sel.set(lo, hi+1);
fireValueChanged(lo, hi, valueIsAdjusting);
}
/**
* Deselects all indices in the inclusive range
* <code>[index0,index1]</code>.
*
* @param index0 The beginning of the range of indices to deselect
* @param index1 The end of the range of indices to deselect
*
* @see #addSelectionInterval
* @see #setSelectionInterval
*/
public void removeSelectionInterval(int index0,
int index1)
{
int lo = Math.min(index0, index1);
int hi = Math.max(index0, index1);
sel.clear(lo, hi+1);
fireValueChanged(lo, hi, valueIsAdjusting);
}
/**
* Removes all intervals in the selection set.
*/
public void clearSelection()
{
int sz = sel.size();
sel.clear();
fireValueChanged(0, sz, valueIsAdjusting);
}
/**
* Clears the current selection and marks a given interval as
* "selected". If the current selection mode is
* <code>SINGLE_SELECTION</code> only the index <code>index2</code> is
* selected.
*
* @param index0 The low end of the new selection
* @param index1 The high end of the new selection
*/
public void setSelectionInterval(int index0, int index1)
{
sel.clear();
if (selectionMode == SINGLE_SELECTION)
index0 = index1;
int lo = Math.min(index0, index1);
int hi = Math.max(index0, index1);
sel.set(lo, hi+1);
fireValueChanged(lo, hi, valueIsAdjusting);
}
/**
* Inserts a number of indices either before or after a particular
* position in the set of indices. Renumbers all indices after the
* inserted range. The new indices in the inserted range are not
* selected. This method is typically called to synchronize the selection
* model with an inserted range of elements in a {@link ListModel}.
*
* @param index The position to insert indices at
* @param length The number of indices to insert
* @param before Indicates whether to insert the indices before the index
* or after it
*/
public void insertIndexInterval(int index,
int length,
boolean before)
{
if (!before)
{
index++;
length--;
}
BitSet tmp = sel.get(index, sel.size());
sel.clear(index, sel.size());
int n = tmp.size();
for (int i = 0; i < n; ++i)
sel.set(index + length + i, tmp.get(i));
}
/**
* Removes a range from the set of indices. Renumbers all indices after
* the removed range. This method is typically called to synchronize the
* selection model with a deleted range of elements in a {@link
* ListModel}.
*
* @param index0 The first index to remove (inclusive)
* @param index1 The last index to remove (inclusive)
*/
public void removeIndexInterval(int index0,
int index1)
{
int lo = Math.min(index0, index1);
int hi = Math.max(index0, index1);
BitSet tmp = sel.get(hi, sel.size());
sel.clear(lo, sel.size());
int n = tmp.size();
for (int i = 0; i < n; ++i)
sel.set(lo + i, tmp.get(i));
}
/**
* Fires a {@link ListSelectionEvent} to all the listeners of type {@link
* ListSelectionListener} registered with this selection model.
*
* @param firstIndex The low index of the changed range
* @param lastIndex The high index of the changed range
*/
protected void fireValueChanged(int firstIndex, int lastIndex)
{
fireValueChanged(firstIndex, lastIndex, getValueIsAdjusting());
}
/**
* Fires a {@link ListSelectionEvent} to all the listeners of type {@link
* ListSelectionListener} registered with this selection model.
*
* @param firstIndex The low index of the changed range
* @param lastIndex The high index of the changed range
* @param isAdjusting Whether this change is part of a seqence of adjustments
* made to the selection, such as during interactive scrolling
*/
protected void fireValueChanged(int firstIndex, int lastIndex,
boolean isAdjusting)
{
ListSelectionEvent evt = new ListSelectionEvent(this, firstIndex,
lastIndex, isAdjusting);
ListSelectionListener[] listeners = getListSelectionListeners();
for (int i = 0; i < listeners.length; ++i)
listeners[i].valueChanged(evt);
}
/**
* Adds a listener.
*
* @param listener The listener to add
*
* @see removeListSelectionListener
* @see getListSelectionListeners
*/
public void addListSelectionListener(ListSelectionListener listener)
{
listenerList.add(ListSelectionListener.class, listener);
}
/**
* Removes a registered listener.
*
* @param listener The listener to remove
*
* @see addListSelectionListener
* @see getListSelectionListeners
*/
public void removeListSelectionListener(ListSelectionListener listener)
{
listenerList.remove(ListSelectionListener.class, listener);
}
/**
* Returns an array of all registerers listeners.
*
* @param listenerType The type of listener to retrieve
*
* @return The array
*
* @see getListSelectionListener
* @since 1.3
*/
public EventListener[] getListeners(Class listenerType)
{
return listenerList.getListeners(listenerType);
}
/**
* Returns an array of all registerd list selection listeners.
*
* @return the array
*
* @see addListSelectionListener
* @see removeListSelectionListener
* @see getListeners
* @since 1.4
*/
public ListSelectionListener[] getListSelectionListeners()
{
return (ListSelectionListener[]) getListeners(ListSelectionListener.class);
}
/**
* Returns a clone of this object.
* <code>listenerList</code> don't gets duplicated.
*
* @return the cloned object
*
* @throws CloneNotSupportedException if an error occurs
*/
public Object clone()
throws CloneNotSupportedException
{
DefaultListSelectionModel model =
(DefaultListSelectionModel) super.clone();
model.sel = (BitSet) sel.clone();
return model;
}
}
|