javax.swing
Class DefaultListSelectionModel

java.lang.Object
  extended by javax.swing.DefaultListSelectionModel
All Implemented Interfaces:
Serializable, Cloneable, ListSelectionModel

public class DefaultListSelectionModel
extends Object
implements Cloneable, ListSelectionModel, Serializable

The default implementation of ListSelectionModel, which is used by JList and similar classes to manage the selection status of a number of data elements.

The class is organized abstractly as a set of intervals of integers. Each interval indicates an inclusive range of indices in a list -- held by some other object and unknown to this class -- which is considered "selected". There are various accessors for querying and modifying the set of intervals, with simplified forms accepting a single index, representing an interval with only one element.

See Also:
Serialized Form

Field Summary
protected  boolean leadAnchorNotificationEnabled
          controls the range of indices provided in any ListSelectionEvent fired by the selectionModel.
protected  EventListenerList listenerList
          The list of ListSelectionListeners subscribed to this selection model.
 
Fields inherited from interface javax.swing.ListSelectionModel
MULTIPLE_INTERVAL_SELECTION, SINGLE_INTERVAL_SELECTION, SINGLE_SELECTION
 
Constructor Summary
DefaultListSelectionModel()
           
 
Method Summary
 void addListSelectionListener(ListSelectionListener listener)
          Adds a listener.
 void addSelectionInterval(int index0, int index1)
          If the selectionMode property is equal to SINGLE_SELECTION equivalent to calling setSelectionInterval(index1, index2); If the selectionMode property is equal to SINGLE_INTERVAL_SELECTION and the interval being added is not adjacent to an already selected interval, equivalent to setSelectionInterval(index1, index2).
 void clearSelection()
          Removes all intervals in the selection set.
 Object clone()
          Returns a clone of this object.
protected  void fireValueChanged(boolean isAdjusting)
          Fires a ListSelectionEvent to all the listeners of type ListSelectionListener registered with this selection model to indicate that a series of adjustment has just ended.
protected  void fireValueChanged(int firstIndex, int lastIndex)
          Fires a ListSelectionEvent to all the listeners of type ListSelectionListener registered with this selection model.
protected  void fireValueChanged(int firstIndex, int lastIndex, boolean isAdjusting)
          Fires a ListSelectionEvent to all the listeners of type ListSelectionListener registered with this selection model.
 int getAnchorSelectionIndex()
          Gets the value of the anchorSelectionIndex property.
 int getLeadSelectionIndex()
          Gets the value of the leadSelectionIndex property.
<T extends EventListener>
T[]
getListeners(Class<T> listenerType)
          Returns an array of all registerers listeners.
 ListSelectionListener[] getListSelectionListeners()
          Returns an array of all registerd list selection listeners.
 int getMaxSelectionIndex()
          Gets the largest index which is currently a member of a selection interval.
 int getMinSelectionIndex()
          Gets the smallest index which is currently a member of a selection interval.
 int getSelectionMode()
          Returns the selection mode, which is one of ListSelectionModel.SINGLE_SELECTION, ListSelectionModel.SINGLE_INTERVAL_SELECTION and ListSelectionModel.MULTIPLE_INTERVAL_SELECTION.
 boolean getValueIsAdjusting()
          Gets the value of the valueIsAdjusting property.
 void insertIndexInterval(int index, int length, boolean before)
          Inserts a number of indices either before or after a particular position in the set of indices.
 boolean isLeadAnchorNotificationEnabled()
          Gets the value of the leadAnchorNotificationEnabled property.
 boolean isSelectedIndex(int a)
          Determines whether a particular index is a member of a selection interval.
 boolean isSelectionEmpty()
          Determines whether the selection is empty.
 void moveLeadSelectionIndex(int leadIndex)
          Moves the lead selection index to leadIndex without changing the selection values.
 void removeIndexInterval(int index0, int index1)
          Removes a range from the set of indices.
 void removeListSelectionListener(ListSelectionListener listener)
          Removes a registered listener.
 void removeSelectionInterval(int index0, int index1)
          Deselects all indices in the inclusive range [index0,index1].
 void setAnchorSelectionIndex(int index)
          Sets the value of the anchorSelectionIndex property.
 void setLeadAnchorNotificationEnabled(boolean l)
          Sets the value of the leadAnchorNotificationEnabled property.
 void setLeadSelectionIndex(int leadIndex)
          Sets the value of the anchorSelectionIndex property.
 void setSelectionInterval(int anchor, int lead)
          Clears the current selection and marks a given interval as "selected".
 void setSelectionMode(int mode)
          Sets the value of the selectionMode property.
 void setValueIsAdjusting(boolean v)
          Sets the value of the valueIsAdjusting property.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

listenerList

protected EventListenerList listenerList
The list of ListSelectionListeners subscribed to this selection model.


leadAnchorNotificationEnabled

protected boolean leadAnchorNotificationEnabled
controls the range of indices provided in any ListSelectionEvent fired by the selectionModel. Let [A,L] be the range of indices between anchorSelectionIndex and leadSelectionIndex inclusive, and let [i0,i1] be the range of indices changed in a given call which generates a ListSelectionEvent. Then when this property is true, the ListSelectionEvent contains the range [A,L] union [i0,i1]; when false it will contain only [i0,i1]. The default is true.

See Also:
isLeadAnchorNotificationEnabled(), setLeadAnchorNotificationEnabled(boolean)
Constructor Detail

DefaultListSelectionModel

public DefaultListSelectionModel()
Method Detail

getSelectionMode

public int getSelectionMode()
Returns the selection mode, which is one of ListSelectionModel.SINGLE_SELECTION, ListSelectionModel.SINGLE_INTERVAL_SELECTION and ListSelectionModel.MULTIPLE_INTERVAL_SELECTION. The default value is ListSelectionModel.MULTIPLE_INTERVAL_SELECTION.

Specified by:
getSelectionMode in interface ListSelectionModel
Returns:
The selection mode.
See Also:
setSelectionMode(int)

setSelectionMode

public void setSelectionMode(int mode)
Sets the value of the selectionMode property.

Specified by:
setSelectionMode in interface ListSelectionModel
Parameters:
mode - The new value of the property
See Also:
ListSelectionModel.getSelectionMode()

getAnchorSelectionIndex

public int getAnchorSelectionIndex()
Gets the value of the anchorSelectionIndex property.

Specified by:
getAnchorSelectionIndex in interface ListSelectionModel
Returns:
The current property value
See Also:
setAnchorSelectionIndex(int)

setAnchorSelectionIndex

public void setAnchorSelectionIndex(int index)
Sets the value of the anchorSelectionIndex property.

Specified by:
setAnchorSelectionIndex in interface ListSelectionModel
Parameters:
index - The new property value
See Also:
getAnchorSelectionIndex()

getLeadSelectionIndex

public int getLeadSelectionIndex()
Gets the value of the leadSelectionIndex property.

Specified by:
getLeadSelectionIndex in interface ListSelectionModel
Returns:
The current property value
See Also:
setLeadSelectionIndex(int)

setLeadSelectionIndex

public void setLeadSelectionIndex(int leadIndex)

Sets the value of the anchorSelectionIndex property. As a side effect, alters the selection status of two ranges of indices. Let OL be the old lead selection index, NL be the new lead selection index, and A be the anchor selection index. Then if A is a valid selection index, one of two things happens depending on the seleciton status of A:

This method generates at most a single ListSelectionEvent despite changing multiple ranges. The range of values provided to the ListSelectionEvent includes only the minimum range of values which changed selection status between the beginning and end of the method.

Specified by:
setLeadSelectionIndex in interface ListSelectionModel
Parameters:
leadIndex - The new property value
See Also:
getAnchorSelectionIndex()

moveLeadSelectionIndex

public void moveLeadSelectionIndex(int leadIndex)
Moves the lead selection index to leadIndex without changing the selection values. If leadAnchorNotificationEnabled is true, send a notification covering the old and new lead cells.

Parameters:
leadIndex - the new lead selection index
Since:
1.5

isLeadAnchorNotificationEnabled

public boolean isLeadAnchorNotificationEnabled()
Gets the value of the leadAnchorNotificationEnabled property.

Returns:
The current property value
See Also:
setLeadAnchorNotificationEnabled(boolean)

setLeadAnchorNotificationEnabled

public void setLeadAnchorNotificationEnabled(boolean l)
Sets the value of the leadAnchorNotificationEnabled property.

Parameters:
l - The new property value
See Also:
isLeadAnchorNotificationEnabled()

getValueIsAdjusting

public boolean getValueIsAdjusting()
Gets the value of the valueIsAdjusting property.

Specified by:
getValueIsAdjusting in interface ListSelectionModel
Returns:
The current property value
See Also:
setValueIsAdjusting(boolean)

setValueIsAdjusting

public void setValueIsAdjusting(boolean v)
Sets the value of the valueIsAdjusting property.

Specified by:
setValueIsAdjusting in interface ListSelectionModel
Parameters:
v - The new property value
See Also:
getValueIsAdjusting()

isSelectionEmpty

public boolean isSelectionEmpty()
Determines whether the selection is empty.

Specified by:
isSelectionEmpty in interface ListSelectionModel
Returns:
true if the selection is empty, otherwise false

getMinSelectionIndex

public int getMinSelectionIndex()
Gets the smallest index which is currently a member of a selection interval.

Specified by:
getMinSelectionIndex in interface ListSelectionModel
Returns:
The least integer i such that i >= 0 and i is a member of a selected interval, or -1 if there are no selected intervals
See Also:
getMaxSelectionIndex()

getMaxSelectionIndex

public int getMaxSelectionIndex()
Gets the largest index which is currently a member of a selection interval.

Specified by:
getMaxSelectionIndex in interface ListSelectionModel
Returns:
The greatest integer i such that i >= 0 and i is a member of a selected interval, or -1 if there are no selected intervals
See Also:
getMinSelectionIndex()

isSelectedIndex

public boolean isSelectedIndex(int a)
Determines whether a particular index is a member of a selection interval.

Specified by:
isSelectedIndex in interface ListSelectionModel
Parameters:
a - The index to search for
Returns:
true if the index is a member of a selection interval, otherwise false

addSelectionInterval

public void addSelectionInterval(int index0,
                                 int index1)
If the selectionMode property is equal to SINGLE_SELECTION equivalent to calling setSelectionInterval(index1, index2); If the selectionMode property is equal to SINGLE_INTERVAL_SELECTION and the interval being added is not adjacent to an already selected interval, equivalent to setSelectionInterval(index1, index2). Otherwise adds the range [index0, index1] to the selection interval set.

Specified by:
addSelectionInterval in interface ListSelectionModel
Parameters:
index0 - The beginning of the range of indices to select
index1 - The end of the range of indices to select
See Also:
setSelectionInterval(int, int), removeSelectionInterval(int, int)

removeSelectionInterval

public void removeSelectionInterval(int index0,
                                    int index1)
Deselects all indices in the inclusive range [index0,index1].

Specified by:
removeSelectionInterval in interface ListSelectionModel
Parameters:
index0 - The beginning of the range of indices to deselect
index1 - The end of the range of indices to deselect
See Also:
addSelectionInterval(int, int), setSelectionInterval(int, int)

clearSelection

public void clearSelection()
Removes all intervals in the selection set.

Specified by:
clearSelection in interface ListSelectionModel

setSelectionInterval

public void setSelectionInterval(int anchor,
                                 int lead)
Clears the current selection and marks a given interval as "selected". If the current selection mode is SINGLE_SELECTION only the index index2 is selected.

Specified by:
setSelectionInterval in interface ListSelectionModel
Parameters:
anchor - the anchor selection index.
lead - the lead selection index.

insertIndexInterval

public void insertIndexInterval(int index,
                                int length,
                                boolean before)
Inserts a number of indices either before or after a particular position in the set of indices. Renumbers all indices after the inserted range. The new indices in the inserted range are not selected. This method is typically called to synchronize the selection model with an inserted range of elements in a ListModel.

Specified by:
insertIndexInterval in interface ListSelectionModel
Parameters:
index - The position to insert indices at
length - The number of indices to insert
before - Indicates whether to insert the indices before the index or after it
See Also:
ListSelectionModel.removeIndexInterval(int, int)

removeIndexInterval

public void removeIndexInterval(int index0,
                                int index1)
Removes a range from the set of indices. Renumbers all indices after the removed range. This method is typically called to synchronize the selection model with a deleted range of elements in a ListModel.

Specified by:
removeIndexInterval in interface ListSelectionModel
Parameters:
index0 - The first index to remove (inclusive)
index1 - The last index to remove (inclusive)
See Also:
ListSelectionModel.insertIndexInterval(int, int, boolean)

fireValueChanged

protected void fireValueChanged(boolean isAdjusting)
Fires a ListSelectionEvent to all the listeners of type ListSelectionListener registered with this selection model to indicate that a series of adjustment has just ended. The values of getMinSelectionIndex() and getMaxSelectionIndex() are used in the ListSelectionEvent that gets fired.

Parameters:
isAdjusting - true if this is the final change in a series of adjustments, false/code> otherwise

fireValueChanged

protected void fireValueChanged(int firstIndex,
                                int lastIndex)
Fires a ListSelectionEvent to all the listeners of type ListSelectionListener registered with this selection model.

Parameters:
firstIndex - The low index of the changed range
lastIndex - The high index of the changed range

fireValueChanged

protected void fireValueChanged(int firstIndex,
                                int lastIndex,
                                boolean isAdjusting)
Fires a ListSelectionEvent to all the listeners of type ListSelectionListener registered with this selection model.

Parameters:
firstIndex - The low index of the changed range
lastIndex - The high index of the changed range
isAdjusting - Whether this change is part of a seqence of adjustments made to the selection, such as during interactive scrolling

addListSelectionListener

public void addListSelectionListener(ListSelectionListener listener)
Adds a listener.

Specified by:
addListSelectionListener in interface ListSelectionModel
Parameters:
listener - The listener to add
See Also:
removeListSelectionListener(javax.swing.event.ListSelectionListener), getListSelectionListeners()

removeListSelectionListener

public void removeListSelectionListener(ListSelectionListener listener)
Removes a registered listener.

Specified by:
removeListSelectionListener in interface ListSelectionModel
Parameters:
listener - The listener to remove
See Also:
addListSelectionListener(javax.swing.event.ListSelectionListener), getListSelectionListeners()

getListeners

public <T extends EventListener> T[] getListeners(Class<T> listenerType)
Returns an array of all registerers listeners.

Parameters:
listenerType - The type of listener to retrieve
Returns:
The array
Since:
1.3
See Also:
getListSelectionListeners()

getListSelectionListeners

public ListSelectionListener[] getListSelectionListeners()
Returns an array of all registerd list selection listeners.

Returns:
the array
Since:
1.4
See Also:
addListSelectionListener(javax.swing.event.ListSelectionListener), removeListSelectionListener(javax.swing.event.ListSelectionListener), getListeners(java.lang.Class)

clone

public Object clone()
             throws CloneNotSupportedException
Returns a clone of this object. listenerList don't gets duplicated.

Overrides:
clone in class Object
Returns:
the cloned object
Throws:
CloneNotSupportedException - if an error occurs
See Also:
Cloneable