Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/share/classes/javax/swing/DefaultListSelectionModel.java
41153 views
1
/*
2
* Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package javax.swing;
27
28
import java.util.EventListener;
29
import java.util.BitSet;
30
import java.io.Serializable;
31
import java.beans.Transient;
32
33
import javax.swing.event.*;
34
35
36
/**
37
* Default data model for list selections.
38
* <p>
39
* <strong>Warning:</strong>
40
* Serialized objects of this class will not be compatible with
41
* future Swing releases. The current serialization support is
42
* appropriate for short term storage or RMI between applications running
43
* the same version of Swing. As of 1.4, support for long term storage
44
* of all JavaBeans
45
* has been added to the <code>java.beans</code> package.
46
* Please see {@link java.beans.XMLEncoder}.
47
*
48
* @author Philip Milne
49
* @author Hans Muller
50
* @see ListSelectionModel
51
* @since 1.2
52
*/
53
@SuppressWarnings("serial") // Same-version serialization only
54
public class DefaultListSelectionModel implements ListSelectionModel, Cloneable, Serializable
55
{
56
private static final int MIN = -1;
57
private static final int MAX = Integer.MAX_VALUE;
58
private int selectionMode = MULTIPLE_INTERVAL_SELECTION;
59
private int minIndex = MAX;
60
private int maxIndex = MIN;
61
private int anchorIndex = -1;
62
private int leadIndex = -1;
63
private int firstAdjustedIndex = MAX;
64
private int lastAdjustedIndex = MIN;
65
private boolean isAdjusting = false;
66
67
private int firstChangedIndex = MAX;
68
private int lastChangedIndex = MIN;
69
70
private BitSet value = new BitSet(32);
71
/**
72
* The list of listeners.
73
*/
74
protected EventListenerList listenerList = new EventListenerList();
75
76
/**
77
* Whether or not the lead anchor notification is enabled.
78
*/
79
protected boolean leadAnchorNotificationEnabled = true;
80
81
/**
82
* Constructs a {@code DefaultListSelectionModel}.
83
*/
84
public DefaultListSelectionModel() {}
85
86
/** {@inheritDoc} */
87
public int getMinSelectionIndex() { return isSelectionEmpty() ? -1 : minIndex; }
88
89
/** {@inheritDoc} */
90
public int getMaxSelectionIndex() { return maxIndex; }
91
92
/** {@inheritDoc} */
93
public boolean getValueIsAdjusting() { return isAdjusting; }
94
95
/** {@inheritDoc} */
96
public int getSelectionMode() { return selectionMode; }
97
98
/**
99
* {@inheritDoc}
100
* @throws IllegalArgumentException {@inheritDoc}
101
*/
102
public void setSelectionMode(int selectionMode) {
103
int oldMode = this.selectionMode;
104
switch (selectionMode) {
105
case SINGLE_SELECTION:
106
case SINGLE_INTERVAL_SELECTION:
107
case MULTIPLE_INTERVAL_SELECTION:
108
this.selectionMode = selectionMode;
109
break;
110
default:
111
throw new IllegalArgumentException("invalid selectionMode");
112
}
113
114
/*
115
This code will only be executed when selection needs to be updated on
116
changing selection mode. It will happen only if selection mode is changed
117
from MULTIPLE_INTERVAL to SINGLE_INTERVAL or SINGLE or from
118
SINGLE_INTERVAL to SINGLE
119
*/
120
if (oldMode > this.selectionMode) {
121
if (this.selectionMode == SINGLE_SELECTION) {
122
if (!isSelectionEmpty()) {
123
setSelectionInterval(minIndex, minIndex);
124
}
125
} else if (this.selectionMode == SINGLE_INTERVAL_SELECTION) {
126
if(!isSelectionEmpty()) {
127
int selectionEndindex = minIndex;
128
while (value.get(selectionEndindex + 1)) {
129
selectionEndindex++;
130
}
131
setSelectionInterval(minIndex, selectionEndindex);
132
}
133
}
134
}
135
}
136
137
/** {@inheritDoc} */
138
public boolean isSelectedIndex(int index) {
139
return ((index < minIndex) || (index > maxIndex)) ? false : value.get(index);
140
}
141
142
/** {@inheritDoc} */
143
public boolean isSelectionEmpty() {
144
return (minIndex > maxIndex);
145
}
146
147
/** {@inheritDoc} */
148
public void addListSelectionListener(ListSelectionListener l) {
149
listenerList.add(ListSelectionListener.class, l);
150
}
151
152
/** {@inheritDoc} */
153
public void removeListSelectionListener(ListSelectionListener l) {
154
listenerList.remove(ListSelectionListener.class, l);
155
}
156
157
/**
158
* Returns an array of all the list selection listeners
159
* registered on this <code>DefaultListSelectionModel</code>.
160
*
161
* @return all of this model's <code>ListSelectionListener</code>s
162
* or an empty
163
* array if no list selection listeners are currently registered
164
*
165
* @see #addListSelectionListener
166
* @see #removeListSelectionListener
167
*
168
* @since 1.4
169
*/
170
public ListSelectionListener[] getListSelectionListeners() {
171
return listenerList.getListeners(ListSelectionListener.class);
172
}
173
174
/**
175
* Notifies listeners that we have ended a series of adjustments.
176
* @param isAdjusting true if this is the final change in a series of
177
* adjustments
178
*/
179
protected void fireValueChanged(boolean isAdjusting) {
180
if (lastChangedIndex == MIN) {
181
return;
182
}
183
/* Change the values before sending the event to the
184
* listeners in case the event causes a listener to make
185
* another change to the selection.
186
*/
187
int oldFirstChangedIndex = firstChangedIndex;
188
int oldLastChangedIndex = lastChangedIndex;
189
firstChangedIndex = MAX;
190
lastChangedIndex = MIN;
191
fireValueChanged(oldFirstChangedIndex, oldLastChangedIndex, isAdjusting);
192
}
193
194
195
/**
196
* Notifies <code>ListSelectionListeners</code> that the value
197
* of the selection, in the closed interval <code>firstIndex</code>,
198
* <code>lastIndex</code>, has changed.
199
*
200
* @param firstIndex the first index in the interval
201
* @param lastIndex the last index in the interval
202
*/
203
protected void fireValueChanged(int firstIndex, int lastIndex) {
204
fireValueChanged(firstIndex, lastIndex, getValueIsAdjusting());
205
}
206
207
/**
208
* @param firstIndex the first index in the interval
209
* @param lastIndex the last index in the interval
210
* @param isAdjusting true if this is the final change in a series of
211
* adjustments
212
* @see EventListenerList
213
*/
214
protected void fireValueChanged(int firstIndex, int lastIndex, boolean isAdjusting)
215
{
216
Object[] listeners = listenerList.getListenerList();
217
ListSelectionEvent e = null;
218
219
for (int i = listeners.length - 2; i >= 0; i -= 2) {
220
if (listeners[i] == ListSelectionListener.class) {
221
if (e == null) {
222
e = new ListSelectionEvent(this, firstIndex, lastIndex, isAdjusting);
223
}
224
((ListSelectionListener)listeners[i+1]).valueChanged(e);
225
}
226
}
227
}
228
229
private void fireValueChanged() {
230
if (lastAdjustedIndex == MIN) {
231
return;
232
}
233
/* If getValueAdjusting() is true, (eg. during a drag opereration)
234
* record the bounds of the changes so that, when the drag finishes (and
235
* setValueAdjusting(false) is called) we can post a single event
236
* with bounds covering all of these individual adjustments.
237
*/
238
if (getValueIsAdjusting()) {
239
firstChangedIndex = Math.min(firstChangedIndex, firstAdjustedIndex);
240
lastChangedIndex = Math.max(lastChangedIndex, lastAdjustedIndex);
241
}
242
/* Change the values before sending the event to the
243
* listeners in case the event causes a listener to make
244
* another change to the selection.
245
*/
246
int oldFirstAdjustedIndex = firstAdjustedIndex;
247
int oldLastAdjustedIndex = lastAdjustedIndex;
248
firstAdjustedIndex = MAX;
249
lastAdjustedIndex = MIN;
250
251
fireValueChanged(oldFirstAdjustedIndex, oldLastAdjustedIndex);
252
}
253
254
/**
255
* Returns an array of all the objects currently registered as
256
* <code><em>Foo</em>Listener</code>s
257
* upon this model.
258
* <code><em>Foo</em>Listener</code>s
259
* are registered using the <code>add<em>Foo</em>Listener</code> method.
260
* <p>
261
* You can specify the <code>listenerType</code> argument
262
* with a class literal, such as <code><em>Foo</em>Listener.class</code>.
263
* For example, you can query a <code>DefaultListSelectionModel</code>
264
* instance <code>m</code>
265
* for its list selection listeners
266
* with the following code:
267
*
268
* <pre>ListSelectionListener[] lsls = (ListSelectionListener[])(m.getListeners(ListSelectionListener.class));</pre>
269
*
270
* If no such listeners exist,
271
* this method returns an empty array.
272
*
273
* @param <T> the type of {@code EventListener} class being requested
274
* @param listenerType the type of listeners requested;
275
* this parameter should specify an interface
276
* that descends from <code>java.util.EventListener</code>
277
* @return an array of all objects registered as
278
* <code><em>Foo</em>Listener</code>s
279
* on this model,
280
* or an empty array if no such
281
* listeners have been added
282
* @exception ClassCastException if <code>listenerType</code> doesn't
283
* specify a class or interface that implements
284
* <code>java.util.EventListener</code>
285
*
286
* @see #getListSelectionListeners
287
*
288
* @since 1.3
289
*/
290
public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
291
return listenerList.getListeners(listenerType);
292
}
293
294
// Updates first and last change indices
295
private void markAsDirty(int r) {
296
if (r == -1) {
297
return;
298
}
299
300
firstAdjustedIndex = Math.min(firstAdjustedIndex, r);
301
lastAdjustedIndex = Math.max(lastAdjustedIndex, r);
302
}
303
304
// Sets the state at this index and update all relevant state.
305
private void set(int r) {
306
if (value.get(r)) {
307
return;
308
}
309
value.set(r);
310
markAsDirty(r);
311
312
// Update minimum and maximum indices
313
minIndex = Math.min(minIndex, r);
314
maxIndex = Math.max(maxIndex, r);
315
}
316
317
// Clears the state at this index and update all relevant state.
318
private void clear(int r) {
319
if (!value.get(r)) {
320
return;
321
}
322
value.clear(r);
323
markAsDirty(r);
324
325
// Update minimum and maximum indices
326
/*
327
If (r > minIndex) the minimum has not changed.
328
The case (r < minIndex) is not possible because r'th value was set.
329
We only need to check for the case when lowest entry has been cleared,
330
and in this case we need to search for the first value set above it.
331
*/
332
if (r == minIndex) {
333
for(minIndex = minIndex + 1; minIndex <= maxIndex; minIndex++) {
334
if (value.get(minIndex)) {
335
break;
336
}
337
}
338
}
339
/*
340
If (r < maxIndex) the maximum has not changed.
341
The case (r > maxIndex) is not possible because r'th value was set.
342
We only need to check for the case when highest entry has been cleared,
343
and in this case we need to search for the first value set below it.
344
*/
345
if (r == maxIndex) {
346
for(maxIndex = maxIndex - 1; minIndex <= maxIndex; maxIndex--) {
347
if (value.get(maxIndex)) {
348
break;
349
}
350
}
351
}
352
/* Performance note: This method is called from inside a loop in
353
changeSelection() but we will only iterate in the loops
354
above on the basis of one iteration per deselected cell - in total.
355
Ie. the next time this method is called the work of the previous
356
deselection will not be repeated.
357
358
We also don't need to worry about the case when the min and max
359
values are in their unassigned states. This cannot happen because
360
this method's initial check ensures that the selection was not empty
361
and therefore that the minIndex and maxIndex had 'real' values.
362
363
If we have cleared the whole selection, set the minIndex and maxIndex
364
to their cannonical values so that the next set command always works
365
just by using Math.min and Math.max.
366
*/
367
if (isSelectionEmpty()) {
368
minIndex = MAX;
369
maxIndex = MIN;
370
}
371
}
372
373
/**
374
* Sets the value of the leadAnchorNotificationEnabled flag.
375
*
376
* @param flag boolean value for {@code leadAnchorNotificationEnabled}
377
* @see #isLeadAnchorNotificationEnabled()
378
*/
379
public void setLeadAnchorNotificationEnabled(boolean flag) {
380
leadAnchorNotificationEnabled = flag;
381
}
382
383
/**
384
* Returns the value of the <code>leadAnchorNotificationEnabled</code> flag.
385
* When <code>leadAnchorNotificationEnabled</code> is true the model
386
* generates notification events with bounds that cover all the changes to
387
* the selection plus the changes to the lead and anchor indices.
388
* Setting the flag to false causes a narrowing of the event's bounds to
389
* include only the elements that have been selected or deselected since
390
* the last change. Either way, the model continues to maintain the lead
391
* and anchor variables internally. The default is true.
392
* <p>
393
* Note: It is possible for the lead or anchor to be changed without a
394
* change to the selection. Notification of these changes is often
395
* important, such as when the new lead or anchor needs to be updated in
396
* the view. Therefore, caution is urged when changing the default value.
397
*
398
* @return the value of the <code>leadAnchorNotificationEnabled</code> flag
399
* @see #setLeadAnchorNotificationEnabled(boolean)
400
*/
401
public boolean isLeadAnchorNotificationEnabled() {
402
return leadAnchorNotificationEnabled;
403
}
404
405
private void updateLeadAnchorIndices(int anchorIndex, int leadIndex) {
406
if (leadAnchorNotificationEnabled) {
407
if (this.anchorIndex != anchorIndex) {
408
markAsDirty(this.anchorIndex);
409
markAsDirty(anchorIndex);
410
}
411
412
if (this.leadIndex != leadIndex) {
413
markAsDirty(this.leadIndex);
414
markAsDirty(leadIndex);
415
}
416
}
417
this.anchorIndex = anchorIndex;
418
this.leadIndex = leadIndex;
419
}
420
421
private boolean contains(int a, int b, int i) {
422
return (i >= a) && (i <= b);
423
}
424
425
private void changeSelection(int clearMin, int clearMax,
426
int setMin, int setMax, boolean clearFirst) {
427
for(int i = Math.min(setMin, clearMin); i <= Math.max(setMax, clearMax); i++) {
428
429
boolean shouldClear = contains(clearMin, clearMax, i);
430
boolean shouldSet = contains(setMin, setMax, i);
431
432
if (shouldSet && shouldClear) {
433
if (clearFirst) {
434
shouldClear = false;
435
}
436
else {
437
shouldSet = false;
438
}
439
}
440
441
if (shouldSet) {
442
set(i);
443
}
444
if (shouldClear) {
445
clear(i);
446
}
447
}
448
fireValueChanged();
449
}
450
451
/**
452
* Change the selection with the effect of first clearing the values
453
* in the inclusive range [clearMin, clearMax] then setting the values
454
* in the inclusive range [setMin, setMax]. Do this in one pass so
455
* that no values are cleared if they would later be set.
456
*/
457
private void changeSelection(int clearMin, int clearMax, int setMin, int setMax) {
458
changeSelection(clearMin, clearMax, setMin, setMax, true);
459
}
460
461
/** {@inheritDoc} */
462
public void clearSelection() {
463
removeSelectionIntervalImpl(minIndex, maxIndex, false);
464
}
465
466
/**
467
* Changes the selection to be between {@code index0} and {@code index1}
468
* inclusive. {@code index0} doesn't have to be less than or equal to
469
* {@code index1}.
470
* <p>
471
* In {@code SINGLE_SELECTION} selection mode, only the second index
472
* is used.
473
* <p>
474
* If this represents a change to the current selection, then each
475
* {@code ListSelectionListener} is notified of the change.
476
* <p>
477
* If either index is {@code -1}, this method does nothing and returns
478
* without exception. Otherwise, if either index is less than {@code -1},
479
* an {@code IndexOutOfBoundsException} is thrown.
480
*
481
* @param index0 one end of the interval.
482
* @param index1 other end of the interval
483
* @throws IndexOutOfBoundsException if either index is less than {@code -1}
484
* (and neither index is {@code -1})
485
* @see #addListSelectionListener
486
*/
487
public void setSelectionInterval(int index0, int index1) {
488
if (index0 == -1 || index1 == -1) {
489
return;
490
}
491
492
if (getSelectionMode() == SINGLE_SELECTION) {
493
index0 = index1;
494
}
495
496
updateLeadAnchorIndices(index0, index1);
497
498
int clearMin = minIndex;
499
int clearMax = maxIndex;
500
int setMin = Math.min(index0, index1);
501
int setMax = Math.max(index0, index1);
502
changeSelection(clearMin, clearMax, setMin, setMax);
503
}
504
505
/**
506
* Changes the selection to be the set union of the current selection
507
* and the indices between {@code index0} and {@code index1} inclusive.
508
* <p>
509
* In {@code SINGLE_SELECTION} selection mode, this is equivalent
510
* to calling {@code setSelectionInterval}, and only the second index
511
* is used. In {@code SINGLE_INTERVAL_SELECTION} selection mode, this
512
* method behaves like {@code setSelectionInterval}, unless the given
513
* interval is immediately adjacent to or overlaps the existing selection,
514
* and can therefore be used to grow it.
515
* <p>
516
* If this represents a change to the current selection, then each
517
* {@code ListSelectionListener} is notified of the change. Note that
518
* {@code index0} doesn't have to be less than or equal to {@code index1}.
519
* <p>
520
* If either index is {@code -1}, this method does nothing and returns
521
* without exception. Otherwise, if either index is less than {@code -1},
522
* an {@code IndexOutOfBoundsException} is thrown.
523
*
524
* @param index0 one end of the interval.
525
* @param index1 other end of the interval
526
* @throws IndexOutOfBoundsException if either index is less than {@code -1}
527
* (and neither index is {@code -1})
528
* @see #addListSelectionListener
529
* @see #setSelectionInterval
530
*/
531
public void addSelectionInterval(int index0, int index1)
532
{
533
if (index0 == -1 || index1 == -1) {
534
return;
535
}
536
537
// If we only allow a single selection, channel through
538
// setSelectionInterval() to enforce the rule.
539
if (getSelectionMode() == SINGLE_SELECTION) {
540
setSelectionInterval(index0, index1);
541
return;
542
}
543
544
updateLeadAnchorIndices(index0, index1);
545
546
int clearMin = MAX;
547
int clearMax = MIN;
548
int setMin = Math.min(index0, index1);
549
int setMax = Math.max(index0, index1);
550
551
// If we only allow a single interval and this would result
552
// in multiple intervals, then set the selection to be just
553
// the new range.
554
if (getSelectionMode() == SINGLE_INTERVAL_SELECTION &&
555
(setMax < minIndex - 1 || setMin > maxIndex + 1)) {
556
557
setSelectionInterval(index0, index1);
558
return;
559
}
560
561
changeSelection(clearMin, clearMax, setMin, setMax);
562
}
563
564
565
/**
566
* Changes the selection to be the set difference of the current selection
567
* and the indices between {@code index0} and {@code index1} inclusive.
568
* {@code index0} doesn't have to be less than or equal to {@code index1}.
569
* <p>
570
* In {@code SINGLE_INTERVAL_SELECTION} selection mode, if the removal
571
* would produce two disjoint selections, the removal is extended through
572
* the greater end of the selection. For example, if the selection is
573
* {@code 0-10} and you supply indices {@code 5,6} (in any order) the
574
* resulting selection is {@code 0-4}.
575
* <p>
576
* If this represents a change to the current selection, then each
577
* {@code ListSelectionListener} is notified of the change.
578
* <p>
579
* If either index is {@code -1}, this method does nothing and returns
580
* without exception. Otherwise, if either index is less than {@code -1},
581
* an {@code IndexOutOfBoundsException} is thrown.
582
*
583
* @param index0 one end of the interval
584
* @param index1 other end of the interval
585
* @throws IndexOutOfBoundsException if either index is less than {@code -1}
586
* (and neither index is {@code -1})
587
* @see #addListSelectionListener
588
*/
589
public void removeSelectionInterval(int index0, int index1)
590
{
591
removeSelectionIntervalImpl(index0, index1, true);
592
}
593
594
// private implementation allowing the selection interval
595
// to be removed without affecting the lead and anchor
596
private void removeSelectionIntervalImpl(int index0, int index1,
597
boolean changeLeadAnchor) {
598
599
if (index0 == -1 || index1 == -1) {
600
return;
601
}
602
603
if (changeLeadAnchor) {
604
updateLeadAnchorIndices(index0, index1);
605
}
606
607
int clearMin = Math.min(index0, index1);
608
int clearMax = Math.max(index0, index1);
609
int setMin = MAX;
610
int setMax = MIN;
611
612
// If the removal would produce to two disjoint selections in a mode
613
// that only allows one, extend the removal to the end of the selection.
614
if (getSelectionMode() != MULTIPLE_INTERVAL_SELECTION &&
615
clearMin > minIndex && clearMax < maxIndex) {
616
clearMax = maxIndex;
617
}
618
619
changeSelection(clearMin, clearMax, setMin, setMax);
620
}
621
622
private void setState(int index, boolean state) {
623
if (state) {
624
set(index);
625
}
626
else {
627
clear(index);
628
}
629
}
630
631
/**
632
* Insert length indices beginning before/after index. If the value
633
* at index is itself selected and the selection mode is not
634
* SINGLE_SELECTION, set all of the newly inserted items as selected.
635
* Otherwise leave them unselected. This method is typically
636
* called to sync the selection model with a corresponding change
637
* in the data model.
638
*/
639
public void insertIndexInterval(int index, int length, boolean before)
640
{
641
/* The first new index will appear at insMinIndex and the last
642
* one will appear at insMaxIndex
643
*/
644
int insMinIndex = (before) ? index : index + 1;
645
int insMaxIndex = (insMinIndex + length) - 1;
646
647
/* Right shift the entire bitset by length, beginning with
648
* index-1 if before is true, index+1 if it's false (i.e. with
649
* insMinIndex).
650
*/
651
for(int i = maxIndex; i >= insMinIndex; i--) {
652
setState(i + length, value.get(i));
653
}
654
655
/* Initialize the newly inserted indices.
656
*/
657
boolean setInsertedValues = ((getSelectionMode() == SINGLE_SELECTION) ?
658
false : value.get(index));
659
for(int i = insMinIndex; i <= insMaxIndex; i++) {
660
setState(i, setInsertedValues);
661
}
662
663
int leadIndex = this.leadIndex;
664
if (leadIndex > index || (before && leadIndex == index)) {
665
leadIndex = this.leadIndex + length;
666
}
667
int anchorIndex = this.anchorIndex;
668
if (anchorIndex > index || (before && anchorIndex == index)) {
669
anchorIndex = this.anchorIndex + length;
670
}
671
if (leadIndex != this.leadIndex || anchorIndex != this.anchorIndex) {
672
updateLeadAnchorIndices(anchorIndex, leadIndex);
673
}
674
675
fireValueChanged();
676
}
677
678
679
/**
680
* Remove the indices in the interval index0,index1 (inclusive) from
681
* the selection model. This is typically called to sync the selection
682
* model width a corresponding change in the data model. Note
683
* that (as always) index0 need not be &lt;= index1.
684
*/
685
public void removeIndexInterval(int index0, int index1)
686
{
687
int rmMinIndex = Math.min(index0, index1);
688
int rmMaxIndex = Math.max(index0, index1);
689
int gapLength = (rmMaxIndex - rmMinIndex) + 1;
690
691
/* Shift the entire bitset to the left to close the index0, index1
692
* gap.
693
*/
694
for(int i = rmMinIndex; i <= maxIndex; i++) {
695
setState(i, value.get(i + gapLength));
696
}
697
698
int leadIndex = this.leadIndex;
699
if (leadIndex == 0 && rmMinIndex == 0) {
700
// do nothing
701
} else if (leadIndex > rmMaxIndex) {
702
leadIndex = this.leadIndex - gapLength;
703
} else if (leadIndex >= rmMinIndex) {
704
leadIndex = rmMinIndex - 1;
705
}
706
707
int anchorIndex = this.anchorIndex;
708
if (anchorIndex == 0 && rmMinIndex == 0) {
709
// do nothing
710
} else if (anchorIndex > rmMaxIndex) {
711
anchorIndex = this.anchorIndex - gapLength;
712
} else if (anchorIndex >= rmMinIndex) {
713
anchorIndex = rmMinIndex - 1;
714
}
715
716
if (leadIndex != this.leadIndex || anchorIndex != this.anchorIndex) {
717
updateLeadAnchorIndices(anchorIndex, leadIndex);
718
}
719
720
fireValueChanged();
721
}
722
723
724
/** {@inheritDoc} */
725
public void setValueIsAdjusting(boolean isAdjusting) {
726
if (isAdjusting != this.isAdjusting) {
727
this.isAdjusting = isAdjusting;
728
this.fireValueChanged(isAdjusting);
729
}
730
}
731
732
733
/**
734
* Returns a string that displays and identifies this
735
* object's properties.
736
*
737
* @return a <code>String</code> representation of this object
738
*/
739
public String toString() {
740
String s = ((getValueIsAdjusting()) ? "~" : "=") + value.toString();
741
return getClass().getName() + " " + Integer.toString(hashCode()) + " " + s;
742
}
743
744
/**
745
* Returns a clone of this selection model with the same selection.
746
* <code>listenerLists</code> are not duplicated.
747
*
748
* @exception CloneNotSupportedException if the selection model does not
749
* both (a) implement the Cloneable interface and (b) define a
750
* <code>clone</code> method.
751
*/
752
public Object clone() throws CloneNotSupportedException {
753
DefaultListSelectionModel clone = (DefaultListSelectionModel)super.clone();
754
clone.value = (BitSet)value.clone();
755
clone.listenerList = new EventListenerList();
756
return clone;
757
}
758
759
/** {@inheritDoc} */
760
@Transient
761
public int getAnchorSelectionIndex() {
762
return anchorIndex;
763
}
764
765
/** {@inheritDoc} */
766
@Transient
767
public int getLeadSelectionIndex() {
768
return leadIndex;
769
}
770
771
/**
772
* Set the anchor selection index, leaving all selection values unchanged.
773
* If leadAnchorNotificationEnabled is true, send a notification covering
774
* the old and new anchor cells.
775
*
776
* @see #getAnchorSelectionIndex
777
* @see #setLeadSelectionIndex
778
*/
779
public void setAnchorSelectionIndex(int anchorIndex) {
780
updateLeadAnchorIndices(anchorIndex, this.leadIndex);
781
fireValueChanged();
782
}
783
784
/**
785
* Set the lead selection index, leaving all selection values unchanged.
786
* If leadAnchorNotificationEnabled is true, send a notification covering
787
* the old and new lead cells.
788
*
789
* @param leadIndex the new lead selection index
790
*
791
* @see #setAnchorSelectionIndex
792
* @see #setLeadSelectionIndex
793
* @see #getLeadSelectionIndex
794
*
795
* @since 1.5
796
*/
797
public void moveLeadSelectionIndex(int leadIndex) {
798
// disallow a -1 lead unless the anchor is already -1
799
if (leadIndex == -1) {
800
if (this.anchorIndex != -1) {
801
return;
802
}
803
804
/* PENDING(shannonh) - The following check is nice, to be consistent with
805
setLeadSelectionIndex. However, it is not absolutely
806
necessary: One could work around it by setting the anchor
807
to something valid, modifying the lead, and then moving
808
the anchor back to -1. For this reason, there's no sense
809
in adding it at this time, as that would require
810
updating the spec and officially committing to it.
811
812
// otherwise, don't do anything if the anchor is -1
813
} else if (this.anchorIndex == -1) {
814
return;
815
*/
816
817
}
818
819
updateLeadAnchorIndices(this.anchorIndex, leadIndex);
820
fireValueChanged();
821
}
822
823
/**
824
* Sets the lead selection index, ensuring that values between the
825
* anchor and the new lead are either all selected or all deselected.
826
* If the value at the anchor index is selected, first clear all the
827
* values in the range [anchor, oldLeadIndex], then select all the values
828
* values in the range [anchor, newLeadIndex], where oldLeadIndex is the old
829
* leadIndex and newLeadIndex is the new one.
830
* <p>
831
* If the value at the anchor index is not selected, do the same thing in
832
* reverse selecting values in the old range and deselecting values in the
833
* new one.
834
* <p>
835
* Generate a single event for this change and notify all listeners.
836
* For the purposes of generating minimal bounds in this event, do the
837
* operation in a single pass; that way the first and last index inside the
838
* ListSelectionEvent that is broadcast will refer to cells that actually
839
* changed value because of this method. If, instead, this operation were
840
* done in two steps the effect on the selection state would be the same
841
* but two events would be generated and the bounds around the changed
842
* values would be wider, including cells that had been first cleared only
843
* to later be set.
844
* <p>
845
* This method can be used in the <code>mouseDragged</code> method
846
* of a UI class to extend a selection.
847
*
848
* @see #getLeadSelectionIndex
849
* @see #setAnchorSelectionIndex
850
*/
851
public void setLeadSelectionIndex(int leadIndex) {
852
int anchorIndex = this.anchorIndex;
853
854
// only allow a -1 lead if the anchor is already -1
855
if (leadIndex == -1) {
856
if (anchorIndex == -1) {
857
updateLeadAnchorIndices(anchorIndex, leadIndex);
858
fireValueChanged();
859
}
860
861
return;
862
// otherwise, don't do anything if the anchor is -1
863
} else if (anchorIndex == -1) {
864
return;
865
}
866
867
if (this.leadIndex == -1) {
868
this.leadIndex = leadIndex;
869
}
870
871
boolean shouldSelect = value.get(this.anchorIndex);
872
873
if (getSelectionMode() == SINGLE_SELECTION) {
874
anchorIndex = leadIndex;
875
shouldSelect = true;
876
}
877
878
int oldMin = Math.min(this.anchorIndex, this.leadIndex);
879
int oldMax = Math.max(this.anchorIndex, this.leadIndex);
880
int newMin = Math.min(anchorIndex, leadIndex);
881
int newMax = Math.max(anchorIndex, leadIndex);
882
883
updateLeadAnchorIndices(anchorIndex, leadIndex);
884
885
if (shouldSelect) {
886
changeSelection(oldMin, oldMax, newMin, newMax);
887
}
888
else {
889
changeSelection(newMin, newMax, oldMin, oldMax, false);
890
}
891
}
892
}
893
894