Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/swing/DefaultListSelectionModel.java
38829 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.swing;2627import java.util.EventListener;28import java.util.BitSet;29import java.io.Serializable;30import java.beans.Transient;3132import javax.swing.event.*;333435/**36* Default data model for list selections.37* <p>38* <strong>Warning:</strong>39* Serialized objects of this class will not be compatible with40* future Swing releases. The current serialization support is41* appropriate for short term storage or RMI between applications running42* the same version of Swing. As of 1.4, support for long term storage43* of all JavaBeans™44* has been added to the <code>java.beans</code> package.45* Please see {@link java.beans.XMLEncoder}.46*47* @author Philip Milne48* @author Hans Muller49* @see ListSelectionModel50*/5152public class DefaultListSelectionModel implements ListSelectionModel, Cloneable, Serializable53{54private static final int MIN = -1;55private static final int MAX = Integer.MAX_VALUE;56private int selectionMode = MULTIPLE_INTERVAL_SELECTION;57private int minIndex = MAX;58private int maxIndex = MIN;59private int anchorIndex = -1;60private int leadIndex = -1;61private int firstAdjustedIndex = MAX;62private int lastAdjustedIndex = MIN;63private boolean isAdjusting = false;6465private int firstChangedIndex = MAX;66private int lastChangedIndex = MIN;6768private BitSet value = new BitSet(32);69protected EventListenerList listenerList = new EventListenerList();7071protected boolean leadAnchorNotificationEnabled = true;7273/** {@inheritDoc} */74public int getMinSelectionIndex() { return isSelectionEmpty() ? -1 : minIndex; }7576/** {@inheritDoc} */77public int getMaxSelectionIndex() { return maxIndex; }7879/** {@inheritDoc} */80public boolean getValueIsAdjusting() { return isAdjusting; }8182/** {@inheritDoc} */83public int getSelectionMode() { return selectionMode; }8485/**86* {@inheritDoc}87* @throws IllegalArgumentException {@inheritDoc}88*/89public void setSelectionMode(int selectionMode) {90switch (selectionMode) {91case SINGLE_SELECTION:92case SINGLE_INTERVAL_SELECTION:93case MULTIPLE_INTERVAL_SELECTION:94this.selectionMode = selectionMode;95break;96default:97throw new IllegalArgumentException("invalid selectionMode");98}99}100101/** {@inheritDoc} */102public boolean isSelectedIndex(int index) {103return ((index < minIndex) || (index > maxIndex)) ? false : value.get(index);104}105106/** {@inheritDoc} */107public boolean isSelectionEmpty() {108return (minIndex > maxIndex);109}110111/** {@inheritDoc} */112public void addListSelectionListener(ListSelectionListener l) {113listenerList.add(ListSelectionListener.class, l);114}115116/** {@inheritDoc} */117public void removeListSelectionListener(ListSelectionListener l) {118listenerList.remove(ListSelectionListener.class, l);119}120121/**122* Returns an array of all the list selection listeners123* registered on this <code>DefaultListSelectionModel</code>.124*125* @return all of this model's <code>ListSelectionListener</code>s126* or an empty127* array if no list selection listeners are currently registered128*129* @see #addListSelectionListener130* @see #removeListSelectionListener131*132* @since 1.4133*/134public ListSelectionListener[] getListSelectionListeners() {135return listenerList.getListeners(ListSelectionListener.class);136}137138/**139* Notifies listeners that we have ended a series of adjustments.140*/141protected void fireValueChanged(boolean isAdjusting) {142if (lastChangedIndex == MIN) {143return;144}145/* Change the values before sending the event to the146* listeners in case the event causes a listener to make147* another change to the selection.148*/149int oldFirstChangedIndex = firstChangedIndex;150int oldLastChangedIndex = lastChangedIndex;151firstChangedIndex = MAX;152lastChangedIndex = MIN;153fireValueChanged(oldFirstChangedIndex, oldLastChangedIndex, isAdjusting);154}155156157/**158* Notifies <code>ListSelectionListeners</code> that the value159* of the selection, in the closed interval <code>firstIndex</code>,160* <code>lastIndex</code>, has changed.161*/162protected void fireValueChanged(int firstIndex, int lastIndex) {163fireValueChanged(firstIndex, lastIndex, getValueIsAdjusting());164}165166/**167* @param firstIndex the first index in the interval168* @param lastIndex the last index in the interval169* @param isAdjusting true if this is the final change in a series of170* adjustments171* @see EventListenerList172*/173protected void fireValueChanged(int firstIndex, int lastIndex, boolean isAdjusting)174{175Object[] listeners = listenerList.getListenerList();176ListSelectionEvent e = null;177178for (int i = listeners.length - 2; i >= 0; i -= 2) {179if (listeners[i] == ListSelectionListener.class) {180if (e == null) {181e = new ListSelectionEvent(this, firstIndex, lastIndex, isAdjusting);182}183((ListSelectionListener)listeners[i+1]).valueChanged(e);184}185}186}187188private void fireValueChanged() {189if (lastAdjustedIndex == MIN) {190return;191}192/* If getValueAdjusting() is true, (eg. during a drag opereration)193* record the bounds of the changes so that, when the drag finishes (and194* setValueAdjusting(false) is called) we can post a single event195* with bounds covering all of these individual adjustments.196*/197if (getValueIsAdjusting()) {198firstChangedIndex = Math.min(firstChangedIndex, firstAdjustedIndex);199lastChangedIndex = Math.max(lastChangedIndex, lastAdjustedIndex);200}201/* Change the values before sending the event to the202* listeners in case the event causes a listener to make203* another change to the selection.204*/205int oldFirstAdjustedIndex = firstAdjustedIndex;206int oldLastAdjustedIndex = lastAdjustedIndex;207firstAdjustedIndex = MAX;208lastAdjustedIndex = MIN;209210fireValueChanged(oldFirstAdjustedIndex, oldLastAdjustedIndex);211}212213/**214* Returns an array of all the objects currently registered as215* <code><em>Foo</em>Listener</code>s216* upon this model.217* <code><em>Foo</em>Listener</code>s218* are registered using the <code>add<em>Foo</em>Listener</code> method.219* <p>220* You can specify the <code>listenerType</code> argument221* with a class literal, such as <code><em>Foo</em>Listener.class</code>.222* For example, you can query a <code>DefaultListSelectionModel</code>223* instance <code>m</code>224* for its list selection listeners225* with the following code:226*227* <pre>ListSelectionListener[] lsls = (ListSelectionListener[])(m.getListeners(ListSelectionListener.class));</pre>228*229* If no such listeners exist,230* this method returns an empty array.231*232* @param listenerType the type of listeners requested;233* this parameter should specify an interface234* that descends from <code>java.util.EventListener</code>235* @return an array of all objects registered as236* <code><em>Foo</em>Listener</code>s237* on this model,238* or an empty array if no such239* listeners have been added240* @exception ClassCastException if <code>listenerType</code> doesn't241* specify a class or interface that implements242* <code>java.util.EventListener</code>243*244* @see #getListSelectionListeners245*246* @since 1.3247*/248public <T extends EventListener> T[] getListeners(Class<T> listenerType) {249return listenerList.getListeners(listenerType);250}251252// Updates first and last change indices253private void markAsDirty(int r) {254if (r == -1) {255return;256}257258firstAdjustedIndex = Math.min(firstAdjustedIndex, r);259lastAdjustedIndex = Math.max(lastAdjustedIndex, r);260}261262// Sets the state at this index and update all relevant state.263private void set(int r) {264if (value.get(r)) {265return;266}267value.set(r);268markAsDirty(r);269270// Update minimum and maximum indices271minIndex = Math.min(minIndex, r);272maxIndex = Math.max(maxIndex, r);273}274275// Clears the state at this index and update all relevant state.276private void clear(int r) {277if (!value.get(r)) {278return;279}280value.clear(r);281markAsDirty(r);282283// Update minimum and maximum indices284/*285If (r > minIndex) the minimum has not changed.286The case (r < minIndex) is not possible because r'th value was set.287We only need to check for the case when lowest entry has been cleared,288and in this case we need to search for the first value set above it.289*/290if (r == minIndex) {291for(minIndex = minIndex + 1; minIndex <= maxIndex; minIndex++) {292if (value.get(minIndex)) {293break;294}295}296}297/*298If (r < maxIndex) the maximum has not changed.299The case (r > maxIndex) is not possible because r'th value was set.300We only need to check for the case when highest entry has been cleared,301and in this case we need to search for the first value set below it.302*/303if (r == maxIndex) {304for(maxIndex = maxIndex - 1; minIndex <= maxIndex; maxIndex--) {305if (value.get(maxIndex)) {306break;307}308}309}310/* Performance note: This method is called from inside a loop in311changeSelection() but we will only iterate in the loops312above on the basis of one iteration per deselected cell - in total.313Ie. the next time this method is called the work of the previous314deselection will not be repeated.315316We also don't need to worry about the case when the min and max317values are in their unassigned states. This cannot happen because318this method's initial check ensures that the selection was not empty319and therefore that the minIndex and maxIndex had 'real' values.320321If we have cleared the whole selection, set the minIndex and maxIndex322to their cannonical values so that the next set command always works323just by using Math.min and Math.max.324*/325if (isSelectionEmpty()) {326minIndex = MAX;327maxIndex = MIN;328}329}330331/**332* Sets the value of the leadAnchorNotificationEnabled flag.333* @see #isLeadAnchorNotificationEnabled()334*/335public void setLeadAnchorNotificationEnabled(boolean flag) {336leadAnchorNotificationEnabled = flag;337}338339/**340* Returns the value of the <code>leadAnchorNotificationEnabled</code> flag.341* When <code>leadAnchorNotificationEnabled</code> is true the model342* generates notification events with bounds that cover all the changes to343* the selection plus the changes to the lead and anchor indices.344* Setting the flag to false causes a narrowing of the event's bounds to345* include only the elements that have been selected or deselected since346* the last change. Either way, the model continues to maintain the lead347* and anchor variables internally. The default is true.348* <p>349* Note: It is possible for the lead or anchor to be changed without a350* change to the selection. Notification of these changes is often351* important, such as when the new lead or anchor needs to be updated in352* the view. Therefore, caution is urged when changing the default value.353*354* @return the value of the <code>leadAnchorNotificationEnabled</code> flag355* @see #setLeadAnchorNotificationEnabled(boolean)356*/357public boolean isLeadAnchorNotificationEnabled() {358return leadAnchorNotificationEnabled;359}360361private void updateLeadAnchorIndices(int anchorIndex, int leadIndex) {362if (leadAnchorNotificationEnabled) {363if (this.anchorIndex != anchorIndex) {364markAsDirty(this.anchorIndex);365markAsDirty(anchorIndex);366}367368if (this.leadIndex != leadIndex) {369markAsDirty(this.leadIndex);370markAsDirty(leadIndex);371}372}373this.anchorIndex = anchorIndex;374this.leadIndex = leadIndex;375}376377private boolean contains(int a, int b, int i) {378return (i >= a) && (i <= b);379}380381private void changeSelection(int clearMin, int clearMax,382int setMin, int setMax, boolean clearFirst) {383for(int i = Math.min(setMin, clearMin); i <= Math.max(setMax, clearMax); i++) {384385boolean shouldClear = contains(clearMin, clearMax, i);386boolean shouldSet = contains(setMin, setMax, i);387388if (shouldSet && shouldClear) {389if (clearFirst) {390shouldClear = false;391}392else {393shouldSet = false;394}395}396397if (shouldSet) {398set(i);399}400if (shouldClear) {401clear(i);402}403}404fireValueChanged();405}406407/**408* Change the selection with the effect of first clearing the values409* in the inclusive range [clearMin, clearMax] then setting the values410* in the inclusive range [setMin, setMax]. Do this in one pass so411* that no values are cleared if they would later be set.412*/413private void changeSelection(int clearMin, int clearMax, int setMin, int setMax) {414changeSelection(clearMin, clearMax, setMin, setMax, true);415}416417/** {@inheritDoc} */418public void clearSelection() {419removeSelectionIntervalImpl(minIndex, maxIndex, false);420}421422/**423* Changes the selection to be between {@code index0} and {@code index1}424* inclusive. {@code index0} doesn't have to be less than or equal to425* {@code index1}.426* <p>427* In {@code SINGLE_SELECTION} selection mode, only the second index428* is used.429* <p>430* If this represents a change to the current selection, then each431* {@code ListSelectionListener} is notified of the change.432* <p>433* If either index is {@code -1}, this method does nothing and returns434* without exception. Otherwise, if either index is less than {@code -1},435* an {@code IndexOutOfBoundsException} is thrown.436*437* @param index0 one end of the interval.438* @param index1 other end of the interval439* @throws IndexOutOfBoundsException if either index is less than {@code -1}440* (and neither index is {@code -1})441* @see #addListSelectionListener442*/443public void setSelectionInterval(int index0, int index1) {444if (index0 == -1 || index1 == -1) {445return;446}447448if (getSelectionMode() == SINGLE_SELECTION) {449index0 = index1;450}451452updateLeadAnchorIndices(index0, index1);453454int clearMin = minIndex;455int clearMax = maxIndex;456int setMin = Math.min(index0, index1);457int setMax = Math.max(index0, index1);458changeSelection(clearMin, clearMax, setMin, setMax);459}460461/**462* Changes the selection to be the set union of the current selection463* and the indices between {@code index0} and {@code index1} inclusive.464* <p>465* In {@code SINGLE_SELECTION} selection mode, this is equivalent466* to calling {@code setSelectionInterval}, and only the second index467* is used. In {@code SINGLE_INTERVAL_SELECTION} selection mode, this468* method behaves like {@code setSelectionInterval}, unless the given469* interval is immediately adjacent to or overlaps the existing selection,470* and can therefore be used to grow it.471* <p>472* If this represents a change to the current selection, then each473* {@code ListSelectionListener} is notified of the change. Note that474* {@code index0} doesn't have to be less than or equal to {@code index1}.475* <p>476* If either index is {@code -1}, this method does nothing and returns477* without exception. Otherwise, if either index is less than {@code -1},478* an {@code IndexOutOfBoundsException} is thrown.479*480* @param index0 one end of the interval.481* @param index1 other end of the interval482* @throws IndexOutOfBoundsException if either index is less than {@code -1}483* (and neither index is {@code -1})484* @see #addListSelectionListener485* @see #setSelectionInterval486*/487public void addSelectionInterval(int index0, int index1)488{489if (index0 == -1 || index1 == -1) {490return;491}492493// If we only allow a single selection, channel through494// setSelectionInterval() to enforce the rule.495if (getSelectionMode() == SINGLE_SELECTION) {496setSelectionInterval(index0, index1);497return;498}499500updateLeadAnchorIndices(index0, index1);501502int clearMin = MAX;503int clearMax = MIN;504int setMin = Math.min(index0, index1);505int setMax = Math.max(index0, index1);506507// If we only allow a single interval and this would result508// in multiple intervals, then set the selection to be just509// the new range.510if (getSelectionMode() == SINGLE_INTERVAL_SELECTION &&511(setMax < minIndex - 1 || setMin > maxIndex + 1)) {512513setSelectionInterval(index0, index1);514return;515}516517changeSelection(clearMin, clearMax, setMin, setMax);518}519520521/**522* Changes the selection to be the set difference of the current selection523* and the indices between {@code index0} and {@code index1} inclusive.524* {@code index0} doesn't have to be less than or equal to {@code index1}.525* <p>526* In {@code SINGLE_INTERVAL_SELECTION} selection mode, if the removal527* would produce two disjoint selections, the removal is extended through528* the greater end of the selection. For example, if the selection is529* {@code 0-10} and you supply indices {@code 5,6} (in any order) the530* resulting selection is {@code 0-4}.531* <p>532* If this represents a change to the current selection, then each533* {@code ListSelectionListener} is notified of the change.534* <p>535* If either index is {@code -1}, this method does nothing and returns536* without exception. Otherwise, if either index is less than {@code -1},537* an {@code IndexOutOfBoundsException} is thrown.538*539* @param index0 one end of the interval540* @param index1 other end of the interval541* @throws IndexOutOfBoundsException if either index is less than {@code -1}542* (and neither index is {@code -1})543* @see #addListSelectionListener544*/545public void removeSelectionInterval(int index0, int index1)546{547removeSelectionIntervalImpl(index0, index1, true);548}549550// private implementation allowing the selection interval551// to be removed without affecting the lead and anchor552private void removeSelectionIntervalImpl(int index0, int index1,553boolean changeLeadAnchor) {554555if (index0 == -1 || index1 == -1) {556return;557}558559if (changeLeadAnchor) {560updateLeadAnchorIndices(index0, index1);561}562563int clearMin = Math.min(index0, index1);564int clearMax = Math.max(index0, index1);565int setMin = MAX;566int setMax = MIN;567568// If the removal would produce to two disjoint selections in a mode569// that only allows one, extend the removal to the end of the selection.570if (getSelectionMode() != MULTIPLE_INTERVAL_SELECTION &&571clearMin > minIndex && clearMax < maxIndex) {572clearMax = maxIndex;573}574575changeSelection(clearMin, clearMax, setMin, setMax);576}577578private void setState(int index, boolean state) {579if (state) {580set(index);581}582else {583clear(index);584}585}586587/**588* Insert length indices beginning before/after index. If the value589* at index is itself selected and the selection mode is not590* SINGLE_SELECTION, set all of the newly inserted items as selected.591* Otherwise leave them unselected. This method is typically592* called to sync the selection model with a corresponding change593* in the data model.594*/595public void insertIndexInterval(int index, int length, boolean before)596{597/* The first new index will appear at insMinIndex and the last598* one will appear at insMaxIndex599*/600int insMinIndex = (before) ? index : index + 1;601int insMaxIndex = (insMinIndex + length) - 1;602603/* Right shift the entire bitset by length, beginning with604* index-1 if before is true, index+1 if it's false (i.e. with605* insMinIndex).606*/607for(int i = maxIndex; i >= insMinIndex; i--) {608setState(i + length, value.get(i));609}610611/* Initialize the newly inserted indices.612*/613boolean setInsertedValues = ((getSelectionMode() == SINGLE_SELECTION) ?614false : value.get(index));615for(int i = insMinIndex; i <= insMaxIndex; i++) {616setState(i, setInsertedValues);617}618619int leadIndex = this.leadIndex;620if (leadIndex > index || (before && leadIndex == index)) {621leadIndex = this.leadIndex + length;622}623int anchorIndex = this.anchorIndex;624if (anchorIndex > index || (before && anchorIndex == index)) {625anchorIndex = this.anchorIndex + length;626}627if (leadIndex != this.leadIndex || anchorIndex != this.anchorIndex) {628updateLeadAnchorIndices(anchorIndex, leadIndex);629}630631fireValueChanged();632}633634635/**636* Remove the indices in the interval index0,index1 (inclusive) from637* the selection model. This is typically called to sync the selection638* model width a corresponding change in the data model. Note639* that (as always) index0 need not be <= index1.640*/641public void removeIndexInterval(int index0, int index1)642{643int rmMinIndex = Math.min(index0, index1);644int rmMaxIndex = Math.max(index0, index1);645int gapLength = (rmMaxIndex - rmMinIndex) + 1;646647/* Shift the entire bitset to the left to close the index0, index1648* gap.649*/650for(int i = rmMinIndex; i <= maxIndex; i++) {651setState(i, value.get(i + gapLength));652}653654int leadIndex = this.leadIndex;655if (leadIndex == 0 && rmMinIndex == 0) {656// do nothing657} else if (leadIndex > rmMaxIndex) {658leadIndex = this.leadIndex - gapLength;659} else if (leadIndex >= rmMinIndex) {660leadIndex = rmMinIndex - 1;661}662663int anchorIndex = this.anchorIndex;664if (anchorIndex == 0 && rmMinIndex == 0) {665// do nothing666} else if (anchorIndex > rmMaxIndex) {667anchorIndex = this.anchorIndex - gapLength;668} else if (anchorIndex >= rmMinIndex) {669anchorIndex = rmMinIndex - 1;670}671672if (leadIndex != this.leadIndex || anchorIndex != this.anchorIndex) {673updateLeadAnchorIndices(anchorIndex, leadIndex);674}675676fireValueChanged();677}678679680/** {@inheritDoc} */681public void setValueIsAdjusting(boolean isAdjusting) {682if (isAdjusting != this.isAdjusting) {683this.isAdjusting = isAdjusting;684this.fireValueChanged(isAdjusting);685}686}687688689/**690* Returns a string that displays and identifies this691* object's properties.692*693* @return a <code>String</code> representation of this object694*/695public String toString() {696String s = ((getValueIsAdjusting()) ? "~" : "=") + value.toString();697return getClass().getName() + " " + Integer.toString(hashCode()) + " " + s;698}699700/**701* Returns a clone of this selection model with the same selection.702* <code>listenerLists</code> are not duplicated.703*704* @exception CloneNotSupportedException if the selection model does not705* both (a) implement the Cloneable interface and (b) define a706* <code>clone</code> method.707*/708public Object clone() throws CloneNotSupportedException {709DefaultListSelectionModel clone = (DefaultListSelectionModel)super.clone();710clone.value = (BitSet)value.clone();711clone.listenerList = new EventListenerList();712return clone;713}714715/** {@inheritDoc} */716@Transient717public int getAnchorSelectionIndex() {718return anchorIndex;719}720721/** {@inheritDoc} */722@Transient723public int getLeadSelectionIndex() {724return leadIndex;725}726727/**728* Set the anchor selection index, leaving all selection values unchanged.729* If leadAnchorNotificationEnabled is true, send a notification covering730* the old and new anchor cells.731*732* @see #getAnchorSelectionIndex733* @see #setLeadSelectionIndex734*/735public void setAnchorSelectionIndex(int anchorIndex) {736updateLeadAnchorIndices(anchorIndex, this.leadIndex);737fireValueChanged();738}739740/**741* Set the lead selection index, leaving all selection values unchanged.742* If leadAnchorNotificationEnabled is true, send a notification covering743* the old and new lead cells.744*745* @param leadIndex the new lead selection index746*747* @see #setAnchorSelectionIndex748* @see #setLeadSelectionIndex749* @see #getLeadSelectionIndex750*751* @since 1.5752*/753public void moveLeadSelectionIndex(int leadIndex) {754// disallow a -1 lead unless the anchor is already -1755if (leadIndex == -1) {756if (this.anchorIndex != -1) {757return;758}759760/* PENDING(shannonh) - The following check is nice, to be consistent with761setLeadSelectionIndex. However, it is not absolutely762necessary: One could work around it by setting the anchor763to something valid, modifying the lead, and then moving764the anchor back to -1. For this reason, there's no sense765in adding it at this time, as that would require766updating the spec and officially committing to it.767768// otherwise, don't do anything if the anchor is -1769} else if (this.anchorIndex == -1) {770return;771*/772773}774775updateLeadAnchorIndices(this.anchorIndex, leadIndex);776fireValueChanged();777}778779/**780* Sets the lead selection index, ensuring that values between the781* anchor and the new lead are either all selected or all deselected.782* If the value at the anchor index is selected, first clear all the783* values in the range [anchor, oldLeadIndex], then select all the values784* values in the range [anchor, newLeadIndex], where oldLeadIndex is the old785* leadIndex and newLeadIndex is the new one.786* <p>787* If the value at the anchor index is not selected, do the same thing in788* reverse selecting values in the old range and deselecting values in the789* new one.790* <p>791* Generate a single event for this change and notify all listeners.792* For the purposes of generating minimal bounds in this event, do the793* operation in a single pass; that way the first and last index inside the794* ListSelectionEvent that is broadcast will refer to cells that actually795* changed value because of this method. If, instead, this operation were796* done in two steps the effect on the selection state would be the same797* but two events would be generated and the bounds around the changed798* values would be wider, including cells that had been first cleared only799* to later be set.800* <p>801* This method can be used in the <code>mouseDragged</code> method802* of a UI class to extend a selection.803*804* @see #getLeadSelectionIndex805* @see #setAnchorSelectionIndex806*/807public void setLeadSelectionIndex(int leadIndex) {808int anchorIndex = this.anchorIndex;809810// only allow a -1 lead if the anchor is already -1811if (leadIndex == -1) {812if (anchorIndex == -1) {813updateLeadAnchorIndices(anchorIndex, leadIndex);814fireValueChanged();815}816817return;818// otherwise, don't do anything if the anchor is -1819} else if (anchorIndex == -1) {820return;821}822823if (this.leadIndex == -1) {824this.leadIndex = leadIndex;825}826827boolean shouldSelect = value.get(this.anchorIndex);828829if (getSelectionMode() == SINGLE_SELECTION) {830anchorIndex = leadIndex;831shouldSelect = true;832}833834int oldMin = Math.min(this.anchorIndex, this.leadIndex);835int oldMax = Math.max(this.anchorIndex, this.leadIndex);836int newMin = Math.min(anchorIndex, leadIndex);837int newMax = Math.max(anchorIndex, leadIndex);838839updateLeadAnchorIndices(anchorIndex, leadIndex);840841if (shouldSelect) {842changeSelection(oldMin, oldMax, newMin, newMax);843}844else {845changeSelection(newMin, newMax, oldMin, oldMax, false);846}847}848}849850851