API Overview API Index Package Overview Direct link to this page
JDK 1.6
  javax.swing. DefaultListSelectionModel View Javadoc
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830

/*
 * @(#)DefaultListSelectionModel.java	1.78 06/07/19
 *
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package javax.swing;

import java.util.EventListener;
import java.util.BitSet;
import java.io.Serializable;

import javax.swing.event.*;


/**
 * Default data model for list selections. 
 * <p>
 * <strong>Warning:</strong>
 * Serialized objects of this class will not be compatible with
 * future Swing releases. The current serialization support is
 * appropriate for short term storage or RMI between applications running
 * the same version of Swing.  As of 1.4, support for long term storage
 * of all JavaBeans<sup><font size="-2">TM</font></sup>
 * has been added to the <code>java.beans</code> package.
 * Please see {@link java.beans.XMLEncoder}.
 *
 * @version 1.78 07/19/06
 * @author Philip Milne
 * @author Hans Muller
 * @see ListSelectionModel
 */

public class DefaultListSelectionModel implements ListSelectionModel, Cloneable, Serializable
{
    private static final int MIN = -1;
    private static final int MAX = Integer.MAX_VALUE;
    private int selectionMode = MULTIPLE_INTERVAL_SELECTION;
    private int minIndex = MAX;
    private int maxIndex = MIN;
    private int anchorIndex = -1;
    private int leadIndex = -1;
    private int firstAdjustedIndex = MAX;
    private int lastAdjustedIndex = MIN;
    private boolean isAdjusting = false; 

    private int firstChangedIndex = MAX; 
    private int lastChangedIndex = MIN; 

    private BitSet value = new BitSet(32);
    protected EventListenerList listenerList = new EventListenerList();

    protected boolean leadAnchorNotificationEnabled = true;

    /** {@inheritDoc} */
    public int getMinSelectionIndex() { return isSelectionEmpty() ? -1 : minIndex; }

    /** {@inheritDoc} */
    public int getMaxSelectionIndex() { return maxIndex; }

    /** {@inheritDoc} */
    public boolean getValueIsAdjusting() { return isAdjusting; }

    /** {@inheritDoc} */
    public int getSelectionMode() { return selectionMode; }

    /**
     * {@inheritDoc}
     * @throws IllegalArgumentException {@inheritDoc}
     */
    public void setSelectionMode(int selectionMode) {
	switch (selectionMode) {
	case SINGLE_SELECTION:
	case SINGLE_INTERVAL_SELECTION:
	case MULTIPLE_INTERVAL_SELECTION:
	    this.selectionMode = selectionMode;
	    break;
	default:
	    throw new IllegalArgumentException("invalid selectionMode");
	}
    }

    /** {@inheritDoc} */
    public boolean isSelectedIndex(int index) {
	return ((index < minIndex) || (index > maxIndex)) ? false : value.get(index);
    }

    /** {@inheritDoc} */
    public boolean isSelectionEmpty() {
	return (minIndex > maxIndex);
    }

    /** {@inheritDoc} */
    public void addListSelectionListener(ListSelectionListener l) {
 	listenerList.add(ListSelectionListener.class, l);
    }

    /** {@inheritDoc} */
    public void removeListSelectionListener(ListSelectionListener l) {
 	listenerList.remove(ListSelectionListener.class, l);
    }

    /**
     * Returns an array of all the list selection listeners 
     * registered on this <code>DefaultListSelectionModel</code>.
     *
     * @return all of this model's <code>ListSelectionListener</code>s 
     *         or an empty
     *         array if no list selection listeners are currently registered
     *
     * @see #addListSelectionListener
     * @see #removeListSelectionListener
     *
     * @since 1.4
     */
    public ListSelectionListener[] getListSelectionListeners() {
        return (ListSelectionListener[])listenerList.getListeners(
                ListSelectionListener.class);
    }

    /**
     * Notifies listeners that we have ended a series of adjustments. 
     */
    protected void fireValueChanged(boolean isAdjusting) {  
        if (lastChangedIndex == MIN) {
	    return; 
	}
	/* Change the values before sending the event to the
	 * listeners in case the event causes a listener to make
	 * another change to the selection.
	 */
	int oldFirstChangedIndex = firstChangedIndex;
	int oldLastChangedIndex = lastChangedIndex;
	firstChangedIndex = MAX;
	lastChangedIndex = MIN; 
	fireValueChanged(oldFirstChangedIndex, oldLastChangedIndex, isAdjusting); 
    }


    /**
     * Notifies <code>ListSelectionListeners</code> that the value
     * of the selection, in the closed interval <code>firstIndex</code>,
     * <code>lastIndex</code>, has changed.
     */
    protected void fireValueChanged(int firstIndex, int lastIndex) {
	fireValueChanged(firstIndex, lastIndex, getValueIsAdjusting());
    }

    /**
     * @param firstIndex the first index in the interval
     * @param lastIndex the last index in the interval
     * @param isAdjusting true if this is the final change in a series of
     *		adjustments
     * @see EventListenerList
     */
    protected void fireValueChanged(int firstIndex, int lastIndex, boolean isAdjusting)
    {
	Object[] listeners = listenerList.getListenerList();
	ListSelectionEvent e = null;

	for (int i = listeners.length - 2; i >= 0; i -= 2) {
	    if (listeners[i] == ListSelectionListener.class) {
		if (e == null) {
		    e = new ListSelectionEvent(this, firstIndex, lastIndex, isAdjusting);
		}
		((ListSelectionListener)listeners[i+1]).valueChanged(e);
	    }
	}
    }

    private void fireValueChanged() {
	if (lastAdjustedIndex == MIN) { 
	    return;
	}
	/* If getValueAdjusting() is true, (eg. during a drag opereration) 
	 * record the bounds of the changes so that, when the drag finishes (and 
	 * setValueAdjusting(false) is called) we can post a single event 
	 * with bounds covering all of these individual adjustments.  
	 */ 
	if (getValueIsAdjusting()) { 
            firstChangedIndex = Math.min(firstChangedIndex, firstAdjustedIndex);
	    lastChangedIndex = Math.max(lastChangedIndex, lastAdjustedIndex);
        }
	/* Change the values before sending the event to the
	 * listeners in case the event causes a listener to make
	 * another change to the selection.
	 */
	int oldFirstAdjustedIndex = firstAdjustedIndex;
	int oldLastAdjustedIndex = lastAdjustedIndex;
	firstAdjustedIndex = MAX;
	lastAdjustedIndex = MIN; 

	fireValueChanged(oldFirstAdjustedIndex, oldLastAdjustedIndex);
    }

    /**
     * Returns an array of all the objects currently registered as
     * <code><em>Foo</em>Listener</code>s
     * upon this model.
     * <code><em>Foo</em>Listener</code>s
     * are registered using the <code>add<em>Foo</em>Listener</code> method.
     * <p>
     * You can specify the <code>listenerType</code> argument
     * with a class literal, such as <code><em>Foo</em>Listener.class</code>.
     * For example, you can query a <code>DefaultListSelectionModel</code>
     * instance <code>m</code>
     * for its list selection listeners
     * with the following code:
     *
     * <pre>ListSelectionListener[] lsls = (ListSelectionListener[])(m.getListeners(ListSelectionListener.class));</pre>
     *
     * If no such listeners exist,
     * this method returns an empty array.
     *
     * @param listenerType  the type of listeners requested;
     *          this parameter should specify an interface
     *          that descends from <code>java.util.EventListener</code>
     * @return an array of all objects registered as
     *          <code><em>Foo</em>Listener</code>s
     *          on this model,
     *          or an empty array if no such
     *          listeners have been added
     * @exception ClassCastException if <code>listenerType</code> doesn't
     *          specify a class or interface that implements
     *          <code>java.util.EventListener</code>
     *
     * @see #getListSelectionListeners
     *
     * @since 1.3
     */
    public <T extends EventListener> T[] getListeners(Class<T> listenerType) { 
	return listenerList.getListeners(listenerType); 
    }

    // Updates first and last change indices
    private void markAsDirty(int r) {
        firstAdjustedIndex = Math.min(firstAdjustedIndex, r);
	lastAdjustedIndex =  Math.max(lastAdjustedIndex, r);
    }

    // Sets the state at this index and update all relevant state.
    private void set(int r) {
	if (value.get(r)) {
	    return;
	}
	value.set(r);
        markAsDirty(r);

        // Update minimum and maximum indices
        minIndex = Math.min(minIndex, r);
        maxIndex = Math.max(maxIndex, r);
    }

    // Clears the state at this index and update all relevant state.
    private void clear(int r) {
	if (!value.get(r)) {
	    return;
	}
	value.clear(r);
        markAsDirty(r);

        // Update minimum and maximum indices
        /*
           If (r > minIndex) the minimum has not changed.
           The case (r < minIndex) is not possible because r'th value was set.
           We only need to check for the case when lowest entry has been cleared,
           and in this case we need to search for the first value set above it.
	*/
	if (r == minIndex) {
	    for(minIndex = minIndex + 1; minIndex <= maxIndex; minIndex++) {
	        if (value.get(minIndex)) {
	            break;
	        }
	    }
	}
        /*
           If (r < maxIndex) the maximum has not changed.
           The case (r > maxIndex) is not possible because r'th value was set.
           We only need to check for the case when highest entry has been cleared,
           and in this case we need to search for the first value set below it.
	*/
	if (r == maxIndex) {
	    for(maxIndex = maxIndex - 1; minIndex <= maxIndex; maxIndex--) {
	        if (value.get(maxIndex)) {
	            break;
	        }
	    }
	}
	/* Performance note: This method is called from inside a loop in
	   changeSelection() but we will only iterate in the loops
	   above on the basis of one iteration per deselected cell - in total.
	   Ie. the next time this method is called the work of the previous
	   deselection will not be repeated.

	   We also don't need to worry about the case when the min and max
	   values are in their unassigned states. This cannot happen because
	   this method's initial check ensures that the selection was not empty
	   and therefore that the minIndex and maxIndex had 'real' values.

	   If we have cleared the whole selection, set the minIndex and maxIndex
	   to their cannonical values so that the next set command always works
	   just by using Math.min and Math.max.
	*/
        if (isSelectionEmpty()) {
            minIndex = MAX;
            maxIndex = MIN;
        }
    }

    /**
     * Sets the value of the leadAnchorNotificationEnabled flag.
     * @see		#isLeadAnchorNotificationEnabled()
     */
    public void setLeadAnchorNotificationEnabled(boolean flag) {
        leadAnchorNotificationEnabled = flag;
    }

    /**
     * Returns the value of the <code>leadAnchorNotificationEnabled</code> flag.
     * When <code>leadAnchorNotificationEnabled</code> is true the model
     * generates notification events with bounds that cover all the changes to
     * the selection plus the changes to the lead and anchor indices.
     * Setting the flag to false causes a narrowing of the event's bounds to
     * include only the elements that have been selected or deselected since
     * the last change. Either way, the model continues to maintain the lead
     * and anchor variables internally. The default is true.
     * <p>
     * Note: It is possible for the lead or anchor to be changed without a
     * change to the selection. Notification of these changes is often
     * important, such as when the new lead or anchor needs to be updated in
     * the view. Therefore, caution is urged when changing the default value.
     *
     * @return 	the value of the <code>leadAnchorNotificationEnabled</code> flag
     * @see		#setLeadAnchorNotificationEnabled(boolean)
     */
    public boolean isLeadAnchorNotificationEnabled() {
        return leadAnchorNotificationEnabled;
    }

    private void updateLeadAnchorIndices(int anchorIndex, int leadIndex) {
        if (leadAnchorNotificationEnabled) {
            if (this.anchorIndex != anchorIndex) {
                if (this.anchorIndex != -1) { // The unassigned state.
                    markAsDirty(this.anchorIndex);
                }
                markAsDirty(anchorIndex);
            }

            if (this.leadIndex != leadIndex) {
                if (this.leadIndex != -1) { // The unassigned state.
                    markAsDirty(this.leadIndex);
                }
                markAsDirty(leadIndex);
            }
        }
        this.anchorIndex = anchorIndex;
        this.leadIndex = leadIndex;
    }

    private boolean contains(int a, int b, int i) {
        return (i >= a) && (i <= b);
    }

    private void changeSelection(int clearMin, int clearMax,
                                 int setMin, int setMax, boolean clearFirst) {
        for(int i = Math.min(setMin, clearMin); i <= Math.max(setMax, clearMax); i++) {

            boolean shouldClear = contains(clearMin, clearMax, i);
            boolean shouldSet = contains(setMin, setMax, i);

            if (shouldSet && shouldClear) {
                if (clearFirst) {
                    shouldClear = false;
                }
                else {
                    shouldSet = false;
                }
            }

            if (shouldSet) {
                set(i);
            }
            if (shouldClear) {
                clear(i);
            }
        }
        fireValueChanged();
    }

   /**  
    * Change the selection with the effect of first clearing the values
    * in the inclusive range [clearMin, clearMax] then setting the values
    * in the inclusive range [setMin, setMax]. Do this in one pass so
    * that no values are cleared if they would later be set.
    */
    private void changeSelection(int clearMin, int clearMax, int setMin, int setMax) {
        changeSelection(clearMin, clearMax, setMin, setMax, true);
    }

    /** {@inheritDoc} */
    public void clearSelection() {
	removeSelectionIntervalImpl(minIndex, maxIndex, false);
    }

    /** 
     * Changes the selection to be between {@code index0} and {@code index1}
     * inclusive. {@code index0} doesn't have to be less than or equal to
     * {@code index1}.
     * <p>
     * In {@code SINGLE_SELECTION} selection mode, only the second index
     * is used.
     * <p>
     * If this represents a change to the current selection, then each
     * {@code ListSelectionListener} is notified of the change.
     * <p>
     * If either index is {@code -1}, this method does nothing and returns
     * without exception. Otherwise, if either index is less than {@code -1},
     * an {@code IndexOutOfBoundsException} is thrown.
     * 
     * @param index0 one end of the interval.
     * @param index1 other end of the interval
     * @throws IndexOutOfBoundsException if either index is less than {@code -1}
     *         (and neither index is {@code -1})
     * @see #addListSelectionListener
     */
    public void setSelectionInterval(int index0, int index1) {
        if (index0 == -1 || index1 == -1) {
            return;
        }

        if (getSelectionMode() == SINGLE_SELECTION) {
            index0 = index1;
        }

        updateLeadAnchorIndices(index0, index1);

        int clearMin = minIndex;
        int clearMax = maxIndex;
	int setMin = Math.min(index0, index1);
	int setMax = Math.max(index0, index1);
        changeSelection(clearMin, clearMax, setMin, setMax);
    }

    /** 
     * Changes the selection to be the set union of the current selection
     * and the indices between {@code index0} and {@code index1} inclusive.
     * <p>
     * In {@code SINGLE_SELECTION} selection mode, this is equivalent
     * to calling {@code setSelectionInterval}, and only the second index
     * is used. In {@code SINGLE_INTERVAL_SELECTION} selection mode, this
     * method behaves like {@code setSelectionInterval}, unless the given
     * interval is immediately adjacent to or overlaps the existing selection,
     * and can therefore be used to grow it.
     * <p>
     * If this represents a change to the current selection, then each
     * {@code ListSelectionListener} is notified of the change. Note that
     * {@code index0} doesn't have to be less than or equal to {@code index1}.
     * <p>
     * If either index is {@code -1}, this method does nothing and returns
     * without exception. Otherwise, if either index is less than {@code -1},
     * an {@code IndexOutOfBoundsException} is thrown.
     * 
     * @param index0 one end of the interval.
     * @param index1 other end of the interval
     * @throws IndexOutOfBoundsException if either index is less than {@code -1}
     *         (and neither index is {@code -1})
     * @see #addListSelectionListener
     * @see #setSelectionInterval
     */
    public void addSelectionInterval(int index0, int index1)
    {
        if (index0 == -1 || index1 == -1) {
            return;
        }

        // If we only allow a single selection, channel through
        // setSelectionInterval() to enforce the rule.
        if (getSelectionMode() == SINGLE_SELECTION) {
            setSelectionInterval(index0, index1);
            return;
        }

        updateLeadAnchorIndices(index0, index1);

        int clearMin = MAX;
        int clearMax = MIN;
	int setMin = Math.min(index0, index1);
	int setMax = Math.max(index0, index1);

        // If we only allow a single interval and this would result
        // in multiple intervals, then set the selection to be just
        // the new range.
        if (getSelectionMode() == SINGLE_INTERVAL_SELECTION &&
                (setMax < minIndex - 1 || setMin > maxIndex + 1)) {

            setSelectionInterval(index0, index1);
            return;
        }

        changeSelection(clearMin, clearMax, setMin, setMax);
    }


    /** 
     * Changes the selection to be the set difference of the current selection
     * and the indices between {@code index0} and {@code index1} inclusive.
     * {@code index0} doesn't have to be less than or equal to {@code index1}.
     * <p>
     * In {@code SINGLE_INTERVAL_SELECTION} selection mode, if the removal
     * would produce two disjoint selections, the removal is extended through
     * the greater end of the selection. For example, if the selection is
     * {@code 0-10} and you supply indices {@code 5,6} (in any order) the
     * resulting selection is {@code 0-4}.
     * <p>
     * If this represents a change to the current selection, then each
     * {@code ListSelectionListener} is notified of the change.
     * <p>
     * If either index is {@code -1}, this method does nothing and returns
     * without exception. Otherwise, if either index is less than {@code -1},
     * an {@code IndexOutOfBoundsException} is thrown.
     * 
     * @param index0 one end of the interval
     * @param index1 other end of the interval
     * @throws IndexOutOfBoundsException if either index is less than {@code -1}
     *         (and neither index is {@code -1})
     * @see #addListSelectionListener
     */
    public void removeSelectionInterval(int index0, int index1)
    {
        removeSelectionIntervalImpl(index0, index1, true);
    }

    // private implementation allowing the selection interval
    // to be removed without affecting the lead and anchor
    private void removeSelectionIntervalImpl(int index0, int index1,
                                             boolean changeLeadAnchor) {

        if (index0 == -1 || index1 == -1) {
            return;
        }

        if (changeLeadAnchor) {
            updateLeadAnchorIndices(index0, index1);
        }

	int clearMin = Math.min(index0, index1);
	int clearMax = Math.max(index0, index1); 
	int setMin = MAX;
	int setMax = MIN;

        // If the removal would produce to two disjoint selections in a mode 
	// that only allows one, extend the removal to the end of the selection. 
	if (getSelectionMode() != MULTIPLE_INTERVAL_SELECTION && 
	       clearMin > minIndex && clearMax < maxIndex) {
	    clearMax = maxIndex; 
        }

        changeSelection(clearMin, clearMax, setMin, setMax);
    }

    private void setState(int index, boolean state) {
        if (state) {
	    set(index); 
	}
	else {
	    clear(index); 
	}
    }

    /**
     * Insert length indices beginning before/after index. If the value 
     * at index is itself selected and the selection mode is not
     * SINGLE_SELECTION, set all of the newly inserted items as selected.
     * Otherwise leave them unselected. This method is typically
     * called to sync the selection model with a corresponding change
     * in the data model.
     */
    public void insertIndexInterval(int index, int length, boolean before)
    {
	/* The first new index will appear at insMinIndex and the last
	 * one will appear at insMaxIndex
	 */
	int insMinIndex = (before) ? index : index + 1;
	int insMaxIndex = (insMinIndex + length) - 1;

	/* Right shift the entire bitset by length, beginning with
	 * index-1 if before is true, index+1 if it's false (i.e. with
	 * insMinIndex).
	 */
	for(int i = maxIndex; i >= insMinIndex; i--) {
	    setState(i + length, value.get(i)); 
	}

	/* Initialize the newly inserted indices.
	 */
       	boolean setInsertedValues = ((getSelectionMode() == SINGLE_SELECTION) ?
                                        false : value.get(index)); 
	for(int i = insMinIndex; i <= insMaxIndex; i++) { 
	    setState(i, setInsertedValues); 
	}

	int leadIndex = this.leadIndex;
	if (leadIndex > index || (before && leadIndex == index)) {
	    leadIndex = this.leadIndex + length;
	}
	int anchorIndex = this.anchorIndex;
	if (anchorIndex > index || (before && anchorIndex == index)) {
	    anchorIndex = this.anchorIndex + length;
	}
	if (leadIndex != this.leadIndex || anchorIndex != this.anchorIndex) {
	    updateLeadAnchorIndices(anchorIndex, leadIndex);
	}

        fireValueChanged();
    }


    /**
     * Remove the indices in the interval index0,index1 (inclusive) from
     * the selection model.  This is typically called to sync the selection
     * model width a corresponding change in the data model.  Note
     * that (as always) index0 need not be <= index1.
     */
    public void removeIndexInterval(int index0, int index1)
    {
	int rmMinIndex = Math.min(index0, index1);
	int rmMaxIndex = Math.max(index0, index1);
	int gapLength = (rmMaxIndex - rmMinIndex) + 1;

	/* Shift the entire bitset to the left to close the index0, index1
	 * gap.
	 */
	for(int i = rmMinIndex; i <= maxIndex; i++) {
	    setState(i, value.get(i + gapLength)); 
	}

	int leadIndex = this.leadIndex;
        if (leadIndex == 0 && rmMinIndex == 0) {
            // do nothing
        } else if (leadIndex > rmMaxIndex) {
	    leadIndex = this.leadIndex - gapLength;
	} else if (leadIndex >= rmMinIndex) {
            leadIndex = rmMinIndex - 1;
        }

	int anchorIndex = this.anchorIndex;
        if (anchorIndex == 0 && rmMinIndex == 0) {
            // do nothing
        } else if (anchorIndex > rmMaxIndex) {
            anchorIndex = this.anchorIndex - gapLength;
        } else if (anchorIndex >= rmMinIndex) {
            anchorIndex = rmMinIndex - 1;
        }

	if (leadIndex != this.leadIndex || anchorIndex != this.anchorIndex) {
	    updateLeadAnchorIndices(anchorIndex, leadIndex);
	}

        fireValueChanged();
    }


    /** {@inheritDoc} */
    public void setValueIsAdjusting(boolean isAdjusting) {
	if (isAdjusting != this.isAdjusting) {
	    this.isAdjusting = isAdjusting;
	    this.fireValueChanged(isAdjusting);
	}
    }


    /**
     * Returns a string that displays and identifies this
     * object's properties.
     *
     * @return a <code>String</code> representation of this object
     */
    public String toString() {
	String s =  ((getValueIsAdjusting()) ? "~" : "=") + value.toString();
	return getClass().getName() + " " + Integer.toString(hashCode()) + " " + s;
    }

    /**
     * Returns a clone of this selection model with the same selection.
     * <code>listenerLists</code> are not duplicated.
     *
     * @exception CloneNotSupportedException if the selection model does not
     *    both (a) implement the Cloneable interface and (b) define a
     *    <code>clone</code> method.
     */
    public Object clone() throws CloneNotSupportedException {
	DefaultListSelectionModel clone = (DefaultListSelectionModel)super.clone();
	clone.value = (BitSet)value.clone();
	clone.listenerList = new EventListenerList();
	return clone;
    }

    /** {@inheritDoc} */
    public int getAnchorSelectionIndex() {
        return anchorIndex;
    }

    /** {@inheritDoc} */
    public int getLeadSelectionIndex() {
        return leadIndex;
    }

    /**
     * Set the anchor selection index, leaving all selection values unchanged. 
     * If leadAnchorNotificationEnabled is true, send a notification covering 
     * the old and new anchor cells. 
     *
     * @see #getAnchorSelectionIndex     
     * @see #setLeadSelectionIndex
     */   
    public void setAnchorSelectionIndex(int anchorIndex) { 
	updateLeadAnchorIndices(anchorIndex, this.leadIndex); 
	fireValueChanged(); 
    }

    /**
     * Set the lead selection index, leaving all selection values unchanged.
     * If leadAnchorNotificationEnabled is true, send a notification covering
     * the old and new lead cells.
     *
     * @param leadIndex the new lead selection index
     *
     * @see #setAnchorSelectionIndex
     * @see #setLeadSelectionIndex
     * @see #getLeadSelectionIndex
     *
     * @since 1.5
     */
    public void moveLeadSelectionIndex(int leadIndex) {
        // disallow a -1 lead unless the anchor is already -1
        if (leadIndex == -1) {
            if (this.anchorIndex != -1) {
                return;
            }

/* PENDING(shannonh) - The following check is nice, to be consistent with
                       setLeadSelectionIndex. However, it is not absolutely
                       necessary: One could work around it by setting the anchor
                       to something valid, modifying the lead, and then moving
                       the anchor back to -1. For this reason, there's no sense
                       in adding it at this time, as that would require
                       updating the spec and officially committing to it.

        // otherwise, don't do anything if the anchor is -1
        } else if (this.anchorIndex == -1) {
            return;
*/

        }

        updateLeadAnchorIndices(this.anchorIndex, leadIndex);
        fireValueChanged();
    }

    /**
     * Sets the lead selection index, ensuring that values between the 
     * anchor and the new lead are either all selected or all deselected. 
     * If the value at the anchor index is selected, first clear all the 
     * values in the range [anchor, oldLeadIndex], then select all the values 
     * values in the range [anchor, newLeadIndex], where oldLeadIndex is the old
     * leadIndex and newLeadIndex is the new one. 
     * <p> 
     * If the value at the anchor index is not selected, do the same thing in 
     * reverse selecting values in the old range and deslecting values in the
     * new one. 
     * <p>
     * Generate a single event for this change and notify all listeners. 
     * For the purposes of generating minimal bounds in this event, do the 
     * operation in a single pass; that way the first and last index inside the 
     * ListSelectionEvent that is broadcast will refer to cells that actually 
     * changed value because of this method. If, instead, this operation were 
     * done in two steps the effect on the selection state would be the same 
     * but two events would be generated and the bounds around the changed 
     * values would be wider, including cells that had been first cleared only 
     * to later be set. 
     * <p>
     * This method can be used in the <code>mouseDragged</code> method
     * of a UI class to extend a selection.  
     *
     * @see #getLeadSelectionIndex     
     * @see #setAnchorSelectionIndex
     */   
    public void setLeadSelectionIndex(int leadIndex) { 
        int anchorIndex = this.anchorIndex;

        // only allow a -1 lead if the anchor is already -1
        if (leadIndex == -1) {
            if (anchorIndex == -1) {
                updateLeadAnchorIndices(anchorIndex, leadIndex);
                fireValueChanged();
            }

            return;
        // otherwise, don't do anything if the anchor is -1
        } else if (anchorIndex == -1) {
            return;
        }

	if (this.leadIndex == -1) { 
	    this.leadIndex = leadIndex; 
	}

	boolean shouldSelect = value.get(this.anchorIndex); 

        if (getSelectionMode() == SINGLE_SELECTION) { 
            anchorIndex = leadIndex;
            shouldSelect = true; 
        }

        int oldMin = Math.min(this.anchorIndex, this.leadIndex);
        int oldMax = Math.max(this.anchorIndex, this.leadIndex);
	int newMin = Math.min(anchorIndex, leadIndex);
	int newMax = Math.max(anchorIndex, leadIndex);

	updateLeadAnchorIndices(anchorIndex, leadIndex); 

        if (shouldSelect) {
            changeSelection(oldMin, oldMax, newMin, newMax);
        }
        else {
            changeSelection(newMin, newMax, oldMin, oldMax, false);
        }
    }
}

Generated By: JavaOnTracks Doclet 0.1.4     ©Thibaut Colar