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 DefaultListSelectionModel property is equal to SINGLE_SELECTION equivalent to calling setSelectionInterval(index1, index2); If the DefaultListSelectionModel 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 DefaultListSelectionModel property.
int
getLeadSelectionIndex()
Gets the value of the DefaultListSelectionModel property.
ListSelectionListener[]
getListSelectionListeners()
Returns an array of all registerd list selection listeners.
EventListener[]
getListeners(Class listenerType)
Returns an array of all registerers 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()
Gets the value of the DefaultListSelectionModel property.
boolean
getValueIsAdjusting()
Gets the value of the DefaultListSelectionModel 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 anchorIndex)
Sets the value of the DefaultListSelectionModel property.
void
setLeadAnchorNotificationEnabled(boolean l)
Sets the value of the leadAnchorNotificationEnabled property.
void
setLeadSelectionIndex(int leadIndex)
Sets the value of the DefaultListSelectionModel property.
void
setSelectionInterval(int index0, int index1)
Clears the current selection and marks a given interval as "selected".
void
setSelectionMode(int a)
Sets the value of the DefaultListSelectionModel property.
void
setValueIsAdjusting(boolean v)
Sets the value of the DefaultListSelectionModel property.

Methods inherited from class java.lang.Object

clone, equals, finalize, getClass, 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 DefaultListSelectionModel and DefaultListSelectionModel 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)


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

See Also:
removeListSelectionListener(ListSelectionListener), getListSelectionListeners()


addSelectionInterval

public void addSelectionInterval(int index0,
                                 int index1)
If the DefaultListSelectionModel property is equal to SINGLE_SELECTION equivalent to calling setSelectionInterval(index1, index2); If the DefaultListSelectionModel 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)


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


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 DefaultListSelectionModel property.
Specified by:
getAnchorSelectionIndex in interface ListSelectionModel

Returns:
The current property value

See Also:
setAnchorSelectionIndex(int)


getLeadSelectionIndex

public int getLeadSelectionIndex()
Gets the value of the DefaultListSelectionModel property.
Specified by:
getLeadSelectionIndex in interface ListSelectionModel

Returns:
The current property value

See Also:
setLeadSelectionIndex(int)


getListSelectionListeners

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

Returns:
the array

Since:
1.4

See Also:
addListSelectionListener(ListSelectionListener), removeListSelectionListener(ListSelectionListener), getListeners(Class)


getListeners

public EventListener[] getListeners(Class 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()


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


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


getSelectionMode

public int getSelectionMode()
Gets the value of the DefaultListSelectionModel property.
Specified by:
getSelectionMode in interface ListSelectionModel

Returns:
The current value of the property


getValueIsAdjusting

public boolean getValueIsAdjusting()
Gets the value of the DefaultListSelectionModel property.
Specified by:
getValueIsAdjusting in interface ListSelectionModel

Returns:
The current property value

See Also:
setValueIsAdjusting(boolean)


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

See Also:
setLeadAnchorNotificationEnabled(boolean)


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

See Also:
addListSelectionListener(ListSelectionListener), getListSelectionListeners()


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)


setAnchorSelectionIndex

public void setAnchorSelectionIndex(int anchorIndex)
Sets the value of the DefaultListSelectionModel property.
Specified by:
setAnchorSelectionIndex in interface ListSelectionModel

Parameters:
anchorIndex - The new property value

See Also:
getAnchorSelectionIndex()


setLeadAnchorNotificationEnabled

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

Parameters:
l - The new property value

See Also:
isLeadAnchorNotificationEnabled()


setLeadSelectionIndex

public void setLeadSelectionIndex(int leadIndex)
Sets the value of the DefaultListSelectionModel 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

See Also:
getAnchorSelectionIndex()


setSelectionInterval

public void setSelectionInterval(int index0,
                                 int index1)
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:
index0 - The low end of the new selection
index1 - The high end of the new selection


setSelectionMode

public void setSelectionMode(int a)
Sets the value of the DefaultListSelectionModel property.
Specified by:
setSelectionMode in interface ListSelectionModel

Parameters:
a - The new value of the property


setValueIsAdjusting

public void setValueIsAdjusting(boolean v)
Sets the value of the DefaultListSelectionModel property.
Specified by:
setValueIsAdjusting in interface ListSelectionModel

Parameters:
v - The new property value

See Also:
getValueIsAdjusting()


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.