javax.swing

Class DefaultListSelectionModel

Implemented Interfaces:
Cloneable, ListSelectionModel, Serializable

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

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.
extends EventListener> T[] getListeners(Class listenerType)
Returns an array of all registerers listeners.
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.
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

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

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.

listenerList

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

Method Details

addListSelectionListener

public void addListSelectionListener(ListSelectionListener listener)
Adds a listener.
Specified by:
addListSelectionListener in interface ListSelectionModel
Parameters:
listener - The listener to add

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

clearSelection

public void clearSelection()
Removes all intervals in the selection set.
Specified by:
clearSelection in interface ListSelectionModel

clone

public Object clone()
            throws CloneNotSupportedException
Returns a clone of this object. listenerList don't gets duplicated.
Overrides:
clone in interface Object
Returns:
the cloned object
Throws:
CloneNotSupportedException - if an error occurs

extends EventListener> T[] getListeners

public extends EventListener> T[] getListeners(Class listenerType)
Returns an array of all registerers listeners.
Parameters:
listenerType - The type of listener to retrieve
Returns:
The array
Since:
1.3

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

getAnchorSelectionIndex

public int getAnchorSelectionIndex()
Gets the value of the anchorSelectionIndex property.
Specified by:
getAnchorSelectionIndex in interface ListSelectionModel
Returns:
The current property value

getLeadSelectionIndex

public int getLeadSelectionIndex()
Gets the value of the leadSelectionIndex property.
Specified by:
getLeadSelectionIndex in interface ListSelectionModel
Returns:
The current property value

getListSelectionListeners

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

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

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

getSelectionMode

public int getSelectionMode()
Specified by:
getSelectionMode in interface ListSelectionModel
Returns:
The selection mode.

getValueIsAdjusting

public boolean getValueIsAdjusting()
Gets the value of the valueIsAdjusting property.
Specified by:
getValueIsAdjusting in interface ListSelectionModel
Returns:
The current property value

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

isLeadAnchorNotificationEnabled

public boolean isLeadAnchorNotificationEnabled()
Gets the value of the leadAnchorNotificationEnabled property.
Returns:
The current property value

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

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

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

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)

removeListSelectionListener

public void removeListSelectionListener(ListSelectionListener listener)
Removes a registered listener.
Specified by:
removeListSelectionListener in interface ListSelectionModel
Parameters:
listener - The listener to remove

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

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

setLeadAnchorNotificationEnabled

public void setLeadAnchorNotificationEnabled(boolean l)
Sets the value of the leadAnchorNotificationEnabled property.
Parameters:
l - The new property value

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:
  • isSelectedIndex(A) == true: set [A,OL] to deselected, then set [A,NL] to selected.
  • isSelectedIndex(A) == false: set [A,OL] to selected, then set [A,NL] to deselected.

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

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.

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

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

DefaultListSelectionModel.java -- Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.