FileDocCategorySizeDatePackage
DefaultListSelectionModel.javaAPI DocJava SE 5 API26911Fri Aug 26 14:57:54 BST 2005javax.swing

DefaultListSelectionModel

public class DefaultListSelectionModel extends Object implements Serializable, ListSelectionModel, Cloneable
Default data model for list selections.

Warning: 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 JavaBeansTM has been added to the java.beans package. Please see {@link java.beans.XMLEncoder}.

version
1.75 05/05/04
author
Philip Milne
author
Hans Muller
see
ListSelectionModel

Fields Summary
private static final int
MIN
private static final int
MAX
private int
selectionMode
private int
minIndex
private int
maxIndex
private int
anchorIndex
private int
leadIndex
private int
firstAdjustedIndex
private int
lastAdjustedIndex
private boolean
isAdjusting
private int
firstChangedIndex
private int
lastChangedIndex
private BitSet
value
protected EventListenerList
listenerList
protected boolean
leadAnchorNotificationEnabled
Constructors Summary
Methods Summary
public voidaddListSelectionListener(javax.swing.event.ListSelectionListener l)

 	listenerList.add(ListSelectionListener.class, l);
    
public voidaddSelectionInterval(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);
    
private voidchangeSelection(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();
    
private voidchangeSelection(int clearMin, int clearMax, int setMin, int setMax)
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.

        changeSelection(clearMin, clearMax, setMin, setMax, true);
    
private voidclear(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;
        }
    
public voidclearSelection()

	removeSelectionIntervalImpl(minIndex, maxIndex, false);
    
public java.lang.Objectclone()
Returns a clone of this selection model with the same selection. listenerLists are not duplicated.

exception
CloneNotSupportedException if the selection model does not both (a) implement the Cloneable interface and (b) define a clone method.

	DefaultListSelectionModel clone = (DefaultListSelectionModel)super.clone();
	clone.value = (BitSet)value.clone();
	clone.listenerList = new EventListenerList();
	return clone;
    
private booleancontains(int a, int b, int i)

        return (i >= a) && (i <= b);
    
protected voidfireValueChanged(boolean isAdjusting)
Notifies listeners that we have ended a series of adjustments.

  
        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); 
    
protected voidfireValueChanged(int firstIndex, int lastIndex)
Notifies ListSelectionListeners that the value of the selection, in the closed interval firstIndex, lastIndex, has changed.

	fireValueChanged(firstIndex, lastIndex, getValueIsAdjusting());
    
protected voidfireValueChanged(int firstIndex, int lastIndex, boolean isAdjusting)

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

	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 voidfireValueChanged()

	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);
    
public intgetAnchorSelectionIndex()

        return anchorIndex;
    
public intgetLeadSelectionIndex()

        return leadIndex;
    
public javax.swing.event.ListSelectionListener[]getListSelectionListeners()
Returns an array of all the list selection listeners registered on this DefaultListSelectionModel.

return
all of this model's ListSelectionListeners or an empty array if no list selection listeners are currently registered
see
#addListSelectionListener
see
#removeListSelectionListener
since
1.4

        return (ListSelectionListener[])listenerList.getListeners(
                ListSelectionListener.class);
    
public T[]getListeners(java.lang.Class listenerType)
Returns an array of all the objects currently registered as FooListeners upon this model. FooListeners are registered using the addFooListener method.

You can specify the listenerType argument with a class literal, such as FooListener.class. For example, you can query a DefaultListSelectionModel instance m for its list selection listeners with the following code:

ListSelectionListener[] lsls = (ListSelectionListener[])(m.getListeners(ListSelectionListener.class));
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 java.util.EventListener
return
an array of all objects registered as FooListeners on this model, or an empty array if no such listeners have been added
exception
ClassCastException if listenerType doesn't specify a class or interface that implements java.util.EventListener
see
#getListSelectionListeners
since
1.3

 
	return listenerList.getListeners(listenerType); 
    
public intgetMaxSelectionIndex()

 return maxIndex; 
public intgetMinSelectionIndex()


    // implements javax.swing.ListSelectionModel
        return isSelectionEmpty() ? -1 : minIndex; 
public intgetSelectionMode()
Returns the selection mode.

return
one of the these values:
  • SINGLE_SELECTION
  • SINGLE_INTERVAL_SELECTION
  • MULTIPLE_INTERVAL_SELECTION
see
#getSelectionMode

 return selectionMode; 
public booleangetValueIsAdjusting()

 return isAdjusting; 
public voidinsertIndexInterval(int index, int length, boolean before)
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.

	/* 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();
    
public booleanisLeadAnchorNotificationEnabled()
Returns the value of the leadAnchorNotificationEnabled flag. When leadAnchorNotificationEnabled 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.

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 leadAnchorNotificationEnabled flag
see
#setLeadAnchorNotificationEnabled(boolean)

        return leadAnchorNotificationEnabled;
    
public booleanisSelectedIndex(int index)

	return ((index < minIndex) || (index > maxIndex)) ? false : value.get(index);
    
public booleanisSelectionEmpty()

	return (minIndex > maxIndex);
    
private voidmarkAsDirty(int r)

        firstAdjustedIndex = Math.min(firstAdjustedIndex, r);
	lastAdjustedIndex =  Math.max(lastAdjustedIndex, r);
    
public voidmoveLeadSelectionIndex(int leadIndex)
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

        // 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();
    
public voidremoveIndexInterval(int index0, int index1)
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.

	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();
    
public voidremoveListSelectionListener(javax.swing.event.ListSelectionListener l)

 	listenerList.remove(ListSelectionListener.class, l);
    
public voidremoveSelectionInterval(int index0, int index1)

        removeSelectionIntervalImpl(index0, index1, true);
    
private voidremoveSelectionIntervalImpl(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 voidset(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);
    
public voidsetAnchorSelectionIndex(int anchorIndex)
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

 
	updateLeadAnchorIndices(anchorIndex, this.leadIndex); 
	fireValueChanged(); 
    
public voidsetLeadAnchorNotificationEnabled(boolean flag)
Sets the value of the leadAnchorNotificationEnabled flag.

see
#isLeadAnchorNotificationEnabled()

        leadAnchorNotificationEnabled = flag;
    
public voidsetLeadSelectionIndex(int leadIndex)
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.

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.

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.

This method can be used in the mouseDragged method of a UI class to extend a selection.

see
#getLeadSelectionIndex
see
#setAnchorSelectionIndex

 
        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);
        }
    
public voidsetSelectionInterval(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);
    
public voidsetSelectionMode(int selectionMode)
Sets the selection mode. The default is MULTIPLE_INTERVAL_SELECTION.

param
selectionMode one of three values:
  • SINGLE_SELECTION
  • SINGLE_INTERVAL_SELECTION
  • MULTIPLE_INTERVAL_SELECTION
exception
IllegalArgumentException if selectionMode is not one of the legal values shown above
see
#setSelectionMode

	switch (selectionMode) {
	case SINGLE_SELECTION:
	case SINGLE_INTERVAL_SELECTION:
	case MULTIPLE_INTERVAL_SELECTION:
	    this.selectionMode = selectionMode;
	    break;
	default:
	    throw new IllegalArgumentException("invalid selectionMode");
	}
    
private voidsetState(int index, boolean state)

        if (state) {
	    set(index); 
	}
	else {
	    clear(index); 
	}
    
public voidsetValueIsAdjusting(boolean isAdjusting)

	if (isAdjusting != this.isAdjusting) {
	    this.isAdjusting = isAdjusting;
	    this.fireValueChanged(isAdjusting);
	}
    
public java.lang.StringtoString()
Returns a string that displays and identifies this object's properties.

return
a String representation of this object

	String s =  ((getValueIsAdjusting()) ? "~" : "=") + value.toString();
	return getClass().getName() + " " + Integer.toString(hashCode()) + " " + s;
    
private voidupdateLeadAnchorIndices(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;